Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/python/base_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
19 changes: 19 additions & 0 deletions tests/test_batteries_included_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading