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
297
298
299
300
301
302
303
304
305
|
# Copyright 2013-2022 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from __future__ import print_function
import re
import shutil
import sys
import pytest
from llnl.util.filesystem import mkdirp, working_dir
import spack.main
import spack.repo
from spack.util.executable import which
pytestmark = pytest.mark.skipif(not which('git'),
reason="spack pkg tests require git")
#: new fake package template
pkg_template = '''\
from spack import *
class {name}(Package):
homepage = "http://www.example.com"
url = "http://www.example.com/test-1.0.tar.gz"
version('1.0', '0123456789abcdef0123456789abcdef')
def install(self, spec, prefix):
pass
'''
abc = set(('pkg-a', 'pkg-b', 'pkg-c'))
abd = set(('pkg-a', 'pkg-b', 'pkg-d'))
# Force all tests to use a git repository *in* the mock packages repo.
@pytest.fixture(scope='module')
def mock_pkg_git_repo(tmpdir_factory):
"""Copy the builtin.mock repo and make a mutable git repo inside it."""
tmproot = tmpdir_factory.mktemp('mock_pkg_git_repo')
repo_path = tmproot.join('builtin.mock')
shutil.copytree(spack.paths.mock_packages_path, str(repo_path))
mock_repo = spack.repo.RepoPath(str(repo_path))
mock_repo_packages = mock_repo.repos[0].packages_path
git = which('git', required=True)
with working_dir(mock_repo_packages):
git('init')
# initial commit with mock packages
# the -f is necessary in case people ignore build-* in their ignores
git('add', '-f', '.')
git('config', 'user.email', 'testing@spack.io')
git('config', 'user.name', 'Spack Testing')
git('-c', 'commit.gpgsign=false', 'commit',
'-m', 'initial mock repo commit')
# add commit with pkg-a, pkg-b, pkg-c packages
mkdirp('pkg-a', 'pkg-b', 'pkg-c')
with open('pkg-a/package.py', 'w') as f:
f.write(pkg_template.format(name='PkgA'))
with open('pkg-b/package.py', 'w') as f:
f.write(pkg_template.format(name='PkgB'))
with open('pkg-c/package.py', 'w') as f:
f.write(pkg_template.format(name='PkgC'))
git('add', 'pkg-a', 'pkg-b', 'pkg-c')
git('-c', 'commit.gpgsign=false', 'commit',
'-m', 'add pkg-a, pkg-b, pkg-c')
# remove pkg-c, add pkg-d
with open('pkg-b/package.py', 'a') as f:
f.write('\n# change pkg-b')
git('add', 'pkg-b')
mkdirp('pkg-d')
with open('pkg-d/package.py', 'w') as f:
f.write(pkg_template.format(name='PkgD'))
git('add', 'pkg-d')
git('rm', '-rf', 'pkg-c')
git('-c', 'commit.gpgsign=false', 'commit',
'-m', 'change pkg-b, remove pkg-c, add pkg-d')
with spack.repo.use_repositories(mock_repo):
yield mock_repo_packages
@pytest.fixture(scope='module')
def mock_pkg_names():
repo = spack.repo.path.get_repo('builtin.mock')
names = set(name for name in repo.all_package_names()
if not name.startswith('pkg-'))
return names
def split(output):
"""Split command line output into an array."""
output = output.strip()
return re.split(r'\s+', output) if output else []
pkg = spack.main.SpackCommand('pkg')
def test_packages_path():
assert (spack.repo.packages_path() ==
spack.repo.path.get_repo('builtin').packages_path)
def test_mock_packages_path(mock_packages):
assert (spack.repo.packages_path() ==
spack.repo.path.get_repo('builtin.mock').packages_path)
def test_pkg_add(mock_pkg_git_repo):
with working_dir(mock_pkg_git_repo):
mkdirp('pkg-e')
with open('pkg-e/package.py', 'w') as f:
f.write(pkg_template.format(name='PkgE'))
pkg('add', 'pkg-e')
git = which('git', required=True)
with working_dir(mock_pkg_git_repo):
try:
assert ('A pkg-e/package.py' in
git('status', '--short', output=str))
finally:
shutil.rmtree('pkg-e')
# Removing a package mid-run disrupts Spack's caching
if spack.repo.path.repos[0]._fast_package_checker:
spack.repo.path.repos[0]._fast_package_checker.invalidate()
with pytest.raises(spack.main.SpackCommandError):
pkg('add', 'does-not-exist')
@pytest.mark.skipif(sys.platform == 'win32', reason="stdout format conflict")
def test_pkg_list(mock_pkg_git_repo, mock_pkg_names):
out = split(pkg('list', 'HEAD^^'))
assert sorted(mock_pkg_names) == sorted(out)
out = split(pkg('list', 'HEAD^'))
assert sorted(
mock_pkg_names.union(['pkg-a', 'pkg-b', 'pkg-c'])) == sorted(out)
out = split(pkg('list', 'HEAD'))
assert sorted(
mock_pkg_names.union(['pkg-a', 'pkg-b', 'pkg-d'])) == sorted(out)
# test with three dots to make sure pkg calls `git merge-base`
out = split(pkg('list', 'HEAD^^...'))
assert sorted(mock_pkg_names) == sorted(out)
@pytest.mark.skipif(sys.platform == 'win32', reason="stdout format conflict")
def test_pkg_diff(mock_pkg_git_repo, mock_pkg_names):
out = split(pkg('diff', 'HEAD^^', 'HEAD^'))
assert out == ['HEAD^:', 'pkg-a', 'pkg-b', 'pkg-c']
out = split(pkg('diff', 'HEAD^^', 'HEAD'))
assert out == ['HEAD:', 'pkg-a', 'pkg-b', 'pkg-d']
out = split(pkg('diff', 'HEAD^', 'HEAD'))
assert out == ['HEAD^:', 'pkg-c', 'HEAD:', 'pkg-d']
@pytest.mark.skipif(sys.platform == 'win32', reason="stdout format conflict")
def test_pkg_added(mock_pkg_git_repo):
out = split(pkg('added', 'HEAD^^', 'HEAD^'))
assert ['pkg-a', 'pkg-b', 'pkg-c'] == out
out = split(pkg('added', 'HEAD^^', 'HEAD'))
assert ['pkg-a', 'pkg-b', 'pkg-d'] == out
out = split(pkg('added', 'HEAD^', 'HEAD'))
assert ['pkg-d'] == out
out = split(pkg('added', 'HEAD', 'HEAD'))
assert out == []
@pytest.mark.skipif(sys.platform == 'win32', reason="stdout format conflict")
def test_pkg_removed(mock_pkg_git_repo):
out = split(pkg('removed', 'HEAD^^', 'HEAD^'))
assert out == []
out = split(pkg('removed', 'HEAD^^', 'HEAD'))
assert out == []
out = split(pkg('removed', 'HEAD^', 'HEAD'))
assert out == ['pkg-c']
@pytest.mark.skipif(sys.platform == 'win32', reason="stdout format conflict")
def test_pkg_changed(mock_pkg_git_repo):
out = split(pkg('changed', 'HEAD^^', 'HEAD^'))
assert out == []
out = split(pkg('changed', '--type', 'c', 'HEAD^^', 'HEAD^'))
assert out == []
out = split(pkg('changed', '--type', 'a', 'HEAD^^', 'HEAD^'))
assert out == ['pkg-a', 'pkg-b', 'pkg-c']
out = split(pkg('changed', '--type', 'r', 'HEAD^^', 'HEAD^'))
assert out == []
out = split(pkg('changed', '--type', 'ar', 'HEAD^^', 'HEAD^'))
assert out == ['pkg-a', 'pkg-b', 'pkg-c']
out = split(pkg('changed', '--type', 'arc', 'HEAD^^', 'HEAD^'))
assert out == ['pkg-a', 'pkg-b', 'pkg-c']
out = split(pkg('changed', 'HEAD^', 'HEAD'))
assert out == ['pkg-b']
out = split(pkg('changed', '--type', 'c', 'HEAD^', 'HEAD'))
assert out == ['pkg-b']
out = split(pkg('changed', '--type', 'a', 'HEAD^', 'HEAD'))
assert out == ['pkg-d']
out = split(pkg('changed', '--type', 'r', 'HEAD^', 'HEAD'))
assert out == ['pkg-c']
out = split(pkg('changed', '--type', 'ar', 'HEAD^', 'HEAD'))
assert out == ['pkg-c', 'pkg-d']
out = split(pkg('changed', '--type', 'arc', 'HEAD^', 'HEAD'))
assert out == ['pkg-b', 'pkg-c', 'pkg-d']
# invalid type argument
with pytest.raises(spack.main.SpackCommandError):
pkg('changed', '--type', 'foo')
def test_pkg_fails_when_not_git_repo(monkeypatch):
monkeypatch.setattr(spack.cmd, 'spack_is_git_repo', lambda: False)
with pytest.raises(spack.main.SpackCommandError):
pkg('added')
def test_pkg_source_requires_one_arg(mock_packages):
with pytest.raises(spack.main.SpackCommandError):
pkg("source", "a", "b")
with pytest.raises(spack.main.SpackCommandError):
pkg("source", "--canonical", "a", "b")
def test_pkg_source(mock_packages):
fake_source = pkg("source", "fake")
fake_file = spack.repo.path.filename_for_package_name("fake")
with open(fake_file) as f:
contents = f.read()
assert fake_source == contents
def test_pkg_canonical_source(mock_packages):
source = pkg("source", "multimethod")
assert "@when('@2.0')" in source
assert "Check that multimethods work with boolean values" in source
canonical_1 = pkg("source", "--canonical", "multimethod@1.0")
assert "@when" not in canonical_1
assert "should_not_be_reached by diamond inheritance test" not in canonical_1
assert "return 'base@1.0'" in canonical_1
assert "return 'base@2.0'" not in canonical_1
assert "return 'first_parent'" not in canonical_1
assert "'should_not_be_reached by diamond inheritance test'" not in canonical_1
canonical_2 = pkg("source", "--canonical", "multimethod@2.0")
assert "@when" not in canonical_2
assert "return 'base@1.0'" not in canonical_2
assert "return 'base@2.0'" in canonical_2
assert "return 'first_parent'" in canonical_2
assert "'should_not_be_reached by diamond inheritance test'" not in canonical_2
canonical_3 = pkg("source", "--canonical", "multimethod@3.0")
assert "@when" not in canonical_3
assert "return 'base@1.0'" not in canonical_3
assert "return 'base@2.0'" not in canonical_3
assert "return 'first_parent'" not in canonical_3
assert "'should_not_be_reached by diamond inheritance test'" not in canonical_3
canonical_4 = pkg("source", "--canonical", "multimethod@4.0")
assert "@when" not in canonical_4
assert "return 'base@1.0'" not in canonical_4
assert "return 'base@2.0'" not in canonical_4
assert "return 'first_parent'" not in canonical_4
assert "'should_not_be_reached by diamond inheritance test'" in canonical_4
def test_pkg_hash(mock_packages):
output = pkg("hash", "a", "b").strip().split()
assert len(output) == 2 and all(len(elt) == 32 for elt in output)
output = pkg("hash", "multimethod").strip().split()
assert len(output) == 1 and all(len(elt) == 32 for elt in output)
|