@@ -692,6 +692,31 @@ def test_parse_config_different_tag_error(self):
692692 in str (ce .exception .problem )
693693 )
694694
695+ def test_parse_config_only_tag_env_vars_resolved (self ):
696+ os .environ [self .env_var1 ] = 'it works!'
697+ os .environ [self .env_var2 ] = 'this works too!'
698+ test_data = '''
699+ test1:
700+ data0: !TEST2 test1${ENV_TAG1}
701+ data1: ${ENV_TAG2}
702+ '''
703+
704+ expected = {
705+ 'test1' : {
706+ 'data0' : f'test1{ os .environ [self .env_var1 ]} ' ,
707+ 'data1' : '${ENV_TAG2}'
708+ }
709+ }
710+
711+ # because None is used as a tag in one of the tests, it messes up expected behavior
712+ # remove None from implicit resolvers:
713+ if None in yaml .SafeLoader .yaml_implicit_resolvers :
714+ del yaml .SafeLoader .yaml_implicit_resolvers [None ]
715+
716+ # only ENV_TAG1 should be parsed because of !TEST2 tag
717+ result = parse_config (data = test_data , tag = '!TEST2' )
718+ self .assertDictEqual (result , expected )
719+
695720 def test_parse_config_no_tag_all_resolved (self ):
696721 os .environ [self .env_var1 ] = 'it works!'
697722 os .environ [self .env_var2 ] = 'this works too!'
@@ -704,9 +729,34 @@ def test_parse_config_no_tag_all_resolved(self):
704729 expected = {
705730 'test1' : {
706731 'data0' : f'test1{ os .environ [self .env_var1 ]} ' ,
707- 'data1' : os . environ [ self . env_var2 ]
732+ 'data1' : 'this works too!'
708733 }
709734 }
735+ # all environment variables will be parsed
710736 result = parse_config (data = test_data , tag = None )
737+ self .assertDictEqual (result , expected )
738+
739+ self .assertDictEqual (result , expected )
711740
712- self .assertDictEqual (result , expected )
741+ def test_numeric_values_with_type_defined (self ):
742+ os .environ [self .env_var1 ] = "1024"
743+
744+ test_data = '''
745+ data0: !TAG ${ENV_TAG1}
746+ data1: !TAG tag:yaml.org,2002:float ${ENV_TAG2:27017}
747+ data2: !!float 1024
748+ data3: !TAG ${ENV_TAG2:some_value}
749+ data4: !TAG tag:yaml.org,2002:bool ${ENV_TAG2:false}
750+ '''
751+ config = parse_config (data = test_data , tag = '!TAG' )
752+ print (config )
753+ self .assertIsInstance (config ['data2' ], float )
754+ self .assertIsInstance (config ['data3' ], str )
755+ self .assertIsInstance (config ['data1' ], float )
756+ self .assertIsInstance (config ['data4' ], bool )
757+ self .assertIsInstance (config ['data0' ], str )
758+ self .assertEqual (config ['data0' ], os .environ [self .env_var1 ])
759+ self .assertEqual (config ['data2' ], float (os .environ [self .env_var1 ]))
760+ self .assertEqual (config ['data1' ], 27017.0 )
761+ self .assertEqual (config ['data3' ], "some_value" )
762+ self .assertEqual (config ['data4' ], False )
0 commit comments