Skip to content

Commit 658d7c7

Browse files
author
José Valim
committed
ExUnit callbacks must return :ok or { :ok, data }
1 parent 1f491df commit 658d7c7

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

Diff for: lib/elixir/test/elixir/file_test.exs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ defmodule FileTest do
1313
setup do
1414
src = fixture_path("cp_r")
1515
:file.make_symlink 'certainly/invalid', Path.join([src, "c"])
16-
[]
16+
:ok
1717
end
1818

1919
teardown do
2020
src = fixture_path("cp_r")
2121
File.rm Path.join([src, "c"])
22-
[]
22+
:ok
2323
end
2424

2525
test :cp_with_src_file_and_dest_file do
@@ -682,13 +682,13 @@ defmodule FileTest do
682682
setup do
683683
src = fixture_path("cp_r")
684684
:file.make_symlink 'certainly/invalid', Path.join([src, "c"])
685-
[]
685+
:ok
686686
end
687687

688688
teardown do
689689
src = fixture_path("cp_r")
690690
File.rm Path.join([src, "c"])
691-
[]
691+
:ok
692692
end
693693

694694
test :rm_file do

Diff for: lib/ex_unit/lib/ex_unit/callbacks.ex

+13-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ defmodule ExUnit.Callbacks do
1414
setup do
1515
IO.puts "This is a setup callback"
1616
17-
# Return no extra meta data
18-
[hello: "world"]
17+
# Returns extra metadata
18+
{ :ok, [hello: "world"] }
1919
end
2020
2121
setup context do
@@ -24,6 +24,9 @@ defmodule ExUnit.Callbacks do
2424
2525
# The metadata returned by the previous setup as well
2626
assert context[:hello] == "world"
27+
28+
# No metadata
29+
:ok
2730
end
2831
2932
test "always pass" do
@@ -115,14 +118,21 @@ defmodule ExUnit.Callbacks do
115118

116119
## Helpers
117120

121+
def __merge__(_mod, other, :ok), do: other
122+
def __merge__(_mod, other, { :ok, data }), do: Keyword.merge(other, data)
123+
def __merge__(mod, _, failure) do
124+
raise "expected ExUnit callback in #{inspect mod} to return :ok " <>
125+
" or { :ok, data }, got #{inspect failure} instead"
126+
end
127+
118128
defp compile_callbacks(env, kind) do
119129
callbacks = Module.get_attribute(env.module, kind) |> Enum.reverse
120130

121131
acc =
122132
Enum.reduce callbacks, quote(do: context), fn(callback, acc) ->
123133
quote do
124134
context = unquote(acc)
125-
Keyword.merge(context, unquote(callback)(context))
135+
unquote(__MODULE__).__merge__(__MODULE__, context, unquote(callback)(context))
126136
end
127137
end
128138

Diff for: lib/ex_unit/test/ex_unit/callbacks_test.exs

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ defmodule ExUnit.CallbacksTest do
44
use ExUnit.Case
55

66
setup_all do
7-
[context: :setup_all]
7+
{ :ok, [context: :setup_all] }
88
end
99

1010
setup do
11-
[initial_setup: true]
11+
{ :ok, [initial_setup: true] }
1212
end
1313

1414
setup context do
1515
assert context[:initial_setup]
1616
assert context[:context] == :setup_all
17-
[context: :setup]
17+
{ :ok, [context: :setup] }
1818
end
1919

2020
setup context do
@@ -23,23 +23,23 @@ defmodule ExUnit.CallbacksTest do
2323
else
2424
Process.put(:ex_unit_callback, context[:test])
2525
end
26-
[]
26+
:ok
2727
end
2828

2929
teardown context do
3030
assert context[:context] == :setup
31-
[]
31+
:ok
3232
end
3333

3434
teardown context do
3535
assert Process.get(:ex_unit_callback) == context[:test]
3636
Process.delete(:ex_unit_callback)
37-
[]
37+
:ok
3838
end
3939

4040
teardown_all context do
4141
assert context[:context] == :setup_all
42-
[]
42+
:ok
4343
end
4444

4545
test "callbacks can run custom code" do

Diff for: lib/ex_unit/test/ex_unit/case_template_test.exs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ defmodule ExUnit.SampleCase do
1414
end
1515

1616
setup_all do
17-
[context: :setup_all]
17+
{ :ok, [context: :setup_all] }
1818
end
1919

2020
setup context do
2121
assert context[:context] == :setup_all
22-
[context: :setup]
22+
{ :ok, [context: :setup] }
2323
end
2424

2525
teardown context do
2626
assert context[:context] == :setup
27-
[]
27+
:ok
2828
end
2929

3030
teardown_all context do
3131
assert context[:context] == :setup_all
32-
[]
32+
:ok
3333
end
3434
end
3535

Diff for: lib/mix/test/test_helper.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ defmodule MixTest.Case do
5959
Mix.Deps.Converger.clear_cache
6060
System.put_env("MIX_HOME", tmp_path(".mix"))
6161
del_tmp_paths
62-
[]
62+
:ok
6363
end
6464
end
6565
end

0 commit comments

Comments
 (0)