Skip to content

Commit 534b969

Browse files
committed
Add tests for .from_yaml() for config and config option
1 parent 3711c43 commit 534b969

File tree

3 files changed

+141
-17
lines changed

3 files changed

+141
-17
lines changed

src/dependency_injector/providers.c

Lines changed: 72 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency_injector/providers.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ cdef class ConfigurationOption(Provider):
15951595

15961596
config_content = _resolve_config_env_markers(
15971597
config_content,
1598-
envs_required=envs_required,
1598+
envs_required=envs_required or self._is_strict_mode_enabled(),
15991599
)
16001600
config = yaml.load(config_content, loader)
16011601

@@ -2025,7 +2025,7 @@ cdef class Configuration(Object):
20252025

20262026
config_content = _resolve_config_env_markers(
20272027
config_content,
2028-
envs_required=envs_required,
2028+
envs_required=envs_required or self._is_strict_mode_enabled(),
20292029
)
20302030
config = yaml.load(config_content, loader)
20312031

tests/unit/providers/test_configuration_py2_py3.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def test_env_variable_interpolation(self):
905905
self.assertEqual(self.config.section1.value2(), 'test-path/path')
906906

907907
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
908-
def test_missing_envs(self):
908+
def test_missing_envs_not_required(self):
909909
del os.environ['CONFIG_TEST_ENV']
910910
del os.environ['CONFIG_TEST_PATH']
911911

@@ -930,6 +930,39 @@ def test_missing_envs(self):
930930
self.assertIsNone(self.config.section1.value1())
931931
self.assertEqual(self.config.section1.value2(), '/path')
932932

933+
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
934+
def test_missing_envs_required(self):
935+
with open(self.config_file, 'w') as config_file:
936+
config_file.write(
937+
'section:\n'
938+
' undefined: ${UNDEFINED}\n'
939+
)
940+
941+
with self.assertRaises(ValueError) as context:
942+
self.config.from_yaml(self.config_file, envs_required=True)
943+
944+
self.assertEqual(
945+
str(context.exception),
946+
'Missing required environment variable "UNDEFINED"',
947+
)
948+
949+
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
950+
def test_missing_envs_strict_mode(self):
951+
with open(self.config_file, 'w') as config_file:
952+
config_file.write(
953+
'section:\n'
954+
' undefined: ${UNDEFINED}\n'
955+
)
956+
957+
self.config.set_strict(True)
958+
with self.assertRaises(ValueError) as context:
959+
self.config.from_yaml(self.config_file)
960+
961+
self.assertEqual(
962+
str(context.exception),
963+
'Missing required environment variable "UNDEFINED"',
964+
)
965+
933966
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
934967
def test_option_missing_envs(self):
935968
del os.environ['CONFIG_TEST_ENV']
@@ -956,6 +989,39 @@ def test_option_missing_envs(self):
956989
self.assertIsNone(self.config.option.section1.value1())
957990
self.assertEqual(self.config.option.section1.value2(), '/path')
958991

992+
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
993+
def test_option_missing_envs_required(self):
994+
with open(self.config_file, 'w') as config_file:
995+
config_file.write(
996+
'section:\n'
997+
' undefined: ${UNDEFINED}\n'
998+
)
999+
1000+
with self.assertRaises(ValueError) as context:
1001+
self.config.option.from_yaml(self.config_file, envs_required=True)
1002+
1003+
self.assertEqual(
1004+
str(context.exception),
1005+
'Missing required environment variable "UNDEFINED"',
1006+
)
1007+
1008+
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
1009+
def test_option_missing_envs_strict_mode(self):
1010+
with open(self.config_file, 'w') as config_file:
1011+
config_file.write(
1012+
'section:\n'
1013+
' undefined: ${UNDEFINED}\n'
1014+
)
1015+
1016+
self.config.set_strict(True)
1017+
with self.assertRaises(ValueError) as context:
1018+
self.config.option.from_yaml(self.config_file)
1019+
1020+
self.assertEqual(
1021+
str(context.exception),
1022+
'Missing required environment variable "UNDEFINED"',
1023+
)
1024+
9591025
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
9601026
def test_default_values(self):
9611027
os.environ['DEFINED'] = 'defined'

0 commit comments

Comments
 (0)