-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
163 lines (139 loc) · 7.06 KB
/
tests.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
import os
import shutil
import unittest
class TestPyxargs(unittest.TestCase):
@classmethod
def setUpClass(cls):
shutil.rmtree("pyxargs.egg-info", True)
file_content = ["Hello\n", "World\n", "192.168.0.1\n"]
with open("test.txt", "w") as f:
f.writelines(file_content)
@classmethod
def tearDownClass(cls):
os.remove("test.txt")
shutil.rmtree("__pycache__", True)
def test_stdin(self):
cmd = "echo hello world | python pyxargs.py -m stdin echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out hello\n', 'out world\n'])
def test_join_command_args(self):
cmd = "echo hello world | python pyxargs.py -m stdin echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out hello\n', 'out world\n'])
def test_read_files(self):
cmd = "python pyxargs.py -r \"(\.git|__pycache__)\" -o echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out LICENSE\n', 'out README.md\n', 'out pyxargs.py\n', 'out setup.py\n', 'out test.txt\n', 'out tests.py\n'])
def test_re_filter_file_path(self):
cmd = "python pyxargs.py -r \"\Aconfig\" echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, [])
def test_re_filter_file_name(self):
cmd = "python pyxargs.py -r \"\\Aconfig\" -b echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out config\n'])
def test_mode_path(self):
cmd = "python pyxargs.py -m path -r \"\\Aconfig\" -b echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
if os.name == "nt":
self.assertEqual(result, ["out '.git\\config'\n"])
else:
self.assertEqual(result, ['out .git/config\n'])
def test_filter_file_extension(self):
cmd = "python pyxargs.py -r \".+\.py$\" echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out pyxargs.py\n', 'out setup.py\n', 'out tests.py\n'])
def test_resub(self):
cmd = "python pyxargs.py -r \".+\.py$\" --resub \"\.py\" \".txt\" \"{}\" echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out pyxargs.txt\n', 'out setup.txt\n', 'out tests.txt\n'])
def test_re_omit(self):
cmd = "python pyxargs.py -r \"(.+\.py)|(\.git)\" -o echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out LICENSE\n', 'out README.md\n', 'out test.txt\n'])
def test_stdin_delimiter(self):
cmd = "echo hello,world,bye,world | python pyxargs.py -d , echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['out hello\n', 'out world\n', 'out bye\n', 'out world\n'])
def test_stdin_delimiter_py(self):
cmd = "echo hello,world,bye,world | python pyxargs.py -d , -x \"print('{}')\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n'])
def test_trailing_chars_removed(self):
cmd = "echo hello,world,bye,world | python pyxargs.py -d , --dry-run -x \"print('{}')\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ["print('hello')\n", "print('world')\n", "print('bye')\n", "print('world')\n"])
def test_extra_delimiter(self):
cmd = "echo hello,world,bye,world , | python pyxargs.py -d , -x \"print('{}')\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world \n', '\n'])
def test_delimiter_space(self):
cmd = "echo hello world bye world | python pyxargs.py -d \" \" -x \"print('{}')\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n'])
def test_delimiter_whitespace(self):
cmd = "echo hello world bye world | python pyxargs.py -m stdin -x \"print('{}')\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n'])
def test_pre_post_exec(self):
cmd = "echo hello world bye world | python pyxargs.py -m stdin --pre \"counter = 0\" --post \"print(counter)\" -x \"print('{}'); counter += 1\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n', '4\n'])
def test_exec_namespace(self):
cmd = "echo hello world bye world | python pyxargs.py -m stdin --pre \"output = 0\" --post \"print(locals())\" -x \"print('{}'); output += 1\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n', "{'output': 4}\n"])
def test_import(self):
cmd = "echo hello world bye world | python pyxargs.py -m stdin --import math --importstar math --pre \"output = 0\" --post \"print(output)\" -x \"print('{}'); output += math.sin(pi/2)\""
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ['hello\n', 'world\n', 'bye\n', 'world\n', '4.0\n'])
def test_dryrun(self):
cmd = "echo hello world | python pyxargs.py -m stdin --dry-run echo out {}"
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, ["['echo', 'out', 'hello']\n", "['echo', 'out', 'world']\n"])
def test_read_items_file(self):
cmd = "python pyxargs.py -a test.txt echo out {}"
with open("test.txt", "r") as f:
file_content = f.readlines()
solution = []
for line in file_content:
solution.append("out " + line)
with os.popen(cmd) as result:
result = result.readlines()
self.assertListEqual(result, solution)
def test_read_items_cat_type(self):
if os.name == "nt":
cmd = "type test.txt | python pyxargs.py -m stdin echo out {}"
elif os.name == "posix":
cmd = "cat test.txt | python pyxargs.py -m stdin echo out {}"
else:
self.assertEqual(True, False, "Unrecognized OS by this test")
with open("test.txt", "r") as f:
file_content = f.readlines()
solution = []
for line in file_content:
solution.append("out " + line)
with os.popen(cmd) as result:
result = result.readlines()
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()