Skip to content

Commit cf8530a

Browse files
Optimize html/epub tests by making them asyncronous by using tmp_dir tag (#1557)
1 parent 8b42650 commit cf8530a

File tree

3 files changed

+213
-208
lines changed

3 files changed

+213
-208
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ex_doc-*.tar
2525
node_modules/
2626
/test/fixtures/umbrella/_build/
2727
/test/tmp/
28+
/tmp/
2829
/npm-debug.log
2930

3031
# Ignore artifacts from non-production builds

test/ex_doc/formatter/epub_test.exs

+56-64
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,103 @@
11
defmodule ExDoc.Formatter.EPUBTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33

4-
setup do
5-
File.rm_rf(output_dir())
6-
File.mkdir_p!(output_dir())
7-
end
8-
9-
defp output_dir do
10-
Path.expand("../../tmp/epub", __DIR__)
11-
end
12-
13-
defp beam_dir do
14-
Path.expand("../../tmp/beam", __DIR__)
15-
end
4+
@moduletag :tmp_dir
165

176
@before_closing_head_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-HEAD-TAG-HTML</dont-escape>"
187
@before_closing_body_tag_content_epub "UNIQUE:<dont-escape>&copy;BEFORE-CLOSING-BODY-TAG-HTML</dont-escape>"
198

209
defp before_closing_head_tag(:epub), do: @before_closing_head_tag_content_epub
2110
defp before_closing_body_tag(:epub), do: @before_closing_body_tag_content_epub
2211

23-
defp doc_config do
12+
defp doc_config(%{tmp_dir: tmp_dir} = _context) do
2413
[
2514
app: :elixir,
2615
project: "Elixir",
2716
version: "1.0.1",
2817
formatter: "epub",
29-
output: output_dir(),
30-
source_beam: beam_dir(),
18+
output: tmp_dir <> "/epub",
19+
source_beam: "test/tmp/beam",
3120
extras: ["test/fixtures/README.md"],
3221
skip_undefined_reference_warnings_on: ["Warnings"]
3322
]
3423
end
3524

36-
defp doc_config(config) do
37-
Keyword.merge(doc_config(), config)
25+
defp doc_config(context, config) when is_map(context) and is_list(config) do
26+
Keyword.merge(doc_config(context), config)
3827
end
3928

4029
defp generate_docs(config) do
4130
ExDoc.generate_docs(config[:project], config[:version], config)
4231
end
4332

44-
defp generate_docs_and_unzip(options) do
45-
generate_docs(options)
46-
unzip_dir = String.to_charlist("#{doc_config()[:output]}")
33+
defp generate_docs_and_unzip(context, config) do
34+
generate_docs(config)
35+
unzip_dir = String.to_charlist("#{doc_config(context)[:output]}")
4736

48-
"#{doc_config()[:output]}/#{doc_config()[:project]}.epub"
37+
"#{doc_config(context)[:output]}/#{doc_config(context)[:project]}.epub"
4938
|> String.to_charlist()
5039
|> :zip.unzip(cwd: unzip_dir)
5140
end
5241

53-
test "generates headers for module pages" do
54-
generate_docs_and_unzip(doc_config(main: "RandomError"))
42+
test "generates headers for module pages", %{tmp_dir: tmp_dir} = context do
43+
generate_docs_and_unzip(context, doc_config(context, main: "RandomError"))
5544

56-
content = File.read!("#{output_dir()}/OEBPS/RandomError.xhtml")
45+
content = File.read!(tmp_dir <> "/epub/OEBPS/RandomError.xhtml")
5746
assert content =~ ~r{<html.*lang="en".*xmlns:epub="http://www.idpf.org/2007/ops">}ms
5847
assert content =~ ~r{<meta charset="utf-8" />}ms
5948
assert content =~ ~r{<meta name="generator" content="ExDoc v[^"]+" />}
6049
assert content =~ ~r{<title>RandomError - Elixir v1.0.1</title>}
6150
end
6251

63-
test "allows to set the primary language of the document" do
64-
generate_docs_and_unzip(doc_config(main: "RandomError", language: "fr"))
52+
test "allows to set the primary language of the document", %{tmp_dir: tmp_dir} = context do
53+
generate_docs_and_unzip(context, doc_config(context, main: "RandomError", language: "fr"))
6554

66-
content = File.read!("#{output_dir()}/OEBPS/RandomError.xhtml")
55+
content = File.read!(tmp_dir <> "/epub/OEBPS/RandomError.xhtml")
6756
assert content =~ ~r{<html.*lang="fr".*xmlns:epub="http://www.idpf.org/2007/ops">}ms
6857
end
6958

70-
test "allows to set the authors of the book" do
71-
generate_docs_and_unzip(doc_config(authors: ["John Doe", "Jane Doe"]))
59+
test "allows to set the authors of the book", %{tmp_dir: tmp_dir} = context do
60+
generate_docs_and_unzip(context, doc_config(context, authors: ["John Doe", "Jane Doe"]))
7261

73-
content = File.read!("#{output_dir()}/OEBPS/content.opf")
62+
content = File.read!(tmp_dir <> "/epub/OEBPS/content.opf")
7463
assert content =~ ~r{<dc:creator id="author1">John Doe</dc:creator>}
7564
assert content =~ ~r{<dc:creator id="author2">Jane Doe</dc:creator>}
7665
end
7766

78-
test "raises when assets are invalid" do
67+
test "raises when assets are invalid", context do
7968
File.mkdir_p!("test/tmp/epub_assets/hello")
8069
File.touch!("test/tmp/epub_assets/hello/world.pdf")
8170

8271
assert_raise(
8372
RuntimeError,
8473
~s{asset with extension ".pdf" is not supported by EPUB format},
85-
fn -> generate_docs(doc_config(assets: "test/tmp/epub_assets")) end
74+
fn -> generate_docs(doc_config(context, assets: "test/tmp/epub_assets")) end
8675
)
8776
after
8877
File.rm_rf!("test/tmp/epub_assets")
8978
end
9079

91-
test "generates an EPUB file in the default directory" do
92-
generate_docs(doc_config())
93-
assert File.regular?("#{output_dir()}/#{doc_config()[:project]}.epub")
80+
test "generates an EPUB file in the default directory", %{tmp_dir: tmp_dir} = context do
81+
generate_docs(doc_config(context))
82+
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
9483
end
9584

96-
test "generates an EPUB file with erlang as proglang" do
97-
generate_docs(Keyword.put(doc_config(), :proglang, :erlang))
98-
assert File.regular?("#{output_dir()}/#{doc_config()[:project]}.epub")
85+
test "generates an EPUB file with erlang as proglang", %{tmp_dir: tmp_dir} = context do
86+
generate_docs(Keyword.put(doc_config(context), :proglang, :erlang))
87+
assert File.regular?(tmp_dir <> "/epub/#{doc_config(context)[:project]}.epub")
9988
end
10089

101-
test "generates an EPUB file in specified output directory" do
102-
config = doc_config(output: "#{output_dir()}/another_dir", main: "RandomError")
90+
test "generates an EPUB file in specified output directory", %{tmp_dir: tmp_dir} = context do
91+
config = doc_config(context, output: tmp_dir <> "/epub/another_dir", main: "RandomError")
10392
generate_docs(config)
10493

105-
assert File.regular?("#{output_dir()}/another_dir/#{doc_config()[:project]}.epub")
94+
assert File.regular?(tmp_dir <> "/epub/another_dir/#{doc_config(context)[:project]}.epub")
10695
end
10796

108-
test "generates an EPUB file with a standardized structure" do
109-
generate_docs_and_unzip(doc_config())
97+
test "generates an EPUB file with a standardized structure", %{tmp_dir: tmp_dir} = context do
98+
generate_docs_and_unzip(context, doc_config(context))
11099

111-
root_dir = "#{output_dir()}"
100+
root_dir = tmp_dir <> "/epub"
112101
meta_dir = "#{root_dir}/META-INF"
113102
oebps_dir = "#{root_dir}/OEBPS"
114103
dist_dir = "#{oebps_dir}/dist"
@@ -126,9 +115,9 @@ defmodule ExDoc.Formatter.EPUBTest do
126115
assert [_] = Path.wildcard("#{dist_dir}/elixir*.css")
127116
end
128117

129-
test "generates all listing files" do
130-
generate_docs_and_unzip(doc_config())
131-
content = File.read!("#{output_dir()}/OEBPS/content.opf")
118+
test "generates all listing files", %{tmp_dir: tmp_dir} = context do
119+
generate_docs_and_unzip(context, doc_config(context))
120+
content = File.read!(tmp_dir <> "/epub/OEBPS/content.opf")
132121

133122
assert content =~ ~r{.*"CompiledWithDocs\".*}ms
134123
assert content =~ ~r{.*"CompiledWithDocs.Nested\".*}ms
@@ -139,11 +128,11 @@ defmodule ExDoc.Formatter.EPUBTest do
139128
assert content =~ ~r{.*"Mix\.Tasks\.TaskWithDocs\".*}ms
140129
end
141130

142-
test "generates the readme file" do
143-
config = doc_config(main: "README")
144-
generate_docs_and_unzip(config)
131+
test "generates the readme file", %{tmp_dir: tmp_dir} = context do
132+
config = doc_config(context, main: "README")
133+
generate_docs_and_unzip(context, config)
145134

146-
content = File.read!("#{output_dir()}/OEBPS/readme.xhtml")
135+
content = File.read!(tmp_dir <> "/epub/OEBPS/readme.xhtml")
147136
assert content =~ ~r{<title>README [^<]*</title>}
148137
assert content =~ ~r{<a href="RandomError.xhtml"><code(\sclass="inline")?>RandomError</code>}
149138

@@ -153,14 +142,14 @@ defmodule ExDoc.Formatter.EPUBTest do
153142
assert content =~
154143
~r{<a href="TypesAndSpecs.Sub.xhtml"><code(\sclass="inline")?>TypesAndSpecs.Sub</code></a>}
155144

156-
content = File.read!("#{output_dir()}/OEBPS/nav.xhtml")
145+
content = File.read!(tmp_dir <> "/epub/OEBPS/nav.xhtml")
157146
assert content =~ ~r{<li><a href="readme.xhtml">README</a></li>}
158147
end
159148

160-
test "uses samp as highlight tag for markdown" do
161-
generate_docs_and_unzip(doc_config())
149+
test "uses samp as highlight tag for markdown", %{tmp_dir: tmp_dir} = context do
150+
generate_docs_and_unzip(context, doc_config(context))
162151

163-
assert File.read!("#{output_dir()}/OEBPS/CompiledWithDocs.xhtml") =~
152+
assert File.read!(tmp_dir <> "/epub/OEBPS/CompiledWithDocs.xhtml") =~
164153
"<samp class=\"nc\">CompiledWithDocs<\/samp>"
165154
end
166155

@@ -174,15 +163,17 @@ defmodule ExDoc.Formatter.EPUBTest do
174163
"CompiledWithDocs.Nested.xhtml"
175164
]
176165

177-
test "before_closing_*_tags required by the user are in the right place" do
166+
test "before_closing_*_tags required by the user are in the right place",
167+
%{tmp_dir: tmp_dir} = context do
178168
generate_docs_and_unzip(
179-
doc_config(
169+
context,
170+
doc_config(context,
180171
before_closing_head_tag: &before_closing_head_tag/1,
181172
before_closing_body_tag: &before_closing_body_tag/1
182173
)
183174
)
184175

185-
oebps_dir = "#{output_dir()}/OEBPS"
176+
oebps_dir = tmp_dir <> "/epub/OEBPS"
186177

187178
for basename <- @example_basenames do
188179
content = File.read!(Path.join(oebps_dir, basename))
@@ -191,21 +182,22 @@ defmodule ExDoc.Formatter.EPUBTest do
191182
end
192183
end
193184

194-
test "assets required by the user end up in the right place" do
185+
test "assets required by the user end up in the right place", %{tmp_dir: tmp_dir} = context do
195186
File.mkdir_p!("test/tmp/epub_assets/hello")
196187
File.touch!("test/tmp/epub_assets/hello/world.png")
197188

198189
generate_docs_and_unzip(
199-
doc_config(
190+
context,
191+
doc_config(context,
200192
assets: "test/tmp/epub_assets",
201193
logo: "test/fixtures/elixir.png",
202194
cover: "test/fixtures/elixir.png"
203195
)
204196
)
205197

206-
assert File.regular?("#{output_dir()}/OEBPS/assets/hello/world.png")
207-
assert File.regular?("#{output_dir()}/OEBPS/assets/logo.png")
208-
assert File.regular?("#{output_dir()}/OEBPS/assets/cover.png")
198+
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/hello/world.png")
199+
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/logo.png")
200+
assert File.regular?(tmp_dir <> "/epub/OEBPS/assets/cover.png")
209201
after
210202
File.rm_rf!("test/tmp/epub_assets")
211203
end

0 commit comments

Comments
 (0)