forked from amoffat/sh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
462 lines (329 loc) · 13.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- 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")
requires_nt = skipUnless(os.name == 'nt', 'Requires NT')
def create_tmp_test(code):
py = tempfile.NamedTemporaryFile(delete=False)
if IS_PY3: code = bytes(code, "UTF-8")
py.write(code)
py.flush()
py.close()
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_bg_to_int(self):
from pbs import echo
# bugs with background might cause the following error:
# ValueError: invalid literal for int() with base 10: ''
self.assertEqual(int(echo("123", _bg=True)), 123)
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)
@requires_nt
class WindowsBasic(unittest.TestCase):
# those are borrowed from Basic,
# you should have msysgit install and in PATH to run them
test_print_command = Basic.test_print_command.__func__
test_number_arg = Basic.test_number_arg.__func__
test_ok_code = Basic.test_ok_code.__func__
# windows handle arg different , need to look into
# test_quote_escaping = Basic.test_quote_escaping.__func__
# this test is depended on pbs to be installed.
#test_environment = Basic.test_environment.__func__
# windows and unicode, doesn't mix and match too good
# test_unicode_arg = Basic.test_unicode_arg.__func__
test_number_arg = Basic.test_number_arg.__func__
test_command_wrapper_equivalence = Basic.test_command_wrapper_equivalence.__func__
#windows handle arg different , need to look into
#test_multiple_args_short_option = Basic.test_multiple_args_short_option.__func__
test_multiple_args_long_option = Basic.test_multiple_args_long_option.__func__
#test_short_bool_option = Basic.test_short_bool_option.__func__
#test_long_bool_option = Basic.test_long_bool_option.__func__
test_composition = Basic.test_composition.__func__ # don't want to think what will happen if this fails...
#test_short_option = Basic.test_short_option.__func__
test_long_option = Basic.test_long_option.__func__
test_command_wrapper = Basic.test_command_wrapper.__func__
test_bg_to_int = Basic.test_bg_to_int.__func__
test_out_redirection = Basic.test_out_redirection.__func__
test_bake_args_come_first = Basic.test_bake_args_come_first.__func__
test_output_equivalence = Basic.test_output_equivalence.__func__
def test_nt_internal_commands(self):
from pbs import echo
s1 = unicode(echo("test")).strip()
s2 = 'test'
self.assertEqual(s1, s2)
def test_nt_internal_commands_pipe(self):
from pbs import dir, find
# dir /b /a | find /c /v ""
c1 = int(find(dir("/b", "/a"),'/c', '/v','\"\"'))
c2 = len(os.listdir('.'))
self.assertEqual(c1, c2)
def test_nt_external_command(self):
from pbs import ipconfig, ErrorReturnCode_1
try:
ret = ipconfig("/?")
except Exception as err:
# windows program fails with err 1, on help [/?] weird...
self.assertIn("/?" , err.full_cmd)
self.assertIn("Display this help message" , err.stdout)
def test_background(self):
from pbs import ping
import time
start = time.time()
sleep_time = 1000
# trick to make sleep work at windows http://malektips.com/dos0017.html
p = ping("127.0.0.1","-n 2", "-w %d" % sleep_time, _bg=True)
now = time.time()
self.assertTrue(now - start < (sleep_time/1000.0))
p.wait()
now = time.time()
self.assertTrue(now - start > (sleep_time/1000.0))
def test_bake(self):
from pbs import git
baked = git.bake( _err_to_out=True)
out = baked.help()
self.assertIn("usage", out)
def test_ok_code(self):
from pbs import ls, ErrorReturnCode_1
self.assertRaises(ErrorReturnCode_1, ls, "/aofwje/garogjao4a/eoan3on")
ls("/aofwje/garogjao4a/eoan3on", _ok_code=1)
ls("/aofwje/garogjao4a/eoan3on", _ok_code=[1])
def test_change_default_encoding(self):
import pbs
self.assertEqual(pbs.config["DEFAULT_ENCODING"], "mbcs")
pbs.set_default_encoding("utf8")
ret = pbs.echo(u"נסיון")
self.assertEqual(pbs.config["DEFAULT_ENCODING"], "utf8")
if __name__ == "__main__":
if len(sys.argv) > 1:
unittest.main()
else:
suite = unittest.TestLoader().loadTestsFromTestCase(Basic)
unittest.TextTestRunner(verbosity=2).run(suite)