forked from amoffat/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
174 lines (128 loc) · 4.19 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
import os
import unittest
requires_posix = unittest.skipUnless(os.name == "posix", "Requires POSIX")
class PbsTestSuite(unittest.TestCase):
@requires_posix
def test_print_command(self):
from pbs import ls, which
actual_location = which("ls")
out = str(ls)
self.assertEqual(out, actual_location)
@requires_posix
def test_which(self):
from pbs import which, ls
self.assertEqual(which("fjoawjefojawe"), None)
self.assertEqual(which("ls"), str(ls))
@requires_posix
def test_no_arg(self):
import pwd
from pbs import whoami
u1 = unicode(whoami()).strip()
u2 = pwd.getpwuid(os.geteuid())[0]
self.assertEqual(u1, u2)
@requires_posix
def test_short_bool_option(self):
from pbs import id
i1 = int(id(u=True))
i2 = os.geteuid()
self.assertEqual(i1, i2)
@requires_posix
def test_long_bool_option(self):
from pbs import id
i1 = int(id(user=True, real=True))
i2 = os.getuid()
self.assertEqual(i1, i2)
@requires_posix
def test_composition(self):
from pbs import ls, wc
c1 = int(wc(ls(A=True), l=True))
c2 = len(os.listdir("."))
self.assertEqual(c1, c2)
@requires_posix
def test_short_option(self):
from pbs import sh
s1 = unicode(sh(c="echo test")).strip()
s2 = "test"
self.assertEqual(s1, s2)
@requires_posix
def test_long_option(self):
from pbs import sed, echo
out = unicode(sed(echo("test"), expression="s/test/lol/")).strip()
self.assertEqual(out, "lol")
@requires_posix
def test_command_wrapper(self):
from pbs import Command, which
ls = Command(which("ls"))
wc = Command(which("wc"))
c1 = int(wc(ls(A=True), l=True))
c2 = len(os.listdir("."))
self.assertEqual(c1, c2)
@requires_posix
def test_background(self):
from pbs import sleep
import time
start = time.time()
sleep_time = 1
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)
@requires_posix
def test_with_context(self):
from pbs import time, ls
with time:
out = ls().stderr
self.assertTrue("pagefaults" in out)
@requires_posix
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)
@requires_posix
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)
@requires_posix
def test_out_redirection(self):
import tempfile
from pbs import ls
file_obj = tempfile.TemporaryFile()
out = ls(_out=file_obj)
self.assertTrue(len(out) == 0)
file_obj.seek(0)
actual_out = file_obj.read()
self.assertTrue(len(actual_out) != 0)
@requires_posix
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()
self.assertTrue(len(actual_out) != 0)
@requires_posix
def test_subcommand(self):
from pbs import time
out = time.ls(_err_to_out=True)
self.assertTrue("pagefaults" in out)
@requires_posix
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)
@requires_posix
def test_output_equivalence(self):
from pbs import whoami
iam1 = whoami()
iam2 = whoami()
self.assertEqual(iam1, iam2)
if __name__ == "__main__":
unittest.main()