1616import unittest
1717from unittest .mock import patch
1818
19+ import yaml
20+
1921from opentelemetry .sdk ._configuration .file import (
2022 EnvSubstitutionError ,
2123 substitute_env_vars ,
@@ -123,8 +125,6 @@ def test_newline_in_value_prevents_yaml_injection(self):
123125 environment variables." A value like "legit\\ nmalicious_key: val"
124126 must be emitted as a quoted scalar, not raw YAML.
125127 """
126- import yaml
127-
128128 with patch .dict (
129129 os .environ ,
130130 {"SERVICE_NAME" : "legit-service\n malicious_key: injected_value" },
@@ -138,34 +138,24 @@ def test_newline_in_value_prevents_yaml_injection(self):
138138
139139 def test_newline_in_value_preserved_as_literal (self ):
140140 """Newline within a value is preserved as a literal newline character."""
141- import yaml
142-
143141 with patch .dict (os .environ , {"MULTI" : "line1\n line2" }):
144142 result = substitute_env_vars ("key: ${MULTI}" )
145143 parsed = yaml .safe_load (result )
146144 self .assertEqual (parsed ["key" ], "line1\n line2" )
147145
148146 def test_carriage_return_in_value_is_escaped (self ):
149147 """Carriage return in value is escaped, not injected."""
150- import yaml
151-
152148 with patch .dict (os .environ , {"VAL" : "text\r \n more" }):
153149 result = substitute_env_vars ("key: ${VAL}" )
154150 parsed = yaml .safe_load (result )
155151 self .assertIsInstance (parsed ["key" ], str )
156152
157153 def test_type_coercion_preserved_for_simple_values (self ):
158154 """Simple values without newlines still undergo YAML type coercion per spec."""
159- import yaml
160-
161- with patch .dict (
162- os .environ , {"BOOL_VAL" : "true" , "INT_VAL" : "42" }
163- ):
155+ with patch .dict (os .environ , {"BOOL_VAL" : "true" , "INT_VAL" : "42" }):
164156 bool_result = yaml .safe_load (
165157 substitute_env_vars ("key: ${BOOL_VAL}" )
166158 )
167- int_result = yaml .safe_load (
168- substitute_env_vars ("key: ${INT_VAL}" )
169- )
159+ int_result = yaml .safe_load (substitute_env_vars ("key: ${INT_VAL}" ))
170160 self .assertIs (bool_result ["key" ], True )
171161 self .assertEqual (int_result ["key" ], 42 )
0 commit comments