diff options
Diffstat (limited to 'lib/spack/spack/test/spec_semantics.py')
-rw-r--r-- | lib/spack/spack/test/spec_semantics.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py index 662ea5ef0e..579ba4486c 100644 --- a/lib/spack/spack/test/spec_semantics.py +++ b/lib/spack/spack/test/spec_semantics.py @@ -3,6 +3,8 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) +import pathlib + import pytest import spack.directives @@ -1005,6 +1007,84 @@ class TestSpecSemantics: assert new_spec.compiler_flags["cxxflags"] == ["-O1"] +@pytest.mark.parametrize( + "spec_str,format_str,expected", + [ + ("zlib@git.foo/bar", "{name}-{version}", str(pathlib.Path("zlib-git.foo_bar"))), + ("zlib@git.foo/bar", "{name}-{version}-{/hash}", None), + ("zlib@git.foo/bar", "{name}/{version}", str(pathlib.Path("zlib", "git.foo_bar"))), + ( + "zlib@{0}=1.0%gcc".format("a" * 40), + "{name}/{version}/{compiler}", + str(pathlib.Path("zlib", "{0}_1.0".format("a" * 40), "gcc")), + ), + ( + "zlib@git.foo/bar=1.0%gcc", + "{name}/{version}/{compiler}", + str(pathlib.Path("zlib", "git.foo_bar_1.0", "gcc")), + ), + ], +) +def test_spec_format_path(spec_str, format_str, expected): + _check_spec_format_path(spec_str, format_str, expected) + + +def _check_spec_format_path(spec_str, format_str, expected, path_ctor=None): + spec = Spec(spec_str) + if not expected: + with pytest.raises((spack.spec.SpecFormatPathError, spack.spec.SpecFormatStringError)): + spec.format_path(format_str, _path_ctor=path_ctor) + else: + formatted = spec.format_path(format_str, _path_ctor=path_ctor) + assert formatted == expected + + +@pytest.mark.parametrize( + "spec_str,format_str,expected", + [ + ( + "zlib@git.foo/bar", + r"C:\\installroot\{name}\{version}", + r"C:\installroot\zlib\git.foo_bar", + ), + ( + "zlib@git.foo/bar", + r"\\hostname\sharename\{name}\{version}", + r"\\hostname\sharename\zlib\git.foo_bar", + ), + # Windows doesn't attribute any significance to a leading + # "/" so it is discarded + ("zlib@git.foo/bar", r"/installroot/{name}/{version}", r"installroot\zlib\git.foo_bar"), + ], +) +def test_spec_format_path_windows(spec_str, format_str, expected): + _check_spec_format_path(spec_str, format_str, expected, path_ctor=pathlib.PureWindowsPath) + + +@pytest.mark.parametrize( + "spec_str,format_str,expected", + [ + ("zlib@git.foo/bar", r"/installroot/{name}/{version}", "/installroot/zlib/git.foo_bar"), + ("zlib@git.foo/bar", r"//installroot/{name}/{version}", "//installroot/zlib/git.foo_bar"), + # This is likely unintentional on Linux: Firstly, "\" is not a + # path separator for POSIX, so this is treated as a single path + # component (containing literal "\" characters); secondly, + # Spec.format treats "\" as an escape character, so is + # discarded (unless directly following another "\") + ( + "zlib@git.foo/bar", + r"C:\\installroot\package-{name}-{version}", + r"C__installrootpackage-zlib-git.foo_bar", + ), + # "\" is not a POSIX separator, and Spec.format treats "\{" as a literal + # "{", which means that the resulting format string is invalid + ("zlib@git.foo/bar", r"package\{name}\{version}", None), + ], +) +def test_spec_format_path_posix(spec_str, format_str, expected): + _check_spec_format_path(spec_str, format_str, expected, path_ctor=pathlib.PurePosixPath) + + @pytest.mark.regression("3887") @pytest.mark.parametrize("spec_str", ["py-extension2", "extension1", "perl-extension"]) def test_is_extension_after_round_trip_to_dict(config, mock_packages, spec_str): |