Windows: fix "spack build-env" (#37923)
"spack build-env" was not generating proper environment variable definitions on Windows; this commit updates the generated commands to succeed with batch/PowerShell.
This commit is contained in:
parent
1c7af83d32
commit
d147ef231f
@ -3,6 +3,7 @@
|
|||||||
#
|
#
|
||||||
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
||||||
import pickle
|
import pickle
|
||||||
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -39,7 +40,10 @@ def test_dump(tmpdir):
|
|||||||
with tmpdir.as_cwd():
|
with tmpdir.as_cwd():
|
||||||
build_env("--dump", _out_file, "zlib")
|
build_env("--dump", _out_file, "zlib")
|
||||||
with open(_out_file) as f:
|
with open(_out_file) as f:
|
||||||
assert any(line.startswith("PATH=") for line in f.readlines())
|
if sys.platform == "win32":
|
||||||
|
assert any(line.startswith('set "PATH=') for line in f.readlines())
|
||||||
|
else:
|
||||||
|
assert any(line.startswith("PATH=") for line in f.readlines())
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("config", "mock_packages", "working_env")
|
@pytest.mark.usefixtures("config", "mock_packages", "working_env")
|
||||||
|
@ -119,7 +119,10 @@ def test_dump_environment(prepare_environment_for_tests, tmpdir):
|
|||||||
dumpfile_path = str(tmpdir.join("envdump.txt"))
|
dumpfile_path = str(tmpdir.join("envdump.txt"))
|
||||||
envutil.dump_environment(dumpfile_path)
|
envutil.dump_environment(dumpfile_path)
|
||||||
with open(dumpfile_path, "r") as dumpfile:
|
with open(dumpfile_path, "r") as dumpfile:
|
||||||
assert "TEST_ENV_VAR={0}; export TEST_ENV_VAR\n".format(test_paths) in list(dumpfile)
|
if sys.platform == "win32":
|
||||||
|
assert 'set "TEST_ENV_VAR={}"\n'.format(test_paths) in list(dumpfile)
|
||||||
|
else:
|
||||||
|
assert "TEST_ENV_VAR={0}; export TEST_ENV_VAR\n".format(test_paths) in list(dumpfile)
|
||||||
|
|
||||||
|
|
||||||
def test_reverse_environment_modifications(working_env):
|
def test_reverse_environment_modifications(working_env):
|
||||||
|
@ -171,7 +171,11 @@ def path_put_first(var_name: str, directories: List[Path]):
|
|||||||
BASH_FUNCTION_FINDER = re.compile(r"BASH_FUNC_(.*?)\(\)")
|
BASH_FUNCTION_FINDER = re.compile(r"BASH_FUNC_(.*?)\(\)")
|
||||||
|
|
||||||
|
|
||||||
def _env_var_to_source_line(var: str, val: str) -> str:
|
def _win_env_var_to_set_line(var: str, val: str) -> str:
|
||||||
|
return f'set "{var}={val}"'
|
||||||
|
|
||||||
|
|
||||||
|
def _nix_env_var_to_source_line(var: str, val: str) -> str:
|
||||||
if var.startswith("BASH_FUNC"):
|
if var.startswith("BASH_FUNC"):
|
||||||
source_line = "function {fname}{decl}; export -f {fname}".format(
|
source_line = "function {fname}{decl}; export -f {fname}".format(
|
||||||
fname=BASH_FUNCTION_FINDER.sub(r"\1", var), decl=val
|
fname=BASH_FUNCTION_FINDER.sub(r"\1", var), decl=val
|
||||||
@ -181,6 +185,13 @@ def _env_var_to_source_line(var: str, val: str) -> str:
|
|||||||
return source_line
|
return source_line
|
||||||
|
|
||||||
|
|
||||||
|
def _env_var_to_source_line(var: str, val: str) -> str:
|
||||||
|
if sys.platform == "win32":
|
||||||
|
return _win_env_var_to_set_line(var, val)
|
||||||
|
else:
|
||||||
|
return _nix_env_var_to_source_line(var, val)
|
||||||
|
|
||||||
|
|
||||||
@system_path_filter(arg_slice=slice(1))
|
@system_path_filter(arg_slice=slice(1))
|
||||||
def dump_environment(path: Path, environment: Optional[MutableMapping[str, str]] = None):
|
def dump_environment(path: Path, environment: Optional[MutableMapping[str, str]] = None):
|
||||||
"""Dump an environment dictionary to a source-able file.
|
"""Dump an environment dictionary to a source-able file.
|
||||||
|
Loading…
Reference in New Issue
Block a user