diff options
Diffstat (limited to 'lib/spack/spack/test/main.py')
-rw-r--r-- | lib/spack/spack/test/main.py | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/spack/spack/test/main.py b/lib/spack/spack/test/main.py index 8af8bc590c..c27a2723bb 100644 --- a/lib/spack/spack/test/main.py +++ b/lib/spack/spack/test/main.py @@ -3,7 +3,6 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) -import os import sys import pytest @@ -11,6 +10,8 @@ import pytest import llnl.util.filesystem as fs import spack.paths +import spack.util.executable as exe +import spack.util.git from spack.main import get_version, main pytestmark = pytest.mark.skipif( @@ -18,7 +19,7 @@ pytestmark = pytest.mark.skipif( ) -def test_version_git_nonsense_output(tmpdir, working_env): +def test_version_git_nonsense_output(tmpdir, working_env, monkeypatch): git = str(tmpdir.join("git")) with open(git, "w") as f: f.write( @@ -28,11 +29,11 @@ echo --|not a hash|---- ) fs.set_executable(git) - os.environ["PATH"] = str(tmpdir) + monkeypatch.setattr(spack.util.git, "git", lambda: exe.which(git)) assert spack.spack_version == get_version() -def test_version_git_fails(tmpdir, working_env): +def test_version_git_fails(tmpdir, working_env, monkeypatch): git = str(tmpdir.join("git")) with open(git, "w") as f: f.write( @@ -43,11 +44,11 @@ exit 1 ) fs.set_executable(git) - os.environ["PATH"] = str(tmpdir) + monkeypatch.setattr(spack.util.git, "git", lambda: exe.which(git)) assert spack.spack_version == get_version() -def test_git_sha_output(tmpdir, working_env): +def test_git_sha_output(tmpdir, working_env, monkeypatch): git = str(tmpdir.join("git")) sha = "26552533be04e83e66be2c28e0eb5011cb54e8fa" with open(git, "w") as f: @@ -60,7 +61,7 @@ echo {0} ) fs.set_executable(git) - os.environ["PATH"] = str(tmpdir) + monkeypatch.setattr(spack.util.git, "git", lambda: exe.which(git)) expected = "{0} ({1})".format(spack.spack_version, sha) assert expected == get_version() @@ -70,18 +71,22 @@ def test_get_version_no_repo(tmpdir, monkeypatch): assert spack.spack_version == get_version() -def test_get_version_no_git(tmpdir, working_env): - os.environ["PATH"] = str(tmpdir) +def test_get_version_no_git(tmpdir, working_env, monkeypatch): + monkeypatch.setattr(spack.util.git, "git", lambda: None) assert spack.spack_version == get_version() -def test_main_calls_get_version(tmpdir, capsys, working_env): - os.environ["PATH"] = str(tmpdir) +def test_main_calls_get_version(tmpdir, capsys, working_env, monkeypatch): + # act like git is not found in the PATH + monkeypatch.setattr(spack.util.git, "git", lambda: None) + + # make sure we get a bare version (without commit) when this happens main(["-V"]) - assert spack.spack_version == capsys.readouterr()[0].strip() + out, err = capsys.readouterr() + assert spack.spack_version == out.strip() -def test_get_version_bad_git(tmpdir, working_env): +def test_get_version_bad_git(tmpdir, working_env, monkeypatch): bad_git = str(tmpdir.join("git")) with open(bad_git, "w") as f: f.write( @@ -91,5 +96,5 @@ exit 1 ) fs.set_executable(bad_git) - os.environ["PATH"] = str(tmpdir) + monkeypatch.setattr(spack.util.git, "git", lambda: exe.which(bad_git)) assert spack.spack_version == get_version() |