-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_testpath.py
More file actions
172 lines (141 loc) · 7.17 KB
/
test_testpath.py
File metadata and controls
172 lines (141 loc) · 7.17 KB
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
import os.path
import pathlib
import subprocess
import sys
import tempfile
import unittest
# hello smart tests
from launchable.testpath import FilePathNormalizer, parse_test_path, prepend_path_if_missing, relative_subpath, unparse_test_path
class TestPathEncodingTest(unittest.TestCase):
def test_parse(self):
self.assertEqual(parse_test_path('a=b#c=d'), [{'type': 'a', 'name': 'b'}, {'type': 'c', 'name': 'd'}])
self.assertEqual(parse_test_path('&name=b#&type=c'), [{'name': 'b'}, {'type': 'c'}])
self.assertEqual(parse_test_path('t=n&a=aa&x=xx'), [{'a': 'aa', 'name': 'n', 'type': 't', 'x': 'xx'}])
self.assertEqual(parse_test_path('&a=aa&type=t&x=xx'), [{'a': 'aa', 'type': 't', 'x': 'xx'}])
self.assertEqual(parse_test_path('a=x%25%3D%23%26'), [{'type': 'a', 'name': 'x%=#&'}])
self.assertEqual(parse_test_path(''), [])
# See the comment on the parse_test_path.
# self.assertEqual(
# parse_test_path('&'),
# [{None: None}])
# self.assertEqual(
# parse_test_path('&#&'),
# [{None: None}, {None: None}])
def test_unparse(self):
self.assertEqual(
unparse_test_path([{'type': 'a', 'name': 'b'}, {'type': 'c', 'name': 'd'}]), 'a=b#c=d')
self.assertEqual(
unparse_test_path([{'type': None, 'name': 'b'}, {'type': 'c', 'name': None}]), '&name=b#&type=c')
self.assertEqual(
unparse_test_path([{'a': 'aa', 'name': 'n', 'type': 't', 'x': 'xx'}]), 't=n&a=aa&x=xx')
self.assertEqual(
unparse_test_path([{'a': 'aa', 'type': 't', 'x': 'xx'}]), '&a=aa&type=t&x=xx')
self.assertEqual(unparse_test_path([{'a': 'x%=#&'}]), '&a=x%25%3D%23%26')
self.assertEqual(unparse_test_path([]), '')
self.assertEqual(unparse_test_path([{None: None}]), '&')
self.assertEqual(unparse_test_path([{None: None}, {None: None}]), '&#&')
class TestFilePathNormalizer(unittest.TestCase):
def test_relative_path(self):
n = FilePathNormalizer()
relpath = os.path.join('foo', 'bar', 'baz')
self.assertEqual(relpath, n.relativize(relpath))
def test_base_path(self):
base = os.path.abspath('base')
relpath = os.path.join('foo', 'bar', 'baz')
abspath = os.path.join(base, 'foo', 'bar', 'baz')
n = FilePathNormalizer(base_path=base)
self.assertEqual(relpath, n.relativize(relpath))
self.assertEqual(relpath, n.relativize(abspath))
def test_normalize_path(self):
relpath = os.path.join('foo', 'bar', 'baz')
non_normalized = os.path.join('foo', 'bar', 'omit', '..', 'baz')
n = FilePathNormalizer()
self.assertEqual(relpath, n.relativize(non_normalized))
def test_no_base_path_inference(self):
base = os.path.abspath('base')
relpath = os.path.join('foo', 'bar', 'baz')
abspath = os.path.join(base, 'foo', 'bar', 'baz')
n = FilePathNormalizer(no_base_path_inference=True)
self.assertEqual(relpath, n.relativize(relpath))
self.assertEqual(abspath, n.relativize(abspath))
n = FilePathNormalizer(base_path=base, no_base_path_inference=True)
self.assertEqual(relpath, n.relativize(relpath))
self.assertEqual(relpath, n.relativize(abspath))
@unittest.skipIf(
sys.platform.startswith("win"),
"tempfile creates 8.3 filenames, and it's hard to deal with them. "
"Practically, we don't see them often, so do not support them now until it's needed."
)
def test_inference_git(self):
with tempfile.TemporaryDirectory() as tempdirname:
temppath = pathlib.PurePath(tempdirname)
base = str(temppath.joinpath("gitrepo"))
relpath = os.path.join('foo', 'bar', 'baz')
abspath = os.path.join(base, 'foo', 'bar', 'baz')
self._run_command(['git', 'init', base])
n = FilePathNormalizer()
self.assertEqual(relpath, n.relativize(abspath))
@unittest.skipIf(
sys.platform.startswith("win"),
"tempfile creates 8.3 filenames, and it's hard to deal with them. "
"Practically, we don't see them often, so do not support them now until it's needed. "
"Also when this runs on Windows, GIT_AUTHOR_NAME etc. is ignored and fails."
)
def test_inference_git_submodule(self):
with tempfile.TemporaryDirectory() as tempdirname:
temppath = pathlib.PurePath(tempdirname)
self._run_command(['git', 'init', str(temppath.joinpath("submod"))])
self._run_command(
['git', 'commit', '--allow-empty', '--message', 'test commit'],
cwd=str(temppath.joinpath("submod")))
self._run_command(['git', 'init', str(temppath.joinpath("gitrepo"))])
self._run_command(
['git', '-c', 'protocol.file.allow=always', 'submodule', 'add', str(temppath.joinpath("submod")), 'submod'],
cwd=str(temppath.joinpath("gitrepo")))
base = str(temppath.joinpath("gitrepo"))
relpath = os.path.join('submod', 'foo', 'bar', 'baz')
abspath = os.path.join(base, 'submod', 'foo', 'bar', 'baz')
n = FilePathNormalizer()
self.assertEqual(relpath, n.relativize(abspath))
def _run_command(self, args, cwd=None):
# Use check_output here to capture stderr for a better error message.
try:
subprocess.check_output(
args,
cwd=cwd,
stderr=subprocess.PIPE,
universal_newlines=True,
env={
"GIT_AUTHOR_NAME": "Test User",
"GIT_AUTHOR_EMAIL": "user@example.com",
"GIT_COMMITTER_NAME": "Test User",
"GIT_COMMITTER_EMAIL": "user@example.com",
})
except subprocess.CalledProcessError as e:
self.fail("Failed to execute a command: {}\nSTDOUT: {}\nSTDERR: {}\n". format(e, e.stdout, e.stderr))
class TestPathHelpers(unittest.TestCase):
def test_relative_subpath(self):
self.assertEqual(
"tests",
relative_subpath(str(pathlib.Path("repo", "tests")), str(pathlib.Path("repo"))))
def test_relative_subpath_returns_empty_when_same_path(self):
self.assertEqual(
"",
relative_subpath(str(pathlib.Path("repo")), str(pathlib.Path("repo"))))
def test_relative_subpath_returns_empty_when_not_under_base(self):
self.assertEqual(
"",
relative_subpath(str(pathlib.Path("repo", "tests")), str(pathlib.Path("other"))))
def test_prepend_path_if_missing(self):
self.assertEqual(
"tests/a.spec.ts",
prepend_path_if_missing("a.spec.ts", "tests"))
def test_prepend_path_if_missing_when_already_prefixed(self):
self.assertEqual(
"tests/a.spec.ts",
prepend_path_if_missing("tests/a.spec.ts", "tests"))
def test_prepend_path_if_missing_when_empty_input(self):
self.assertEqual("", prepend_path_if_missing("", "tests"))
self.assertEqual("a.spec.ts", prepend_path_if_missing("a.spec.ts", ""))
if __name__ == '__main__':
unittest.main()