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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# Copyright 2013-2021 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)
import ast
import os
import pytest
import spack.directives
import spack.paths
import spack.util.package_hash as ph
from spack.spec import Spec
from spack.util.unparse import unparse
datadir = os.path.join(spack.paths.test_path, "data", "unparse")
def test_hash(tmpdir, mock_packages, config):
ph.package_hash("hash-test1@1.2")
def test_different_variants(tmpdir, mock_packages, config):
spec1 = Spec("hash-test1@1.2 +variantx")
spec2 = Spec("hash-test1@1.2 +varianty")
assert ph.package_hash(spec1) == ph.package_hash(spec2)
def test_all_same_but_name(tmpdir, mock_packages, config):
spec1 = Spec("hash-test1@1.2")
spec2 = Spec("hash-test2@1.2")
compare_sans_name(True, spec1, spec2)
spec1 = Spec("hash-test1@1.2 +varianty")
spec2 = Spec("hash-test2@1.2 +varianty")
compare_sans_name(True, spec1, spec2)
def test_all_same_but_archive_hash(tmpdir, mock_packages, config):
"""
Archive hash is not intended to be reflected in Package hash.
"""
spec1 = Spec("hash-test1@1.3")
spec2 = Spec("hash-test2@1.3")
compare_sans_name(True, spec1, spec2)
def test_all_same_but_patch_contents(tmpdir, mock_packages, config):
spec1 = Spec("hash-test1@1.1")
spec2 = Spec("hash-test2@1.1")
compare_sans_name(True, spec1, spec2)
def test_all_same_but_patches_to_apply(tmpdir, mock_packages, config):
spec1 = Spec("hash-test1@1.4")
spec2 = Spec("hash-test2@1.4")
compare_sans_name(True, spec1, spec2)
def test_all_same_but_install(tmpdir, mock_packages, config):
spec1 = Spec("hash-test1@1.5")
spec2 = Spec("hash-test2@1.5")
compare_sans_name(False, spec1, spec2)
def compare_sans_name(eq, spec1, spec2):
content1 = ph.package_content(spec1)
content1 = content1.replace(spec1.package.__class__.__name__, '')
content2 = ph.package_content(spec2)
content2 = content2.replace(spec2.package.__class__.__name__, '')
if eq:
assert content1 == content2
else:
assert content1 != content2
many_strings = '''\
"""ONE"""
"""TWO"""
var = "THREE" # make sure this is not removed
"FOUR"
class ManyDocstrings:
"""FIVE"""
"""SIX"""
x = "SEVEN"
def method1():
"""EIGHT"""
print("NINE")
"TEN"
for i in range(10):
print(i)
def method2():
"""ELEVEN"""
return "TWELVE"
'''
def test_remove_docstrings():
tree = ast.parse(many_strings)
tree = ph.RemoveDocstrings().visit(tree)
unparsed = unparse(tree)
# make sure the methods are preserved
assert "method1" in unparsed
assert "method2" in unparsed
# all of these are unassigned and should be removed
assert "ONE" not in unparsed
assert "TWO" not in unparsed
assert "FOUR" not in unparsed
assert "FIVE" not in unparsed
assert "SIX" not in unparsed
assert "EIGHT" not in unparsed
assert "TEN" not in unparsed
assert "ELEVEN" not in unparsed
# these are used in legitimate expressions
assert "THREE" in unparsed
assert "SEVEN" in unparsed
assert "NINE" in unparsed
assert "TWELVE" in unparsed
many_directives = """\
class HasManyDirectives:
{directives}
def foo():
# just a method to get in the way
pass
{directives}
""".format(directives="\n".join(
" %s()" % name for name in spack.directives.directive_names
))
def test_remove_directives():
"""Ensure all directives are removed from packages before hashing."""
tree = ast.parse(many_directives)
spec = Spec("has-many-directives")
tree = ph.RemoveDirectives(spec).visit(tree)
unparsed = unparse(tree)
for name in spack.directives.directive_names:
assert name not in unparsed
@pytest.mark.parametrize("package_spec,expected_hash", [
("amdfftw", "nfrk76xyu6wxs4xb4nyichm3om3kb7yp"),
("grads", "rrlmwml3f2frdnqavmro3ias66h5b2ce"),
("llvm", "ngact4ds3xwgsbn5bruxpfs6f4u4juba"),
# has @when("@4.1.0")
("mfem", "65xryd5zxarwzqlh2pojq7ykohpod4xz"),
("mfem@4.0.0", "65xryd5zxarwzqlh2pojq7ykohpod4xz"),
("mfem@4.1.0", "2j655nix3oe57iwvs2mlgx2mresk7czl"),
# has @when("@1.5.0:")
("py-torch", "lnwmqk4wadtlsc2badrt7foid5tl5vaw"),
("py-torch@1.0", "lnwmqk4wadtlsc2badrt7foid5tl5vaw"),
("py-torch@1.6", "5nwndnknxdfs5or5nrl4pecvw46xc5i2"),
])
def test_package_hash_consistency(package_spec, expected_hash):
"""Ensure that that package hash is consistent python version to version.
We assume these tests run across all supported Python versions in CI, and we ensure
consistency with recorded hashes for some well known inputs.
If this fails, then something about the way the python AST works has likely changed.
If Spack is running in a new python version, we might need to modify the unparser to
handle it. If not, then something has become inconsistent about the way we unparse
Python code across versions.
"""
spec = Spec(package_spec)
filename = os.path.join(datadir, "%s.txt" % spec.name)
print(ph.canonical_source(spec, filename))
h = ph.canonical_source_hash(spec, filename)
assert expected_hash == h
many_multimethods = """\
class Pkg:
def foo(self):
print("ONE")
@when("@1.0")
def foo(self):
print("TWO")
@when("@2.0")
@when(sys.platform == "darwin")
def foo(self):
print("THREE")
@when("@3.0")
def foo(self):
print("FOUR")
# this one should always stay
@run_after("install")
def some_function(self):
print("FIVE")
"""
def test_multimethod_resolution(tmpdir):
when_pkg = tmpdir.join("pkg.py")
with when_pkg.open("w") as f:
f.write(many_multimethods)
# all are false but the default
filtered = ph.canonical_source("pkg@4.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" not in filtered
assert "THREE" not in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
# we know first @when overrides default and others are false
filtered = ph.canonical_source("pkg@1.0", str(when_pkg))
assert "ONE" not in filtered
assert "TWO" in filtered
assert "THREE" not in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
# we know last @when overrides default and others are false
filtered = ph.canonical_source("pkg@3.0", str(when_pkg))
assert "ONE" not in filtered
assert "TWO" not in filtered
assert "THREE" not in filtered
assert "FOUR" in filtered
assert "FIVE" in filtered
# we don't know if default or THREE will win, include both
filtered = ph.canonical_source("pkg@2.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" not in filtered
assert "THREE" in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
more_dynamic_multimethods = """\
class Pkg:
@when(sys.platform == "darwin")
def foo(self):
print("ONE")
@when("@1.0")
def foo(self):
print("TWO")
# this one isn't dynamic, but an int fails the Spec parse,
# so it's kept because it has to be evaluated at runtime.
@when("@2.0")
@when(1)
def foo(self):
print("THREE")
@when("@3.0")
def foo(self):
print("FOUR")
# this one should always stay
@run_after("install")
def some_function(self):
print("FIVE")
"""
def test_more_dynamic_multimethod_resolution(tmpdir):
when_pkg = tmpdir.join("pkg.py")
with when_pkg.open("w") as f:
f.write(more_dynamic_multimethods)
# we know the first one is the only one that can win.
filtered = ph.canonical_source("pkg@4.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" not in filtered
assert "THREE" not in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
# now we have to include ONE and TWO because ONE may win dynamically.
filtered = ph.canonical_source("pkg@1.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" in filtered
assert "THREE" not in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
# we know FOUR is true and TWO and THREE are false, but ONE may
# still win dynamically.
filtered = ph.canonical_source("pkg@3.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" not in filtered
assert "THREE" not in filtered
assert "FOUR" in filtered
assert "FIVE" in filtered
# TWO and FOUR can't be satisfied, but ONE or THREE could win
filtered = ph.canonical_source("pkg@2.0", str(when_pkg))
assert "ONE" in filtered
assert "TWO" not in filtered
assert "THREE" in filtered
assert "FOUR" not in filtered
assert "FIVE" in filtered
|