Repository: janehmueller/python-json-config

Issue:
Environment variable prefix should only match those variables with an underscore after the prefix
E.g. with prefix `TEST` the following should happen
- match `TEST_VALUE`
- not match `TESTSUITE_VERSION`

Currently it matches both

Discussion:

Patch:
diff --git a/python_json_config/__version__.py b/python_json_config/__version__.py
index c68196d..a955fda 100644
--- a/python_json_config/__version__.py
+++ b/python_json_config/__version__.py
@@ -1,1 +1,1 @@
-__version__ = "1.2.0"
+__version__ = "1.2.1"
diff --git a/python_json_config/config_node.py b/python_json_config/config_node.py
index 9fb07b3..07a8d04 100644
--- a/python_json_config/config_node.py
+++ b/python_json_config/config_node.py
@@ -144,11 +144,9 @@ class ConfigNode(object):
         prefixes = [prefix] if isinstance(prefix, str) else prefix
         for key in os.environ:
             for prefix in prefixes:
-                if key.startswith(prefix):
+                if key.startswith(f"{prefix}_"):
                     value = os.environ[key]
-                    cleaned_key = key[len(prefix):]
-                    if cleaned_key[0] == "_":
-                        cleaned_key = cleaned_key[1:]
+                    cleaned_key = key[len(prefix) + 1:]
                     cleaned_key = cleaned_key.lower().split("_")
                     self.update(path=cleaned_key, value=value, upsert=True)
 


Test patch:
diff --git a/tests/config_builder_test.py b/tests/config_builder_test.py
index 6a6030d..e01fa54 100644
--- a/tests/config_builder_test.py
+++ b/tests/config_builder_test.py
@@ -1,5 +1,4 @@
 import json
-import os
 
 import pytest
 from jsonschema import ValidationError
@@ -168,21 +167,15 @@ def test_optional_validation(path):
 
 
 def test_merge_env_variable():
-    prefix = "PYTHONJSONCONFIG"
-    variables = {f"{prefix}_TESTVALUE1": "bla", f"{prefix}_TESTVALUE2": "1"}
-    for key, value in variables.items():
-        os.environ[key] = value
-
+    prefix1 = "PYTHONJSONCONFIG"
+    prefixes2 = ["PREFIX1", "PREFIX2"]
     builder = ConfigBuilder()
-    builder.merge_with_env_variables(prefix)
-    config = builder.parse_config({"testvalue1": "blub", "testvalue3": 5})
 
-    assert config.testvalue1 == "bla"
-    assert config.testvalue2 == "1"
-    assert config.testvalue3 == 5
+    builder.merge_with_env_variables(prefix1)
+    assert builder._ConfigBuilder__environment_variable_prefixes == [prefix1]
 
-    for key, value in variables.items():
-        del os.environ[key]
+    builder.merge_with_env_variables(prefixes2)
+    assert builder._ConfigBuilder__environment_variable_prefixes == [prefix1] + prefixes2
 
 
 def test_build_from_serialized_config(path):
diff --git a/tests/config_node/test_crud.py b/tests/config_node/test_crud.py
index 503e4c1..16737a7 100644
--- a/tests/config_node/test_crud.py
+++ b/tests/config_node/test_crud.py
@@ -142,12 +142,16 @@ def test_merge_env_variable():
     for key, value in variables.items():
         os.environ[key] = value
 
+    invalid_prefix = "PYTHONJSONCONFIGTEST"
+    os.environ[invalid_prefix] = "invalid"
+
     config = ConfigNode({"testvalue1": "blub", "testvalue3": 5})
     config.merge_with_env_variables(prefix)
 
     assert config.testvalue1 == "bla"
     assert config.testvalue2 == "1"
     assert config.testvalue3 == 5
+    assert "test" not in config
 
     for key, value in variables.items():
         del os.environ[key]