Skip to content

Commit 550d9b7

Browse files
committed
Simplify warnings as errors handling
1 parent 8c11a7a commit 550d9b7

File tree

7 files changed

+12
-34
lines changed

7 files changed

+12
-34
lines changed

lib/ex_doc/cli.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ defmodule ExDoc.CLI do
9292
quiet? ||
9393
IO.puts(IO.ANSI.format([:green, "View #{inspect(formatter)} docs at #{inspect(index)}"]))
9494

95-
if opts[:warnings_as_errors] == true and ExDoc.Utils.warned?() do
95+
if opts[:warnings_as_errors] == true and ExDoc.Utils.unset_warned() do
9696
{:error, %{reason: :warnings_as_errors, formatter: formatter}}
9797
else
9898
{:ok, index}

lib/ex_doc/formatter/epub.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ defmodule ExDoc.Formatter.EPUB do
1212
"""
1313
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
1414
def run(project_nodes, filtered_modules, config) when is_map(config) do
15-
Utils.unset_warned()
16-
1715
config = normalize_config(config)
1816
File.rm_rf!(config.output)
1917
File.mkdir_p!(Path.join(config.output, "OEBPS"))

lib/ex_doc/formatter/html.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ defmodule ExDoc.Formatter.HTML do
1212
"""
1313
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
1414
def run(project_nodes, filtered_modules, config) when is_map(config) do
15-
Utils.unset_warned()
16-
1715
config = normalize_config(config)
1816
config = %{config | output: Path.expand(config.output)}
1917

lib/ex_doc/utils.ex

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,17 @@ defmodule ExDoc.Utils do
55
Emits a warning.
66
"""
77
def warn(message, stacktrace_info) do
8-
set_warned()
8+
:persistent_term.put({__MODULE__, :warned?}, true)
99
IO.warn(message, stacktrace_info)
1010
end
1111

1212
@doc """
13-
Stores that a warning has been generated.
14-
"""
15-
def set_warned() do
16-
unless warned?() do
17-
:persistent_term.put({__MODULE__, :warned?}, true)
18-
end
19-
20-
true
21-
end
22-
23-
@doc """
24-
Removes that a warning has been generated.
13+
Removes that a warning has been generated and returns its previous value.
2514
"""
2615
def unset_warned() do
27-
if warned?() do
28-
:persistent_term.put({__MODULE__, :warned?}, false)
29-
end
30-
end
31-
32-
@doc """
33-
Returns `true` if any warning has been generated during the document building. Otherwise returns `false`.
34-
"""
35-
def warned?() do
36-
:persistent_term.get({__MODULE__, :warned?}, false)
16+
warned? = :persistent_term.get({__MODULE__, :warned?}, false)
17+
:persistent_term.erase({__MODULE__, :warned?})
18+
warned?
3719
end
3820

3921
@doc """

lib/mix/tasks/docs.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ defmodule Mix.Tasks.Docs do
404404
browser_open(index)
405405
end
406406

407-
if options[:warnings_as_errors] == true and ExDoc.Utils.warned?() do
407+
if options[:warnings_as_errors] == true and ExDoc.Utils.unset_warned() do
408408
{:error, %{reason: :warnings_as_errors, formatter: formatter}}
409409
else
410410
{:ok, index}

test/ex_doc/formatter/epub_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ defmodule ExDoc.Formatter.EPUBTest do
266266
assert output =~ ~S|doc `Warnings.bar/0`|
267267
end
268268

269-
assert Utils.warned?() == true
269+
assert Utils.unset_warned()
270270
end
271271

272272
test "warnings are registered even with warnings_as_errors: false", context do
@@ -281,7 +281,7 @@ defmodule ExDoc.Formatter.EPUBTest do
281281
)
282282
end)
283283

284-
assert Utils.warned?() == true
284+
assert Utils.unset_warned()
285285
end
286286
end
287287
end

test/ex_doc/formatter/html_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ defmodule ExDoc.Formatter.HTMLTest do
158158
assert output =~
159159
~r"warning:(\e\[0m)? .*index.html redirects to DoesNotExist.html, which does not exist\n"
160160

161-
assert Utils.warned?() == true
161+
assert Utils.unset_warned()
162162
end
163163

164164
test "multiple warnings are registered when using warnings_as_errors: true", context do
@@ -182,7 +182,7 @@ defmodule ExDoc.Formatter.HTMLTest do
182182
assert output =~ ~S|doc `Warnings.bar/0`|
183183
end
184184

185-
assert Utils.warned?() == true
185+
assert Utils.unset_warned()
186186
end
187187

188188
test "warnings are registered even with warnings_as_errors: false", context do
@@ -197,7 +197,7 @@ defmodule ExDoc.Formatter.HTMLTest do
197197
)
198198
end)
199199

200-
assert Utils.warned?() == true
200+
assert Utils.unset_warned()
201201
end
202202
end
203203

0 commit comments

Comments
 (0)