From 7c94d5c3d28daa8413b67486ea7293ae3722d21b Mon Sep 17 00:00:00 2001 From: Zapta Date: Thu, 28 Nov 2024 20:56:04 -0800 Subject: [PATCH] Changed the scons code back to using the DefaultEnvironment. Now we just reset it in the tests before creating a new environment. This simplifies the code. --- apio/scons/scons_util.py | 37 ++++++++----------------- test/test_scons_util.py | 58 ++++------------------------------------ 2 files changed, 16 insertions(+), 79 deletions(-) diff --git a/apio/scons/scons_util.py b/apio/scons/scons_util.py index 11157979..55dc6d69 100644 --- a/apio/scons/scons_util.py +++ b/apio/scons/scons_util.py @@ -28,8 +28,8 @@ from SCons.Node import NodeList from SCons.Node.FS import File from SCons.Node.Alias import Alias -from SCons.Script import Environment from SCons.Script.SConscript import SConsEnvironment +from SCons.Script import DefaultEnvironment from SCons.Action import FunctionAction, Action from SCons.Builder import Builder import SCons.Defaults @@ -114,14 +114,20 @@ def is_windows(env: SConsEnvironment) -> bool: def create_construction_env(args: Dict[str, str]) -> SConsEnvironment: """Creates a scons env. Should be called very early in SConstruct.py""" - # Check that the default env is not used by mistake. SConstruct to use - # only our env. - check_default_scons_env_not_used() + # -- Make sure that the default environment doesn't exist, to make sure + # -- we create a fresh environment. This is important with pytest which + # -- can run multiple tests in the same python process. + # -- + # pylint: disable=protected-access + assert ( + SCons.Defaults._default_env is None + ), "DefaultEnvironment already exists" + # pylint: enable=protected-access # Create the env. We don't use the DefaultEnvironment (a singleton) to # allow the creation in pytest multiple test environments in the same # tesing process. - env: SConsEnvironment = Environment(ENV=os.environ, tools=[]) + env: SConsEnvironment = DefaultEnvironment(ENV=os.environ, tools=[]) # Add the args dictionary as a new ARGUMENTS var. assert env.get("ARGUMENTS") is None @@ -798,24 +804,3 @@ def set_up_cleanup(env: SConsEnvironment) -> None: # -- Associate all the files with the dummy target. env.Clean(dummy_target, files_to_clean) - - # -- Since the this function is called at the end of the SCOnstruct files, - # -- we use it to check that the SConstruct code didn't use by mistate - # -- the scons default env instead of the env we created in this file. - check_default_scons_env_not_used() - - -def check_default_scons_env_not_used(): - """A method to check that the default scons env has not been used. - We avoid using the default scons env because it's a singleton and thus, - don't play well with pytest where multiple indendepent scons envs - are created by tests in the same python process. This function asserts - that the default scons env was not instantiated by mistake, e.g. - by calling in SConstrut AllwaysBuild() instead of env.AlwaysBuild(). - """ - - # pylint: disable=protected-access - assert ( - SCons.Defaults._default_env is None - ), "Detected usage of scons default env." - # pylint: enable=protected-access diff --git a/test/test_scons_util.py b/test/test_scons_util.py index 3d4c3a2e..ccb8dca2 100644 --- a/test/test_scons_util.py +++ b/test/test_scons_util.py @@ -26,7 +26,6 @@ arg_bool, is_windows, force_colors, - check_default_scons_env_not_used, set_up_cleanup, map_params, fatal_error, @@ -63,9 +62,6 @@ def reset_scons_state() -> None: are running in the same process, we need to reset though variables before each test.""" - # -- We don't reset the _default_env, just make sure it was never used. - assert not SconsHacks.default_scons_env_exists() - # -- The Cons.Script.Main.OptionsParser variables contains the command # -- line options of scons. We reset them here and tests can access # -- them using SetOption() and GetOption(). @@ -83,33 +79,17 @@ def reset_scons_state() -> None: # -- Clear the SCons targets SCons.Environment.CleanTargets = {} - # -- Check again, just in case. - assert not SconsHacks.default_scons_env_exists() + # -- Reset the default scons env, if it exists. + # + # pylint: disable=protected-access + SCons.Defaults._default_env = None + # pylint: enable=protected-access @staticmethod def get_targets() -> Dict: """Get the scons {target -> dependencies} dictionary.""" return SCons.Environment.CleanTargets - @staticmethod - def default_scons_env_exists() -> bool: - """Tests if the default scons env was created. The default env is a - singleton that don't play well with pytest so we avoid it and use - instead an expclicit environment we create, and verify that the - default environement was not used by mistake, for example by - SConstruct calling AllwaysBuild() instead of env.AllwaysBuild(). - """ - # pylint: disable=protected-access - return SCons.Defaults._default_env is not None - # pylint: enable=protected-access - - @staticmethod - def clear_scons_default_env() -> None: - """Clear the scons default environment.""" - # pylint: disable=protected-access - SCons.Defaults._default_env = None - # pylint: enable=protected-access - def _make_test_env( args: Dict[str, str] = None, extra_args: Dict[str, str] = None @@ -134,9 +114,6 @@ def _make_test_env( # -- Use the apio's scons env creation function. env = create_construction_env(args) - # -- Check that the default scons env was not used. - check_default_scons_env_not_used() - return env @@ -264,31 +241,6 @@ def test_env_forcing_color(): assert not force_colors(env) -def test_default_env_check(): - """Teest that check_default_scons_env_not_used() throws an assertion - exception when the default scons env is used. - """ - # -- The starting point is having no default env. - assert not SconsHacks.default_scons_env_exists() - - # -- Should not throw an exception since the default env does not exist. - check_default_scons_env_not_used() - - # -- Create the scons default env. - SCons.Defaults.DefaultEnvironment(ENV={}, tools=[]) - assert SconsHacks.default_scons_env_exists() - - # -- Verify that the assertion in the function fails. - with pytest.raises(AssertionError) as e: - check_default_scons_env_not_used() - - # -- Verify the assertion text. - assert "Detected usage of scons default env" in str(e.value) - - # -- Restore the no-default-env state for the reset of the tests. - SconsHacks.clear_scons_default_env() - - def test_set_up_cleanup_ok(apio_runner: ApioRunner): """Tests the success path of set_up_cleanup()."""