pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.31k stars 365 forks source link

MockContext does not honor "warn=False" which is the default behaviour #960

Open red8888 opened 11 months ago

red8888 commented 11 months ago

I just ran into an issue where my test wasn't catching a bug I had because of this.

I gracefully catch errors:

result = ctx.run("my command", hide=False, echo=True)
if result.exited:
    ... do stuff

Now I forgot to add warn=True here, but wrote my tests with MockContext:

ctx = MockContext(
    run={
        "my command": Result("Mock output", exited=1),
    }
)
... and checked the "do stuff" after the command returned a non-zero exit code worked as expected

My tests were passing without issue because MockContext isn't honoring the default warn=False. I found in production the error was not being caught because I forgot to set warn=True.

What should have happened was:

  1. MockContext sees warn=False and pyinvoke immediately exits after the command returns a non-zero exit code
  2. My tests fail and I catch the misconfiguration