forked from BobBuildTool/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
297 lines (231 loc) · 9.93 KB
/
test_utils.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
# Bob build tool
# Copyright (C) 2016 Jan Klötzke
#
# SPDX-License-Identifier: GPL-3.0-or-later
from tempfile import NamedTemporaryFile, TemporaryDirectory
from unittest import TestCase
from unittest.mock import patch
import os, stat
import asyncio
from bob.utils import joinScripts, removePath, emptyDirectory, compareVersion, \
getPlatformTag, run, check_output, removeUserFromUrl, runInEventLoop
from bob.errors import BuildError, ParseError
class TestJoinScripts(TestCase):
def testEmpty(self):
self.assertEqual(joinScripts([], "not used"), None)
def testSingle(self):
self.assertEqual(joinScripts(["asdf"], "not used"), "asdf")
self.assertEqual(joinScripts([None], "not used"), None)
def testDual(self):
self.assertEqual(joinScripts(["asdf", "qwer"], "\n"), "asdf\nqwer")
self.assertEqual(joinScripts(["asdf", None], "unused"), "asdf")
self.assertEqual(joinScripts([None, "asdf"], "unused"), "asdf")
self.assertEqual(joinScripts([None, None], "unused"), None)
class TestRemove(TestCase):
def testFile(self):
with TemporaryDirectory() as tmp:
fn = os.path.join(tmp, "file")
with open(fn, "w") as f:
f.write("data")
removePath(fn)
assert not os.path.exists(fn)
def testDir(self):
with TemporaryDirectory() as tmp:
d = os.path.join(tmp, "dir")
os.mkdir(d)
with open(os.path.join(d, "file"), "w") as f:
f.write("data")
removePath(d)
assert not os.path.exists(d)
def testPermission(self):
with TemporaryDirectory() as tmp:
d = os.path.join(tmp, "dir")
os.mkdir(d)
with open(os.path.join(d, "file"), "w") as f:
f.write("data")
os.chmod(d, stat.S_IRUSR | stat.S_IXUSR)
self.assertRaises(BuildError, removePath, tmp)
os.chmod(d, stat.S_IRWXU)
class TestEmpty(TestCase):
def testFile(self):
with TemporaryDirectory() as tmp:
fn = os.path.join(tmp, "file")
with open(fn, "w") as f:
f.write("data")
emptyDirectory(tmp)
assert os.path.exists(tmp)
assert not os.path.exists(fn)
def testDir(self):
with TemporaryDirectory() as tmp:
d = os.path.join(tmp, "dir")
os.mkdir(d)
with open(os.path.join(d, "file"), "w") as f:
f.write("data")
emptyDirectory(tmp)
assert os.path.exists(tmp)
assert not os.path.exists(d)
def testPermission(self):
with TemporaryDirectory() as tmp:
d = os.path.join(tmp, "dir")
os.mkdir(d)
with open(os.path.join(d, "file"), "w") as f:
f.write("data")
os.chmod(d, stat.S_IRUSR | stat.S_IXUSR)
self.assertRaises(BuildError, emptyDirectory, tmp)
os.chmod(d, stat.S_IRWXU)
class TestVersions(TestCase):
def testRegular(self):
self.assertTrue(compareVersion("0.1", "0.1.0") == 0)
self.assertTrue(compareVersion("0.2.0", "0.2") == 0)
self.assertTrue(compareVersion("0.2", "0.1.0") > 0)
self.assertTrue(compareVersion("0.2.1", "0.3") < 0)
def testRc(self):
self.assertTrue(compareVersion("0.1rc1", "0.1") < 0)
self.assertTrue(compareVersion("0.1.0rc1", "0.1") < 0)
self.assertTrue(compareVersion("0.2.0rc1", "0.2rc1") == 0)
self.assertTrue(compareVersion("0.2.0rc2", "0.2.0rc1") > 0)
self.assertTrue(compareVersion("0.4.0", "0.4rc4") > 0)
def testDev(self):
self.assertTrue(compareVersion("0.15", "0.15.0.dev4") > 0)
self.assertTrue(compareVersion("0.15.dev5", "0.15.0.dev4") > 0)
self.assertTrue(compareVersion("0.15.0rc1", "0.15.0rc1.dev4") > 0)
def testInvalid(self):
self.assertRaises(ParseError, compareVersion, "v0.15", "0.15")
self.assertRaises(ParseError, compareVersion, "0.15", "0.15a4")
self.assertRaises(ParseError, compareVersion, "0.15.devv4", "0.15")
def copyAsSymlink(target, name):
import shutil
shutil.copy(target, name)
def symlinkFail(target, name):
raise OSError
class TestPlatformTag(TestCase):
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'linux')
def testPosix(self):
self.assertEqual(getPlatformTag(), b'')
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'msys')
@patch('bob.utils.os.symlink', symlinkFail)
def testMSYSLinkFail(self):
self.assertEqual(getPlatformTag(), b'm')
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'msys')
@patch('bob.utils.os.symlink', copyAsSymlink)
def testMSYSLinkCopy(self):
self.assertEqual(getPlatformTag(), b'm')
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'msys')
def testMSYSLinkOk(self):
self.assertEqual(getPlatformTag(), b'ml')
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'win32')
@patch('bob.utils.os.symlink', symlinkFail)
def testWindowsLinkFail(self):
self.assertEqual(getPlatformTag(), b'w')
@patch('bob.utils.__canSymlink', None)
@patch('bob.utils.__platformTag', None)
@patch('bob.utils.sys.platform', 'win32')
def testWindowsLinkOk(self):
self.assertEqual(getPlatformTag(), b'wl')
class TestAsyncSubprocess(TestCase):
"""Test our asyncio subprocess convenience wrappers"""
def testRunSuccess(self):
coro = run("echo ok", shell=True)
proc = runInEventLoop(coro)
self.assertEqual(proc.returncode, 0)
def testRunCaptureStr(self):
import subprocess
coro = run("echo ok; echo err >&2", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
proc = runInEventLoop(coro)
self.assertEqual(proc.returncode, 0)
self.assertEqual(proc.stdout, "ok\n")
self.assertEqual(proc.stderr, "err\n")
def testRunFail(self):
coro = run(["/bin/false"])
proc = runInEventLoop(coro)
self.assertNotEqual(proc.returncode, 0)
def testCheckOutputBin(self):
coro = check_output("echo ok", shell=True)
stdout = runInEventLoop(coro)
self.assertEqual(stdout, b'ok\n')
def testCheckOutputStr(self):
coro = check_output("echo ok", shell=True, universal_newlines=True)
stdout = runInEventLoop(coro)
self.assertEqual(stdout, "ok\n")
def testCheckOutputFail(self):
from subprocess import CalledProcessError
coro = check_output(["/bin/false"])
self.assertRaises(CalledProcessError,
runInEventLoop, coro)
class TestRemoveUserFromUrl(TestCase):
"""Test removal of user from URL"""
def testRegular(self):
self.assertEqual(removeUserFromUrl("scheme://host.xz/path/to/repo.git"),
"scheme://host.xz/path/to/repo.git")
def testRegularWithAt(self):
self.assertEqual(removeUserFromUrl("scheme://host.xz/path/t@/repo.git"),
"scheme://host.xz/path/t@/repo.git")
def testRegularTrailingSlash(self):
self.assertEqual(removeUserFromUrl("scheme://host.xz/path/to/repo.git/"),
"scheme://host.xz/path/to/repo.git/")
def testRegularWithPort(self):
self.assertEqual(removeUserFromUrl("scheme://host.xz:8080/path/to/repo.git/"),
"scheme://host.xz:8080/path/to/repo.git/")
def testRegularUser(self):
self.assertEqual(removeUserFromUrl("scheme://[email protected]/path/to/repo.git/"),
"scheme://host.xz/path/to/repo.git/")
def testRegulserUserAndPort(self):
self.assertEqual(removeUserFromUrl("scheme://[email protected]:8080/path/to/repo.git/"),
"scheme://host.xz:8080/path/to/repo.git/")
def testScpLike(self):
self.assertEqual(removeUserFromUrl("host.xz:path/to/repo.git/"),
"host.xz:path/to/repo.git/")
def testScpLikeUser(self):
self.assertEqual(removeUserFromUrl("[email protected]:path/to/repo.git/"),
"host.xz:path/to/repo.git/")
def testAbsPath(self):
self.assertEqual(removeUserFromUrl("/path/to/repo.git/"),
"/path/to/repo.git/")
def testAbsPathWithColon(self):
self.assertEqual(removeUserFromUrl("/foo:bar/repo.git/"),
"/foo:bar/repo.git/")
def testRelPath(self):
self.assertEqual(removeUserFromUrl("repo.git"),
"repo.git")
def testAbsPathWithColon(self):
self.assertEqual(removeUserFromUrl("./foo:bar"),
"./foo:bar")
def testAbsPathWithColonAndAt(self):
self.assertEqual(removeUserFromUrl("./foo@bar:baz"),
"./foo@bar:baz")
def testFileUrl(self):
self.assertEqual(removeUserFromUrl("file:///path/to/repo.git/"),
"file:///path/to/repo.git/")
def testInvalid(self):
self.assertEqual(removeUserFromUrl("invalid"),
"invalid")
def testWinPath(self):
self.assertEqual(removeUserFromUrl(r"C:\foo.bar"),
r"C:\foo.bar")
def testWinPathForwardSlash(self):
self.assertEqual(removeUserFromUrl(r"C:/foo.bar"),
r"C:/foo.bar")
def testWinPathFileUrl(self):
self.assertEqual(removeUserFromUrl(r"file:///C:/foo.bar"),
r"file:///C:/foo.bar")
def testWinPathFileUrlBackslash(self):
self.assertEqual(removeUserFromUrl(r"file:///C:\foo.bar"),
r"file:///C:\foo.bar")
def testWinUncPath(self):
self.assertEqual(removeUserFromUrl(r"\\server\path"),
r"\\server\path")
def testWinUncFileUrl(self):
self.assertEqual(removeUserFromUrl(r"file:///\\server\path"),
r"file:///\\server\path")