-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_project.py
218 lines (180 loc) · 7.21 KB
/
test_project.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
import os
import unittest
import unittest.mock as mock
from builder.core.project import Project
from builder.core.spec import BuildSpec
from builder.actions.script import Script
import builder.core.api # force API to load and expose the virtual module
here = os.path.dirname(os.path.abspath(__file__))
test_data_dir = os.path.join(here, 'data')
# base config -- copy for tests
_test_proj_config = {
'name': 'test-proj',
'search_dirs': [test_data_dir],
'path': here,
'run_tests': True,
}
def _collect_steps(step):
"""
collect the list of steps
"""
def _collect_steps_impl(out, curr):
if isinstance(curr, list):
for s in curr:
_collect_steps_impl(out, s)
elif isinstance(curr, Script):
out.append(str(curr))
_collect_steps_impl(out, curr.commands)
else:
out.append(str(curr))
stack = []
_collect_steps_impl(stack, step)
return stack
def _fuzzy_find_step(step_stack, step, name):
"""
attempt to find a step name or value that either matches name or contains name as a fragment
:return: tuple(step, stack idx) | None
"""
for i in range(len(step_stack)):
s = step_stack[i]
if s == name or name in s:
return s, i
return None
def _step_exists(step, name):
"""
test if the step [name] exists in the set of [step]s
"""
step_stack = _collect_steps(step)
return _fuzzy_find_step(step_stack, step, name) is not None
def _dump_step(step):
import pprint
steps = _collect_steps(step)
pprint.pprint(steps, width=240)
class TestProject(unittest.TestCase):
def setUp(self):
# remove possible inter test behavior
Project._projects.clear()
def _format_step(self, step):
step_stack = _collect_steps(step)
return "\n".join(step_stack)
def _assert_step_contains(self, step, name):
if not _step_exists(step, name):
steps = self._format_step(step)
self.fail(f"{name} not contained in stack:\n{steps}")
def _assert_step_not_contains(self, step, name):
if _step_exists(step, name):
steps = self._format_step(step)
self.fail(f"unexpected step {name} found in stack:\n{steps}")
def _assert_step_contains_all(self, step, names, ordered=True):
for name in names:
self._assert_step_contains(step, name)
if ordered:
stack = _collect_steps(step)
steps = [_fuzzy_find_step(stack, step, name) for name in names]
step_indices = [t[1] for t in steps]
steps_in_order = all(step_indices[i] <= step_indices[i+1] for i in range(len(step_indices) - 1))
formatted_steps = self._format_step(step)
self.assertTrue(
steps_in_order, f"steps exist but not in order expected:\nexpected:{names}\nfound:\n{formatted_steps}")
def test_build_defaults(self):
"""cmake build step should be default when not specified and toolchain exists"""
p = Project(**_test_proj_config.copy())
mock_env = mock.Mock(name='MockEnv')
steps = p.build(mock_env)
self._assert_step_contains(steps, 'cmake build')
def test_override_build_steps(self):
"""explict build steps take precedence"""
config = _test_proj_config.copy()
config['build_steps'] = ['foo']
p = Project(**config)
mock_env = mock.Mock(name='MockEnv')
steps = p.build(mock_env)
self._assert_step_contains(steps, 'foo')
def test_upstream_builds_first(self):
"""upstream dependencies should be built first"""
config = _test_proj_config.copy()
config['upstream'] = [
{'name': 'lib-1'}
]
p = Project(**config)
mock_env = mock.Mock(name='MockEnv', config=config)
mock_env.spec = BuildSpec()
steps = p.pre_build(mock_env)
self._assert_step_contains_all(steps, ['build dependencies', 'build lib-1'])
def test_default_test_step(self):
"""downstream tests should build by default"""
config = _test_proj_config.copy()
p = Project(**config)
m_toolchain = mock.Mock(name='mock toolchain', cross_compile=False)
mock_env = mock.Mock(name='MockEnv', config=config,
toolchain=m_toolchain)
steps = p.test(mock_env)
self._assert_step_contains(steps, 'test')
def test_downstream_tests_build_by_default(self):
"""downstream tests should build by default"""
config = _test_proj_config.copy()
config['downstream'] = [
{
'name': 'lib-1'
}
]
p = Project(**config)
m_toolchain = mock.Mock(name='mock toolchain', cross_compile=False)
mock_env = mock.Mock(name='MockEnv', config=config, project=p, toolchain=m_toolchain)
mock_env.spec = BuildSpec()
steps = p.build_consumers(mock_env)
self._assert_step_contains_all(steps, ['test lib-1'])
def test_downstream_post_build_runs_before_tests(self):
"""downstream post_build_steps should run before tests"""
config = _test_proj_config.copy()
config['downstream'] = [
{
'name': 'lib-1'
}
]
p = Project(**config)
m_toolchain = mock.Mock(name='mock toolchain', cross_compile=False)
mock_env = mock.Mock(name='MockEnv', config=config, project=p, toolchain=m_toolchain)
mock_env.spec = BuildSpec()
steps = p.build_consumers(mock_env)
self._assert_step_contains_all(steps, ['post build lib-1', 'test lib-1'])
def test_explicit_upstream_branch(self):
"""upstream with specific revision should override the detected branch"""
config = _test_proj_config.copy()
config['upstream'] = [
{
'name': 'lib-1',
'revision': 'explicit-branch'
}
]
p = Project(**config)
spec = BuildSpec(target='linux')
deps = p.get_dependencies(spec)
self.assertEqual('explicit-branch', deps[0].revision)
def test_upstream_targets_filtered_for_spec(self):
"""upstream with specific targets should only be applied if target matches current spec"""
config = _test_proj_config.copy()
config['upstream'] = [
{
'name': 'lib-1',
'targets': ['linux']
}
]
p = Project(**config)
spec = BuildSpec(target='macos')
dependencies = p.get_dependencies(spec)
self.assertEqual(0, len(dependencies), "dependencies should have filtered upstream with specific target")
def test_project_source_dir_replaced(self):
"""project specific dependency variables should be replaced"""
config = _test_proj_config.copy()
config['upstream'] = [
{
'name': 'lib-1'
}
]
p = Project(**config)
spec = BuildSpec(target='macos')
dependencies = p.get_dependencies(spec)
m_env = mock.Mock(name='MockEnv', config=config)
steps = dependencies[0].post_build(m_env)
self._assert_step_contains(steps, "{}/gradlew postBuildTask".format(os.path.join(test_data_dir, "lib-1")))