From fd87e87cf8a80d8bf0124b4924863921f409037a Mon Sep 17 00:00:00 2001 From: Mohamed Zeidan Date: Mon, 5 Oct 2026 11:04:03 -0700 Subject: [PATCH] fix: stop test_sourcecode_args from depending on an ambient HOME test_environment_variables_in_command_still_expand compared the shell-expanded command against os.environ["HOME"], which fails with KeyError: 'HOME' on every Python version in CI. The unit tests run under tox, whose filtered environment does not pass HOME through, so the variable the test reads is simply absent. Export a variable the module owns instead, via an autouse fixture, and assert against its known value. The parametrized literal-argument cases that used $HOME/${HOME} now use the same variable, so they compare a literal against a non-empty expansion rather than against whatever the ambient environment happens to hold. Test-only change: the SourceCode.args behavior added for #5226 is untouched, and reintroducing the original double-quoted CMD= assignment still fails 23 of the 28 tests in this file, including both reworked cases. --- .../tests/unit/train/test_sourcecode_args.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/sagemaker-train/tests/unit/train/test_sourcecode_args.py b/sagemaker-train/tests/unit/train/test_sourcecode_args.py index a42ad9afae..d27260577f 100644 --- a/sagemaker-train/tests/unit/train/test_sourcecode_args.py +++ b/sagemaker-train/tests/unit/train/test_sourcecode_args.py @@ -55,6 +55,19 @@ max_wait_time_in_seconds=None, ) +# The shell-expansion assertions below need a variable that is guaranteed to be exported with a +# known, non-empty value, so this module sets one itself rather than reading the ambient +# environment. CI runs these tests under tox, whose filtered environment does not pass HOME +# through, so os.environ["HOME"] is not available here. +EXPANDABLE_VAR = "SM_TEST_SOURCECODE_ARGS_VALUE" +EXPANDABLE_VAR_VALUE = "expanded-by-the-shell" + + +@pytest.fixture(autouse=True) +def expandable_env_var(monkeypatch): + """Export a known variable for the tests that run the generated command block.""" + monkeypatch.setenv(EXPANDABLE_VAR, EXPANDABLE_VAR_VALUE) + @pytest.fixture(autouse=True) def modules_session(): @@ -237,8 +250,8 @@ def test_prepare_train_script_command_only_no_regression(): "value", [ "hello world", - "$HOME", - "${HOME}", + "$" + EXPANDABLE_VAR, + "${" + EXPANDABLE_VAR + "}", "$(id -u)", "`id -u`", 'a"b', @@ -301,12 +314,12 @@ def test_environment_variables_in_command_still_expand(): The heredoc stops expansion at *assignment* time only; ``eval`` still parses the command once, which is the behavior users of ``command`` rely on. """ - source_code = SourceCode(source_dir="scripts", command="capture_args $HOME") + source_code = SourceCode(source_dir="scripts", command=f"capture_args ${EXPANDABLE_VAR}") trainer = _make_trainer(source_code) script = _written_train_script(trainer, source_code) argv = _argv_from_running_base_command(script) - assert argv == [os.environ["HOME"]] + assert argv == [EXPANDABLE_VAR_VALUE] @requires_bash