33"""
44
55import os
6- import sys
6+ import logging
77import importlib .util
88from importlib .machinery import SourceFileLoader
99from unittest .mock import patch
1010
1111import pytest
1212
13+ import shared
14+
1315# Import rimport module from file without .py extension
1416rimport_path = os .path .join (
1517 os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))),
2022if spec is None :
2123 raise ImportError (f"Could not create spec for rimport from { rimport_path } " )
2224rimport = 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
2426loader .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" )
2839def 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