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

Support for non-PIPE outputs #57

Closed
abjonnes opened this issue Oct 29, 2021 · 3 comments · Fixed by #58
Closed

Support for non-PIPE outputs #57

abjonnes opened this issue Oct 29, 2021 · 3 comments · Fixed by #58

Comments

@abjonnes
Copy link

abjonnes commented Oct 29, 2021

I have some code with uses subprocess with a file handle as the stdout argument. The subprocess module will write the output of the command to the file handle, and my code does not directly call communicate. When I register a command with fake_process in the tests with some stdout content specified, the file doesn't seem to have the expected content written to it. Have I overlooked something, or is that not supported by this library?

Example:

# tests/test_sp.py
import subprocess


def test_sp(fake_process):
    fake_process.register_subprocess(["echo", "hello"], stdout="hello\n")

    filename = "file.txt"
    with open(filename, "w") as handle:
        subprocess.run(["echo", "hello"], stdout=handle)

    assert open(filename).read() == "hello\n"

Result:

╰─❯ poetry run pytest
============================= test session starts ==============================
platform darwin -- Python 3.9.7, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: [...]/fake_sp_test
plugins: subprocess-1.3.0
collected 1 item                                                               

tests/test_sp.py F                                                       [100%]

=================================== FAILURES ===================================
___________________________________ test_sp ____________________________________

fake_process = <pytest_subprocess.core.FakeProcess object at 0x10ee90190>

    def test_sp(fake_process):
        fake_process.register_subprocess(["echo", "hello"], stdout="hello\n")
    
        filename = "file.txt"
        with open(filename, "w") as handle:
            subprocess.run(["echo", "hello"], stdout=handle)
    
>       assert open(filename).read() == "hello\n"
E       AssertionError: assert '' == 'hello\n'
E         - hello

tests/test_sp.py:11: AssertionError
=========================== short test summary info ============================
FAILED tests/test_sp.py::test_sp - AssertionError: assert '' == 'hello\n'
============================== 1 failed in 0.06s ===============================
@aklajnert
Copy link
Owner

I've missed that use case, it's not supported right now. I think it could be done currently by using the callback function, but it would be quite clunky.
I'll fix the code to support this feature soon. Thanks for reporting.

@abjonnes
Copy link
Author

Awesome, thanks!

@aklajnert
Copy link
Owner

This fix has been just released in 1.3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants