mirror of
https://github.com/smittix/intercept.git
synced 2026-07-04 15:53:39 -07:00
test: add shared fake_process fixture for complete Popen mocking
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -138,3 +138,28 @@ def test_db(tmp_path):
|
||||
conn.execute("PRAGMA journal_mode = WAL")
|
||||
yield conn
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_process():
|
||||
"""Factory for complete subprocess.Popen replacements.
|
||||
|
||||
Hand-rolled Popen mocks keep missing two things: __enter__ (subprocess.run
|
||||
wraps Popen in a context manager) and a communicate() tuple. Use this
|
||||
factory instead of building MagicMock processes inline.
|
||||
"""
|
||||
|
||||
def _make(returncode=0, stdout="", stderr="", running=True, pid=12345):
|
||||
proc = MagicMock()
|
||||
proc.poll.return_value = None if running else returncode
|
||||
proc.returncode = returncode
|
||||
proc.pid = pid
|
||||
proc.wait.return_value = returncode
|
||||
proc.communicate.return_value = (stdout, stderr)
|
||||
proc.stdout.read.return_value = stdout
|
||||
proc.stderr.read.return_value = stderr
|
||||
proc.stdin = MagicMock()
|
||||
proc.__enter__.return_value = proc
|
||||
return proc
|
||||
|
||||
return _make
|
||||
|
||||
Reference in New Issue
Block a user