From d960290efd29d870ada40ba91ea2e9b03f903f31 Mon Sep 17 00:00:00 2001 From: Ramesh Padmanabhaiah <22363102+codeforester@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:58:14 +0530 Subject: [PATCH] fix: preserve selected environment in config snapshots --- lib/python/base_cli/config.py | 7 +++++++ tests/test_batteries_included_config.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/python/base_cli/config.py b/lib/python/base_cli/config.py index 26db7a6..2b9519a 100644 --- a/lib/python/base_cli/config.py +++ b/lib/python/base_cli/config.py @@ -285,6 +285,13 @@ def load( ): _merge_mapping(merged, provenance, values, source) + if environment is not None: + # The caller selected this environment before loading its layers. + # Keep the snapshot and provenance aligned with that authoritative + # selection even when a base or environment file declares another + # lifecycle value. + _merge_mapping(merged, provenance, {"environment": selected_environment}, "command-line") + framework_values = {key: merged[key] for key in _FRAMEWORK_KEYS if key in merged} framework = _validate_framework_config(framework_values) consumer_config = {key: value for key, value in merged.items() if key not in _FRAMEWORK_KEYS} diff --git a/tests/test_batteries_included_config.py b/tests/test_batteries_included_config.py index 8854fd6..f65f03f 100644 --- a/tests/test_batteries_included_config.py +++ b/tests/test_batteries_included_config.py @@ -218,6 +218,25 @@ def main(ctx: base_cli.Context) -> None: self.assertEqual(seen["config"], {"answer": 42}) self.assertEqual(seen["provenance"]["answer"], "user:environment:prod") + def test_explicit_environment_selection_is_authoritative_in_snapshot(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + user_dir = root / "user-config" / "tool" + explicit = root / "explicit.yaml" + _write_yaml(user_dir / "config.yaml", "environment: dev\n") + _write_yaml(user_dir / "environments" / "prod.yaml", "answer: 42\n") + _write_yaml(explicit, "environment: staging\n") + + snapshot = BatteriesIncludedConfigLoader(user_config_dir=user_dir).load( + None, + explicit, + environment="prod", + ) + + self.assertEqual(snapshot.framework.environment, "prod") + self.assertEqual(snapshot.provenance["environment"], "command-line") + self.assertEqual(snapshot.config, {"answer": 42}) + def test_missing_optional_layers_are_empty_but_explicit_paths_are_strict(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir)