1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import logging
import stat
from collections.abc import Callable
from subprocess import CalledProcessError
import pytest
from _pytest.logging import LogCaptureFixture
from testlib.fixtures.command import Command
from testlib.fixtures.env import ManagedEnv
from testlib.fixtures.file_helper import File, with_files
def test_command_valid_runs(command: Callable[[list[str]], Command]):
cmd = command(["echo", "water"])
cmd.run().ok()
def test_command_captures_stdout(command: Callable[[list[str]], Command]):
cmd = command(["echo", "fire"])
res = cmd.run().ok()
assert res.stdout_s == "fire\n"
def test_command_plain_strips(command: Callable[[list[str]], Command]):
cmd = command(["echo", " earth "])
res = cmd.run().ok()
assert res.stdout_plain == "earth"
def test_command_stdin_passed_correctly(command: Callable[[list[str]], Command]):
inp = b"air"
cmd = command(["cat", "/dev/stdin"]).with_stdin(inp)
res = cmd.run().ok()
assert res.stdout == inp
def test_command_expect_failure(env: ManagedEnv):
env.path.add_program("grep")
Command(_env=env, argv=["grep", "xxx"], stdin=b"").run().expect(1)
@with_files(
{
"script.sh": File(
"#!/bin/sh\necho forb\nexit 1", mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
)
}
)
def test_command_ok_fails_on_bad_exit_code(
command: Callable[[list[str]], Command], caplog: LogCaptureFixture
):
cmd = command(["./script.sh"])
with pytest.raises(CalledProcessError), caplog.at_level(logging.ERROR):
cmd.run().ok()
msgs = caplog.messages
assert len(msgs) == 2
out_msg, err_msg = msgs
assert out_msg == "stdout: forb\n"
assert err_msg == "stderr: "
@with_files(
{
"script.sh": File(
"#!/bin/sh\necho drgn fops\nexit 2", mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
)
}
)
def test_command_exec_fails_on_other_bad_exit_code(
command: Callable[[list[str]], Command], caplog: LogCaptureFixture
):
cmd = command(["./script.sh"])
with pytest.raises(CalledProcessError), caplog.at_level(logging.ERROR):
cmd.run().expect(1)
msgs = caplog.messages
assert len(msgs) == 2
out_msg, err_msg = msgs
assert out_msg == "stdout: drgn fops\n"
assert err_msg == "stderr: "
def test_command_wrapping(command: Callable[[list[str]], Command]):
cmd = command(["echo $0 -- $@", "wrapper", "hello", "world"]).with_wrapper("bash", "-c")
res = cmd.run().ok()
assert res.stdout_plain == "wrapper -- hello world"
|