Debian Codegraph @

tests/test_commands.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import ast
5 import contextlib
6 import io
7 from pathlib import Path
8 import sys
9 import tempfile
10 import unittest.mock
11
12 from drgn.commands import (
13 CommandArgumentError,
14 CommandExitStatusError,
15 CommandNamespace,
16 CommandNotFoundError,
17 DrgnCodeBuilder,
18 ParsedCommand,
19 _repr_black,
20 _repr_raw_bytes,
21 _sanitize_rst,
22 argument,
23 command,
24 parse_shell_command,
25 raw_command,
26 )
27 from tests import TestCase
28
29
30 class TestParseShellCommand(TestCase):
31 def assert_parses_to(self, source, *, args, redirections=[], pipeline=None):
32 self.assertEqual(
33 parse_shell_command(source),
34 ParsedCommand(args=args, redirections=redirections, pipeline=pipeline),
35 )
36
37 def test_one_arg(self):
38 self.assert_parses_to("foo", args=["foo"])
39
40 def test_multiple_args(self):
41 self.assert_parses_to("foo bar baz\t qux", args=["foo", "bar", "baz", "qux"])
42
43 def test_escape_space(self):
44 self.assert_parses_to(r"foo\ bar baz", args=["foo bar", "baz"])
45
46 def test_escape_backslash(self):
47 self.assert_parses_to(r"foo\\ bar", args=["foo\\", "bar"])
48
49 def test_escape_double_quote(self):
50 self.assert_parses_to(r"foo\"bar", args=['foo"bar'])
51
52 def test_escape_single_quote(self):
53 self.assert_parses_to(r"foo\'bar", args=["foo'bar"])
54
55 def test_escape_other(self):
56 self.assert_parses_to(r"foo\a", args=["fooa"])
57
58 def test_double_quotes(self):
59 self.assert_parses_to('"foo bar baz\'qux"', args=["foo bar baz'qux"])
60
61 def test_double_quotes_empty(self):
62 self.assert_parses_to('""', args=[""])
63
64 def test_double_quotes_escape(self):
65 # In double quotes, backslash only has special meaning before these
66 # characters and newline.
67 self.assert_parses_to(r' "\$\`\"\\" ', args=['$`"\\'])
68
69 def test_double_quotes_escape_newline(self):
70 self.assert_parses_to('"\\\n"', args=["\n"])
71
72 def test_double_quotes_backslash_single_quote(self):
73 self.assert_parses_to(r""" "\'" """, args=[r"\'"])
74
75 def test_double_quotes_backslash_other(self):
76 self.assert_parses_to(r'"\a"', args=[r"\a"])
77
78 def test_single_quotes(self):
79 self.assert_parses_to("' foo bar \"baz'", args=[' foo bar "baz'])
80
81 def test_single_quotes_empty(self):
82 self.assert_parses_to("''", args=[""])
83
84 def test_single_quotes_backslash(self):
85 # In single quotes, backslash does not have a special meaning.
86 self.assert_parses_to(r"'\$'", args=[r"\$"])
87
88 def test_single_quotes_backslash_single_quote(self):
89 self.assert_parses_to(r"'\'", args=["\\"])
90
91 def test_concatenate_escapes_and_quotes(self):
92 self.assert_parses_to(r""" foo' bar'\ baz"q u x" """, args=["foo bar bazq u x"])
93
94 def test_redirect_input(self):
95 for source in (
96 "foo < infile",
97 "foo <infile",
98 "foo< infile",
99 "foo<infile",
100 "<infile foo",
101 "< infile foo",
102 ):
103 with self.subTest(source=source):
104 self.assert_parses_to(
105 source, args=["foo"], redirections=[(0, "<", "infile")]
106 )
107
108 def test_redirect_input_fd(self):
109 for source in (
110 "foo 0< infile",
111 "foo 0<infile",
112 "0<infile foo",
113 "0< infile foo",
114 ):
115 with self.subTest(source=source):
116 self.assert_parses_to(
117 source, args=["foo"], redirections=[(0, "<", "infile")]
118 )
119
120 def test_redirect_output(self):
121 for source in (
122 "foo > outfile",
123 "foo >outfile",
124 "foo> outfile",
125 "foo>outfile",
126 ">outfile foo",
127 "> outfile foo",
128 ):
129 with self.subTest(source=source):
130 self.assert_parses_to(
131 source, args=["foo"], redirections=[(1, ">", "outfile")]
132 )
133
134 def test_redirect_output_fd(self):
135 for source in (
136 "foo 1> outfile",
137 "foo 1>outfile",
138 "1>outfile foo",
139 "1> outfile foo",
140 ):
141 with self.subTest(source=source):
142 self.assert_parses_to(
143 source, args=["foo"], redirections=[(1, ">", "outfile")]
144 )
145
146 for source in (
147 "foo 2> outfile",
148 "foo 2>outfile",
149 "2>outfile foo",
150 "2> outfile foo",
151 ):
152 with self.subTest(source=source):
153 self.assert_parses_to(
154 source, args=["foo"], redirections=[(2, ">", "outfile")]
155 )
156
157 def test_append_output(self):
158 for source in (
159 "foo >> outfile",
160 "foo >>outfile",
161 "foo>> outfile",
162 "foo>>outfile",
163 ">>outfile foo",
164 ">> outfile foo",
165 ):
166 with self.subTest(source=source):
167 self.assert_parses_to(
168 source, args=["foo"], redirections=[(1, ">>", "outfile")]
169 )
170
171 def test_append_output_fd(self):
172 for source in (
173 "foo 1>> outfile",
174 "foo 1>>outfile",
175 "1>>outfile foo",
176 "1>> outfile foo",
177 ):
178 with self.subTest(source=source):
179 self.assert_parses_to(
180 source, args=["foo"], redirections=[(1, ">>", "outfile")]
181 )
182
183 for source in (
184 "foo 2>> outfile",
185 "foo 2>>outfile",
186 "2>>outfile foo",
187 "2>> outfile foo",
188 ):
189 with self.subTest(source=source):
190 self.assert_parses_to(
191 source, args=["foo"], redirections=[(2, ">>", "outfile")]
192 )
193
194 def test_redirect_input_and_output(self):
195 self.assert_parses_to(
196 "foo < infile > outfile",
197 args=["foo"],
198 redirections=[(0, "<", "infile"), (1, ">", "outfile")],
199 )
200
201 def test_redirect_input_multiple(self):
202 self.assert_parses_to(
203 "< infile1 foo< infile2 <infile3",
204 args=["foo"],
205 redirections=[
206 (0, "<", "infile1"),
207 (0, "<", "infile2"),
208 (0, "<", "infile3"),
209 ],
210 )
211
212 def test_redirect_output_multiple(self):
213 self.assert_parses_to(
214 "> outfile1 foo>> outfile2 >outfile3",
215 args=["foo"],
216 redirections=[
217 (1, ">", "outfile1"),
218 (1, ">>", "outfile2"),
219 (1, ">", "outfile3"),
220 ],
221 )
222
223 def test_pipeline(self):
224 self.assert_parses_to(
225 "foo | grep 'bar baz' | sort",
226 args=["foo"],
227 pipeline="grep 'bar baz' | sort",
228 )
229
230 def test_pipeline_and_redirect(self):
231 self.assert_parses_to(
232 "foo < infile | grep 'bar baz' >outfile2",
233 args=["foo"],
234 redirections=[(0, "<", "infile")],
235 pipeline="grep 'bar baz' >outfile2",
236 )
237
238 def test_only_redirect(self):
239 self.assert_parses_to(
240 "< infile >outfile",
241 args=[],
242 redirections=[(0, "<", "infile"), (1, ">", "outfile")],
243 )
244
245 def test_pipeline_empty(self):
246 self.assertRaisesRegex(
247 SyntaxError, "unexpected end of input", parse_shell_command, "foo | "
248 )
249
250 def test_pipe_after_redirect(self):
251 for source in (
252 "foo < |",
253 "foo > |",
254 "foo >> |",
255 ):
256 self.assertRaisesRegex(
257 SyntaxError, r"unexpected '\|'", parse_shell_command, source
258 )
259
260 def test_nothing_before_pipe(self):
261 self.assert_parses_to("| cat", args=[], pipeline="cat")
262
263 def test_redirect_after_redirect(self):
264 self.assertRaisesRegex(
265 SyntaxError, "unexpected '<'", parse_shell_command, "foo < <"
266 )
267 self.assertRaisesRegex(
268 SyntaxError, "unexpected '>'", parse_shell_command, "foo < >"
269 )
270 self.assertRaisesRegex(
271 SyntaxError, "unexpected '>>'", parse_shell_command, "foo < >>"
272 )
273
274 def test_nothing_after_redirect(self):
275 self.assertRaisesRegex(
276 SyntaxError, "unexpected end of input", parse_shell_command, ">"
277 )
278 self.assertRaisesRegex(
279 SyntaxError, "unexpected end of input", parse_shell_command, "foo >"
280 )
281
282 def test_comment(self):
283 self.assert_parses_to("foo # bar < infile | baz", args=["foo"])
284
285 def test_hash_in_word(self):
286 self.assert_parses_to("foo#bar #baz", args=["foo#bar"])
287
288
289 _COMMANDS = CommandNamespace()
290
291
292 @command(
293 description="write arguments to standard output",
294 arguments=(argument("string", nargs="*"),),
295 namespace=_COMMANDS,
296 )
297 def _cmd_echo(prog, name, args, **kwargs):
298 print(*args.string)
299
300
301 @command(
302 description="write arguments to standard error",
303 arguments=(argument("string", nargs="*"),),
304 namespace=_COMMANDS,
305 )
306 def _cmd_echoerr(prog, name, args, **kwargs):
307 print(*args.string, file=sys.stderr)
308
309
310 @command(
311 description="reverse standard input",
312 namespace=_COMMANDS,
313 )
314 def _cmd_tac(prog, name, args, **kwargs):
315 lines = sys.stdin.readlines()
316 lines.reverse()
317 sys.stdout.writelines(lines)
318
319
320 @raw_command(
321 description="call ast.literal_eval()",
322 usage="literal expr",
323 long_description="",
324 namespace=_COMMANDS,
325 )
326 def _cmd_literal(prog, name, args, **kwargs):
327 return ast.literal_eval(args)
328
329
330 class TestRunCommand(TestCase):
331 @staticmethod
332 def run_command(source, **kwargs):
333 return _COMMANDS.run(None, source, globals={}, **kwargs)
334
335 def test_command_not_found(self):
336 self.assertRaises(CommandNotFoundError, self.run_command, "asdf")
337
338 def test_redirect_output(self):
339 with tempfile.TemporaryDirectory() as temp_dir:
340 path = Path(temp_dir) / "file"
341 self.run_command(f"echo foo bar > {path}")
342 self.assertEqual(path.read_text(), "foo bar\n")
343
344 def test_clobber_output(self):
345 with tempfile.TemporaryDirectory() as temp_dir:
346 path = Path(temp_dir) / "file"
347 path.write_text("x\n")
348 self.run_command(f"echo foo bar > {path}")
349 self.assertEqual(path.read_text(), "foo bar\n")
350
351 def test_append_output(self):
352 with tempfile.TemporaryDirectory() as temp_dir:
353 path = Path(temp_dir) / "file"
354 path.write_text("foo\n")
355 self.run_command(f"echo bar >> {path}")
356 self.assertEqual(path.read_text(), "foo\nbar\n")
357
358 def test_redirect_output_multiple(self):
359 with tempfile.TemporaryDirectory() as temp_dir:
360 path1 = Path(temp_dir) / "file1"
361 path2 = Path(temp_dir) / "file2"
362 self.run_command(f"echo foo bar > {path1} > {path2}")
363 # Both files are created, but the output is written to the last
364 # one.
365 self.assertEqual(path1.read_text(), "")
366 self.assertEqual(path2.read_text(), "foo bar\n")
367
368 def test_redirect_error(self):
369 with tempfile.TemporaryDirectory() as temp_dir:
370 path = Path(temp_dir) / "file"
371 self.run_command(f"echoerr foo bar 2> {path}")
372 self.assertEqual(path.read_text(), "foo bar\n")
373
374 def test_clobber_error(self):
375 with tempfile.TemporaryDirectory() as temp_dir:
376 path = Path(temp_dir) / "file"
377 path.write_text("x\n")
378 self.run_command(f"echoerr foo bar 2> {path}")
379 self.assertEqual(path.read_text(), "foo bar\n")
380
381 def test_append_error(self):
382 with tempfile.TemporaryDirectory() as temp_dir:
383 path = Path(temp_dir) / "file"
384 path.write_text("foo\n")
385 self.run_command(f"echoerr bar 2>> {path}")
386 self.assertEqual(path.read_text(), "foo\nbar\n")
387
388 def test_redirect_input(self):
389 with tempfile.TemporaryDirectory() as temp_dir:
390 path = Path(temp_dir) / "file"
391 path.write_text("foo\nbar\n")
392 f = io.StringIO()
393 with contextlib.redirect_stdout(f):
394 self.run_command(f"tac < {path}")
395 self.assertEqual(f.getvalue(), "bar\nfoo\n")
396
397 def test_redirect_input_multiple(self):
398 with tempfile.TemporaryDirectory() as temp_dir:
399 path1 = Path(temp_dir) / "file1"
400 path2 = Path(temp_dir) / "file2"
401 path1.write_text("foo\n")
402 path2.write_text("bar\n")
403 f = io.StringIO()
404 with contextlib.redirect_stdout(f):
405 self.run_command(f"tac < {path1} < {path2}")
406 # The last redirection wins.
407 self.assertEqual(f.getvalue(), "bar\n")
408
409 def test_pipe(self):
410 with tempfile.TemporaryDirectory() as temp_dir:
411 path = Path(temp_dir) / "file"
412 self.run_command(f"echo hello | grep -o hell > {path}")
413 self.assertEqual(path.read_text(), "hell\n")
414
415 def test_redirect_input_and_pipe(self):
416 with tempfile.TemporaryDirectory() as temp_dir:
417 in_path = Path(temp_dir) / "infile"
418 out_path = Path(temp_dir) / "outfile"
419 in_path.write_text("foo\nbar\n")
420 self.run_command(f"tac < {in_path} | grep foo > {out_path}")
421 self.assertEqual(out_path.read_text(), "foo\n")
422
423 def test_redirect_output_and_pipe(self):
424 with tempfile.TemporaryDirectory() as temp_dir:
425 path1 = Path(temp_dir) / "file1"
426 path2 = Path(temp_dir) / "file2"
427 self.run_command(f"echo foo bar > {path1} | cat > {path2}")
428 # Redirection wins over pipe.
429 self.assertEqual(path1.read_text(), "foo bar\n")
430 self.assertEqual(path2.read_text(), "")
431
432 def test_redirect_input_failure(self):
433 with tempfile.TemporaryDirectory() as temp_dir:
434 path = Path(temp_dir) / "file"
435 self.assertRaises(OSError, self.run_command, f"tac < {path}")
436
437 def test_redirect_output_failure(self):
438 with tempfile.TemporaryDirectory() as temp_dir:
439 path1 = Path(temp_dir) / "file"
440 path2 = Path(temp_dir) / "dir/file"
441 path1.write_text("foo\n")
442 self.assertRaises(OSError, self.run_command, f"echo < {path1} > {path2}")
443
444 def test_empty(self):
445 self.assertRaisesRegex(
446 SyntaxError, "expected command name", self.run_command, ""
447 )
448 self.assertRaisesRegex(
449 SyntaxError, "expected command name", self.run_command, " \t"
450 )
451 self.assertRaisesRegex(
452 SyntaxError, "expected command name", self.run_command, "# comment"
453 )
454
455 def test_not_command_name(self):
456 self.assertRaisesRegex(
457 SyntaxError, "expected command name", self.run_command, "< /dev/null"
458 )
459 self.assertRaisesRegex(
460 SyntaxError, "expected command name", self.run_command, "| cat"
461 )
462
463 def test_redirect_invalid_input_fd(self):
464 self.assertRaises(NotImplementedError, self.run_command, "echo 1< /dev/null")
465
466 def test_redirect_invalid_output_fd(self):
467 self.assertRaises(NotImplementedError, self.run_command, "echo 0> /dev/null")
468 self.assertRaises(NotImplementedError, self.run_command, "echo 0>> /dev/null")
469
470 def test_pipe_failure(self):
471 self.assertRaises(CommandExitStatusError, self.run_command, "echo foo | false")
472
473 def test_onerror(self):
474 onerror = unittest.mock.Mock()
475 self.run_command("asdf", onerror=onerror)
476 onerror.assert_called_once()
477
478 def test_onerror_raises(self):
479 def onerror(e):
480 1 / 0
481
482 self.assertRaises(ZeroDivisionError, self.run_command, "asdf", onerror=onerror)
483
484 def test_raw(self):
485 self.assertEqual(self.run_command("literal (1, 2)"), (1, 2))
486
487 def test_argument_error(self):
488 self.assertRaises(
489 CommandArgumentError, self.run_command, "tac --garbage 2> /dev/null"
490 )
491
492
493 class TestSanitizeRst(TestCase):
494 def test_empty(self):
495 self.assertEqual(_sanitize_rst(""), "")
496
497 def test_none(self):
498 self.assertEqual(_sanitize_rst(None), None)
499
500 def test_bold(self):
501 self.assertEqual(_sanitize_rst("**true**"), "true")
502
503 def test_italic(self):
504 self.assertEqual(_sanitize_rst("*CPU*"), "CPU")
505
506 def test_escaped_asterisk(self):
507 self.assertEqual(_sanitize_rst(r"\*"), "*")
508
509 def test_bold_escaped_asterisk(self):
510 self.assertEqual(_sanitize_rst(r"**\***"), "*")
511
512 def test_escaped_backslash(self):
513 self.assertEqual(_sanitize_rst("\\\\"), "\\")
514
515 def test_bold_and_escaped_dash(self):
516 self.assertEqual(_sanitize_rst(r"**\-\-drgn**"), "--drgn")
517
518
519 class TestReprBlack(TestCase):
520 def test_simple(self):
521 self.assertEqual(_repr_black("foo"), '"foo"')
522
523 def test_single_quote(self):
524 self.assertEqual(_repr_black("ne'er"), '"ne\'er"')
525
526 def test_double_quote(self):
527 self.assertEqual(_repr_black('l"oL'), "'l\"oL'")
528
529
530 class TestReprRawBytes(TestCase):
531 def test_simple(self):
532 self.assertEqual(_repr_raw_bytes(b"foo"), 'rb"foo"')
533
534 def test_single_quote(self):
535 self.assertEqual(_repr_raw_bytes(b"ne'er"), 'rb"ne\'er"')
536
537 def test_double_quote(self):
538 self.assertEqual(_repr_raw_bytes(b'l"oL'), "rb'l\"oL'")
539
540 def test_single_and_double_quote(self):
541 self.assertEqual(_repr_raw_bytes(b"ne'er l\"oL"), 'b"ne\'er l\\"oL"')
542
543 def test_ends_with_backslash(self):
544 self.assertEqual(_repr_raw_bytes(b"foo\\"), r'b"foo\\"')
545
546 def test_single_quote_and_ends_with_backslash(self):
547 self.assertEqual(_repr_raw_bytes(b"ne'er\\"), 'b"ne\'er\\\\"')
548
549 def test_double_quote_and_ends_with_backslash(self):
550 self.assertEqual(_repr_raw_bytes(b'l"oL\\'), "b'l\"oL\\\\'")
551
552 def test_single_and_double_quote_and_ends_with_backslash(self):
553 self.assertEqual(_repr_raw_bytes(b"ne'er l\"oL\\"), 'b"ne\'er l\\"oL\\\\"')
554
555
556 class TestDrgnCodeBuilder(TestCase):
557 def test_empty(self):
558 self.assertEqual(DrgnCodeBuilder(None).get(), "")
559
560 def test_no_imports(self):
561 code = DrgnCodeBuilder(None)
562 code.append("pass\n")
563 self.assertEqual(code.get(), "pass\n")
564
565 def test_sort_imports(self):
566 code = DrgnCodeBuilder(None)
567 code.add_import("sys")
568 code.add_import("os")
569 self.assertEqual(
570 code.get(),
571 """\
572 import os
573 import sys
574 """,
575 )
576
577 def test_deduplicate_imports(self):
578 code = DrgnCodeBuilder(None)
579 code.add_import("os")
580 code.add_import("sys")
581 code.add_import("os")
582 self.assertEqual(
583 code.get(),
584 """\
585 import os
586 import sys
587 """,
588 )
589
590 def test_sort_from_imports(self):
591 code = DrgnCodeBuilder(None)
592 code.add_import("sys")
593 code.add_import("os")
594 code.add_from_import("sys", "stdout")
595 self.assertEqual(
596 code.get(),
597 """\
598 import os
599 import sys
600 from sys import stdout
601 """,
602 )
603
604 def test_first_party_imports(self):
605 code = DrgnCodeBuilder(None)
606 code.add_import("os")
607 code.add_import("sys")
608 code.add_import("drgn")
609 self.assertEqual(
610 code.get(),
611 """\
612 import os
613 import sys
614
615 import drgn
616 """,
617 )
618
619 def test_merge_from_imports(self):
620 code = DrgnCodeBuilder(None)
621 code.add_from_import("drgn", "Object", "Program")
622 code.add_from_import("drgn", "Object")
623 code.add_from_import("drgn", "Type")
624 self.assertEqual(
625 code.get(),
626 """\
627 from drgn import Object, Program, Type
628 """,
629 )
630
631 def test_long_from_imports(self):
632 code = DrgnCodeBuilder(None)
633 code.add_from_import(
634 "os",
635 "abort",
636 "access",
637 "chdir",
638 "chmod",
639 "chown",
640 "close",
641 "closerange",
642 "confstr",
643 "copy_file_range",
644 )
645 self.assertEqual(
646 code.get(),
647 """\
648 from os import (
649 abort,
650 access,
651 chdir,
652 chmod,
653 chown,
654 close,
655 closerange,
656 confstr,
657 copy_file_range,
658 )
659 """,
660 )
661
662 def test_imports_and_code(self):
663 code = DrgnCodeBuilder(None)
664 code.append("pass\n")
665 code.add_import("os")
666 self.assertEqual(
667 code.get(),
668 """\
669 import os
670
671
672 pass
673 """,
674 )