Skip to content

Commit f17d810

Browse files
committed
test_open_dataset_with_fallback that failed on Windows with a PermissionError. The temporary file was still open during deletion
1 parent de22fb9 commit f17d810

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

test/core/test_api.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import xarray as xr
77
from unittest.mock import patch
88
from uxarray.core.utils import _open_dataset_with_fallback
9+
import os
910

1011
def test_open_geoflow_dataset(gridpath, datasetpath):
1112
"""Loads a single dataset with its grid topology file using uxarray's
@@ -121,13 +122,16 @@ def test_open_dataset_grid_kwargs(gridpath, datasetpath):
121122
def test_open_dataset_with_fallback():
122123
"""Test that the fallback mechanism works when the default engine fails."""
123124

124-
# Create a simple test dataset
125-
with tempfile.NamedTemporaryFile(suffix='.nc', delete=False) as tmp:
126-
data = xr.Dataset({'temp': (['x', 'y'], np.random.rand(5, 5))})
127-
data.to_netcdf(tmp.name)
128-
tmp_path = tmp.name
129-
125+
tmp_path = ""
126+
ds = None
127+
ds_fallback = None
130128
try:
129+
# Create a simple test dataset
130+
with tempfile.NamedTemporaryFile(suffix='.nc', delete=False) as tmp:
131+
data = xr.Dataset({'temp': (['x', 'y'], np.random.rand(5, 5))})
132+
data.to_netcdf(tmp.name)
133+
tmp_path = tmp.name
134+
131135
# Test normal case
132136
ds = _open_dataset_with_fallback(tmp_path)
133137
assert isinstance(ds, xr.Dataset)
@@ -149,5 +153,9 @@ def mock_open_dataset(*args, **kwargs):
149153
assert call_count == 2 # First failed, second succeeded
150154

151155
finally:
152-
import os
153-
os.unlink(tmp_path)
156+
if ds is not None:
157+
ds.close()
158+
if ds_fallback is not None:
159+
ds_fallback.close()
160+
if os.path.exists(tmp_path):
161+
os.unlink(tmp_path)

0 commit comments

Comments
 (0)