Skip to content
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

test Mix task should exit with 1 when --warnings-as-errors opt is used #14019

Conversation

eksperimental
Copy link
Contributor

@eksperimental eksperimental commented Nov 25, 2024

Previously some warnings were not detected as errors, exiting the command with 1.

A real case example is wrongly naming a test file with the extension .ex instead of .exs. This was making the CI flow not to fail because of this.

A test app has been uploaded to: https://github.com/eksperimental-debug/debug_elixir_test_wrong_ext

Previously:

❯ mix test --warnings-as-errors; echo $?
Compiling 1 file (.ex)
Generated debug_elixir_test_wrong_ext app
warning: test/foo_test.ex does not match "*_test.exs" and won't be loaded
Running ExUnit with seed: 194476, max_cases: 20

..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 doctest, 1 test, 0 failures
0

Now:

❯ mix test --warnings-as-errors; echo $?
Compiling 1 file (.ex)
Generated debug_elixir_test_wrong_ext app
warning: test/foo_test.ex does not match "*_test.exs" and won't be loaded
Running ExUnit with seed: 560339, max_cases: 20

..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 doctest, 1 test, 0 failures

ERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option
1

All tests will be executed, the warning is printed, but it prints an error message and exits with 1.

Screenshot with this fix:
image

Previously some warnings were not detected as errors, exiting the command with 1.

An example is wronly naming a test file with the extension .ex instead of .exs

A test app has been uploaded to: https://github.com/eksperimental-debug/debug_elixir_test_wrong_ext

Previously:

    ❯ mix test --warnings-as-errors; echo $?
    Compiling 1 file (.ex)
    Generated debug_elixir_test_wrong_ext app
    warning: test/foo_test.ex does not match "*_test.exs" and won't be loaded
    Running ExUnit with seed: 194476, max_cases: 20

    ..
    Finished in 0.00 seconds (0.00s async, 0.00s sync)
    1 doctest, 1 test, 0 failures
    0

Now:

    ❯ mix test --warnings-as-errors; echo $?
    Compiling 1 file (.ex)
    Generated debug_elixir_test_wrong_ext app
    warning: test/foo_test.ex does not match "*_test.exs" and won't be loaded
    Running ExUnit with seed: 560339, max_cases: 20

    ..
    Finished in 0.00 seconds (0.00s async, 0.00s sync)
    1 doctest, 1 test, 0 failures

    ERROR! Test suite aborted after successful execution due to warnings while using the --warnings-as-errors option
    1

All tests will be executed, the warning is printed, but it prints an error message and exits with 1.
Comment on lines +121 to +122
capture_io(:stderr, fn ->
capture_io(fn ->
Copy link
Contributor Author

@eksperimental eksperimental Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find a way to catch both stdio and stderr in one go, so I resorted to this:


ensure_touched(file)
assert {:ok, []} = Mix.Tasks.Compile.Erlang.run([])
end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, why was this file changed? It seems unrelated to this PR?

Copy link
Contributor Author

@eksperimental eksperimental Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was part of the other PR #14018. It ended up here when I split up the commits.

@josevalim
Copy link
Member

Thank you for the PR.

The --warnings-as-errors flag applies to compiler errors, not any warning that may be emitted, as mentioned in the docs. For example, if you get warnings in your test_helper.exs, those won't fail either. So we don't have plans to change this for now, just make it consistent with the compiler flag itself.

@josevalim josevalim closed this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants