Skip to content

Commit 6ce366c

Browse files
kelvinstwhatyouhide
authored andcommitted
Better behavior for the default test task (#5561)
Before, if the task didn't find a test_helper.exs file, it failed. Now, if no test_helper.exs is found, a warning is emitted. Beside this, some messages on empty states were improved.
1 parent 10f9b78 commit 6ce366c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lib/mix/lib/mix/compilers/test.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ defmodule Mix.Compilers.Test do
3636
Mix.shell.info "No stale tests."
3737
:noop
3838

39+
[] when test_patterns == [] ->
40+
Mix.shell.info "There are no tests to run"
41+
:noop
42+
3943
[] ->
4044
Mix.shell.error "Test patterns did not match any file: " <> Enum.join(test_patterns, ", ")
4145
:noop

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ defmodule Mix.Tasks.Test do
122122
## Configuration
123123
124124
* `:test_paths` - list of paths containing test files, defaults to
125-
`["test"]`. It is expected all test paths to contain a `test_helper.exs`
126-
file.
125+
`["test"]` if the `test` directory exists, otherwise it defaults to `[]`.
126+
It is expected all test paths to contain a `test_helper.exs` file.
127127
128128
* `:test_pattern` - a pattern to load test files, defaults to `*_test.exs`.
129129
@@ -225,7 +225,7 @@ defmodule Mix.Tasks.Test do
225225
ex_unit_opts = ex_unit_opts(opts)
226226
ExUnit.configure(ex_unit_opts)
227227

228-
test_paths = project[:test_paths] || ["test"]
228+
test_paths = project[:test_paths] || default_test_paths()
229229
Enum.each(test_paths, &require_test_helper(&1))
230230
ExUnit.configure(merge_helper_opts(ex_unit_opts))
231231

@@ -365,4 +365,12 @@ defmodule Mix.Tasks.Test do
365365
Mix.raise "Cannot run tests because test helper file #{inspect file} does not exist"
366366
end
367367
end
368+
369+
defp default_test_paths do
370+
if File.dir?("test") do
371+
["test"]
372+
else
373+
[]
374+
end
375+
end
368376
end

lib/mix/test/mix/tasks/test_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ defmodule Mix.Tasks.TestTest do
108108
end
109109
end
110110

111+
test "logs test absence for a project with no test paths" do
112+
in_fixture "test_stale", fn ->
113+
File.rm_rf! "test"
114+
115+
assert_run_output "There are no tests to run"
116+
end
117+
end
118+
111119
test "--listen-on-stdin: runs tests after input" do
112120
in_fixture "test_stale", fn ->
113121
port = mix_port(~w[test --stale --listen-on-stdin])
@@ -190,6 +198,10 @@ defmodule Mix.Tasks.TestTest do
190198
end
191199

192200
defp assert_stale_run_output(opts \\ [], expected) do
193-
assert mix(~w[test --stale] ++ opts) =~ expected
201+
assert_run_output(["--stale" | opts], expected)
202+
end
203+
204+
defp assert_run_output(opts \\ [], expected) do
205+
assert mix(["test" | opts]) =~ expected
194206
end
195207
end

0 commit comments

Comments
 (0)