Skip to content

Commit ff0cb54

Browse files
committed
consolidate warn configuration into test_warn_ignore_patterns
1 parent a0df137 commit ff0cb54

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

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

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,17 @@ defmodule Mix.Tasks.Test do
234234
* `:test_load_pattern` - a regular expression to load test files.
235235
Defaults to `~r/.*_test\\.exs$/`
236236
237-
* `:test_warn_pattern` - a regular expression to match potentially
238-
misnamed test files and display a warning.
239-
Defaults to `~r/^(?!.*_helper\.exs$)(?:.*_test\.ex|.*\.exs)$/`,
240-
matching any file that ends in `_test.ex` or `.exs`, ignoring files
241-
ending in `_helper.exs`. Warnings can be disabled by setting this to
242-
`nil` or `false`.
243-
244-
* `:test_warn_ignore_files` - a list of files to ignore when displaying
245-
warnings about potentially misnamed test files. Defaults to `[]`.
237+
* `:test_warn_ignore_patterns` - a list of files, regexes or one-arity functions
238+
that restrict which files trigger a warning for a potentially misnamed test file.
239+
By default, any file that does not match the `test_load_pattern` and does not
240+
end in `_helper.exs` potentially triggers a warning. Defaults to:
241+
```elixir
242+
[
243+
~r/.*_helper\\.exs$/,
244+
fn file -> Enum.any?(elixirc_paths(Mix.env()), &String.starts_with?(file, &1)) end
245+
]
246+
```
247+
This ensures that any helper or test support files are not triggering a warning.
246248
247249
## Coloring
248250
@@ -607,20 +609,8 @@ defmodule Mix.Tasks.Test do
607609
# Prepare and extract all files to require and run
608610
test_paths = project[:test_paths] || default_test_paths()
609611
test_pattern = project[:test_pattern] || "*.{ex,exs}"
610-
test_load_pattern = project[:test_load_pattern] || ~r/.*_test\.exs$/
611-
612-
test_warn_pattern =
613-
Keyword.get(project, :test_warn_pattern, ~r/^(?!.*_helper\.exs$)(?:.*_test\.ex|.*\.exs)$/)
614-
615-
test_warn_ignore_files = project[:test_warn_ignore_files] || []
616-
617-
# Warn about deprecated configurations
618-
if project[:test_pattern] do
619-
Mix.shell().info(
620-
"warning: the `:test_pattern` configuration is deprecated and will be ignored. Use `:test_load_pattern` instead."
621-
)
622-
end
623612

