diff --git a/pyconfigparser.py b/pyconfigparser.py index eb83556..a851a03 100644 --- a/pyconfigparser.py +++ b/pyconfigparser.py @@ -97,7 +97,7 @@ def _extract_env_variable_key(variable): return variable -_VARIABLE_PATTERN = r'\$([a-zA-Z][\w]+|\{[a-zA-Z][\w]+\})$' +_VARIABLE_PATTERN = r'^\$([a-zA-Z][\w]+|\{[a-zA-Z][\w]+\})$' _DEFAULT_CONFIG_FILES = ('config.json', 'config.yaml', 'config.yml') _ENTITY_NAME_PATTERN = r'^[a-zA-Z][\w]+$' _SUPPORTED_EXTENSIONS = { diff --git a/test_configparser.py b/test_configparser.py index 8530ef1..fd41ff5 100644 --- a/test_configparser.py +++ b/test_configparser.py @@ -1,4 +1,4 @@ -from pyconfigparser import ConfigParser, ConfigError, ConfigFileNotFoundError +from pyconfigparser import ConfigParser, ConfigError, ConfigFileNotFoundError, _is_variable from config.schemas import SIMPLE_SCHEMA_CONFIG, UNSUPPORTED_OBJECT_KEYS_SCHEMA import unittest import os @@ -92,6 +92,33 @@ def assign_a_bad_type_ignore_unsetted_env_vars(): self.assertIs(configparser.ignore_unset_env_vars, True) self.assertIsInstance(configparser.ignore_unset_env_vars, bool) + def test_variable_pattern_matching(self) -> None: + """Test the regex pattern for environment variable matching.""" + + valid_env_vars = [ + "$FOO", + "${FOO}", + "$My_Var123" + ] + + invalid_env_vars = [ + "FOO", # no $ + "$", # missing name + "${}", # empty braces + "$1VAR", # starts with number + "${1VAR}", # starts with number in braces + "foo$VAR", # not at start + "