-
-
Notifications
You must be signed in to change notification settings - Fork 525
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
Add bang to invert exit code #3271
Conversation
@sillydan1 Thanks for the pull request. Ideally we would have had a quick discussion before a pull request was created. Are you aware of that you could already ignore the exit code for commands, see https://tox.wiki/en/4.14.2/faq.html#ignoring-the-exit-code-of-a-given-command? Could you give us a couple of real world examples why and when inverting the exit code would make sense? I have a hard time to come up with examples. |
Sorry about that. I can create a new discussion if this is inappropriate 😄
Yes I am aware 😄 . The problem with that is I want the task to fail if the command succeeds and pass if the command fails and ignoring the exit code will always succeed. This is useful when you want to assert that some some usage explicitly fails. Say you're writing a unit testing framework and you want to make sure that when a test fails, the exit code should be non-zero. Simply ignoring the exit code won't catch the case where the invocation erroneously exits with code zero. |
I see. We also need to test the exit code when tox fails and thus returns a non-zero exit code. For that we use the I would recommend that way, as then you can also explicitly check for the exit code - as in exit code number/value. I am very hesitant to add new features to tox, especially those changing the syntax, as with the wide user base we need to be very careful not to break other users' configuration. I would like to have @gaborbernat's opinion on that. P.S:: FWIW I think you could also make use of the |
I am aware of this, but I am not invoking the command from a python context (or pytest for that matter). I might be wrong, but as far as I know, the In a more concrete example, I have a tox file that looks something like this: [testenv:invocations]
description = Run my own custom tailor made testing framework that is not pytest or anything else.
basepython = py310
commands =
python3 -m my_testing_framework test/these_pass.py # This invocation will exit with code 0
python3 -m my_testing_framework test/these_fail.py # This invocation will exit with a non-zero exit code - on purpose.
- python3 -m my_testing_framework test/these_fail.py # Here we simply ignore the exit code, but if my_testing_framework exits with code 0, then the tox pipeline errorniously succeeds In a regular shell, I can use the bang-operator to achieve this exit-code inversion behavior like so: $ python3 -m my_testing_framework test/these_pass.py
$ echo $?
0
$ python3 -m my_testing_framework test/these_fail.py
$ echo $?
1
$ ! python3 -m my_testing_framework test/these_fail.py
$ echo $?
0 # the shell invocation exit code is success! As I mentioned in the PR description, I can emulate this by wrapping it in a [testenv:invocations]
description = Run my own custom tailor made testing framework that is not pytest or anything else.
basepython = py310
allowlist_external = sh # required now
commands =
python3 -m my_testing_framework test/these_pass.py # This invocation will exit with code 0
sh -c "! python3 -m my_testing_framework test/these_fail.py" # Kinda odd and inelegant, but technically works (missing: envpython usage). |
I think the problem is now well understood. The question is whether we want to add this feature, and I lean towards not doing so, but as mentioned I would like to hear the feedback of @gaborbernat. tox is meant to run other test and linting tools, which themselves do the error handling. In your case, you directly test your application with tox - that way is not one we had anticipated. |
That sounds fair to me 😄 I'm looking forward to the feedback |
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'm personally okay to add this.
061501d
to
830ea40
Compare
This makes it so that you can provide a "!" before the command line to invert the success/failure of a command. Just like with regular POSIX shell.
830ea40
to
0ed33e4
Compare
for more information, see https://pre-commit.ci
Can you fix the CI? |
It seems that the only thing that is failing is a link to the github user Maybe just run the job again? |
This makes it so that you can provide a "!" before the command line to invert the success/failure of a command. Just like with regular POSIX shell. This is useful if you expect that the command should crash / return an error.
Currently, to achieve this behavior, you'd have to do wrap it in a shell command:
sh -c "! python3 -m foo"
and that won't necessarily use the correct virtualenv.tox -e fix
)docs/changelog
folder