Skip to content

Commit bb24a80

Browse files
committed
test(environment): improve test coverage to 100%
- Add test for unsetting previously set variables - Add test for __exit__ with exception parameters - Fix line length issues in random.py - Fix Self type imports in random.py
1 parent d661ecf commit bb24a80

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/libtmux/test/random.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def get_test_session_name(server: Server, prefix: str = TEST_SESSION_PREFIX) ->
7979
'libtmux_...'
8080
8181
Never the same twice:
82-
>>> get_test_session_name(server=server) != get_test_session_name(server=server) # pragma: no cover
82+
>>> get_test_session_name(server=server) != \
83+
... get_test_session_name(server=server) # pragma: no cover
8384
True
8485
"""
8586
while True:
@@ -118,7 +119,8 @@ def get_test_window_name(
118119
119120
Never the same twice:
120121
121-
>>> get_test_window_name(session=session) != get_test_window_name(session=session) # pragma: no cover
122+
>>> get_test_window_name(session=session) != \
123+
... get_test_window_name(session=session) # pragma: no cover
122124
True
123125
"""
124126
assert prefix is not None

tests/test/test_environment.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import os
6+
import typing as t
67

78
from libtmux.test.environment import EnvironmentVarGuard
89

@@ -81,3 +82,70 @@ def _raise_error() -> None:
8182
# Test cleanup after exception
8283
assert "TEST_NEW_VAR" not in os.environ
8384
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

Comments
 (0)