Skip to content

Commit 8e12c62

Browse files
committed
implement suggestions from code review
1 parent 42e9086 commit 8e12c62

File tree

10 files changed

+87
-48
lines changed

10 files changed

+87
-48
lines changed

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,27 +234,29 @@ defmodule Mix.Tasks.Test do
234234
In Elixir versions earlier than 1.19.0, this option defaulted to `*_test.exs`,
235235
but to allow better warnings for misnamed test files, it since matches any
236236
Elixir file and expects those to be filtered by `:test_load_filters` and
237-
`:test_warn_filters`.
237+
`:test_ignore_filters`.
238238
239239
* `:test_load_filters` - a list of files, regular expressions or one-arity
240240
functions to restrict which files matched by the `:test_pattern` are loaded.
241-
Defaults to `[~r/.*_test\\.exs$/]`
241+
Defaults to `[&String.ends_with?(&1, "_test.exs")]`. Paths are relative to
242+
the project root and separated by `/`, even on Windows.
242243
243-
* `:test_warn_filters` - a list of files, regular expressions or one-arity
244+
* `:test_ignore_filters` - a list of files, regular expressions or one-arity
244245
functions to restrict which files matched by the `:test_pattern`, but not loaded
245246
by `:test_load_filters`, trigger a warning for a potentially misnamed test file.
246247
247248
Defaults to:
248249
249250
```elixir
250251
[
251-
~r/.*_helper\.exs$/,
252+
&String.ends_with?(&1, "_helper.exs"),
252253
fn file -> Enum.any?(elixirc_paths(Mix.env()), &String.starts_with?(file, &1)) end
253254
]
254255
```
255256
256257
This ensures that any helper or test support files are not triggering a warning.
257258
Warnings can be disabled by setting this option to `false` or `nil`.
259+
Paths are relative to the project root and separated by `/`, even on Windows.
258260
259261
## Coloring
260262
@@ -624,15 +626,15 @@ defmodule Mix.Tasks.Test do
624626
if project[:warn_test_pattern] do
625627
Mix.shell().info("""
626628
warning: the `:warn_test_pattern` configuration is deprecated and will be ignored. \
627-
Use `:test_load_filters` and `:test_warn_filters` instead.
629+
Use `:test_load_filters` and `:test_ignore_filters` instead.
628630
""")
629631
end
630632

631633
{test_files, test_opts} =
632634
if files != [], do: ExUnit.Filters.parse_paths(files), else: {test_paths, []}
633635

634636
# get a list of all files in the test folders, which we filter by the test_load_filters
635-
potential_test_files = Mix.Utils.extract_files(test_files, test_pattern)
637+
{potential_test_files, directly_included_test_files} = extract_files(test_files, test_pattern)
636638

637639
{load_files, _ignored_files, warn_files} =
638640
classify_test_files(potential_test_files, project)
@@ -641,7 +643,7 @@ defmodule Mix.Tasks.Test do
641643
# even if the test_load_filters don't match
642644
load_files =
643645
if files != [],
644-
do: Enum.uniq(load_files ++ directly_included_test_files(files)),
646+
do: Enum.uniq(load_files ++ directly_included_test_files),
645647
else: load_files
646648

647649
matched_test_files =
@@ -701,6 +703,33 @@ defmodule Mix.Tasks.Test do
701703
end
702704
end
703705

706+
# similar to Mix.Utils.extract_files/2, but returns a list of directly included test files,
707+
# that should be not filtered by the test_load_filters, e.g.
708+
# mix test test/some_file.exs
709+
defp extract_files(paths, pattern) do
710+
{files, directly_included} =
711+
for path <- paths, reduce: {[], []} do
712+
{acc, directly_included} ->
713+
case :elixir_utils.read_file_type(path) do
714+
{:ok, :directory} ->
715+
{[acc, Path.wildcard("#{path}/**/#{pattern}")], directly_included}
716+
717+
{:ok, :regular} ->
718+
{[acc, path], [path | directly_included]}
719+
720+
_ ->
721+
{acc, directly_included}
722+
end
723+
end
724+
725+
files =
726+
files
727+
|> List.flatten()
728+
|> Enum.uniq()
729+
730+
{files, directly_included}
731+
end
732+
704733
defp raise_with_shell(shell, message) do
705734
Mix.shell(shell)
706735
Mix.raise(message)
@@ -720,19 +749,15 @@ defmodule Mix.Tasks.Test do
720749
end
721750
end
722751

723-
defp directly_included_test_files(files) do
724-
Enum.filter(files, fn path -> :elixir_utils.read_file_type(path) == {:ok, :regular} end)
725-
end
726-
727752
defp classify_test_files(potential_test_files, project) do
728-
test_load_filters = project[:test_load_filters] || [~r/.*_test\.exs$/]
753+
test_load_filters = project[:test_load_filters] || [&String.starts_with?(&1, "_test.exs")]
729754
elixirc_paths = project[:elixirc_paths] || []
730755

