tests: fix more cases of env variables (#41226)

This commit is contained in:
Harmen Stoppels 2023-11-27 16:37:31 +01:00
parent 9f2b8eef7a
commit 03d7643480
3 changed files with 83 additions and 93 deletions

View File

@ -43,7 +43,7 @@ def test_find_gpg(cmd_name, version, tmpdir, mock_gnupghome, monkeypatch):
f.write(TEMPLATE.format(version=version)) f.write(TEMPLATE.format(version=version))
fs.set_executable(fname) fs.set_executable(fname)
monkeypatch.setitem(os.environ, "PATH", str(tmpdir)) monkeypatch.setenv("PATH", str(tmpdir))
if version == "undetectable" or version.endswith("1.3.4"): if version == "undetectable" or version.endswith("1.3.4"):
with pytest.raises(spack.util.gpg.SpackGPGError): with pytest.raises(spack.util.gpg.SpackGPGError):
spack.util.gpg.init(force=True) spack.util.gpg.init(force=True)
@ -54,7 +54,7 @@ def test_find_gpg(cmd_name, version, tmpdir, mock_gnupghome, monkeypatch):
def test_no_gpg_in_path(tmpdir, mock_gnupghome, monkeypatch, mutable_config): def test_no_gpg_in_path(tmpdir, mock_gnupghome, monkeypatch, mutable_config):
monkeypatch.setitem(os.environ, "PATH", str(tmpdir)) monkeypatch.setenv("PATH", str(tmpdir))
bootstrap("disable") bootstrap("disable")
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
spack.util.gpg.init(force=True) spack.util.gpg.init(force=True)

View File

@ -904,11 +904,10 @@ def test_install_help_cdash():
@pytest.mark.disable_clean_stage_check @pytest.mark.disable_clean_stage_check
def test_cdash_auth_token(tmpdir, mock_fetch, install_mockery, capfd): def test_cdash_auth_token(tmpdir, mock_fetch, install_mockery, monkeypatch, capfd):
# capfd interferes with Spack's capturing # capfd interferes with Spack's capturing
with tmpdir.as_cwd(): with tmpdir.as_cwd(), capfd.disabled():
with capfd.disabled(): monkeypatch.setenv("SPACK_CDASH_AUTH_TOKEN", "asdf")
os.environ["SPACK_CDASH_AUTH_TOKEN"] = "asdf"
out = install("-v", "--log-file=cdash_reports", "--log-format=cdash", "a") out = install("-v", "--log-file=cdash_reports", "--log-format=cdash", "a")
assert "Using CDash auth token from environment" in out assert "Using CDash auth token from environment" in out

View File

@ -9,10 +9,7 @@
This just tests whether the right args are getting passed to make. This just tests whether the right args are getting passed to make.
""" """
import os import os
import shutil
import sys import sys
import tempfile
import unittest
import pytest import pytest
@ -20,110 +17,104 @@
from spack.util.environment import path_put_first from spack.util.environment import path_put_first
pytestmark = pytest.mark.skipif( pytestmark = pytest.mark.skipif(
sys.platform == "win32", sys.platform == "win32", reason="MakeExecutable not supported on Windows"
reason="MakeExecutable \
not supported on Windows",
) )
class MakeExecutableTest(unittest.TestCase): @pytest.fixture(autouse=True)
def setUp(self): def make_executable(tmp_path, working_env):
self.tmpdir = tempfile.mkdtemp() make_exe = tmp_path / "make"
make_exe = os.path.join(self.tmpdir, "make")
with open(make_exe, "w") as f: with open(make_exe, "w") as f:
f.write("#!/bin/sh\n") f.write("#!/bin/sh\n")
f.write('echo "$@"') f.write('echo "$@"')
os.chmod(make_exe, 0o700) os.chmod(make_exe, 0o700)
path_put_first("PATH", [self.tmpdir]) path_put_first("PATH", [tmp_path])
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_make_normal(self): def test_make_normal():
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
self.assertEqual(make(output=str).strip(), "-j8") assert make(output=str).strip() == "-j8"
self.assertEqual(make("install", output=str).strip(), "-j8 install") assert make("install", output=str).strip() == "-j8 install"
def test_make_explicit(self):
def test_make_explicit():
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
self.assertEqual(make(parallel=True, output=str).strip(), "-j8") assert make(parallel=True, output=str).strip() == "-j8"
self.assertEqual(make("install", parallel=True, output=str).strip(), "-j8 install") assert make("install", parallel=True, output=str).strip() == "-j8 install"
def test_make_one_job(self):
def test_make_one_job():
make = MakeExecutable("make", 1) make = MakeExecutable("make", 1)
self.assertEqual(make(output=str).strip(), "-j1") assert make(output=str).strip() == "-j1"
self.assertEqual(make("install", output=str).strip(), "-j1 install") assert make("install", output=str).strip() == "-j1 install"
def test_make_parallel_false(self):
def test_make_parallel_false():
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
self.assertEqual(make(parallel=False, output=str).strip(), "-j1") assert make(parallel=False, output=str).strip() == "-j1"
self.assertEqual(make("install", parallel=False, output=str).strip(), "-j1 install") assert make("install", parallel=False, output=str).strip() == "-j1 install"
def test_make_parallel_disabled(self):
def test_make_parallel_disabled(monkeypatch):
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
os.environ["SPACK_NO_PARALLEL_MAKE"] = "true" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "true")
self.assertEqual(make(output=str).strip(), "-j1") assert make(output=str).strip() == "-j1"
self.assertEqual(make("install", output=str).strip(), "-j1 install") assert make("install", output=str).strip() == "-j1 install"
os.environ["SPACK_NO_PARALLEL_MAKE"] = "1" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "1")
self.assertEqual(make(output=str).strip(), "-j1") assert make(output=str).strip() == "-j1"
self.assertEqual(make("install", output=str).strip(), "-j1 install") assert make("install", output=str).strip() == "-j1 install"
# These don't disable (false and random string) # These don't disable (false and random string)
os.environ["SPACK_NO_PARALLEL_MAKE"] = "false" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "false")
self.assertEqual(make(output=str).strip(), "-j8") assert make(output=str).strip() == "-j8"
self.assertEqual(make("install", output=str).strip(), "-j8 install") assert make("install", output=str).strip() == "-j8 install"
os.environ["SPACK_NO_PARALLEL_MAKE"] = "foobar" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "foobar")
self.assertEqual(make(output=str).strip(), "-j8") assert make(output=str).strip() == "-j8"
self.assertEqual(make("install", output=str).strip(), "-j8 install") assert make("install", output=str).strip() == "-j8 install"
del os.environ["SPACK_NO_PARALLEL_MAKE"]
def test_make_parallel_precedence(self): def test_make_parallel_precedence(monkeypatch):
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
# These should work # These should work
os.environ["SPACK_NO_PARALLEL_MAKE"] = "true" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "true")
self.assertEqual(make(parallel=True, output=str).strip(), "-j1") assert make(parallel=True, output=str).strip() == "-j1"
self.assertEqual(make("install", parallel=True, output=str).strip(), "-j1 install") assert make("install", parallel=True, output=str).strip() == "-j1 install"
os.environ["SPACK_NO_PARALLEL_MAKE"] = "1" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "1")
self.assertEqual(make(parallel=True, output=str).strip(), "-j1") assert make(parallel=True, output=str).strip() == "-j1"
self.assertEqual(make("install", parallel=True, output=str).strip(), "-j1 install") assert make("install", parallel=True, output=str).strip() == "-j1 install"
# These don't disable (false and random string) # These don't disable (false and random string)
os.environ["SPACK_NO_PARALLEL_MAKE"] = "false" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "false")
self.assertEqual(make(parallel=True, output=str).strip(), "-j8") assert make(parallel=True, output=str).strip() == "-j8"
self.assertEqual(make("install", parallel=True, output=str).strip(), "-j8 install") assert make("install", parallel=True, output=str).strip() == "-j8 install"
os.environ["SPACK_NO_PARALLEL_MAKE"] = "foobar" monkeypatch.setenv("SPACK_NO_PARALLEL_MAKE", "foobar")
self.assertEqual(make(parallel=True, output=str).strip(), "-j8") assert make(parallel=True, output=str).strip() == "-j8"
self.assertEqual(make("install", parallel=True, output=str).strip(), "-j8 install") assert make("install", parallel=True, output=str).strip() == "-j8 install"
del os.environ["SPACK_NO_PARALLEL_MAKE"]
def test_make_jobs_env(self): def test_make_jobs_env():
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
dump_env = {} dump_env = {}
self.assertEqual( assert make(output=str, jobs_env="MAKE_PARALLELISM", _dump_env=dump_env).strip() == "-j8"
make(output=str, jobs_env="MAKE_PARALLELISM", _dump_env=dump_env).strip(), "-j8" assert dump_env["MAKE_PARALLELISM"] == "8"
)
self.assertEqual(dump_env["MAKE_PARALLELISM"], "8")
def test_make_jobserver(self):
def test_make_jobserver(monkeypatch):
make = MakeExecutable("make", 8) make = MakeExecutable("make", 8)
os.environ["MAKEFLAGS"] = "--jobserver-auth=X,Y" monkeypatch.setenv("MAKEFLAGS", "--jobserver-auth=X,Y")
self.assertEqual(make(output=str).strip(), "") assert make(output=str).strip() == ""
self.assertEqual(make(parallel=False, output=str).strip(), "-j1") assert make(parallel=False, output=str).strip() == "-j1"
del os.environ["MAKEFLAGS"]
def test_make_jobserver_not_supported(self):
def test_make_jobserver_not_supported(monkeypatch):
make = MakeExecutable("make", 8, supports_jobserver=False) make = MakeExecutable("make", 8, supports_jobserver=False)
os.environ["MAKEFLAGS"] = "--jobserver-auth=X,Y" monkeypatch.setenv("MAKEFLAGS", "--jobserver-auth=X,Y")
# Currently fallback on default job count, Maybe it should force -j1 ? # Currently fallback on default job count, Maybe it should force -j1 ?
self.assertEqual(make(output=str).strip(), "-j8") assert make(output=str).strip() == "-j8"
del os.environ["MAKEFLAGS"]