@@ -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