1
|
|
|
import os |
2
|
|
|
import sys |
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
from coalib.misc.Shell import ( |
6
|
|
|
escape_path_argument, prepare_string_argument, |
7
|
|
|
run_interactive_shell_command, run_shell_command) |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class EscapePathArgumentTest(unittest.TestCase): |
11
|
|
|
|
12
|
|
|
def test_escape_path_argument_sh(self): |
13
|
|
|
_type = "sh" |
14
|
|
|
self.assertEqual( |
15
|
|
|
escape_path_argument("/home/usr/a-file", _type), |
16
|
|
|
"/home/usr/a-file") |
17
|
|
|
self.assertEqual( |
18
|
|
|
escape_path_argument("/home/usr/a-dir/", _type), |
19
|
|
|
"/home/usr/a-dir/") |
20
|
|
|
self.assertEqual( |
21
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", |
22
|
|
|
_type), |
23
|
|
|
"/home/us\\ r/a-file\\ with\\ spaces.bla") |
24
|
|
|
self.assertEqual( |
25
|
|
|
escape_path_argument("/home/us r/a-dir with spaces/x/", |
26
|
|
|
_type), |
27
|
|
|
"/home/us\\ r/a-dir\\ with\\ spaces/x/") |
28
|
|
|
self.assertEqual( |
29
|
|
|
escape_path_argument( |
30
|
|
|
"relative something/with cherries and/pickles.delicious", |
31
|
|
|
_type), |
32
|
|
|
"relative\\ something/with\\ cherries\\ and/pickles.delicious") |
33
|
|
|
|
34
|
|
|
def test_escape_path_argument_cmd(self): |
35
|
|
|
_type = "cmd" |
36
|
|
|
self.assertEqual( |
37
|
|
|
escape_path_argument("C:\\Windows\\has-a-weird-shell.txt", _type), |
38
|
|
|
"\"C:\\Windows\\has-a-weird-shell.txt\"") |
39
|
|
|
self.assertEqual( |
40
|
|
|
escape_path_argument("C:\\Windows\\lolrofl\\dirs\\", _type), |
41
|
|
|
"\"C:\\Windows\\lolrofl\\dirs\\\"") |
42
|
|
|
self.assertEqual( |
43
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\fi le.exe", _type), |
44
|
|
|
"\"X:\\Users\\Maito Gai\\fi le.exe\"") |
45
|
|
|
self.assertEqual( |
46
|
|
|
escape_path_argument("X:\\Users\\Mai to Gai\\director y\\", |
47
|
|
|
_type), |
48
|
|
|
"\"X:\\Users\\Mai to Gai\\director y\\\"") |
49
|
|
|
self.assertEqual( |
50
|
|
|
escape_path_argument("X:\\Users\\Maito Gai\\\"seven-gates\".y", |
51
|
|
|
_type), |
52
|
|
|
"\"X:\\Users\\Maito Gai\\^\"seven-gates^\".y\"") |
53
|
|
|
self.assertEqual( |
54
|
|
|
escape_path_argument("System32\\my-custom relative tool\\", |
55
|
|
|
_type), |
56
|
|
|
"\"System32\\my-custom relative tool\\\"") |
57
|
|
|
self.assertEqual( |
58
|
|
|
escape_path_argument("System32\\illegal\" name \"\".curd", _type), |
59
|
|
|
"\"System32\\illegal^\" name ^\"^\".curd\"") |
60
|
|
|
|
61
|
|
|
def test_escape_path_argument_unsupported(self): |
62
|
|
|
_type = "INVALID" |
63
|
|
|
self.assertEqual( |
64
|
|
|
escape_path_argument("/home/usr/a-file", _type), |
65
|
|
|
"/home/usr/a-file") |
66
|
|
|
self.assertEqual( |
67
|
|
|
escape_path_argument("/home/us r/a-file with spaces.bla", _type), |
68
|
|
|
"/home/us r/a-file with spaces.bla") |
69
|
|
|
self.assertEqual( |
70
|
|
|
escape_path_argument("|home|us r|a*dir with spaces|x|", _type), |
71
|
|
|
"|home|us r|a*dir with spaces|x|") |
72
|
|
|
self.assertEqual( |
73
|
|
|
escape_path_argument("system|a|b|c?d", _type), |
74
|
|
|
"system|a|b|c?d") |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
class RunShellCommandTest(unittest.TestCase): |
78
|
|
|
|
79
|
|
|
@staticmethod |
80
|
|
|
def construct_testscript_command(scriptname): |
81
|
|
|
return " ".join( |
82
|
|
|
escape_path_argument(s) for s in ( |
83
|
|
|
sys.executable, |
84
|
|
|
os.path.join(os.path.dirname(os.path.realpath(__file__)), |
85
|
|
|
"run_shell_command_testfiles", |
86
|
|
|
scriptname))) |
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: |
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
|
|
|
def test_run_interactive_shell_command_kwargs_delegation(self): |
101
|
|
|
with self.assertRaises(TypeError): |
102
|
|
|
with run_interactive_shell_command("some_command", |
103
|
|
|
weird_parameter=30): |
104
|
|
|
pass |
105
|
|
|
|
106
|
|
|
# Test one of the forbidden parameters. |
107
|
|
|
with self.assertRaises(TypeError): |
108
|
|
|
with run_interactive_shell_command("some_command", shell=False): |
109
|
|
|
pass |
110
|
|
|
|
111
|
|
|
def test_run_shell_command_without_stdin(self): |
112
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
113
|
|
|
"test_program.py") |
114
|
|
|
|
115
|
|
|
stdout, stderr = run_shell_command(command) |
116
|
|
|
|
117
|
|
|
expected = ("test_program Z\n" |
118
|
|
|
"non-interactive mode.\n" |
119
|
|
|
"Exiting...\n") |
120
|
|
|
self.assertEqual(stdout, expected) |
121
|
|
|
self.assertEqual(stderr, "") |
122
|
|
|
|
123
|
|
|
def test_run_shell_command_with_stdin(self): |
124
|
|
|
command = RunShellCommandTest.construct_testscript_command( |
125
|
|
|
"test_input_program.py") |
126
|
|
|
|
127
|
|
|
stdout, stderr = run_shell_command(command, "1 4 10 22") |
128
|
|
|
|
129
|
|
|
self.assertEqual(stdout, "37\n") |
130
|
|
|
self.assertEqual(stderr, "") |
131
|
|
|
|
132
|
|
|
stdout, stderr = run_shell_command(command, "1 p 5") |
133
|
|
|
|
134
|
|
|
self.assertEqual(stdout, "") |
135
|
|
|
self.assertEqual(stderr, "INVALID INPUT\n") |
136
|
|
|
|
137
|
|
|
def test_run_shell_command_kwargs_delegation(self): |
138
|
|
|
with self.assertRaises(TypeError): |
139
|
|
|
run_shell_command("super-cool-command", weird_parameter2="abc") |
140
|
|
|
|
141
|
|
|
# Test one of the forbidden parameters. |
142
|
|
|
with self.assertRaises(TypeError): |
143
|
|
|
run_shell_command("super-cool-command", universal_newlines=False) |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
class PrepareStringArgumentTest(unittest.TestCase): |
147
|
|
|
|
148
|
|
|
def setUp(self): |
149
|
|
|
self.test_strings = ("normal_string", |
150
|
|
|
"string with spaces", |
151
|
|
|
'string with quotes"a', |
152
|
|
|
"string with s-quotes'b", |
153
|
|
|
"bsn \n A", |
154
|
|
|
"unrecognized \\q escape") |
155
|
|
|
|
156
|
|
|
def test_prepare_string_argument_sh(self): |
157
|
|
|
expected_results = ('"normal_string"', |
158
|
|
|
'"string with spaces"', |
159
|
|
|
'"string with quotes\\"a"', |
160
|
|
|
'"string with s-quotes\'b"', |
161
|
|
|
'"bsn \n A"', |
162
|
|
|
'"unrecognized \\q escape"') |
163
|
|
|
|
164
|
|
|
for string, result in zip(self.test_strings, expected_results): |
165
|
|
|
self.assertEqual(prepare_string_argument(string, "sh"), |
166
|
|
|
result) |
167
|
|
|
|
168
|
|
|
def test_prepare_string_argument_unsupported(self): |
169
|
|
|
for string in self.test_strings: |
170
|
|
|
self.assertEqual(prepare_string_argument(string, "WeIrD_O/S"), |
171
|
|
|
string) |
172
|
|
|
|