1
1
defmodule ExDoc.Formatter.EPUBTest do
2
- use ExUnit.Case
2
+ use ExUnit.Case , async: true
3
3
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
16
5
17
6
@ before_closing_head_tag_content_epub "UNIQUE:<dont-escape>©BEFORE-CLOSING-HEAD-TAG-HTML</dont-escape>"
18
7
@ before_closing_body_tag_content_epub "UNIQUE:<dont-escape>©BEFORE-CLOSING-BODY-TAG-HTML</dont-escape>"
19
8
20
9
defp before_closing_head_tag ( :epub ) , do: @ before_closing_head_tag_content_epub
21
10
defp before_closing_body_tag ( :epub ) , do: @ before_closing_body_tag_content_epub
22
11
23
- defp doc_config do
12
+ defp doc_config ( % { tmp_dir: tmp_dir } = _context ) do
24
13
[
25
14
app: :elixir ,
26
15
project: "Elixir" ,
27
16
version: "1.0.1" ,
28
17
formatter: "epub" ,
29
- output: output_dir ( ) ,
30
- source_beam: beam_dir ( ) ,
18
+ output: tmp_dir <> "/epub" ,
19
+ source_beam: "test/tmp/beam" ,
31
20
extras: [ "test/fixtures/README.md" ] ,
32
21
skip_undefined_reference_warnings_on: [ "Warnings" ]
33
22
]
34
23
end
35
24
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 )
38
27
end
39
28
40
29
defp generate_docs ( config ) do
41
30
ExDoc . generate_docs ( config [ :project ] , config [ :version ] , config )
42
31
end
43
32
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 ] } " )
47
36
48
- "#{ doc_config ( ) [ :output ] } /#{ doc_config ( ) [ :project ] } .epub"
37
+ "#{ doc_config ( context ) [ :output ] } /#{ doc_config ( context ) [ :project ] } .epub"
49
38
|> String . to_charlist ( )
50
39
|> :zip . unzip ( cwd: unzip_dir )
51
40
end
52
41
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" ) )
55
44
56
- content = File . read! ( " #{ output_dir ( ) } /OEBPS/RandomError.xhtml")
45
+ content = File . read! ( tmp_dir <> "/epub /OEBPS/RandomError.xhtml")
57
46
assert content =~ ~r{ <html.*lang="en".*xmlns:epub="http://www.idpf.org/2007/ops">} ms
58
47
assert content =~ ~r{ <meta charset="utf-8" />} ms
59
48
assert content =~ ~r{ <meta name="generator" content="ExDoc v[^"]+" />}
60
49
assert content =~ ~r{ <title>RandomError - Elixir v1.0.1</title>}
61
50
end
62
51
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" ) )
65
54
66
- content = File . read! ( " #{ output_dir ( ) } /OEBPS/RandomError.xhtml")
55
+ content = File . read! ( tmp_dir <> "/epub /OEBPS/RandomError.xhtml")
67
56
assert content =~ ~r{ <html.*lang="fr".*xmlns:epub="http://www.idpf.org/2007/ops">} ms
68
57
end
69
58
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" ] ) )
72
61
73
- content = File . read! ( " #{ output_dir ( ) } /OEBPS/content.opf")
62
+ content = File . read! ( tmp_dir <> "/epub /OEBPS/content.opf")
74
63
assert content =~ ~r{ <dc:creator id="author1">John Doe</dc:creator>}
75
64
assert content =~ ~r{ <dc:creator id="author2">Jane Doe</dc:creator>}
76
65
end
77
66
78
- test "raises when assets are invalid" do
67
+ test "raises when assets are invalid" , context do
79
68
File . mkdir_p! ( "test/tmp/epub_assets/hello" )
80
69
File . touch! ( "test/tmp/epub_assets/hello/world.pdf" )
81
70
82
71
assert_raise (
83
72
RuntimeError ,
84
73
~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
86
75
)
87
76
after
88
77
File . rm_rf! ( "test/tmp/epub_assets" )
89
78
end
90
79
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")
94
83
end
95
84
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")
99
88
end
100
89
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" )
103
92
generate_docs ( config )
104
93
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")
106
95
end
107
96
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 ) )
110
99
111
- root_dir = " #{ output_dir ( ) } "
100
+ root_dir = tmp_dir <> "/epub "
112
101
meta_dir = "#{ root_dir } /META-INF"
113
102
oebps_dir = "#{ root_dir } /OEBPS"
114
103
dist_dir = "#{ oebps_dir } /dist"
@@ -126,9 +115,9 @@ defmodule ExDoc.Formatter.EPUBTest do
126
115
assert [ _ ] = Path . wildcard ( "#{ dist_dir } /elixir*.css" )
127
116
end
128
117
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")
132
121
133
122
assert content =~ ~r{ .*"CompiledWithDocs\" .*} ms
134
123
assert content =~ ~r{ .*"CompiledWithDocs.Nested\" .*} ms
@@ -139,11 +128,11 @@ defmodule ExDoc.Formatter.EPUBTest do
139
128
assert content =~ ~r{ .*"Mix\. Tasks\. TaskWithDocs\" .*} ms
140
129
end
141
130
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 )
145
134
146
- content = File . read! ( " #{ output_dir ( ) } /OEBPS/readme.xhtml")
135
+ content = File . read! ( tmp_dir <> "/epub /OEBPS/readme.xhtml")
147
136
assert content =~ ~r{ <title>README [^<]*</title>}
148
137
assert content =~ ~r{ <a href="RandomError.xhtml"><code(\s class="inline")?>RandomError</code>}
149
138
@@ -153,14 +142,14 @@ defmodule ExDoc.Formatter.EPUBTest do
153
142
assert content =~
154
143
~r{ <a href="TypesAndSpecs.Sub.xhtml"><code(\s class="inline")?>TypesAndSpecs.Sub</code></a>}
155
144
156
- content = File . read! ( " #{ output_dir ( ) } /OEBPS/nav.xhtml")
145
+ content = File . read! ( tmp_dir <> "/epub /OEBPS/nav.xhtml")
157
146
assert content =~ ~r{ <li><a href="readme.xhtml">README</a></li>}
158
147
end
159
148
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 ) )
162
151
163
- assert File . read! ( " #{ output_dir ( ) } /OEBPS/CompiledWithDocs.xhtml") =~
152
+ assert File . read! ( tmp_dir <> "/epub /OEBPS/CompiledWithDocs.xhtml") =~
164
153
"<samp class=\" nc\" >CompiledWithDocs<\/ samp>"
165
154
end
166
155
@@ -174,15 +163,17 @@ defmodule ExDoc.Formatter.EPUBTest do
174
163
"CompiledWithDocs.Nested.xhtml"
175
164
]
176
165
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
178
168
generate_docs_and_unzip (
179
- doc_config (
169
+ context ,
170
+ doc_config ( context ,
180
171
before_closing_head_tag: & before_closing_head_tag / 1 ,
181
172
before_closing_body_tag: & before_closing_body_tag / 1
182
173
)
183
174
)
184
175
185
- oebps_dir = " #{ output_dir ( ) } /OEBPS"
176
+ oebps_dir = tmp_dir <> "/epub /OEBPS"
186
177
187
178
for basename <- @ example_basenames do
188
179
content = File . read! ( Path . join ( oebps_dir , basename ) )
@@ -191,21 +182,22 @@ defmodule ExDoc.Formatter.EPUBTest do
191
182
end
192
183
end
193
184
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
195
186
File . mkdir_p! ( "test/tmp/epub_assets/hello" )
196
187
File . touch! ( "test/tmp/epub_assets/hello/world.png" )
197
188
198
189
generate_docs_and_unzip (
199
- doc_config (
190
+ context ,
191
+ doc_config ( context ,
200
192
assets: "test/tmp/epub_assets" ,
201
193
logo: "test/fixtures/elixir.png" ,
202
194
cover: "test/fixtures/elixir.png"
203
195
)
204
196
)
205
197
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")
209
201
after
210
202
File . rm_rf! ( "test/tmp/epub_assets" )
211
203
end
0 commit comments