diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2021-12-23 00:41:45 -0800 |
---|---|---|
committer | Greg Becker <becker33@llnl.gov> | 2022-01-12 06:14:18 -0800 |
commit | 8880a00862026700a82de1004e24ff06e54a165a (patch) | |
tree | 84d021c13ce3d3780284e455f1f7a366679285c0 /lib/spack/spack/test/util/package_hash.py | |
parent | 572fbf4f49564669d8157567c6d18684cc6e086a (diff) | |
download | spack-8880a00862026700a82de1004e24ff06e54a165a.tar.gz spack-8880a00862026700a82de1004e24ff06e54a165a.tar.bz2 spack-8880a00862026700a82de1004e24ff06e54a165a.tar.xz spack-8880a00862026700a82de1004e24ff06e54a165a.zip |
package_hash: remove all unassigned strings, not just docstrings
Some packages use top-level unassigned strings instead of comments, either just after a
docstring on in the body somewhere else. Ignore those strings becasue they have no
effect on package behavior.
- [x] adjust RemoveDocstrings to remove all free-standing strings.
- [x] move tests for util/package_hash.py to test/util/package_hash.py
Co-authored-by: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com>
Diffstat (limited to 'lib/spack/spack/test/util/package_hash.py')
-rw-r--r-- | lib/spack/spack/test/util/package_hash.py | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/lib/spack/spack/test/util/package_hash.py b/lib/spack/spack/test/util/package_hash.py new file mode 100644 index 0000000000..16829586e4 --- /dev/null +++ b/lib/spack/spack/test/util/package_hash.py @@ -0,0 +1,125 @@ +# 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 spack.paths +import spack.util.package_hash as ph +from spack.spec import Spec +from spack.util.unparse import 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 |