|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 | 5 | import os
|
| 6 | +import typing as t |
6 | 7 |
|
7 | 8 | from libtmux.test.environment import EnvironmentVarGuard
|
8 | 9 |
|
@@ -81,3 +82,70 @@ def _raise_error() -> None:
|
81 | 82 | # Test cleanup after exception
|
82 | 83 | assert "TEST_NEW_VAR" not in os.environ
|
83 | 84 | assert os.environ["TEST_EXISTING_VAR"] == "original_value"
|
| 85 | + |
| 86 | + |
| 87 | +def test_environment_var_guard_unset_and_reset() -> None: |
| 88 | + """Test unsetting and then resetting a variable.""" |
| 89 | + env = EnvironmentVarGuard() |
| 90 | + |
| 91 | + # Set up test variables |
| 92 | + os.environ["TEST_VAR1"] = "value1" |
| 93 | + os.environ["TEST_VAR2"] = "value2" |
| 94 | + |
| 95 | + # Unset a variable |
| 96 | + env.unset("TEST_VAR1") |
| 97 | + assert "TEST_VAR1" not in os.environ |
| 98 | + |
| 99 | + # Set it again with a different value |
| 100 | + env.set("TEST_VAR1", "new_value1") |
| 101 | + assert os.environ["TEST_VAR1"] == "new_value1" |
| 102 | + |
| 103 | + # Unset a variable that was previously set in this context |
| 104 | + env.set("TEST_VAR2", "new_value2") |
| 105 | + env.unset("TEST_VAR2") |
| 106 | + assert "TEST_VAR2" not in os.environ |
| 107 | + |
| 108 | + # Cleanup |
| 109 | + env.__exit__(None, None, None) |
| 110 | + assert os.environ["TEST_VAR1"] == "value1" |
| 111 | + assert os.environ["TEST_VAR2"] == "value2" |
| 112 | + |
| 113 | + |
| 114 | +def test_environment_var_guard_exit_with_exception() -> None: |
| 115 | + """Test __exit__ method with exception parameters.""" |
| 116 | + env = EnvironmentVarGuard() |
| 117 | + |
| 118 | + # Set up test variables |
| 119 | + os.environ["TEST_VAR"] = "original_value" |
| 120 | + env.set("TEST_VAR", "new_value") |
| 121 | + |
| 122 | + # Call __exit__ with exception parameters |
| 123 | + env.__exit__( |
| 124 | + t.cast("type[BaseException]", RuntimeError), |
| 125 | + RuntimeError("Test exception"), |
| 126 | + None, |
| 127 | + ) |
| 128 | + |
| 129 | + # Verify cleanup still happened |
| 130 | + assert os.environ["TEST_VAR"] == "original_value" |
| 131 | + |
| 132 | + |
| 133 | +def test_environment_var_guard_unset_previously_set() -> None: |
| 134 | + """Test unsetting a variable that was previously set in the same context.""" |
| 135 | + env = EnvironmentVarGuard() |
| 136 | + |
| 137 | + # Make sure the variable doesn't exist initially |
| 138 | + if "TEST_NEW_VAR" in os.environ: |
| 139 | + del os.environ["TEST_NEW_VAR"] |
| 140 | + |
| 141 | + # Set a new variable |
| 142 | + env.set("TEST_NEW_VAR", "new_value") |
| 143 | + assert "TEST_NEW_VAR" in os.environ |
| 144 | + assert os.environ["TEST_NEW_VAR"] == "new_value" |
| 145 | + |
| 146 | + # Now unset it - this should hit line 57 |
| 147 | + env.unset("TEST_NEW_VAR") |
| 148 | + assert "TEST_NEW_VAR" not in os.environ |
| 149 | + |
| 150 | + # No need to check after cleanup since the variable was never in the environment |
| 151 | + # before we started |
0 commit comments