-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(mix test) add test_load_filters and test_warn_filters #14036
Conversation
5b56d00
to
5e43b87
Compare
What do you think about keeping It is a smaller breaking change and if someone has changed the test_pattern, then worst case scenario they don’t get a warning. |
But if we keep test_pattern as glob pattern, we need to evaluate the pattern twice, don't we? I really wish there was a built in glob pattern to regex or something... |
test_pattern is the glob, the others are strings and regexes we run over the matched files |
5e43b87
to
a0df137
Compare
Adjusted to use test_pattern with new default |
ff0cb54
to
09ebf2b
Compare
Updated description (taken from the commit message): (mix test) add test_load_filters and test_warn_filters When using the default project configuration, mix test would only warn about files ending in Because there is no easy way to evaluate glob patterns in Elixir without touching the filesystem, I decided to deprecate the old
Now, by changing the default of The For projects with an existing Projects with an existing custom |
I don't know how far you want to take the default pattern, just sharing my experience - it happened to me calling the test files |
@mgibowski no file extension at all? 😅 With the changes from the PR, you'd get a warning for |
@SteffenDE |
@mgibowski that should warn with the new defaults :) |
09ebf2b
to
42e9086
Compare
8e12c62
to
3d41609
Compare
When using the default project configuration, mix test would only warn about files ending in `_test.ex`. The only mistake this warns about is forgetting the `s` of `.exs`. In a work project we noticed (after more than a year) that we had multiple test files that did not match the test pattern, preventing them from running in mix test locally and CI. Because we have many other tests, nobody noticed this. If CI passes, all is good, right? Because there is no easy way to evaluate glob patterns in Elixir without touching the filesystem, I decided to deprecate the old `warn_test_pattern` configuration and instead add two new configurations: `test_load_filters` and `test_warn_filters` Now, by changing the default of `test_pattern` to `*.{ex,exs}`, we can load all potential test files once and then match their paths to the patterns. The `test_load_filters` is used to filter the files that are loaded by mix test. This defaults to the regex equivalent of "*_test.exs". The `test_warn_filters` is used to filter the files that we warn about if they are not loaded. By default, we ignore any file ending in `_helper.exs`, which will prevent the default test_helper.exs from generating a warning and also provides a simple way to name other files that might be required explicitly in tests. We also default to ignore any files that start with a configured elixirc_path, which are compiled often test support files. For projects with an existing `warn_test_pattern` configuration, a deprecation warning is logged. The warnings can be disabled by setting `test_warn_filters` to a falsy value. Projects with an existing custom `test_pattern` should check if their pattern conflicts with the new `test_load_filters` and adjust their configuration accordingly. It is also possible to keep the old `test_pattern` and configure the `test_load_filters` to accept any file, for example by configuring it to `[fn _ -> true end]`. In that case, the `test_warn_filters` don't have an effect, as any potential test file is also loaded.
3d41609
to
e6b21b5
Compare
lib/mix/lib/mix/tasks/test.ex
Outdated
Keyword.get_lazy(project, :test_ignore_filters, fn -> | ||
[ | ||
&String.ends_with?(&1, "_helper.exs"), | ||
fn file -> Enum.any?(elixirc_paths, &String.starts_with?(file, &1)) end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should include this filter by default, because if the user wants to add a new one, we should not ask them to reimplement this. WDYT? I am not sure if we should exclude the test_helper.exs by default too, but we probably should. So the user simply passes additional filters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed! If anyone feels the need to change those defaults, they'll hopefully open up an issue :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added only nits now, we are good to go!
Co-authored-by: José Valim <jose.valim@gmail.com>
💚 💙 💜 💛 ❤️ |
Edit: This is the old suggestion, but I kept it for posterity. For the updated description, please scroll down :)
This is an alternative to #14034, which only traverses the file system once.
When using the default project configuration, mix test would only warn about files ending in
_test.ex
. The only mistake this warns about is forgetting thes
of.exs
. In a work project we noticed (after more than a year) that we had multiple test files that did not match the test pattern, preventing them from running in mix test locally and CI. Because we have many other tests, nobody noticed this. If CI passes, all is good, right?Because there is no easy way to evaluate glob patterns in Elixir without touching the filesystem, I decided to deprecate the old configurations and instead add two new configurations:
test_load_pattern
andtest_warn_pattern
. Now, we can load all potential test files once and then match their paths to the patterns.The
test_load_pattern
is used to filter the files that are loaded by mix test. This defaults to the regex equivalent of "*_test.exs". Thetest_warn_pattern
is used to filter the files that we warned about if they are not loaded. By default, we warn about any file that either ends in_test.ex
(the old behavior) but also any file that ends in.exs
, but does not end in_helper.exs
, which will prevent the default test_helper.exs from generating a warning and also provides a way to name other files that might be required explicitly in tests. As an escape hatch thetest_warn_ignore_files
list can be used to ignore specific files where the warning should not be generated.For projects with an existing
test_pattern
orwarn_test_pattern
configuration, a deprecation warning is logged. The new warnings can be disabled by settingtest_warn_pattern
to a falsy value.