From 8c6773f33e0f3acac9db1470b16dcc49d3428216 Mon Sep 17 00:00:00 2001 From: psakiev Date: Wed, 13 Nov 2024 16:11:10 -0700 Subject: [PATCH] Add a cd test --- lib/spack/spack/cmd/common/env_utility.py | 8 ++++++- lib/spack/spack/test/cmd/build_env.py | 29 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/spack/spack/cmd/common/env_utility.py b/lib/spack/spack/cmd/common/env_utility.py index eba4d5661b2..f78d4742a70 100644 --- a/lib/spack/spack/cmd/common/env_utility.py +++ b/lib/spack/spack/cmd/common/env_utility.py @@ -100,7 +100,13 @@ def run_command_in_subshell( if cd_arg: prefix = "-" if len(cd_arg) == 1 else "--" - location = location_emulator(f"{prefix}{cd_arg}", f"/{spec.dag_hash()}") + loc_args = [f"{prefix}{cd_arg}"] + + # don't add spec for cd if using env since spec hash is not the env + if not (cd_arg == "e" or cd_arg == "env"): + loc_args.append(f"/{spec.dag_hash()}") + + location = location_emulator(*loc_args) os.chdir(location) os.execvp(cmd[0], cmd) diff --git a/lib/spack/spack/test/cmd/build_env.py b/lib/spack/spack/test/cmd/build_env.py index 545981bd4af..ccd8bb5bdcf 100644 --- a/lib/spack/spack/test/cmd/build_env.py +++ b/lib/spack/spack/test/cmd/build_env.py @@ -2,13 +2,18 @@ # Spack Project Developers. See the top-level COPYRIGHT file for details. # # SPDX-License-Identifier: (Apache-2.0 OR MIT) +import os import pickle +import subprocess import sys import pytest import spack.error +from spack.cmd.common.env_utility import run_command_in_subshell +from spack.context import Context from spack.main import SpackCommand +from spack.spec import Spec build_env = SpackCommand("build-env") @@ -58,6 +63,30 @@ def test_pickle(tmpdir): assert "PATH" in environment +# TODO params [i, b, c] require a spec that has proceeded with a directory +# TODO praram [e] requires an active env +@pytest.mark.parametrize("cd_key", ["r", "spack-root"]) +@pytest.mark.usefixtures("config", "mock_packages", "working_env") +def test_cd(cd_key, tmpdir, monkeypatch, capfd): + """test that a subshell will navigate using spack cd before running commands""" + cmd = "pwd" if sys.platform != "win32" else "Get-Location" + def mock_execvp(_, args): + """os.execvp will kill take over the pytest process when it is successful""" + result = subprocess.check_output(args, universal_newlines=True) + print(result) + + with tmpdir.as_cwd(): + monkeypatch.setattr(os, "execvp", mock_execvp) + + pwd = os.getcwd() + spec = Spec("zlib").concretized() + run_command_in_subshell(spec, Context.BUILD, [cmd], cd_arg=cd_key) + + output = capfd.readouterr() + assert pwd not in output.out + assert output.err == "" + + def test_failure_when_uninstalled_deps(config, mock_packages): with pytest.raises( spack.error.SpackError, match="Not all dependencies of dttop are installed"