1
|
|
|
import os |
2
|
|
|
import sys |
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
sys.path.insert(0, ".") |
6
|
|
|
from coalib.misc.Shell import (prepare_string_argument, |
7
|
|
|
escape_path_argument, |
8
|
|
|
run_interactive_shell_command, |
9
|
|
|
run_shell_command) |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class ShellTest(unittest.TestCase): |
13
|
|
|
# Tests the function that makes a path shell-argument-ready. |
14
|
|
|
|
15
|
|
|
def test_escape_path_argument(self): |
16
|
|
|
osname = "Linux" |
17
|
|
|
self.assertEqual( |
18
|
|
|
escape_path_argument("/home/usr/a-file", osname), |
19
|
|
|
"/home/usr/a-file") |
20
|
|
|
self.assertEqual( |
21
|
|
|
escape_path_argument("/home/usr/a-dir/", osname), |
22
|
|
|
"/home/usr/a-dir/") |
23
|
|
|
self.assertEqual( |
24
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", osname), |
25
|
|
|
"/home/us\\ r/a-file\\ with\\ spaces.bla") |
26
|
|
|
self.assertEqual( |
27
|
|
|
escape_path_argument("/home/us r/a-dir with spaces/x/", osname), |
28
|
|
|
"/home/us\\ r/a-dir\\ with\\ spaces/x/") |
29
|
|
|
self.assertEqual( |
30
|
|
|
escape_path_argument( |
31
|
|
|
"relative something/with cherries and/pickles.delicious", |
32
|
|
|
osname), |
33
|
|
|
"relative\\ something/with\\ cherries\\ and/pickles.delicious") |
34
|
|
|
|
35
|
|
|
osname = "Windows" |
36
|
|
|
self.assertEqual( |
37
|
|
|
escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", osname), |
38
|
|
|
"\"C:\\Windows\\has-a-weird-shell.txt\"") |
39
|
|
|
self.assertEqual( |
40
|
|
|
escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", osname), |
41
|
|
|
"\"C:\\Windows\\lolrofl\\dirs\\\"") |
42
|
|
|
self.assertEqual( |
43
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", osname), |
44
|
|
|
"\"X:\\Users\\Maito Gai\\fi le.exe\"") |
45
|
|
|
self.assertEqual( |
46
|
|
|
escape_path_argument("X:\\Users\\Mai to Gai\\director y\\", |
47
|
|
|
osname), |
48
|
|
|
"\"X:\\Users\\Mai to Gai\\director y\\\"") |
49
|
|
|
self.assertEqual( |
50
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y", |
51
|
|
|
osname), |
52
|
|
|
"\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"") |
53
|
|
|
self.assertEqual( |
54
|
|
|
escape_path_argument("System32\\my-custom relative tool\\", |
55
|
|
|
osname), |
56
|
|
|
"\"System32\\my-custom relative tool\\\"") |
57
|
|
|
self.assertEqual( |
58
|
|
|
escape_path_argument("System32\\illegal\" name \"\".curd", osname), |
59
|
|
|
"\"System32\\illegal^\" name ^\"^\".curd\"") |
60
|
|
|
|
61
|
|
|
osname = "INVALID" |
62
|
|
|
self.assertEqual( |
63
|
|
|
escape_path_argument("/home/usr/a-file", osname), |
64
|
|
|
"/home/usr/a-file") |
65
|
|
|
self.assertEqual( |
66
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", osname), |
67
|
|
|
"/home/us r/a-file with spaces.bla") |
68
|
|
|
self.assertEqual( |
69
|
|
|
escape_path_argument("|home|us r|a*dir with spaces|x|", osname), |
70
|
|
|
"|home|us r|a*dir with spaces|x|") |
71
|
|
|
self.assertEqual( |
72
|
|
|
escape_path_argument("system|a|b|c?d", osname), |
73
|
|
|
"system|a|b|c?d") |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class RunShellCommandTest(unittest.TestCase): |
77
|
|
|
|
78
|
|
|
@staticmethod |
79
|
|
|
def construct_testscript_command(scriptname): |
80
|
|
|
return " ".join( |
81
|
|
|
escape_path_argument(s) for s in ( |
82
|
|
|
escape_path_argument(sys.executable), |
83
|
|
|
os.path.join(os.path.dirname(os.path.realpath(__file__)), |
84
|
|
|
"run_shell_command_testfiles", |
85
|
|
|
scriptname))) |
86
|
|
|
|
87
|
|
|
def test_run_interactive_shell_command(self): |
88
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
89
|
|
|
"test_interactive_program.py") |
90
|
|
|
|
91
|
|
|
with run_interactive_shell_command(command) as p: |
92
|
|
|
self.assertEqual(p.stdout.readline(), "test_program X\n") |
93
|
|
|
self.assertEqual(p.stdout.readline(), "Type in a number:\n") |
94
|
|
|
p.stdin.write("33\n") |
95
|
|
|
p.stdin.flush() |
96
|
|
|
self.assertEqual(p.stdout.readline(), "33\n") |
97
|
|
|
self.assertEqual(p.stdout.readline(), "Exiting program.\n") |
98
|
|
|
|
99
|
|
|
def test_run_interactive_shell_command_kwargs_delegation(self): |
100
|
|
|
with self.assertRaises(TypeError): |
101
|
|
|
with run_interactive_shell_command("some_command", |
102
|
|
|
weird_parameter=30): |
103
|
|
|
pass |
104
|
|
|
|
105
|
|
|
# Test one of the forbidden parameters. |
106
|
|
|
with self.assertRaises(TypeError): |
107
|
|
|
with run_interactive_shell_command("some_command", shell=False): |
108
|
|
|
pass |
109
|
|
|
|
110
|
|
|
def test_run_noninteractive_shell_command(self): |
111
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
112
|
|
|
"test_program.py") |
113
|
|
|
|
114
|
|
|
stdout, stderr = run_shell_command(command) |
115
|
|
|
|
116
|
|
|
expected = ("test_program Z\n" |
117
|
|
|
"non-interactive mode.\n" |
118
|
|
|
"Exiting...\n") |
119
|
|
|
self.assertEqual(stdout, expected) |
120
|
|
|
self.assertEqual(stderr, "") |
121
|
|
|
|
122
|
|
|
def test_run_input_shell_command(self): |
123
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
124
|
|
|
"test_input_program.py") |
125
|
|
|
|
126
|
|
|
stdout, stderr = run_shell_command(command, "1 4 10 22") |
127
|
|
|
|
128
|
|
|
self.assertEqual(stdout, "37\n") |
129
|
|
|
self.assertEqual(stderr, "") |
130
|
|
|
|
131
|
|
|
stdout, stderr = run_shell_command(command, "1 p 5") |
132
|
|
|
|
133
|
|
|
self.assertEqual(stdout, "INVALID INPUT\n") |
134
|
|
|
self.assertEqual(stderr, "") |
135
|
|
|
|
136
|
|
|
def test_run_shell_command_kwargs_delegation(self): |
137
|
|
|
with self.assertRaises(TypeError): |
138
|
|
|
run_shell_command("super-cool-command", weird_parameter2="abc") |
139
|
|
|
|
140
|
|
|
# Test one of the forbidden parameters. |
141
|
|
|
with self.assertRaises(TypeError): |
142
|
|
|
run_shell_command("super-cool-command", universal_newlines=False) |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
class PrepareStringArgumentTest(unittest.TestCase): |
146
|
|
|
def setUp(self): |
147
|
|
|
self.test_strings = ("normal_string", |
148
|
|
|
"string with spaces", |
149
|
|
|
'string with quotes"a', |
150
|
|
|
"string with s-quotes'b", |
151
|
|
|
"bsn \n A", |
152
|
|
|
"unrecognized \\q escape") |
153
|
|
|
|
154
|
|
|
def test_prepare_string_argument_linux(self): |
155
|
|
|
os = "Linux" |
|
|
|
|
156
|
|
|
expected_results = ('"normal_string"', |
157
|
|
|
'"string with spaces"', |
158
|
|
|
'"string with quotes\\"a"', |
159
|
|
|
'"string with s-quotes\'b"', |
160
|
|
|
'"bsn \n A"', |
161
|
|
|
'"unrecognized \\q escape"') |
162
|
|
|
|
163
|
|
|
for string, result in zip(self.test_strings, expected_results): |
164
|
|
|
self.assertEqual(prepare_string_argument(string, os), result) |
165
|
|
|
|
166
|
|
|
def test_prepare_string_argument_windows(self): |
167
|
|
|
os = "Windows" |
|
|
|
|
168
|
|
|
expected_results = ('"normal_string"', |
169
|
|
|
'"string with spaces"', |
170
|
|
|
'"string with quotes^"a"', |
171
|
|
|
'"string with s-quotes\'b"', |
172
|
|
|
'"bsn ^\n\n A"', |
173
|
|
|
'"unrecognized \\q escape"') |
174
|
|
|
|
175
|
|
|
for string, result in zip(self.test_strings, expected_results): |
176
|
|
|
self.assertEqual(prepare_string_argument(string, os), result) |
177
|
|
|
|
178
|
|
|
def test_prepare_string_argument_unsupported_os(self): |
179
|
|
|
os = "WeIrD_O/S" |
|
|
|
|
180
|
|
|
|
181
|
|
|
for string in self.test_strings: |
182
|
|
|
self.assertEqual(prepare_string_argument(string, os), string) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
if __name__ == '__main__': |
186
|
|
|
unittest.main(verbosity=2) |
187
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: