Skip to content

Commit b1e06f7

Browse files
committed
Improve robustness of relink test imports.
1 parent a48ed44 commit b1e06f7

7 files changed

Lines changed: 38 additions & 29 deletions

tests/rimport/test_build_parser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"""
44

55
import os
6-
import sys
76
import argparse
8-
from pathlib import Path
97
import importlib.util
108
from importlib.machinery import SourceFileLoader
119

@@ -21,7 +19,7 @@
2119
if spec is None:
2220
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2321
rimport = importlib.util.module_from_spec(spec)
24-
sys.modules["rimport"] = rimport
22+
# Don't add to sys.modules to avoid conflict with other test files
2523
loader.exec_module(rimport)
2624

2725

tests/rimport/test_can_file_be_downloaded.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import sys
76
import importlib.util
87
from importlib.machinery import SourceFileLoader
98
from pathlib import Path
@@ -22,7 +21,7 @@
2221
if spec is None:
2322
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2423
rimport = importlib.util.module_from_spec(spec)
25-
sys.modules["rimport"] = rimport
24+
# Don't add to sys.modules to avoid conflict with other test files
2625
loader.exec_module(rimport)
2726

2827
RELPATH_THAT_DOES_EXIST = os.path.join(

tests/rimport/test_ensure_running_as.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import sys
76
import importlib.util
87
from importlib.machinery import SourceFileLoader
98
from unittest.mock import patch, MagicMock
@@ -20,7 +19,7 @@
2019
if spec is None:
2120
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2221
rimport = importlib.util.module_from_spec(spec)
23-
sys.modules["rimport"] = rimport
22+
# Don't add to sys.modules to avoid conflict with other test files
2423
loader.exec_module(rimport)
2524

2625

tests/rimport/test_get_staging_root.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import sys
76
import importlib.util
87
from importlib.machinery import SourceFileLoader
98
from pathlib import Path
@@ -20,7 +19,7 @@
2019
if spec is None:
2120
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2221
rimport = importlib.util.module_from_spec(spec)
23-
sys.modules["rimport"] = rimport
22+
# Don't add to sys.modules to avoid conflict with other test files
2423
loader.exec_module(rimport)
2524

2625

tests/rimport/test_normalize_paths.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import sys
76
import importlib.util
87
from importlib.machinery import SourceFileLoader
98
from pathlib import Path
@@ -20,7 +19,7 @@
2019
if spec is None:
2120
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2221
rimport = importlib.util.module_from_spec(spec)
23-
sys.modules["rimport"] = rimport
22+
# Don't add to sys.modules to avoid conflict with other test files
2423
loader.exec_module(rimport)
2524

2625

tests/rimport/test_read_filelist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import sys
76
import importlib.util
87
from importlib.machinery import SourceFileLoader
98

@@ -17,7 +16,7 @@
1716
if spec is None:
1817
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
1918
rimport = importlib.util.module_from_spec(spec)
20-
sys.modules["rimport"] = rimport
19+
# Don't add to sys.modules to avoid conflict with other test files
2120
loader.exec_module(rimport)
2221

2322

tests/rimport/test_stage_data.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"""
44

55
import os
6-
import sys
6+
import logging
77
import importlib.util
88
from importlib.machinery import SourceFileLoader
99
from unittest.mock import patch
1010

1111
import pytest
1212

13+
import shared
14+
1315
# Import rimport module from file without .py extension
1416
rimport_path = os.path.join(
1517
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
@@ -20,10 +22,19 @@
2022
if spec is None:
2123
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
2224
rimport = importlib.util.module_from_spec(spec)
23-
sys.modules["rimport"] = rimport
25+
# Don't add to sys.modules to avoid conflict with other test files
2426
loader.exec_module(rimport)
2527

2628

29+
@pytest.fixture(autouse=True)
30+
def configure_logging_for_tests():
31+
"""Configure logging for all tests in this module."""
32+
shared.configure_logging(logging.INFO)
33+
yield
34+
# Cleanup
35+
rimport.logger.handlers.clear()
36+
37+
2738
@pytest.fixture(name="inputdata_root")
2839
def fixture_inputdata_root(tmp_path):
2940
"""Create and return an inputdata root directory."""
@@ -119,8 +130,9 @@ def test_prints_live_symlink_already_published_not_downloadable(
119130
# Verify that shutil.copy2 was never called (function returned early)
120131
mock_copy.assert_not_called()
121132

133+
@patch.object(rimport, "can_file_be_downloaded")
122134
def test_prints_live_symlink_already_published_is_downloadable(
123-
self, inputdata_root, staging_root, caplog
135+
self, mock_can_file_be_downloaded, inputdata_root, staging_root, caplog
124136
):
125137
"""
126138
Like test_prints_live_symlink_already_published_not_downloadable, but mocks
@@ -132,15 +144,16 @@ def test_prints_live_symlink_already_published_is_downloadable(
132144
src = inputdata_root / "link.nc"
133145
src.symlink_to(real_file)
134146

147+
# Mock can_file_be_downloaded to return True
148+
mock_can_file_be_downloaded.return_value = True
149+
135150
# Mock shutil.copy2 to verify it's never called
136151
with patch("shutil.copy2") as mock_copy:
137-
# Mock can_file_be_downloaded to return True
138-
with patch("rimport.can_file_be_downloaded", return_value=True):
139-
# Should print message for live symlink and return early
140-
rimport.stage_data(src, inputdata_root, staging_root)
152+
# Should print message for live symlink and return early
153+
rimport.stage_data(src, inputdata_root, staging_root)
141154

142-
# Verify that shutil.copy2 was never called (function returned early)
143-
mock_copy.assert_not_called()
155+
# Verify that shutil.copy2 was never called (function returned early)
156+
mock_copy.assert_not_called()
144157

145158
# Verify the right messages were logged
146159
msg = "File is already published and linked"
@@ -152,8 +165,9 @@ def test_prints_live_symlink_already_published_is_downloadable(
152165
msg = "is already under staging directory"
153166
assert msg not in caplog.text
154167

168+
@patch.object(rimport, "can_file_be_downloaded")
155169
def test_prints_published_but_not_linked(
156-
self, inputdata_root, staging_root, caplog
170+
self, mock_can_file_be_downloaded, inputdata_root, staging_root, caplog
157171
):
158172
"""
159173
Tests printed message for when a file has been published (copied to staging root) but not
@@ -166,15 +180,17 @@ def test_prints_published_but_not_linked(
166180
inputdata = inputdata_root / filename
167181
inputdata.write_text("data")
168182

183+
# Mock can_file_be_downloaded to return True
184+
mock_can_file_be_downloaded.return_value = True
185+
169186
# Mock shutil.copy2 to verify it's never called
170187
with patch("shutil.copy2") as mock_copy:
171-
# Mock can_file_be_downloaded to return True
172-
with patch("rimport.can_file_be_downloaded", return_value=True):
173-
# Should print message for live symlink and return early
174-
rimport.stage_data(inputdata, inputdata_root, staging_root)
175188

176-
# Verify that shutil.copy2 was never called (function returned early)
177-
mock_copy.assert_not_called()
189+
# Should print message for live symlink and return early
190+
rimport.stage_data(inputdata, inputdata_root, staging_root)
191+
192+
# Verify that shutil.copy2 was never called (function returned early)
193+
mock_copy.assert_not_called()
178194

179195
# Verify the right messages were logged or not
180196
msg = "File is already published and linked"

0 commit comments

Comments
 (0)