|
7 | 7 | import importlib.util |
8 | 8 | from importlib.machinery import SourceFileLoader |
9 | 9 | from pathlib import Path |
10 | | -from unittest.mock import patch |
11 | 10 |
|
12 | 11 | import pytest |
13 | 12 |
|
@@ -48,62 +47,62 @@ def wrapper(*args, **kwargs): |
48 | 47 | class TestGetStagingRoot: |
49 | 48 | """Test suite for get_staging_root() function.""" |
50 | 49 |
|
51 | | - def test_returns_default_when_env_not_set(self): |
| 50 | + def test_returns_default_when_env_not_set(self, monkeypatch): |
52 | 51 | """Test that default staging root is returned when RIMPORT_STAGING is not set.""" |
53 | 52 | # Ensure RIMPORT_STAGING is not set |
54 | | - with patch.dict(os.environ, {}, clear=True): |
| 53 | + monkeypatch.delenv("RIMPORT_STAGING", raising=False) |
55 | 54 |
|
56 | | - result = rimport.get_staging_root() |
| 55 | + result = rimport.get_staging_root() |
57 | 56 |
|
58 | | - assert result == rimport.DEFAULT_STAGING_ROOT |
| 57 | + assert result == rimport.DEFAULT_STAGING_ROOT |
59 | 58 |
|
60 | | - def test_returns_env_value_when_set(self, tmp_path): |
| 59 | + def test_returns_env_value_when_set(self, tmp_path, monkeypatch): |
61 | 60 | """Test that RIMPORT_STAGING environment variable is used when set.""" |
62 | 61 | custom_staging = tmp_path / "custom_staging" |
63 | 62 | custom_staging.mkdir() |
64 | 63 |
|
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() |
67 | 66 |
|
68 | | - assert result == custom_staging.resolve() |
| 67 | + assert result == custom_staging.resolve() |
69 | 68 |
|
70 | | - def test_expands_tilde_in_env_value(self): |
| 69 | + def test_expands_tilde_in_env_value(self, monkeypatch): |
71 | 70 | """Test that ~ is expanded in RIMPORT_STAGING value.""" |
72 | 71 | # 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() |
75 | 74 |
|
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() |
79 | 78 |
|
80 | | - def test_resolves_relative_path_in_env_value(self): |
| 79 | + def test_resolves_relative_path_in_env_value(self, monkeypatch): |
81 | 80 | """Test that relative paths in RIMPORT_STAGING are resolved.""" |
82 | 81 | # 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() |
85 | 84 |
|
86 | | - # Should be resolved to absolute path |
87 | | - assert result.is_absolute() |
| 85 | + # Should be resolved to absolute path |
| 86 | + assert result.is_absolute() |
88 | 87 |
|
89 | | - def test_env_value_with_spaces(self, tmp_path): |
| 88 | + def test_env_value_with_spaces(self, tmp_path, monkeypatch): |
90 | 89 | """Test handling of RIMPORT_STAGING with spaces in path.""" |
91 | 90 | custom_staging = tmp_path / "staging with spaces" |
92 | 91 | custom_staging.mkdir() |
93 | 92 |
|
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() |
96 | 95 |
|
97 | | - assert result == custom_staging.resolve() |
| 96 | + assert result == custom_staging.resolve() |
98 | 97 |
|
99 | | - def test_env_value_overrides_default(self, tmp_path): |
| 98 | + def test_env_value_overrides_default(self, tmp_path, monkeypatch): |
100 | 99 | """Test that RIMPORT_STAGING overrides the default value.""" |
101 | 100 | custom_staging = tmp_path / "override" |
102 | 101 | custom_staging.mkdir() |
103 | 102 |
|
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() |
106 | 105 |
|
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