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

PT012 possible false positive: context manager that catches exceptions #114

Open
SwampFalc opened this issue May 11, 2021 · 0 comments
Open
Labels
bug Something isn't working

Comments

@SwampFalc
Copy link

SwampFalc commented May 11, 2021

@contextmanager
def cli():
    interface = CLI()
    try:
        yield interface
    except subprocess.CalledProcessError as cpe:
        traceback.print_exc()
        interface.warning(f"stdout: {cpe.stdout}")
        interface.error(f"stderr: {cpe.stderr}")
        sys.exit(1)

This context manager was built to provide a commandline interface. Since during the execution of the rest of the code, a number of subprocess calls happen and, well, there's always a chance of unexpected errors there, I placed this try/except in there. This ensures that in case of unexpected error, as much info as possible is printed.

However, I have found that I cannot test this with just a "simple statement". The most basic test I can run is this:

def test_cli(capsys):
    with pytest.raises(SystemExit):
        with library.cli():
            raise CalledProcessError(1, "cmd", output="findme", stderr="findmetoo")

    stdout, stderr = capsys.readouterr()
    assert stdout.splitlines() == ["stdout: findme", "stderr: findmetoo"]

Now, I did read #93 so I am aware of the possible work arounds.

But I thought I might file this bug report in case you do see a simpler way of testing it, or if maybe you saw a way to add it as an exception.

@SwampFalc SwampFalc added the bug Something isn't working label May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant