Skip to content

Commit 6de2a70

Browse files
committed
rimport/test_get_staging_root: Use monkeypatch instead of patching os.environ.
1 parent a5b0be0 commit 6de2a70

1 file changed

Lines changed: 29 additions & 30 deletions

File tree

tests/rimport/test_get_staging_root.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import importlib.util
88
from importlib.machinery import SourceFileLoader
99
from pathlib import Path
10-
from unittest.mock import patch
1110

1211
import pytest
1312

@@ -48,62 +47,62 @@ def wrapper(*args, **kwargs):
4847
class TestGetStagingRoot:
4948
"""Test suite for get_staging_root() function."""
5049

51-
def test_returns_default_when_env_not_set(self):
50+
def test_returns_default_when_env_not_set(self, monkeypatch):
5251
"""Test that default staging root is returned when RIMPORT_STAGING is not set."""
5352
# Ensure RIMPORT_STAGING is not set
54-
with patch.dict(os.environ, {}, clear=True):
53+
monkeypatch.delenv("RIMPORT_STAGING", raising=False)
5554

56-
result = rimport.get_staging_root()
55+
result = rimport.get_staging_root()
5756

58-
assert result == rimport.DEFAULT_STAGING_ROOT
57+
assert result == rimport.DEFAULT_STAGING_ROOT
5958

60-
def test_returns_env_value_when_set(self, tmp_path):
59+
def test_returns_env_value_when_set(self, tmp_path, monkeypatch):
6160
"""Test that RIMPORT_STAGING environment variable is used when set."""
6261
custom_staging = tmp_path / "custom_staging"
6362
custom_staging.mkdir()
6463

65-
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
66-
result = rimport.get_staging_root()
64+
monkeypatch.setenv("RIMPORT_STAGING", str(custom_staging))
65+
result = rimport.get_staging_root()
6766

68-
assert result == custom_staging.resolve()
67+
assert result == custom_staging.resolve()
6968

70-
def test_expands_tilde_in_env_value(self):
69+
def test_expands_tilde_in_env_value(self, monkeypatch):
7170
"""Test that ~ is expanded in RIMPORT_STAGING value."""
7271
# Use a path with ~ that will be expanded
73-
with patch.dict(os.environ, {"RIMPORT_STAGING": "~/my_staging"}):
74-
result = rimport.get_staging_root()
72+
monkeypatch.setenv("RIMPORT_STAGING", "~/my_staging")
73+
result = rimport.get_staging_root()
7574

76-
# Should be expanded and resolved
77-
assert "~" not in str(result)
78-
assert result.is_absolute()
75+
# Should be expanded and resolved
76+
assert "~" not in str(result)
77+
assert result.is_absolute()
7978

80-
def test_resolves_relative_path_in_env_value(self):
79+
def test_resolves_relative_path_in_env_value(self, monkeypatch):
8180
"""Test that relative paths in RIMPORT_STAGING are resolved."""
8281
# Set a relative path
83-
with patch.dict(os.environ, {"RIMPORT_STAGING": "./staging"}):
84-
result = rimport.get_staging_root()
82+
monkeypatch.setenv("RIMPORT_STAGING", "./staging")
83+
result = rimport.get_staging_root()
8584

86-
# Should be resolved to absolute path
87-
assert result.is_absolute()
85+
# Should be resolved to absolute path
86+
assert result.is_absolute()
8887

89-
def test_env_value_with_spaces(self, tmp_path):
88+
def test_env_value_with_spaces(self, tmp_path, monkeypatch):
9089
"""Test handling of RIMPORT_STAGING with spaces in path."""
9190
custom_staging = tmp_path / "staging with spaces"
9291
custom_staging.mkdir()
9392

94-
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
95-
result = rimport.get_staging_root()
93+
monkeypatch.setenv("RIMPORT_STAGING", str(custom_staging))
94+
result = rimport.get_staging_root()
9695

97-
assert result == custom_staging.resolve()
96+
assert result == custom_staging.resolve()
9897

99-
def test_env_value_overrides_default(self, tmp_path):
98+
def test_env_value_overrides_default(self, tmp_path, monkeypatch):
10099
"""Test that RIMPORT_STAGING overrides the default value."""
101100
custom_staging = tmp_path / "override"
102101
custom_staging.mkdir()
103102

104-
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
105-
result = rimport.get_staging_root()
103+
monkeypatch.setenv("RIMPORT_STAGING", str(custom_staging))
104+
result = rimport.get_staging_root()
106105

107-
# Should NOT be the default
108-
assert result != rimport.DEFAULT_STAGING_ROOT
109-
assert result == custom_staging.resolve()
106+
# Should NOT be the default
107+
assert result != rimport.DEFAULT_STAGING_ROOT
108+
assert result == custom_staging.resolve()

0 commit comments

Comments
 (0)