731756
# ignore any _helper.exs files and files that are compiled (test support files)
732-
test_warn_filters =
733-
Keyword.get_lazy(project, :test_warn_filters, fn ->
757+
test_ignore_filters =
758+
Keyword.get_lazy(project, :test_ignore_filters, fn ->
734759
[
735-
~r/.*_helper\.exs$/,
760+
&String.ends_with?(&1, "_helper.exs"),
736761
fn file -> Enum.any?(elixirc_paths, &String.starts_with?(file, &1)) end
737762
]
738763
end)
@@ -744,11 +769,11 @@ defmodule Mix.Tasks.Test do
744769
any_file_matches?(file, test_load_filters) ->
745770
{[file | to_load], to_ignore, to_warn}
746771

747-
any_file_matches?(file, test_warn_filters) ->
772+
any_file_matches?(file, test_ignore_filters) ->
748773
{to_load, [file | to_ignore], to_warn}
749774

750-
# don't warn if test_warn_filters is explicitly set to nil / false
751-
!!test_warn_filters ->
775+
# don't warn if test_ignore_filters is explicitly set to nil / false
776+
!!test_ignore_filters ->
752777
{to_load, to_ignore, [file | to_warn]}
753778
end
754779
end
@@ -774,13 +799,13 @@ defmodule Mix.Tasks.Test do
774799

775800
defp warn_misnamed_test_files(ignored) do
776801
Mix.shell().info("""
777-
warning: the following files do not match any of the configured `:test_load_filters` / `:test_warn_filters`:
802+
warning: the following files do not match any of the configured `:test_load_filters` / `:test_ignore_filters`:
778803
779804
#{Enum.join(ignored, "\n")}
780805
781806
This might indicate a typo in a test file name (for example, using "foo_tests.exs" instead of "foo_test.exs").
782807
783-
You can adjust which files trigger this warning by configuring the `:test_warn_filters` option in your
808+
You can adjust which files trigger this warning by configuring the `:test_ignore_filters` option in your
784809
Mix project's configuration. To disable the warning entirely, set that option to false.
785810
786811
For more information, run `mix help test`.

lib/mix/test/fixtures/test_warn/mix.exs

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/mix/test/fixtures/test_warn/test/a_missing.exs

Whitespace-only changes.

lib/mix/test/fixtures/test_warn/test/a_tests.ex

Whitespace-only changes.

lib/mix/test/fixtures/test_warn/test/a_tests.exs

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/mix/test/fixtures/test_warn/test/ignored_file.exs

Whitespace-only changes.

lib/mix/test/fixtures/test_warn/test/ignored_regex.exs

Whitespace-only changes.

lib/mix/test/fixtures/test_warn/test/other_file.txt

Whitespace-only changes.

lib/mix/test/fixtures/test_warn/test/test_helper.exs

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,47 @@ defmodule Mix.Tasks.TestTest do
624624
end
625625
end
626626

627-
describe "test_load_filters and test_warn_filters" do
628-
test "warns for files that are not loaded and don't match test_warn_filters" do
629-
in_fixture("test_warn", fn ->
627+
describe "test_load_filters and test_ignore_filters" do
628+
test "warns for files that are not loaded and don't match test_ignore_filters" do
629+
in_tmp("test_warn", fn ->
630+
File.write!("mix.exs", """
631+
defmodule TestWarn.MixProject do
632+
use Mix.Project
633+
634+
def project do
635+
[
636+
app: :test_warn,
637+
version: "0.0.1",
638+
test_load_filters: [~r/.*_tests\.exs/],
639+
test_ignore_filters: [
640+
"test/test_helper.exs",
641+
~r/ignored_regex/,
642+
fn file -> file == "test/ignored_file.exs" end
643+
]
644+
]
645+
end
646+
end
647+
""")
648+
649+
File.mkdir!("test")
650+
651+
File.write!("test/a_tests.exs", """
652+
defmodule ATests do
653+
use ExUnit.Case
654+
655+
test "dummy" do
656+
assert true
657+
end
658+
end
659+
""")
660+
661+
File.write!("test/test_helper_tests.exs", "ExUnit.start()")
662+
File.touch("test/a_missing.exs")
663+
File.touch("test/a_tests.ex")
664+
File.touch("test/ignored_file.exs")
665+
File.touch("test/ignored_regex.exs")
666+
File.write!("test/other_file.txt", "this is not a test file")
667+
630668
output = mix(["test"])
631669

632670
# This test relies on the files present in the test_warn fixture.
@@ -637,7 +675,7 @@ defmodule Mix.Tasks.TestTest do
637675
#
638676
# Therefore, we expect to get a warning for a_missing.exs and a_tests.ex.
639677
assert output =~ """
640-
the following files do not match any of the configured `:test_load_filters` / `:test_warn_filters`:
678+
the following files do not match any of the configured `:test_load_filters` / `:test_ignore_filters`:
641679
642680
test/a_missing.exs
643681
test/a_tests.ex

0 commit comments

Comments
 (0)