testutil: tweak mock_command to write a call_log

Instead of just mocking the binary also write a log of the way
it got called so that tests can use this to check if the right
options are passed.

Note that the API should be improved here, instead of returning
a "naked" path to the calllog file there should be a class wrapping
it. And of course there should be tests.
This commit is contained in:
Michael Vogt 2024-03-11 11:21:11 +01:00 committed by Achilleas Koutsou
parent fd0167f130
commit 9393211b8a
2 changed files with 56 additions and 5 deletions

View file

@ -12,16 +12,28 @@ def test_mock_command_integration():
output = subprocess.check_output(["echo", "hello"])
assert output == b"hello\n"
fake_echo = textwrap.dedent("""\
#!/bin/sh
echo i-am-not-echo
""")
with mock_command("echo", fake_echo):
with mock_command("echo", fake_echo) as mocked_cmd:
output = subprocess.check_output(["echo", "hello"])
assert output == b"i-am-not-echo\n"
assert mocked_cmd.call_args_list == [
["hello"],
]
output = subprocess.check_output(["echo", "hello"])
assert output == b"hello\n"
def test_mock_command_multi():
with mock_command("echo", "") as mocked_cmd:
subprocess.check_output(["echo", "call1-arg1", "call1-arg2"])
subprocess.check_output(["echo", "call2-arg1", "call2-arg2"])
assert mocked_cmd.call_args_list == [
["call1-arg1", "call1-arg2"],
["call2-arg1", "call2-arg2"],
]
def test_mock_command_environ_is_modified_and_restored():
orig_path = os.environ["PATH"]
with mock_command("something", "#!/bin/sh\ntrue\n"):