forked from amoffat/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
363 lines (247 loc) · 9.36 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -*- coding: utf8 -*-
import os
import unittest
import tempfile
import sys
IS_PY3 = sys.version_info[0] == 3
if IS_PY3:
unicode = str
skipUnless = getattr(unittest, "skipUnless", None)
if not skipUnless:
def skipUnless(*args, **kwargs):
def wrapper(thing): return thing
return wrapper
requires_posix = skipUnless(os.name == "posix", "Requires POSIX")
def create_tmp_test(code):
py = tempfile.NamedTemporaryFile()
if IS_PY3: code = bytes(code, "UTF-8")
py.write(code)
py.flush()
return py
@requires_posix
class Basic(unittest.TestCase):
def test_print_command(self):
from pbs import ls, which
actual_location = which("ls")
out = str(ls)
self.assertEqual(out, actual_location)
def test_unicode_arg(self):
from pbs import echo
if IS_PY3: test = "漢字"
else: test = "漢字".decode("utf8")
p = echo(test).strip()
self.assertEqual(test, p)
def test_number_arg(self):
from pbs import python
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
print args[0]
""")
out = python(py.name, 3).strip()
self.assertEqual(out, "3")
def test_ok_code(self):
from pbs import ls, ErrorReturnCode_2
self.assertRaises(ErrorReturnCode_2, ls, "/aofwje/garogjao4a/eoan3on")
ls("/aofwje/garogjao4a/eoan3on", _ok_code=2)
ls("/aofwje/garogjao4a/eoan3on", _ok_code=[2])
def test_quote_escaping(self):
from pbs import python
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
print args
""")
out = python(py.name, "one two three").strip()
self.assertEqual(out, "['one two three']")
out = python(py.name, "one \"two three").strip()
self.assertEqual(out, "['one \"two three']")
out = python(py.name, "one", "two three").strip()
self.assertEqual(out, "['one', 'two three']")
out = python(py.name, "one", "two \"haha\" three").strip()
self.assertEqual(out, "['one', 'two \"haha\" three']")
out = python(py.name, "one two's three").strip()
self.assertEqual(out, "[\"one two's three\"]")
out = python(py.name, 'one two\'s three').strip()
self.assertEqual(out, "[\"one two's three\"]")
def test_environment(self):
from pbs import python
import os
env = {"HERP": "DERP"}
py = create_tmp_test("""
import os
print os.environ["HERP"], len(os.environ)
""")
out = python(py.name, _env=env).strip()
self.assertEqual(out, "DERP 1")
py = create_tmp_test("""
import pbs, os
print pbs.HERP, len(os.environ)
""")
out = python(py.name, _env=env).strip()
self.assertEqual(out, "DERP 1")
def test_which(self):
from pbs import which, ls
self.assertEqual(which("fjoawjefojawe"), None)
self.assertEqual(which("ls"), str(ls))
def test_no_arg(self):
import pwd
from pbs import whoami
u1 = whoami().strip()
u2 = pwd.getpwuid(os.geteuid())[0]
self.assertEqual(u1, u2)
def test_exception(self):
from pbs import ls, ErrorReturnCode_2
self.assertRaises(ErrorReturnCode_2, ls, "/aofwje/garogjao4a/eoan3on")
def test_command_not_found(self):
from pbs import CommandNotFound
def do_import(): from pbs import aowjgoawjoeijaowjellll
self.assertRaises(CommandNotFound, do_import)
def test_command_wrapper_equivalence(self):
from pbs import Command, ls, which
self.assertEqual(Command(which("ls")), ls)
def test_multiple_args_short_option(self):
from pbs import python
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", dest="long_option")
options, args = parser.parse_args()
print len(options.long_option.split())
""")
num_args = int(python(py.name, l="one two three"))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, l="\"one two three\""))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, l='\"one two three\"'))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, l='"one two three"'))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, "-l", "one's two's three's"))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, "-l", "\"one's two's three's\""))
self.assertEqual(num_args, 3)
def test_multiple_args_long_option(self):
from pbs import python
py = create_tmp_test("""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", dest="long_option")
options, args = parser.parse_args()
print len(options.long_option.split())
""")
num_args = int(python(py.name, long_option="one two three"))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, "--long-option", "one's two's three's"))
self.assertEqual(num_args, 3)
def test_short_bool_option(self):
from pbs import id
i1 = int(id(u=True))
i2 = os.geteuid()
self.assertEqual(i1, i2)
def test_long_bool_option(self):
from pbs import id
i1 = int(id(user=True, real=True))
i2 = os.getuid()
self.assertEqual(i1, i2)
def test_composition(self):
from pbs import ls, wc
c1 = int(wc(ls("-A1"), l=True))
c2 = len(os.listdir("."))
if c1 != c2:
with open("/tmp/fail", "a") as h: h.write("FUCK\n")
self.assertEqual(c1, c2)
def test_short_option(self):
from pbs import sh
s1 = sh(c="echo test").strip()
s2 = "test"
self.assertEqual(s1, s2)
def test_long_option(self):
from pbs import sed, echo
out = sed(echo("test"), expression="s/test/lol/").strip()
self.assertEqual(out, "lol")
def test_command_wrapper(self):
from pbs import Command, which
ls = Command(which("ls"))
wc = Command(which("wc"))
c1 = int(wc(ls("-A1"), l=True))
c2 = len(os.listdir("."))
self.assertEqual(c1, c2)
def test_background(self):
from pbs import sleep
import time
start = time.time()
sleep_time = .5
p = sleep(sleep_time, _bg=True)
now = time.time()
self.assertTrue(now - start < sleep_time)
p.wait()
now = time.time()
self.assertTrue(now - start > sleep_time)
def test_with_context(self):
from pbs import time, ls
with time:
out = ls().stderr
self.assertTrue("pagefaults" in out)
def test_with_context_args(self):
from pbs import time, ls
with time(verbose=True, _with=True):
out = ls().stderr
self.assertTrue("Voluntary context switches" in out)
def test_err_to_out(self):
from pbs import time, ls
with time(_with=True):
out = ls(_err_to_out=True)
self.assertTrue("pagefaults" in out)
def test_out_redirection(self):
import tempfile
from pbs import ls
file_obj = tempfile.TemporaryFile()
out = ls(_out=file_obj)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertTrue(len(actual_out) != 0)
def test_err_redirection(self):
import tempfile
from pbs import time, ls
file_obj = tempfile.TemporaryFile()
with time(_with=True):
out = ls(_err=file_obj)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertTrue(len(actual_out) != 0)
def test_subcommand(self):
from pbs import time
out = time.ls(_err_to_out=True)
self.assertTrue("pagefaults" in out)
def test_bake(self):
from pbs import time, ls
timed = time.bake("--verbose", _err_to_out=True)
out = timed.ls()
self.assertTrue("Voluntary context switches" in out)
def test_multiple_bakes(self):
from pbs import time
timed = time.bake("--verbose", _err_to_out=True)
out = timed.bake("ls")()
self.assertTrue("Voluntary context switches" in out)
def test_bake_args_come_first(self):
from pbs import ls
ls = ls.bake(full_time=True)
ran = ls("-la").command_ran
ft = ran.index("full-time")
self.assertTrue("-la" in ran[ft:])
def test_output_equivalence(self):
from pbs import whoami
iam1 = whoami()
iam2 = whoami()
self.assertEqual(iam1, iam2)
if __name__ == "__main__":
if len(sys.argv) > 1:
unittest.main()
else:
suite = unittest.TestLoader().loadTestsFromTestCase(Basic)
unittest.TextTestRunner(verbosity=2).run(suite)