Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
eksperimental committed Feb 13, 2024
1 parent d5351b8 commit 8470c85
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
14 changes: 7 additions & 7 deletions lib/ex_doc/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ defmodule ExDoc.CLI do
--package Hex package name
--paths Prepends the given path to Erlang code path. The path might contain a glob
pattern but in that case, remember to quote it: --paths "_build/dev/lib/*/ebin".
This option can be given multiple times
--proglang The project's programming language, default: "elixir"
-q, --quiet Only output warnings and errors
--source-ref Branch/commit/tag used for source link inference, default: "master"
-u, --source-url URL to the source code
-v, --version Print ExDoc version
--warnings-as-errors Exit with non-zero status if doc generation has one or more warnings.
This option can be given multiple times.
--proglang The project's programming language, default: "elixir".
-q, --quiet Only output warnings and errors.
--source-ref Branch/commit/tag used for source link inference, default: "master".
-u, --source-url URL to the source code.
-v, --version Print ExDoc version.
--warnings-as-errors Exit with non-zero status if doc generation produces warnings.
## Custom config
Expand Down
4 changes: 3 additions & 1 deletion lib/ex_doc/formatter/epub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ defmodule ExDoc.Formatter.EPUB do
alias ExDoc.Formatter.HTML

@doc """
Generate EPUB documentation for the given modules.
Generates EPUB documentation for the given modules.
"""
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
def run(project_nodes, filtered_modules, config) when is_map(config) do
ExDoc.Utils.unset_warned()

config = normalize_config(config)
File.rm_rf!(config.output)
File.mkdir_p!(Path.join(config.output, "OEBPS"))
Expand Down
4 changes: 3 additions & 1 deletion lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ defmodule ExDoc.Formatter.HTML do
@assets_dir "assets"

@doc """
Generate HTML documentation for the given modules.
Generates HTML documentation for the given modules.
"""
@spec run([ExDoc.ModuleNode.t()], [ExDoc.ModuleNode.t()], ExDoc.Config.t()) :: String.t()
def run(project_nodes, filtered_modules, config) when is_map(config) do
ExDoc.Utils.unset_warned()

config = normalize_config(config)
config = %{config | output: Path.expand(config.output)}

Expand Down
17 changes: 12 additions & 5 deletions lib/ex_doc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ExDoc.Utils do
Emits a warning.
"""
def warn(message, stacktrace_info) do
put_warned()
set_warned()

# TODO: remove check when we require Elixir v1.14
if @elixir_gte_1_14? do
Expand All @@ -17,22 +17,29 @@ defmodule ExDoc.Utils do
end
end

@doc false
def put_warned() do
@doc """
Stores that a warning has been generated.
"""
def set_warned() do
unless warned?() do
:persistent_term.put({__MODULE__, :warned?}, true)
end

true
end

@doc false
def delete_warned() do
@doc """
Removes that a warning has been generated.
"""
def unset_warned() do
if warned?() do
:persistent_term.put({__MODULE__, :warned?}, false)
end
end

@doc """
Returns `true` if any warning has been generated during the document building. Otherwise returns `false`.
"""
def warned?() do
:persistent_term.get({__MODULE__, :warned?}, false)
end
Expand Down
10 changes: 5 additions & 5 deletions test/ex_doc/cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule ExDoc.CLITest do
@describetag :warnings

test "exits with code 0 when no warnings" do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

{[html, epub], _io} = run(["ExDoc", "1.2.3", @ebin, "--warnings-as-errors"])

Expand All @@ -98,7 +98,7 @@ defmodule ExDoc.CLITest do
end

test "exits with 1 when there is a warning" do
ExDoc.Utils.put_warned()
ExDoc.Utils.set_warned()

fun = fn ->
run(["ExDoc", "1.2.3", @ebin, "--warnings-as-errors"])
Expand All @@ -114,9 +114,9 @@ defmodule ExDoc.CLITest do
end

test "exits with 1 when there are multiple warnings" do
ExDoc.Utils.put_warned()
ExDoc.Utils.put_warned()
ExDoc.Utils.put_warned()
ExDoc.Utils.set_warned()
ExDoc.Utils.set_warned()
ExDoc.Utils.set_warned()

fun = fn ->
run(["ExDoc", "1.2.3", @ebin, "--warnings-as-errors"])
Expand Down
4 changes: 2 additions & 2 deletions test/ex_doc/formatter/epub_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ defmodule ExDoc.Formatter.EPUBTest do
@describetag :warnings

test "multiple warnings are registered when using warnings_as_errors: true", context do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

output =
capture_io(:stderr, fn ->
Expand All @@ -279,7 +279,7 @@ defmodule ExDoc.Formatter.EPUBTest do
end

test "warnings are registered even with warnings_as_errors: false", context do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

capture_io(:stderr, fn ->
generate_docs(
Expand Down
6 changes: 3 additions & 3 deletions test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ defmodule ExDoc.Formatter.HTMLTest do
@describetag :warnings

test "single warning is registered when using warnings_as_errors: true", context do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

output =
capture_io(:stderr, fn ->
Expand All @@ -166,7 +166,7 @@ defmodule ExDoc.Formatter.HTMLTest do
end

test "multiple warnings are registered when using warnings_as_errors: true", context do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

output =
capture_io(:stderr, fn ->
Expand All @@ -190,7 +190,7 @@ defmodule ExDoc.Formatter.HTMLTest do
end

test "warnings are registered even with warnings_as_errors: false", context do
ExDoc.Utils.delete_warned()
ExDoc.Utils.unset_warned()

capture_io(:stderr, fn ->
generate_docs(
Expand Down
8 changes: 4 additions & 4 deletions test/mix/tasks/docs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,16 @@ defmodule Mix.Tasks.DocsTest do
end

test "exits with 1 due to warning, with flag --warnings_as_errors", context do
ExDoc.Utils.put_warned()
ExDoc.Utils.set_warned()

assert catch_exit(run(context, [], app: :ex_doc, docs: [warnings_as_errors: true])) ==
{:shutdown, 1}
end

test "exits with 1 due to multiple warnings, with flag --warnings_as_errors", context do
ExDoc.Utils.put_warned()
ExDoc.Utils.put_warned()
ExDoc.Utils.put_warned()
ExDoc.Utils.set_warned()
ExDoc.Utils.set_warned()
ExDoc.Utils.set_warned()

assert catch_exit(run(context, [], app: :ex_doc, docs: [warnings_as_errors: true])) ==
{:shutdown, 1}
Expand Down

0 comments on commit 8470c85

Please sign in to comment.