-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
256 lines (227 loc) · 10.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import os
import unittest
import subprocess
import sys
import psutil
import platform
class LavaSH(unittest.TestCase):
res_score = 0
def _run(self, command, expected_out='', expected_err='', code=0, pre=None, post=None):
if pre:
pre(self)
processes = psutil.pids()
p = subprocess.Popen(['./lavash', '-c', command], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
self.assertEqual(code, p.returncode)
self.assertEqual(out.decode(), expected_out)
self.assertEqual(err.decode(), expected_err)
self.assertFalse(set(psutil.pids()) - set(processes), "Some processes still exist. "
"This may False positive on local runs")
if post:
post(self)
@classmethod
def tearDownClass(cls):
print()
print('Score:', cls.res_score)
with open('res.txt', 'w') as f:
f.write(str(cls.res_score))
@staticmethod
def _remove_file(name):
def inner(self):
if os.path.exists(name):
os.remove(name)
return inner
@staticmethod
def _create_file(name, data):
def inner(self):
if os.path.exists(name):
os.remove(name)
with open(name, 'w') as f:
f.write(data)
return inner
@staticmethod
def _seq(*acts):
def inner(self):
for act in acts:
act(self)
return inner
@staticmethod
def _check_file_contains(name, expected):
def inner(self):
if expected is None:
self.assertFalse(os.path.exists(name))
else:
with open(name, 'r') as f:
self.assertEqual(f.read(), expected)
return inner
@staticmethod
def score(score):
def decorator(func):
def inner(self, *args, **kwargs):
try:
res = func(self, *args, **kwargs)
except Exception as e:
raise
else:
LavaSH.res_score += score
return res
return inner
return decorator
@score(10)
def test_simple(self):
self._run('echo', '\n')
self._run('echo hello', 'hello\n')
self._run('echo hello world', 'hello world\n')
@score(10)
def test_relative_path(self):
self._run('./tools/print_args', './tools/print_args\n')
self._run('./tools/print_args 1', './tools/print_args\n1\n')
self._run('./tools/print_args 1 2 3 4', './tools/print_args\n1\n2\n3\n4\n')
@score(20)
def test_spaces(self):
self._run('./tools/print_args 1 "2 3" 4', './tools/print_args\n1\n2 3\n4\n')
self._run('./tools/print_args " \' " "1 2 3 4\\\\"', './tools/print_args\n \' \n1 2 3 4\\\n')
@score(20)
def test_pipe(self):
if platform.system() == 'Darwin':
self._run('echo hello | wc', ' 1 1 6\n')
self._run('echo hello | wc | wc', ' 1 3 25\n')
self._run('echo hello | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc',
' 1 3 25\n')
else:
self._run('echo hello | wc', ' 1 1 6\n')
self._run('echo hello | wc | wc', ' 1 3 24\n')
self._run('echo hello | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc | wc',
' 1 3 24\n')
@score(10)
def test_out_files(self):
self._run('echo hello > output.txt',
pre=self._remove_file('output.txt'),
post=self._check_file_contains('output.txt', 'hello\n'))
self._run('echo hello >output.txt',
pre=self._remove_file('output.txt'),
post=self._check_file_contains('output.txt', 'hello\n'))
@score(10)
def test_in_files(self):
if platform.system() == 'Darwin':
self._run('wc < input.txt',
' 9 9 26\n',
pre=self._create_file('input.txt', 'abracabra\n1\n2\n3\n4\n5\n6\n7\n8\n'))
else:
self._run('wc < input.txt',
' 9 9 26\n',
pre=self._create_file('input.txt', 'abracabra\n1\n2\n3\n4\n5\n6\n7\n8\n'))
@score(10)
def test_in_out_files(self):
self._run('cat > output.txt < input.txt',
pre=self._seq(self._create_file('input.txt', '1\n2\n3\n'), self._remove_file('output.txt')),
post=self._check_file_contains('output.txt', '1\n2\n3\n'))
@score(10)
def test_pipe_and_files(self):
if platform.system() == 'Darwin':
self._run('echo hello1 > output.txt | wc',
' 0 0 0\n',
pre=self._remove_file('output.txt'),
post=self._check_file_contains('output.txt', 'hello1\n'))
self._run('echo hello1 | wc < input.txt',
' 1 1 4\n',
pre=self._create_file('input.txt', '123\n'))
else:
self._run('echo hello1 > output.txt | wc',
' 0 0 0\n',
pre=self._remove_file('output.txt'),
post=self._check_file_contains('output.txt', 'hello1\n'))
self._run('echo hello1 | wc < input.txt',
'1 1 4\n',
pre=self._create_file('input.txt', '123\n'))
@score(10)
def test_escaping_in_redirect(self):
self._run('echo hello >\\\".txt',
pre=self._remove_file('\".txt'),
post=self._check_file_contains('\".txt', 'hello\n'))
@score(10)
def test_yoda(self):
self._run('>1.txt echo hello',
pre=self._remove_file('1.txt'),
post=self._check_file_contains('1.txt', 'hello\n'))
self._run('< 1.txt cat', 'hello',
pre=self._create_file('1.txt', 'hello'))
@score(10)
def test_escaped_signs_do_not_redirect(self):
self._run('echo hello \">\" 1.txt', 'hello > 1.txt\n',
pre=self._remove_file('1.txt'),
post=self._check_file_contains('1.txt', None))
@score(10)
def test_1984(self):
# Этот тест не совместим с bash
self._run('echo hello | 1984', '')
@score(10)
def test_return_code_simple(self):
self._run('true', code=0)
self._run('false', code=1)
@score(20)
def test_line_or(self):
self._run('echo hello || echo world', 'hello\n')
self._run('false || echo world', 'world\n')
self._run('echo < unexisting.txt || echo world', 'world\n',
expected_err='./lavash: line 1: unexisting.txt: No such file or directory\n')
self._run('true || echo world', '')
self._run('false || n',
code=127,
expected_err='./lavash: line 1: n: command not found\n')
@score(20)
def test_line_and(self):
self._run('echo hello && echo world', 'hello\nworld\n')
self._run('true && echo world', 'world\n')
self._run('false && echo world', '',
code=1)
self._run('< unexisting.txt && echo world',
code=1,
expected_err='./lavash: line 1: unexisting.txt: No such file or directory\n')
self._run('false && n',
code=1)
@score(20)
def test_line_and_or(self):
self._run('echo hello && echo world1 || echo world2', 'hello\nworld1\n')
self._run('< unexisting.txt && echo 1 || echo 2', '2\n',
expected_err='./lavash: line 1: unexisting.txt: No such file or directory\n')
self._run('echo 1 > 1.txt && echo 2 < unexisting.txt > 2.txt && echo 3 > 3.txt || echo 4 > 4.txt',
pre=lambda self: self._remove_file('1.txt') and self._remove_file('2.txt') and self._remove_file('3.txt') and self._remove_file('4.txt'),
post=lambda self: self._check_file_contains('1.txt', '1\n') and self._check_file_contains('2.txt', '2\n') and self._check_file_contains('4.txt', '4\n'),
expected_err='./lavash: line 1: unexisting.txt: No such file or directory\n')
self._run('er && echo 1 | cat || echo 2', '2\n',
expected_err='./lavash: line 1: er: command not found\n')
@score(20)
def test_line_and_or_2(self):
if platform.system() == 'Darwin':
self._run('false || true && false || false', '',
code=1)
self._run('true || false && true', '')
self._run('er | wc || echo 1', ' 0 0 0\n',
expected_err='./lavash: line 1: er: command not found\n')
self._run('echo 2 | wc || echo 1', ' 1 1 2\n')
self._run('echo 2 || echo 1 | wc', '2\n')
self._run('er || echo 1 | wc', ' 1 1 2\n',
expected_err='./lavash: line 1: er: command not found\n')
else:
self._run('false || true && false || false', '',
code=1)
self._run('true || false && true', '')
self._run('er | wc || echo 1', ' 0 0 0\n',
expected_err='./lavash: line 1: er: command not found\n')
self._run('echo 2 | wc || echo 1', ' 1 1 2\n')
self._run('echo 2 || echo 1 | wc', '2\n')
self._run('er || echo 1 | wc', ' 1 1 2\n',
expected_err='./lavash: line 1: er: command not found\n')
if __name__ == '__main__':
test_suite = unittest.defaultTestLoader.discover('.', 'test.py')
test_runner = unittest.TextTestRunner(resultclass=unittest.TextTestResult)
result = test_runner.run(test_suite)
try:
with open('res.txt', 'r') as f:
res_score = int(f.read())
except:
res_score = 0
if result.wasSuccessful() or res_score != 0:
sys.exit(0)
sys.exit(1)