Skip to content

Commit 898b333

Browse files
Create .exdoc file after warning about using an existing directory (#1713)
1 parent a09e708 commit 898b333

File tree

5 files changed

+78
-54
lines changed

5 files changed

+78
-54
lines changed

lib/ex_doc/formatter/html.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ defmodule ExDoc.Formatter.HTML do
165165
"ExDoc is outputting to an existing directory. " <>
166166
"Beware documentation output may be mixed with other entries"
167167
)
168+
169+
create.(root)
168170
end
169171
end
170172

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule ExDoc.Formatter.EPUBIOTest do
2+
use ExUnit.Case, async: false
3+
4+
@moduletag :tmp_dir
5+
6+
test "succeeds if trying to write into an empty existing directory", %{tmp_dir: tmp_dir} do
7+
File.mkdir!("#{tmp_dir}/doc")
8+
9+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
10+
generate_docs(tmp_dir)
11+
end) == ""
12+
end
13+
14+
test "warns if trying to write into existing directory with files", %{tmp_dir: tmp_dir} do
15+
File.mkdir!("#{tmp_dir}/doc")
16+
File.touch!("#{tmp_dir}/doc/foo.txt")
17+
18+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
19+
generate_docs(tmp_dir)
20+
end) =~ "ExDoc is outputting to an existing directory"
21+
22+
# Warn only once
23+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
24+
generate_docs(tmp_dir)
25+
end) == ""
26+
end
27+
28+
defp generate_docs(tmp_dir) do
29+
config = [
30+
app: :foo,
31+
formatter: "epub",
32+
output: "#{tmp_dir}/doc",
33+
source_beam: "#{tmp_dir}/ebin"
34+
]
35+
36+
ExDoc.generate_docs("Foo", "1.0.0", config)
37+
end
38+
end

test/ex_doc/formatter/epub_test.exs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,6 @@ defmodule ExDoc.Formatter.EPUBTest do
103103
assert File.regular?(tmp_dir <> "/epub/another_dir/#{doc_config(context)[:project]}.epub")
104104
end
105105

106-
test "succeeds if trying to write into an empty existing directory", context do
107-
config = doc_config(context)
108-
109-
new_output = config[:output] <> "/new-dir"
110-
File.mkdir_p!(new_output)
111-
112-
new_config = Keyword.put(config, :output, new_output)
113-
114-
refute ExUnit.CaptureIO.capture_io(:stderr, fn ->
115-
generate_docs(new_config)
116-
end) =~ "ExDoc is outputting to an existing directory"
117-
end
118-
119-
test "warns if trying to write into existing directory with files", context do
120-
config = doc_config(context)
121-
new_output = config[:output] <> "/new-dir"
122-
123-
File.mkdir_p!(new_output)
124-
File.touch!(Path.join(new_output, "dummy-file"))
125-
126-
new_config = Keyword.put(config, :output, new_output)
127-
128-
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
129-
generate_docs(new_config)
130-
end) =~ "ExDoc is outputting to an existing directory"
131-
end
132-
133106
test "generates an EPUB file with a standardized structure", %{tmp_dir: tmp_dir} = context do
134107
generate_docs_and_unzip(context, doc_config(context))
135108

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule ExDoc.Formatter.HtmlIOTest do
2+
use ExUnit.Case, async: false
3+
4+
@moduletag :tmp_dir
5+
6+
test "succeeds if trying to write into an empty existing directory", %{tmp_dir: tmp_dir} do
7+
File.mkdir!("#{tmp_dir}/doc")
8+
9+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
10+
generate_docs(tmp_dir)
11+
end) == ""
12+
end
13+
14+
test "warns if trying to write into existing directory with files", %{tmp_dir: tmp_dir} do
15+
File.mkdir!("#{tmp_dir}/doc")
16+
File.touch!("#{tmp_dir}/doc/foo.txt")
17+
18+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
19+
generate_docs(tmp_dir)
20+
end) =~ "ExDoc is outputting to an existing directory"
21+
22+
# Warn only once
23+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
24+
generate_docs(tmp_dir)
25+
end) == ""
26+
end
27+
28+
defp generate_docs(tmp_dir) do
29+
config = [
30+
app: :foo,
31+
formatter: "html",
32+
output: "#{tmp_dir}/doc",
33+
source_beam: "#{tmp_dir}/ebin"
34+
]
35+
36+
ExDoc.generate_docs("Foo", "1.0.0", config)
37+
end
38+
end

test/ex_doc/formatter/html_test.exs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,33 +180,6 @@ defmodule ExDoc.Formatter.HTMLTest do
180180
refute content_module =~ re[:index][:refresh]
181181
end
182182

183-
test "succeeds if trying to write into an empty existing directory", context do
184-
config = doc_config(context)
185-
186-
new_output = config[:output] <> "/new-dir"
187-
File.mkdir_p!(new_output)
188-
189-
new_config = Keyword.put(config, :output, new_output)
190-
191-
refute ExUnit.CaptureIO.capture_io(:stderr, fn ->
192-
generate_docs(new_config)
193-
end) =~ "ExDoc is outputting to an existing directory"
194-
end
195-
196-
test "warns if trying to write into existing directory with files", context do
197-
config = doc_config(context)
198-
new_output = config[:output] <> "/new-dir"
199-
200-
File.mkdir_p!(new_output)
201-
File.touch!(Path.join(new_output, "dummy-file"))
202-
203-
new_config = Keyword.put(config, :output, new_output)
204-
205-
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
206-
generate_docs(new_config)
207-
end) =~ "ExDoc is outputting to an existing directory"
208-
end
209-
210183
test "allows to set the authors of the document", %{tmp_dir: tmp_dir} = context do
211184
generate_docs(doc_config(context, authors: ["John Doe", "Jane Doe"]))
212185
content_index = File.read!(tmp_dir <> "/html/api-reference.html")

0 commit comments

Comments
 (0)