613+
# Warn about deprecated warn configuration
624614
if project[:warn_test_pattern] do
625615
Mix.shell().info(
626616
"warning: the `:warn_test_pattern` configuration is deprecated and will be ignored. Use `:test_warn_pattern` instead."
@@ -633,23 +623,15 @@ defmodule Mix.Tasks.Test do
633623
# get a list of all files in the test folders, which we filter by the test_load_pattern
634624
potential_test_files = Mix.Utils.extract_files(test_files, test_pattern)
635625

636-
unfiltered_test_files =
637-
Enum.filter(potential_test_files, &Regex.match?(test_load_pattern, &1))
626+
{unfiltered_test_files, _ignored_files, warn_files} =
627+
classify_test_files(potential_test_files, project)
638628

639629
matched_test_files =
640630
unfiltered_test_files
641631
|> filter_to_allowed_files(allowed_files)
642632
|> filter_by_partition(shell, partitions)
643633

644-
# do not warn if test_warn_pattern is nil or false
645-
test_warn_pattern &&
646-
maybe_warn_misnamed_test_files(
647-
potential_test_files,
648-
unfiltered_test_files,
649-
test_warn_ignore_files,
650-
test_load_pattern,
651-
test_warn_pattern
652-
)
634+
warn_files != [] && warn_misnamed_test_files(warn_files, project)
653635

654636
try do
655637
Enum.each(test_paths, &require_test_helper(shell, &1))
@@ -720,32 +702,65 @@ defmodule Mix.Tasks.Test do
720702
end
721703
end
722704

723-
defp maybe_warn_misnamed_test_files(
724-
potential_test_files,
725-
unfiltered_test_files,
726-
test_warn_ignore_files,
727-
test_load_pattern,
728-
test_warn_pattern
729-
)
730-
when is_struct(test_warn_pattern, Regex) do
731-
missing_files = (potential_test_files -- unfiltered_test_files) -- test_warn_ignore_files
705+
defp classify_test_files(potential_test_files, project) do
706+
test_load_pattern = project[:test_load_pattern] || ~r/.*_test\.exs$/
707+
elixirc_paths = project[:elixirc_paths] || []
732708

733-
ignored = Enum.filter(missing_files, &Regex.match?(test_warn_pattern, &1))
709+
# ignore any _helper.exs files and files that are compiled (test support files)
710+
test_warn_ignore_patterns =
711+
project[:test_warn_ignore_patterns] ||
712+
[
713+
~r/.*_helper\.exs$/,
714+
fn file -> Enum.any?(elixirc_paths, &String.starts_with?(file, &1)) end
715+
]
734716

735-
if ignored != [] do
736-
Mix.shell().info("""
737-
warning: the following files do not match the test load pattern #{inspect(test_load_pattern)} and won't be loaded:
717+
{to_load, to_ignore, to_warn} =
718+
for file <- potential_test_files, reduce: {[], [], []} do
719+
{to_load, to_ignore, to_warn} ->
720+
cond do
721+
Regex.match?(test_load_pattern, file) ->
722+
{[file | to_load], to_ignore, to_warn}
738723

739-
#{Enum.join(ignored, "\n")}
724+
ignore_file?(file, test_warn_ignore_patterns) ->
725+
{to_load, [file | to_ignore], to_warn}
740726

741-
This might indicate a typo in a test file name (for example, using "foo_tests.exs" instead of "foo_test.exs").
727+
true ->
728+
{to_load, to_ignore, [file | to_warn]}
729+
end
730+
end
742731

743-
You can adjust which files trigger this warning by configuring the `:test_warn_pattern` option in your
744-
Mix project's configuration. To disable the warning entirely, set that option to false.
732+
# get the files back in the original order
733+
{Enum.reverse(to_load), Enum.reverse(to_ignore), Enum.reverse(to_warn)}
734+
end
745735

746-
For more information, run `mix help test`.
747-
""")
748-
end
736+
defp ignore_file?(file, test_warn_ignore_patterns) do
737+
Enum.any?(test_warn_ignore_patterns, fn pattern ->
738+
case pattern do
739+
regex when is_struct(regex, Regex) ->
740+
Regex.match?(regex, file)
741+
742+
binary when is_binary(binary) ->
743+
file == binary
744+
745+
fun when is_function(fun, 1) ->
746+
fun.(file)
747+
end
748+
end)
749+
end
750+
751+
defp warn_misnamed_test_files(ignored, project) do
752+
Mix.shell().info("""
753+
warning: the following files do not match the test load pattern #{inspect(project[:test_load_pattern])} and won't be loaded:
754+
755+
#{Enum.join(ignored, "\n")}
756+
757+
This might indicate a typo in a test file name (for example, using "foo_tests.exs" instead of "foo_test.exs").
758+
759+
You can adjust which files trigger this warning by configuring the `:test_warn_pattern` option in your
760+
Mix project's configuration. To disable the warning entirely, set that option to false.
761+
762+
For more information, run `mix help test`.
763+
""")
749764
end
750765

751766
@option_keys [

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ defmodule TestWarn.MixProject do
66
app: :test_warn,
77
version: "0.0.1",
88
test_load_pattern: ~r/.*_tests\.exs/,
9-
test_warn_pattern: ~r/^(?!.*_helper\.exs$)(?:.*_tests\.ex|.*\.exs)$/
9+
test_warn_ignore_patterns: [
10+
"test/test_helper.exs",
11+
~r/ignored_regex/,
12+
fn file -> file == "test/ignored_file.exs" end
13+
]
1014
]
1115
end
1216
end

0 commit comments

Comments
 (0)