Add a cd test

This commit is contained in:
psakiev 2024-11-13 16:11:10 -07:00
parent 7a79fe88e2
commit 8c6773f33e
2 changed files with 36 additions and 1 deletions

View File

@ -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)

View File

@ -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"