summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamir Vandic <info@dvic.io>2023-05-07 12:57:44 +0200
committerGitHub <noreply@github.com>2023-05-07 12:57:44 +0200
commitef673a103dd58b7c4ff1b3c796201a59e072dbb3 (patch)
treea45262d0a59b1455ee8e6fb5d4f82a6693b1725b
parent90dd12a5ba480feae1531e7f0cab85b2c998caf6 (diff)
downloadelixir-ef673a103dd58b7c4ff1b3c796201a59e072dbb3.tar.gz
Pass module tags to setup_all (#12545)
-rw-r--r--lib/ex_unit/lib/ex_unit.ex3
-rw-r--r--lib/ex_unit/lib/ex_unit/case.ex18
-rw-r--r--lib/ex_unit/lib/ex_unit/runner.ex12
-rw-r--r--lib/ex_unit/test/ex_unit/case_test.exs6
4 files changed, 35 insertions, 4 deletions
diff --git a/lib/ex_unit/lib/ex_unit.ex b/lib/ex_unit/lib/ex_unit.ex
index 72dd10a65..8ff717fcb 100644
--- a/lib/ex_unit/lib/ex_unit.ex
+++ b/lib/ex_unit/lib/ex_unit.ex
@@ -128,12 +128,13 @@ defmodule ExUnit do
* `:tests` - all tests in this module
"""
- defstruct [:file, :name, :state, tests: []]
+ defstruct [:file, :name, :state, tags: %{}, tests: []]
@type t :: %__MODULE__{
file: binary(),
name: module,
state: ExUnit.state(),
+ tags: map,
tests: [ExUnit.Test.t()]
}
end
diff --git a/lib/ex_unit/lib/ex_unit/case.ex b/lib/ex_unit/lib/ex_unit/case.ex
index 73823b5d8..158701e11 100644
--- a/lib/ex_unit/lib/ex_unit/case.ex
+++ b/lib/ex_unit/lib/ex_unit/case.ex
@@ -138,6 +138,8 @@ defmodule ExUnit.Case do
If the same key is set via `@tag`, the `@tag` value has higher
precedence.
+ The `setup_all` blocks only receive tags that are set using `@moduletag`.
+
### Known tags
The following tags are set automatically by ExUnit and are
@@ -491,9 +493,23 @@ defmodule ExUnit.Case do
|> Enum.reverse()
|> Macro.escape()
+ moduletag = Module.get_attribute(env.module, :moduletag)
+
+ tags =
+ moduletag
+ |> normalize_tags()
+ |> validate_tags()
+ |> Map.new()
+ |> Map.merge(%{module: env.module, case: env.module})
+
quote do
def __ex_unit__ do
- %ExUnit.TestModule{file: __ENV__.file, name: __MODULE__, tests: unquote(tests)}
+ %ExUnit.TestModule{
+ file: __ENV__.file,
+ name: __MODULE__,
+ tags: unquote(Macro.escape(tags)),
+ tests: unquote(tests)
+ }
end
end
end
diff --git a/lib/ex_unit/lib/ex_unit/runner.ex b/lib/ex_unit/lib/ex_unit/runner.ex
index 2d68241da..19de95114 100644
--- a/lib/ex_unit/lib/ex_unit/runner.ex
+++ b/lib/ex_unit/lib/ex_unit/runner.ex
@@ -309,7 +309,7 @@ defmodule ExUnit.Runner do
result =
try do
- {:ok, module.__ex_unit__(:setup_all, %{module: module, case: module})}
+ {:ok, module.__ex_unit__(:setup_all, test_module.tags)}
catch
kind, error ->
failed = failed(kind, error, prune_stacktrace(__STACKTRACE__))
@@ -387,7 +387,7 @@ defmodule ExUnit.Runner do
maybe_capture_log(capture_log, test, fn ->
context = maybe_create_tmp_dir(test, context, tags)
- case exec_test_setup(test, Map.merge(test.tags, context)) do
+ case exec_test_setup(test, test_tags_context(test.tags, context)) do
{:ok, context} -> exec_test(test, context)
{:error, test} -> test
end
@@ -399,6 +399,14 @@ defmodule ExUnit.Runner do
end)
end
+ defp test_tags_context(test_tags, parent_context) do
+ Map.merge(parent_context, test_tags, fn
+ :tmp_dir, _v0, false -> false
+ :tmp_dir, v0, _v1 -> v0
+ _k, _v0, v1 -> v1
+ end)
+ end
+
defp maybe_capture_log(true, test, fun) do
maybe_capture_log([], test, fun)
end
diff --git a/lib/ex_unit/test/ex_unit/case_test.exs b/lib/ex_unit/test/ex_unit/case_test.exs
index 6f64188c9..2c2635912 100644
--- a/lib/ex_unit/test/ex_unit/case_test.exs
+++ b/lib/ex_unit/test/ex_unit/case_test.exs
@@ -42,8 +42,14 @@ defmodule ExUnit.CaseTest do
assert is_nil(context[:world])
end
+ # tags are passed to setup_all
+ setup_all context do
+ %{moduletag_from_setup_all: context[:moduletag]}
+ end
+
test "module tags", context do
assert context[:moduletag] == true
+ assert context[:moduletag_from_setup_all] == true
end
@tag moduletag: :overridden