Debian Codegraph @

tests/test_cli.py

raw
1 # Copyright (c) 2025, Oracle and/or its affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4
5 import os
6 import sys
7 import tempfile
8 import traceback
9 import types
10
11 import drgn.cli
12 from tests import TestCase
13
14
15 class TestCli(TestCase):
16 def run_cli(self, args, *, input=None):
17 stdout_r, stdout_w = os.pipe()
18 stderr_r, stderr_w = os.pipe()
19 if input is not None:
20 stdin_r, stdin_w = os.pipe()
21
22 pid = os.fork()
23 if pid == 0:
24 try:
25 os.close(stdout_r)
26 sys.stdout = open(stdout_w, "w")
27 os.close(stderr_r)
28 sys.stderr = open(stderr_w, "w")
29
30 if input is not None:
31 os.close(stdin_w)
32 sys.stdin = open(stdin_r, "r")
33
34 sys.argv = ["drgn"] + args
35
36 drgn.cli._main()
37 finally:
38 exception = sys.exc_info()[1] is not None
39 if exception:
40 traceback.print_exc()
41 sys.stdout.flush()
42 sys.stderr.flush()
43 os._exit(1 if exception else 0)
44
45 os.close(stdout_w)
46 os.close(stderr_w)
47
48 if input is not None:
49 os.close(stdin_r)
50 with open(stdin_w, "w") as f:
51 f.write(input)
52
53 with open(stdout_r, "r") as f:
54 stdout = f.read()
55 with open(stderr_r, "r") as f:
56 stderr = f.read()
57
58 _, wstatus = os.waitpid(pid, 0)
59 if not os.WIFEXITED(wstatus) or os.WEXITSTATUS(wstatus) != 0:
60 if os.WIFEXITED(wstatus):
61 msg = f"Exited with status {os.WEXITSTATUS(wstatus)}"
62 elif os.WIFSIGNALED(wstatus):
63 msg = f"Terminated by signal {os.WTERMSIG(wstatus)}"
64 else:
65 msg = "Exited abnormally"
66 self.fail(
67 f"""\
68 {msg}
69 STDOUT:
70 {stdout}
71 STDERR:
72 {stderr}
73 """
74 )
75
76 return types.SimpleNamespace(stdout=stdout, stderr=stderr)
77
78 def test_e(self):
79 script = r"""
80 import sys
81
82 assert drgn.get_default_prog() is prog
83 assert __name__ == "__main__"
84 assert "__file__" not in globals()
85 assert sys.path[0] == ""
86 print(sys.argv)
87 """
88 proc = self.run_cli(
89 ["--quiet", "--pid", "0", "--no-default-symbols", "-e", script, "pass"]
90 )
91 self.assertEqual(proc.stdout, "['-e', 'pass']\n")
92
93 def test_e_empty(self):
94 self.run_cli(
95 ["--quiet", "--pid", "0", "--no-default-symbols", "-e", ""],
96 # This shouldn't be executed.
97 input="raise Exception('-e was ignored')",
98 )
99
100 def test_script(self):
101 with tempfile.NamedTemporaryFile() as f:
102 f.write(
103 rb"""
104 assert "drgn" not in globals()
105
106 import drgn
107 import os.path
108 import sys
109
110 assert drgn.get_default_prog() is prog
111 assert __name__ == "__main__"
112 assert __file__ == sys.argv[0]
113 assert sys.path[0] == os.path.dirname(__file__)
114 print(sys.argv)
115 """
116 )
117 f.flush()
118 proc = self.run_cli(
119 ["--quiet", "--pid", "0", "--no-default-symbols", f.name, "pass"]
120 )
121 self.assertEqual(proc.stdout, f"[{f.name!r}, 'pass']\n")
122
123 def test_pipe(self):
124 script = r"""
125 import sys
126
127 assert drgn.get_default_prog() is prog
128 assert __name__ == "__main__"
129 assert __file__ == "<stdin>"
130 assert sys.path[0] == ""
131 # Dummy if statement to test handling of multi-line blocks.
132 if True:
133 print(sys.argv)
134 """
135 proc = self.run_cli(
136 ["--quiet", "--pid", "0", "--no-default-symbols"], input=script
137 )
138 self.assertEqual(proc.stdout, "['']\n")