diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2024-04-15 10:20:23 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-15 09:20:23 -0700 |
commit | f9f97bf22b6d96e8554c944b76b6aa1c55f50924 (patch) | |
tree | cad17e14f6c582fd32e3f31aca1d14653e2f5a32 /lib | |
parent | 8033455d5ff4071eeef71801ced9f13e1bcfd16b (diff) | |
download | spack-f9f97bf22b6d96e8554c944b76b6aa1c55f50924.tar.gz spack-f9f97bf22b6d96e8554c944b76b6aa1c55f50924.tar.bz2 spack-f9f97bf22b6d96e8554c944b76b6aa1c55f50924.tar.xz spack-f9f97bf22b6d96e8554c944b76b6aa1c55f50924.zip |
tests: Spec tests shouldn't fetch remote git repositories. (#43656)
Currently, some of the tests in `spec_format` and `spec_semantics` fetch
the actual zlib repository when run, because they call `str()` on specs
like `zlib@foo/bar`, which at least currently requires a remote git clone
to resolve.
This doesn't change the behavior of git versions, but it uses our mock git
repo infrastructure and clones the `git-test` package instead of the *real*
URL from the mock `zlib` package.
This should speed up tests. We could probably refactor more so that the git
tests *all* use such a fixture, but the `checks` field that unfortunately
tightly couples the mock git repository and the `git_fetch` tests complicates
this. We could also consider *not* making `str()` resolve git versions, but
I did not dig into that here.
- [x] add a mock_git_test_package fixture that sets up a mock git repo *and*
monkeypatches the `git-test` package (like our git test packages do)
- [x] use fixture in `test_spec_format_path`
- [x] use fixture in `test_spec_format_path_posix`
- [x] use fixture in `test_spec_format_path_windows`
- [x] use fixture in `test_parse_single_spec`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/test/conftest.py | 10 | ||||
-rw-r--r-- | lib/spack/spack/test/spec_semantics.py | 52 | ||||
-rw-r--r-- | lib/spack/spack/test/spec_syntax.py | 8 |
3 files changed, 46 insertions, 24 deletions
diff --git a/lib/spack/spack/test/conftest.py b/lib/spack/spack/test/conftest.py index 543cfb4b2f..f25cf42e6c 100644 --- a/lib/spack/spack/test/conftest.py +++ b/lib/spack/spack/test/conftest.py @@ -55,6 +55,7 @@ import spack.util.git import spack.util.gpg import spack.util.spack_yaml as syaml import spack.util.url as url_util +import spack.version from spack.fetch_strategy import URLFetchStrategy from spack.util.pattern import Bunch @@ -1440,6 +1441,15 @@ def mock_git_repository(git, tmpdir_factory): yield t +@pytest.fixture(scope="function") +def mock_git_test_package(mock_git_repository, mutable_mock_repo, monkeypatch): + # install a fake git version in the package class + pkg_class = spack.repo.PATH.get_pkg_class("git-test") + monkeypatch.delattr(pkg_class, "git") + monkeypatch.setitem(pkg_class.versions, spack.version.Version("git"), mock_git_repository.url) + return pkg_class + + @pytest.fixture(scope="session") def mock_hg_repository(tmpdir_factory): """Creates a very simple hg repository with two commits.""" diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py index 7a075b3710..2f4e7f43a4 100644 --- a/lib/spack/spack/test/spec_semantics.py +++ b/lib/spack/spack/test/spec_semantics.py @@ -1096,22 +1096,22 @@ class TestSpecSemantics: @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"))), + ("git-test@git.foo/bar", "{name}-{version}", str(pathlib.Path("git-test-git.foo_bar"))), + ("git-test@git.foo/bar", "{name}-{version}-{/hash}", None), + ("git-test@git.foo/bar", "{name}/{version}", str(pathlib.Path("git-test", "git.foo_bar"))), ( - "zlib@{0}=1.0%gcc".format("a" * 40), + "git-test@{0}=1.0%gcc".format("a" * 40), "{name}/{version}/{compiler}", - str(pathlib.Path("zlib", "{0}_1.0".format("a" * 40), "gcc")), + str(pathlib.Path("git-test", "{0}_1.0".format("a" * 40), "gcc")), ), ( - "zlib@git.foo/bar=1.0%gcc", + "git-test@git.foo/bar=1.0%gcc", "{name}/{version}/{compiler}", - str(pathlib.Path("zlib", "git.foo_bar_1.0", "gcc")), + str(pathlib.Path("git-test", "git.foo_bar_1.0", "gcc")), ), ], ) -def test_spec_format_path(spec_str, format_str, expected): +def test_spec_format_path(spec_str, format_str, expected, mock_git_test_package): _check_spec_format_path(spec_str, format_str, expected) @@ -1129,45 +1129,57 @@ def _check_spec_format_path(spec_str, format_str, expected, path_ctor=None): "spec_str,format_str,expected", [ ( - "zlib@git.foo/bar", + "git-test@git.foo/bar", r"C:\\installroot\{name}\{version}", - r"C:\installroot\zlib\git.foo_bar", + r"C:\installroot\git-test\git.foo_bar", ), ( - "zlib@git.foo/bar", + "git-test@git.foo/bar", r"\\hostname\sharename\{name}\{version}", - r"\\hostname\sharename\zlib\git.foo_bar", + r"\\hostname\sharename\git-test\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"), + ( + "git-test@git.foo/bar", + r"/installroot/{name}/{version}", + r"installroot\git-test\git.foo_bar", + ), ], ) -def test_spec_format_path_windows(spec_str, format_str, expected): +def test_spec_format_path_windows(spec_str, format_str, expected, mock_git_test_package): _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"), + ( + "git-test@git.foo/bar", + r"/installroot/{name}/{version}", + "/installroot/git-test/git.foo_bar", + ), + ( + "git-test@git.foo/bar", + r"//installroot/{name}/{version}", + "//installroot/git-test/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", + "git-test@git.foo/bar", r"C:\\installroot\package-{name}-{version}", - r"C__installrootpackage-zlib-git.foo_bar", + r"C__installrootpackage-git-test-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), + ("git-test@git.foo/bar", r"package\{name}\{version}", None), ], ) -def test_spec_format_path_posix(spec_str, format_str, expected): +def test_spec_format_path_posix(spec_str, format_str, expected, mock_git_test_package): _check_spec_format_path(spec_str, format_str, expected, path_ctor=pathlib.PurePosixPath) diff --git a/lib/spack/spack/test/spec_syntax.py b/lib/spack/spack/test/spec_syntax.py index 49f6de3b65..2e6f4b4fba 100644 --- a/lib/spack/spack/test/spec_syntax.py +++ b/lib/spack/spack/test/spec_syntax.py @@ -551,12 +551,12 @@ def specfile_for(default_mock_concretization): "^[deptypes=build,link] zlib", ), ( - "zlib@git.foo/bar", + "git-test@git.foo/bar", [ - Token(TokenType.UNQUALIFIED_PACKAGE_NAME, "zlib"), + Token(TokenType.UNQUALIFIED_PACKAGE_NAME, "git-test"), Token(TokenType.GIT_VERSION, "@git.foo/bar"), ], - "zlib@git.foo/bar", + "git-test@git.foo/bar", ), # Variant propagation ( @@ -585,7 +585,7 @@ def specfile_for(default_mock_concretization): ), ], ) -def test_parse_single_spec(spec_str, tokens, expected_roundtrip): +def test_parse_single_spec(spec_str, tokens, expected_roundtrip, mock_git_test_package): parser = SpecParser(spec_str) assert tokens == parser.tokens() assert expected_roundtrip == str(parser.next_spec()) |