Fix spack -c <override> when env active (#47403)

Set command line scopes last in _main, so they are higher scopes

Restore the global configuration in a spawned process by inspecting
the result of ctx.get_start_method()

Add the ability to pass a mp.context to PackageInstallContext.

Add shell-tests to check overriding the configuration:
- Using both -c and -C from command line
- With and without an environment active
This commit is contained in:
Massimiliano Culpo
2024-11-06 17:18:58 +01:00
committed by GitHub
parent ee2723dc46
commit e62cf9c45b
6 changed files with 94 additions and 46 deletions

View File

@@ -0,0 +1,36 @@
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
"""Used to test correct application of config line scopes in various cases.
The option `config:cache` is supposed to be False, and overridden to True
from the command line.
"""
import multiprocessing as mp
import spack.config
import spack.subprocess_context
def show_config(serialized_state):
_ = serialized_state.restore()
result = spack.config.CONFIG.get("config:ccache")
if result is not True:
raise RuntimeError(f"Expected config:ccache:true, but got {result}")
if __name__ == "__main__":
print("Testing spawn")
ctx = mp.get_context("spawn")
serialized_state = spack.subprocess_context.PackageInstallContext(None, ctx=ctx)
p = ctx.Process(target=show_config, args=(serialized_state,))
p.start()
p.join()
print("Testing fork")
ctx = mp.get_context("fork")
serialized_state = spack.subprocess_context.PackageInstallContext(None, ctx=ctx)
p = ctx.Process(target=show_config, args=(serialized_state,))
p.start()
p.join()

View File

@@ -52,7 +52,7 @@ if [[ "$UNIT_TEST_COVERAGE" != "true" ]] && python -m pytest -VV 2>&1 | grep xdi
fi
# We are running pytest-cov after the addition of pytest-xdist, since it integrates
# other pugins for pytest automatically. We still need to use "coverage" explicitly
# other plugins for pytest automatically. We still need to use "coverage" explicitly
# for the commands above.
#
# There is a need to pass the configuration file explicitly due to a bug:

View File

@@ -207,3 +207,20 @@ fails spack env deactivate
echo "Correct error exit codes for unit-test when it fails"
fails spack unit-test fail
title "Testing config override from command line, outside of an environment"
contains 'True' spack -c config:ccache:true python -c "import spack.config;print(spack.config.CONFIG.get('config:ccache'))"
contains 'True' spack -C "$SHARE_DIR/qa/configuration" python -c "import spack.config;print(spack.config.CONFIG.get('config:ccache'))"
succeeds spack -c config:ccache:true python "$SHARE_DIR/qa/config_state.py"
succeeds spack -C "$SHARE_DIR/qa/configuration" python "$SHARE_DIR/qa/config_state.py"
title "Testing config override from command line, inside an environment"
spack env activate --temp
spack config add "config:ccache:false"
contains 'True' spack -c config:ccache:true python -c "import spack.config;print(spack.config.CONFIG.get('config:ccache'))"
contains 'True' spack -C "$SHARE_DIR/qa/configuration" python -c "import spack.config;print(spack.config.CONFIG.get('config:ccache'))"
succeeds spack -c config:ccache:true python "$SHARE_DIR/qa/config_state.py"
succeeds spack -C "$SHARE_DIR/qa/configuration" python "$SHARE_DIR/qa/config_state.py"
spack env deactivate