From 56fac81637bfa5258d1531ac41394d9cd7593bb6 Mon Sep 17 00:00:00 2001 From: Keith Harvey Date: Wed, 12 Nov 2025 12:37:13 +0000 Subject: [PATCH 1/2] feat: make loc optional in get_env --- src/envars/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/envars/main.py b/src/envars/main.py index 2b25664..dae5969 100644 --- a/src/envars/main.py +++ b/src/envars/main.py @@ -444,7 +444,7 @@ def _get_resolved_variables( return resolved_vars -def get_all_envs(loc: str, file_path: str = "envars.yml") -> dict: +def get_all_envs(loc: str | None, file_path: str = "envars.yml") -> dict: """Loads and resolves variables for all environments in a given location.""" manager = load_from_yaml(file_path) if loc is None: @@ -457,7 +457,7 @@ def get_all_envs(loc: str, file_path: str = "envars.yml") -> dict: return all_envs -def get_env(env: str, loc: str, file_path: str = "envars.yml") -> dict: +def get_env(env: str, loc: str | None = None, file_path: str = "envars.yml") -> dict: """Loads and resolves variables for a given environment and location.""" manager = load_from_yaml(file_path) if loc is None: From b18bd2f4a8495786ad84fd772a27313f262f23cf Mon Sep 17 00:00:00 2001 From: Keith Harvey Date: Wed, 12 Nov 2025 13:29:55 +0000 Subject: [PATCH 2/2] more fixes --- src/envars/main.py | 6 +++--- tests/test_main.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/envars/main.py b/src/envars/main.py index dae5969..04a8685 100644 --- a/src/envars/main.py +++ b/src/envars/main.py @@ -444,10 +444,10 @@ def _get_resolved_variables( return resolved_vars -def get_all_envs(loc: str | None, file_path: str = "envars.yml") -> dict: +def get_all_envs(loc: str | None = None, file_path: str = "envars.yml") -> dict: """Loads and resolves variables for all environments in a given location.""" manager = load_from_yaml(file_path) - if loc is None: + if loc is None and manager.locations: loc = get_default_location_name(manager) if loc is None: raise ValueError("Could not determine default location. Please specify with --loc.") @@ -460,7 +460,7 @@ def get_all_envs(loc: str | None, file_path: str = "envars.yml") -> dict: def get_env(env: str, loc: str | None = None, file_path: str = "envars.yml") -> dict: """Loads and resolves variables for a given environment and location.""" manager = load_from_yaml(file_path) - if loc is None: + if loc is None and manager.locations: loc = get_default_location_name(manager) if loc is None: raise ValueError("Could not determine default location. Please specify with --loc.") diff --git a/tests/test_main.py b/tests/test_main.py index 8cd9b12..b21033f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -307,6 +307,39 @@ def test_safe_loader_no_duplicates(tmp_path): assert data == {"key1": "value1", "key2": "value2"} +def test_get_env_without_locations_and_no_loc_arg(tmp_path): + """Test that get_env works correctly when no locations are configured and no --loc is provided.""" + yaml_content = """ +configuration: + environments: + - dev + +environment_variables: + MY_VAR: + default: "default_value" + ANOTHER_VAR: + dev: "dev_value" +""" + file_path = create_yaml_file(tmp_path, yaml_content) + manager = load_from_yaml(file_path) + assert not manager.locations # Ensure no locations are loaded + + # Test get_env + from src.envars.main import get_env + + resolved_vars = get_env(env="dev", file_path=file_path) + assert resolved_vars["MY_VAR"] == "default_value" + assert resolved_vars["ANOTHER_VAR"] == "dev_value" + + # Test get_all_envs + from src.envars.main import get_all_envs + + all_envs = get_all_envs(loc=None, file_path=file_path) + assert "dev" in all_envs + assert all_envs["dev"]["MY_VAR"] == "default_value" + assert all_envs["dev"]["ANOTHER_VAR"] == "dev_value" + + def test_resolve_cloudformation_export(tmp_path, monkeypatch): """Test that cloudformation_export: values are resolved correctly.""" yaml_content = """