Skip to content

Commit 09882b1

Browse files
committed
fix: use explicit env var set/restore in user agent tests
1 parent c05c437 commit 09882b1

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

tests/test_user_agent.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ def test_user_agent_without_integration(
1919
self, mock_python_version, mock_machine, mock_release, mock_system
2020
):
2121
"""Test the User-Agent string without specifying an integration method."""
22-
if "RUNPOD_UA_INTEGRATION" in os.environ:
23-
del os.environ["RUNPOD_UA_INTEGRATION"]
22+
saved = {k: os.environ.pop(k) for k in ("RUNPOD_UA_INTEGRATION", "CLAUDECODE") if k in os.environ}
2423

2524
expected_ua = f"RunPod-Python-SDK/{runpod_version} (Windows 10; AMD64) Language/Python 3.8.10" # pylint: disable=line-too-long
2625
self.assertEqual(construct_user_agent(), expected_ua)
2726

27+
os.environ.update(saved)
28+
2829
assert mock_python_version.called
2930
assert mock_machine.called
3031
assert mock_release.called
@@ -34,16 +35,19 @@ def test_user_agent_without_integration(
3435
@patch("runpod.user_agent.platform.release", return_value="5.4")
3536
@patch("runpod.user_agent.platform.machine", return_value="x86_64")
3637
@patch("runpod.user_agent.platform.python_version", return_value="3.9.5")
37-
@patch.dict(os.environ, {"RUNPOD_UA_INTEGRATION": "SkyPilot"})
3838
def test_user_agent_with_integration(
3939
self, mock_python_version, mock_machine, mock_release, mock_system
4040
):
4141
"""Test the User-Agent string with an integration method specified."""
42-
expected_ua = f"RunPod-Python-SDK/{runpod_version} (Linux 5.4; x86_64) Language/Python 3.9.5 Integration/SkyPilot" # pylint: disable=line-too-long
43-
42+
saved_claude = os.environ.pop("CLAUDECODE", None)
4443
os.environ["RUNPOD_UA_INTEGRATION"] = "SkyPilot"
44+
45+
expected_ua = f"RunPod-Python-SDK/{runpod_version} (Linux 5.4; x86_64) Language/Python 3.9.5 Integration/SkyPilot" # pylint: disable=line-too-long
4546
self.assertEqual(construct_user_agent(), expected_ua)
47+
4648
os.environ.pop("RUNPOD_UA_INTEGRATION")
49+
if saved_claude is not None:
50+
os.environ["CLAUDECODE"] = saved_claude
4751

4852
assert mock_python_version.called
4953
assert mock_machine.called
@@ -55,16 +59,16 @@ def test_user_agent_with_integration(
5559
@patch("runpod.user_agent.platform.release", return_value="5.4")
5660
@patch("runpod.user_agent.platform.machine", return_value="x86_64")
5761
@patch("runpod.user_agent.platform.python_version", return_value="3.9.5")
58-
@patch.dict(os.environ, {"CLAUDECODE": "1"}, clear=False)
5962
def test_user_agent_with_claude_code(
6063
self, mock_python_version, mock_machine, mock_release, mock_system
6164
):
6265
"""Test the User-Agent string includes claude-code agent tag."""
63-
if "RUNPOD_UA_INTEGRATION" in os.environ:
64-
del os.environ["RUNPOD_UA_INTEGRATION"]
66+
os.environ.pop("RUNPOD_UA_INTEGRATION", None)
67+
os.environ["CLAUDECODE"] = "1"
6568

6669
expected_ua = f"RunPod-Python-SDK/{runpod_version} (Linux 5.4; x86_64) Language/Python 3.9.5 (via claude-code)"
6770
self.assertEqual(construct_user_agent(), expected_ua)
71+
os.environ.pop("CLAUDECODE", None)
6872

6973
@patch("runpod.user_agent.platform.system", return_value="Linux")
7074
@patch("runpod.user_agent.platform.release", return_value="5.4")
@@ -74,13 +78,13 @@ def test_user_agent_without_claude_code(
7478
self, mock_python_version, mock_machine, mock_release, mock_system
7579
):
7680
"""Test the User-Agent string excludes agent tag when env var is not set."""
77-
for key in ("RUNPOD_UA_INTEGRATION", "CLAUDECODE"):
78-
if key in os.environ:
79-
del os.environ[key]
81+
saved = {k: os.environ.pop(k) for k in ("RUNPOD_UA_INTEGRATION", "CLAUDECODE") if k in os.environ}
8082

8183
ua = construct_user_agent()
8284
self.assertNotIn("via claude-code", ua)
8385

86+
os.environ.update(saved)
87+
8488

8589
if __name__ == "__main__":
8690
unittest.main()

0 commit comments

Comments
 (0)