Skip to content

Commit fe340fe

Browse files
author
José Valim
committed
test_helper files still need to be required upfront
Since they have to be required upfront, it makes no sense to have a Code.require_file("test_helper.exs", __DIR__) in Mix projects since the files can't run in isolation anyway.
1 parent 1e90076 commit fe340fe

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* [Kernel] Functions now points to the module and function they were defined when inspected
1111
* [Kernel] A documentation attached to a function that is never defined now prints warnings
1212
* [List] Add `List.keysort/2`
13-
* [Mix] `:test_helper` project configuration did not affect `mix test` and was therefore removed
13+
* [Mix] `:test_helper` project configuration did not affect `mix test` and was therefore removed. A `test/test_helper.exs` file is still necessary albeit it doesn't need to be automatically required in each test file
1414
* [Mix] Add manifests for yecc, leex and Erlang compilers, making it easier to detect dependencies in between compilers and providing a more useful clean behaviour
1515
* [Mix] `mix help` now outputs information about the default mix task
1616
* [Mix] Add `--no-deps-check` option to `mix run`, `mix compile` and friends to not check dependency status

lib/mix/lib/mix/tasks/new.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ defmodule Mix.Tasks.New do
218218
"""
219219

220220
embed_template :test_lib, """
221-
Code.require_file "test_helper.exs", __DIR__
222-
223221
defmodule <%= @mod %>Test do
224222
use ExUnit.Case
225223

lib/mix/lib/mix/tasks/test.ex

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ defmodule Mix.Tasks.Test do
2929
@moduledoc """
3030
Run the tests for a project.
3131
32-
This task starts the current application and then requires
33-
all files that match the given `test_pattern` in parallel.
32+
This task starts the current application, loads up
33+
`test/test_helper.exs` and then requires all files matching the
34+
`test/**/_test.exs` pattern in parallel.
3435
35-
It is expected that each test file will properly setup the
36-
test framework, usually by providing a `test/test_helper.exs`
37-
file that is required at the top of the file.
36+
A list of files can be given after the task name in order to select
37+
the files to compile:
3838
39-
A list of files can be given after the task name in
40-
order to select the files to compile.
39+
mix test test/some/particular/file_test.exs
4140
4241
## Command line options
4342
@@ -51,7 +50,8 @@ defmodule Mix.Tasks.Test do
5150
5251
## Configuration
5352
54-
* `:test_paths` - list of paths containing test files, defaults to `["test"]`
53+
* `:test_paths` - list of paths containing test files, defaults to `["test"]`.
54+
it is expected all test paths to contain a `test_helper.exs` file
5555
5656
* `:test_pattern` - a pattern to load test files, defaults to `*_test.exs`
5757
@@ -109,13 +109,26 @@ defmodule Mix.Tasks.Test do
109109
"please set the output directory as test_coverage: [output: \"PATH\"] instead"
110110
end
111111

112-
test_paths = if files == [], do: project[:test_paths] || ["test"], else: files
113-
test_pattern = project[:test_pattern] || "*_test.exs"
114-
115112
:application.load(:ex_unit)
116113
ExUnit.configure(Dict.take(opts, [:trace, :max_cases, :color]))
117114

115+
test_paths = project[:test_paths] || ["test"]
116+
Enum.each(test_paths, require_test_helper(&1))
117+
118+
test_paths = if files == [], do: test_paths, else: files
119+
test_pattern = project[:test_pattern] || "*_test.exs"
120+
118121
files = Mix.Utils.extract_files(test_paths, test_pattern)
119122
Kernel.ParallelRequire.files files
120123
end
124+
125+
defp require_test_helper(dir) do
126+
file = Path.join(dir, "test_helper.exs")
127+
128+
if File.exists?(file) do
129+
Code.require_file file
130+
else
131+
raise Mix.Error, message: "Cannot run tests because test helper file #{inspect file} does not exist"
132+
end
133+
end
121134
end

0 commit comments

Comments
 (0)