Avoid creating help.sh and test.sh in cwd (#35123)

When running unit-test the test/ci.py module is leaving
garbage (help.sh, test.sh files) in the current working
directory.

This commit changes the current working directory to a
temporary path before those files are created.
This commit is contained in:
Massimiliano Culpo 2023-01-24 12:42:15 +01:00 committed by GitHub
parent cd2d6a6397
commit 115b6b2a51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,9 @@
# Spack Project Developers. See the top-level COPYRIGHT file for details. # Spack Project Developers. See the top-level COPYRIGHT file for details.
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import itertools
import itertools as it
import os import os
import subprocess
import sys import sys
import pytest import pytest
@ -14,7 +14,7 @@
import spack.ci as ci import spack.ci as ci
import spack.ci_needs_workaround as cinw import spack.ci_needs_workaround as cinw
import spack.ci_optimization as ci_opt import spack.ci_optimization as ci_opt
import spack.config as cfg import spack.config
import spack.environment as ev import spack.environment as ev
import spack.error import spack.error
import spack.paths as spack_paths import spack.paths as spack_paths
@ -23,12 +23,16 @@
import spack.util.spack_yaml as syaml import spack.util.spack_yaml as syaml
@pytest.fixture
def repro_dir(tmp_path):
result = tmp_path / "repro_dir"
result.mkdir()
with fs.working_dir(str(tmp_path)):
yield result
def test_urlencode_string(): def test_urlencode_string():
s = "Spack Test Project" assert ci._url_encode_string("Spack Test Project") == "Spack+Test+Project"
s_enc = ci._url_encode_string(s)
assert s_enc == "Spack+Test+Project"
@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)") @pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)")
@ -54,16 +58,16 @@ def assert_present(config):
"install_missing_compilers" in config and config["install_missing_compilers"] is True "install_missing_compilers" in config and config["install_missing_compilers"] is True
) )
original_config = cfg.get("config") original_config = spack.config.get("config")
assert_missing(original_config) assert_missing(original_config)
ci.configure_compilers("FIND_ANY", scope="site") ci.configure_compilers("FIND_ANY", scope="site")
second_config = cfg.get("config") second_config = spack.config.get("config")
assert_missing(second_config) assert_missing(second_config)
ci.configure_compilers("INSTALL_MISSING") ci.configure_compilers("INSTALL_MISSING")
last_config = cfg.get("config") last_config = spack.config.get("config")
assert_present(last_config) assert_present(last_config)
@ -380,7 +384,7 @@ def make_yaml_obj(use_artifact_buildcache, optimize, use_dependencies):
use_artifact_buildcache=use_ab, optimize=False, use_dependencies=False use_artifact_buildcache=use_ab, optimize=False, use_dependencies=False
) )
for opt, deps in it.product(*(((False, True),) * 2)): for opt, deps in itertools.product(*(((False, True),) * 2)):
# neither optimizing nor converting needs->dependencies # neither optimizing nor converting needs->dependencies
if not (opt or deps): if not (opt or deps):
# therefore, nothing to test # therefore, nothing to test
@ -453,33 +457,24 @@ def test_affected_specs_on_first_concretization(mutable_mock_env_path, mock_pack
@pytest.mark.skipif( @pytest.mark.skipif(
sys.platform == "win32", reason="Reliance on bash script not supported on Windows" sys.platform == "win32", reason="Reliance on bash script not supported on Windows"
) )
def test_ci_process_command(tmpdir): def test_ci_process_command(repro_dir):
repro_dir = tmpdir.join("repro_dir").strpath result = ci.process_command("help", commands=[], repro_dir=str(repro_dir))
os.makedirs(repro_dir) help_sh = repro_dir / "help.sh"
result = ci.process_command("help", [], repro_dir) assert help_sh.exists() and not result
assert os.path.exists(fs.join_path(repro_dir, "help.sh"))
assert not result
@pytest.mark.skipif( @pytest.mark.skipif(
sys.platform == "win32", reason="Reliance on bash script not supported on Windows" sys.platform == "win32", reason="Reliance on bash script not supported on Windows"
) )
def test_ci_process_command_fail(tmpdir, monkeypatch): def test_ci_process_command_fail(repro_dir, monkeypatch):
import subprocess msg = "subprocess wait exception"
err = "subprocess wait exception"
def _fail(self, args): def _fail(self, args):
raise RuntimeError(err) raise RuntimeError(msg)
monkeypatch.setattr(subprocess.Popen, "__init__", _fail) monkeypatch.setattr(subprocess.Popen, "__init__", _fail)
with pytest.raises(RuntimeError, match=msg):
repro_dir = tmpdir.join("repro_dir").strpath ci.process_command("help", [], str(repro_dir))
os.makedirs(repro_dir)
with pytest.raises(RuntimeError, match=err):
ci.process_command("help", [], repro_dir)
def test_ci_create_buildcache(tmpdir, working_env, config, mock_packages, monkeypatch): def test_ci_create_buildcache(tmpdir, working_env, config, mock_packages, monkeypatch):
@ -513,16 +508,15 @@ def test_ci_run_standalone_tests_missing_requirements(
sys.platform == "win32", reason="Reliance on bash script not supported on Windows" sys.platform == "win32", reason="Reliance on bash script not supported on Windows"
) )
def test_ci_run_standalone_tests_not_installed_junit( def test_ci_run_standalone_tests_not_installed_junit(
tmpdir, working_env, default_mock_concretization, mock_test_stage, capfd tmp_path, repro_dir, working_env, default_mock_concretization, mock_test_stage, capfd
): ):
log_file = tmpdir.join("junit.xml").strpath log_file = tmp_path / "junit.xml"
args = { args = {
"log_file": log_file, "log_file": str(log_file),
"job_spec": default_mock_concretization("printing-package"), "job_spec": default_mock_concretization("printing-package"),
"repro_dir": tmpdir.join("repro_dir").strpath, "repro_dir": str(repro_dir),
"fail_fast": True, "fail_fast": True,
} }
os.makedirs(args["repro_dir"])
ci.run_standalone_tests(**args) ci.run_standalone_tests(**args)
err = capfd.readouterr()[1] err = capfd.readouterr()[1]
@ -534,16 +528,15 @@ def test_ci_run_standalone_tests_not_installed_junit(
sys.platform == "win32", reason="Reliance on bash script not supported on Windows" sys.platform == "win32", reason="Reliance on bash script not supported on Windows"
) )
def test_ci_run_standalone_tests_not_installed_cdash( def test_ci_run_standalone_tests_not_installed_cdash(
tmpdir, working_env, default_mock_concretization, mock_test_stage, capfd tmp_path, repro_dir, working_env, default_mock_concretization, mock_test_stage, capfd
): ):
"""Test run_standalone_tests with cdash and related options.""" """Test run_standalone_tests with cdash and related options."""
log_file = tmpdir.join("junit.xml").strpath log_file = tmp_path / "junit.xml"
args = { args = {
"log_file": log_file, "log_file": str(log_file),
"job_spec": default_mock_concretization("printing-package"), "job_spec": default_mock_concretization("printing-package"),
"repro_dir": tmpdir.join("repro_dir").strpath, "repro_dir": str(repro_dir),
} }
os.makedirs(args["repro_dir"])
# Cover when CDash handler provided (with the log file as well) # Cover when CDash handler provided (with the log file as well)
ci_cdash = { ci_cdash = {
@ -564,9 +557,9 @@ def test_ci_run_standalone_tests_not_installed_cdash(
assert "0 passed of 0" in out assert "0 passed of 0" in out
# copy test results (though none) # copy test results (though none)
artifacts_dir = tmpdir.join("artifacts") artifacts_dir = tmp_path / "artifacts"
fs.mkdirp(artifacts_dir.strpath) artifacts_dir.mkdir()
handler.copy_test_results(tmpdir.strpath, artifacts_dir.strpath) handler.copy_test_results(str(tmp_path), str(artifacts_dir))
err = capfd.readouterr()[1] err = capfd.readouterr()[1]
assert "Unable to copy files" in err assert "Unable to copy files" in err
assert "No such file or directory" in err assert "No such file or directory" in err