|
2 | 2 |
|
3 | 3 | import unittest |
4 | 4 | import io |
| 5 | +import os |
5 | 6 |
|
6 | 7 | from isso import config |
7 | 8 |
|
@@ -34,3 +35,51 @@ def test_parser(self): |
34 | 35 | # Section.get() should function the same way as plain IssoParser |
35 | 36 | foosection = parser.section("foo") |
36 | 37 | self.assertEqual(foosection.get("password"), '%s%%foo') |
| 38 | + |
| 39 | + def test_parser_with_environment_variables(self): |
| 40 | + |
| 41 | + parser = config.IssoParser() |
| 42 | + parser.read_file(io.StringIO(""" |
| 43 | + [foo] |
| 44 | + bar = $TEST_ENV_VAR |
| 45 | + baz = ${TEST_ENV_VAR2} |
| 46 | + """)) |
| 47 | + |
| 48 | + # Set environment variables |
| 49 | + os.environ['TEST_ENV_VAR'] = 'test_value' |
| 50 | + os.environ['TEST_ENV_VAR2'] = 'another_test_value' |
| 51 | + |
| 52 | + # Test environment variable expansion |
| 53 | + self.assertEqual(parser.get("foo", "bar"), 'test_value') |
| 54 | + self.assertEqual(parser.get("foo", "baz"), 'another_test_value') |
| 55 | + |
| 56 | + # Test Section.get() with environment variables |
| 57 | + foosection = parser.section("foo") |
| 58 | + self.assertEqual(foosection.get("bar"), 'test_value') |
| 59 | + self.assertEqual(foosection.get("baz"), 'another_test_value') |
| 60 | + |
| 61 | + # Clean up environment variables |
| 62 | + del os.environ['TEST_ENV_VAR'] |
| 63 | + del os.environ['TEST_ENV_VAR2'] |
| 64 | + |
| 65 | + def test_parser_with_missing_environment_variables(self): |
| 66 | + |
| 67 | + parser = config.IssoParser() |
| 68 | + parser.read_file(io.StringIO(""" |
| 69 | + [foo] |
| 70 | + bar = $MISSING_ENV_VAR |
| 71 | + """)) |
| 72 | + |
| 73 | + self.assertEqual(parser.get("foo", "bar"), '$MISSING_ENV_VAR') |
| 74 | + |
| 75 | + def test_parser_with_special_characters_in_environment_variables(self): |
| 76 | + |
| 77 | + os.environ['SPECIAL_ENV_VAR'] = 'value_with_$pecial_characters' |
| 78 | + parser = config.IssoParser() |
| 79 | + parser.read_file(io.StringIO(""" |
| 80 | + [foo] |
| 81 | + bar = $SPECIAL_ENV_VAR |
| 82 | + """)) |
| 83 | + |
| 84 | + self.assertEqual(parser.get("foo", "bar"), 'value_with_$pecial_characters') |
| 85 | + del os.environ['SPECIAL_ENV_VAR'] |
0 commit comments