1 | from contextlib import ExitStack |
||
2 | import os |
||
3 | import sys |
||
4 | from tempfile import NamedTemporaryFile |
||
5 | import unittest |
||
6 | |||
7 | from coalib.misc.Shell import ( |
||
8 | escape_path_argument, prepare_string_argument, |
||
9 | run_interactive_shell_command, run_shell_command) |
||
10 | |||
11 | |||
12 | class EscapePathArgumentTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
13 | |||
14 | def test_escape_path_argument_sh(self): |
||
15 | _type = "sh" |
||
16 | self.assertEqual( |
||
17 | escape_path_argument("/home/usr/a-file", _type), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
18 | "/home/usr/a-file") |
||
19 | self.assertEqual( |
||
20 | escape_path_argument("/home/usr/a-dir/", _type), |
||
21 | "/home/usr/a-dir/") |
||
22 | self.assertEqual( |
||
23 | escape_path_argument("/home/us r/a-file with spaces.bla", |
||
24 | _type), |
||
25 | "/home/us\\ r/a-file\\ with\\ spaces.bla") |
||
26 | self.assertEqual( |
||
27 | escape_path_argument("/home/us r/a-dir with spaces/x/", |
||
28 | _type), |
||
29 | "/home/us\\ r/a-dir\\ with\\ spaces/x/") |
||
30 | self.assertEqual( |
||
31 | escape_path_argument( |
||
32 | "relative something/with cherries and/pickles.delicious", |
||
33 | _type), |
||
34 | "relative\\ something/with\\ cherries\\ and/pickles.delicious") |
||
35 | |||
36 | def test_escape_path_argument_cmd(self): |
||
37 | _type = "cmd" |
||
38 | self.assertEqual( |
||
39 | escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", _type), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
40 | "\"C:\\Windows\\has-a-weird-shell.txt\"") |
||
41 | self.assertEqual( |
||
42 | escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", _type), |
||
43 | "\"C:\\Windows\\lolrofl\\dirs\\\"") |
||
44 | self.assertEqual( |
||
45 | escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", _type), |
||
46 | "\"X:\\Users\\Maito Gai\\fi le.exe\"") |
||
47 | self.assertEqual( |
||
48 | escape_path_argument("X:\\Users\\Mai to Gai\\director y\\", |
||
49 | _type), |
||
50 | "\"X:\\Users\\Mai to Gai\\director y\\\"") |
||
51 | self.assertEqual( |
||
52 | escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y", |
||
53 | _type), |
||
54 | "\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"") |
||
55 | self.assertEqual( |
||
56 | escape_path_argument("System32\\my-custom relative tool\\", |
||
57 | _type), |
||
58 | "\"System32\\my-custom relative tool\\\"") |
||
59 | self.assertEqual( |
||
60 | escape_path_argument("System32\\illegal\" name \"\".curd", _type), |
||
61 | "\"System32\\illegal^\" name ^\"^\".curd\"") |
||
62 | |||
63 | def test_escape_path_argument_unsupported(self): |
||
64 | _type = "INVALID" |
||
65 | self.assertEqual( |
||
66 | escape_path_argument("/home/usr/a-file", _type), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
67 | "/home/usr/a-file") |
||
68 | self.assertEqual( |
||
69 | escape_path_argument("/home/us r/a-file with spaces.bla", _type), |
||
70 | "/home/us r/a-file with spaces.bla") |
||
71 | self.assertEqual( |
||
72 | escape_path_argument("|home|us r|a*dir with spaces|x|", _type), |
||
73 | "|home|us r|a*dir with spaces|x|") |
||
74 | self.assertEqual( |
||
75 | escape_path_argument("system|a|b|c?d", _type), |
||
76 | "system|a|b|c?d") |
||
77 | |||
78 | |||
79 | class RunShellCommandTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
80 | |||
81 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
82 | def construct_testscript_command(scriptname): |
||
83 | return (sys.executable, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
84 | os.path.join(os.path.dirname(os.path.realpath(__file__)), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
85 | "run_shell_command_testfiles", |
||
86 | scriptname)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
87 | |||
88 | def test_run_interactive_shell_command(self): |
||
89 | command = RunShellCommandTest.construct_testscript_command( |
||
90 | "test_interactive_program.py") |
||
91 | |||
92 | with run_interactive_shell_command(command) as p: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
93 | self.assertEqual(p.stdout.readline(), "test_program X\n") |
||
94 | self.assertEqual(p.stdout.readline(), "Type in a number:\n") |
||
95 | p.stdin.write("33\n") |
||
96 | p.stdin.flush() |
||
97 | self.assertEqual(p.stdout.readline(), "33\n") |
||
98 | self.assertEqual(p.stdout.readline(), "Exiting program.\n") |
||
99 | |||
100 | self.assertEqual(p.stdout.read(), "") |
||
101 | self.assertEqual(p.stderr.read(), "") |
||
102 | |||
103 | def test_run_interactive_shell_command_custom_streams(self): |
||
104 | command = RunShellCommandTest.construct_testscript_command( |
||
105 | "test_interactive_program.py") |
||
106 | |||
107 | with ExitStack() as stack: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
108 | streams = {s: stack.enter_context(NamedTemporaryFile(mode="w+")) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
109 | for s in ["stdout", "stderr", "stdin"]} |
||
110 | |||
111 | with run_interactive_shell_command(command, **streams) as p: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
112 | streams["stdin"].write("712\n") |
||
113 | streams["stdin"].flush() |
||
114 | streams["stdin"].seek(0) |
||
115 | |||
116 | self.assertFalse(streams["stdout"].closed) |
||
117 | self.assertFalse(streams["stderr"].closed) |
||
118 | self.assertFalse(streams["stdin"].closed) |
||
119 | |||
120 | streams["stdout"].seek(0) |
||
121 | self.assertEqual(streams["stdout"].read(), |
||
122 | "test_program X\nType in a number:\n712\n" |
||
123 | "Exiting program.\n") |
||
124 | |||
125 | streams["stderr"].seek(0) |
||
126 | self.assertEqual(streams["stderr"].read(), "") |
||
127 | |||
128 | def test_run_interactive_shell_command_kwargs_delegation(self): |
||
129 | with self.assertRaises(TypeError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
130 | with run_interactive_shell_command("some_command", |
||
131 | weird_parameter=30): |
||
132 | pass |
||
133 | |||
134 | def test_run_shell_command_without_stdin(self): |
||
135 | command = RunShellCommandTest.construct_testscript_command( |
||
136 | "test_program.py") |
||
137 | |||
138 | stdout, stderr = run_shell_command(command) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
139 | |||
140 | expected = ("test_program Z\n" |
||
141 | "non-interactive mode.\n" |
||
142 | "Exiting...\n") |
||
143 | self.assertEqual(stdout, expected) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
144 | self.assertEqual(stderr, "") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
145 | |||
146 | def test_run_shell_command_with_stdin(self): |
||
147 | command = RunShellCommandTest.construct_testscript_command( |
||
148 | "test_input_program.py") |
||
149 | |||
150 | stdout, stderr = run_shell_command(command, "1 4 10 22") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
151 | |||
152 | self.assertEqual(stdout, "37\n") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
153 | self.assertEqual(stderr, "") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
154 | |||
155 | stdout, stderr = run_shell_command(command, "1 p 5") |
||
156 | |||
157 | self.assertEqual(stdout, "") |
||
158 | self.assertEqual(stderr, "INVALID INPUT\n") |
||
159 | |||
160 | def test_run_shell_command_kwargs_delegation(self): |
||
161 | with self.assertRaises(TypeError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
162 | run_shell_command("super-cool-command", weird_parameter2="abc") |
||
163 | |||
164 | |||
165 | class PrepareStringArgumentTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
166 | |||
167 | def setUp(self): |
||
168 | self.test_strings = ("normal_string", |
||
169 | "string with spaces", |
||
170 | 'string with quotes"a', |
||
171 | "string with s-quotes'b", |
||
172 | "bsn \n A", |
||
173 | "unrecognized \\q escape") |
||
174 | |||
175 | def test_prepare_string_argument_sh(self): |
||
176 | expected_results = ('"normal_string"', |
||
177 | '"string with spaces"', |
||
178 | '"string with quotes\\"a"', |
||
179 | '"string with s-quotes\'b"', |
||
180 | '"bsn \n A"', |
||
181 | '"unrecognized \\q escape"') |
||
182 | |||
183 | for string, result in zip(self.test_strings, expected_results): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
184 | self.assertEqual(prepare_string_argument(string, "sh"), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
185 | result) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
186 | |||
187 | def test_prepare_string_argument_unsupported(self): |
||
188 | for string in self.test_strings: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
189 | self.assertEqual(prepare_string_argument(string, "WeIrD_O/S"), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
190 | string) |
||
191 |