Skip to content

Commit b57c79d

Browse files
committed
rimport: Add tests of get_staging_root().
1 parent ef8128a commit b57c79d

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
Tests for get_staging_root() function in rimport script.
3+
"""
4+
5+
import os
6+
import sys
7+
import importlib.util
8+
from importlib.machinery import SourceFileLoader
9+
from pathlib import Path
10+
from unittest.mock import patch
11+
12+
import pytest
13+
14+
# Import rimport module from file without .py extension
15+
rimport_path = os.path.join(
16+
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
17+
"rimport",
18+
)
19+
loader = SourceFileLoader("rimport", rimport_path)
20+
spec = importlib.util.spec_from_loader("rimport", loader)
21+
if spec is None:
22+
raise ImportError(f"Could not create spec for rimport from {rimport_path}")
23+
rimport = importlib.util.module_from_spec(spec)
24+
sys.modules["rimport"] = rimport
25+
loader.exec_module(rimport)
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def verify_returns_path():
30+
"""Fixture that verifies get_staging_root always returns a Path object."""
31+
# Store the original function
32+
original_func = rimport.get_staging_root
33+
34+
def wrapper(*args, **kwargs):
35+
result = original_func(*args, **kwargs)
36+
assert isinstance(
37+
result, Path
38+
), f"get_staging_root should return Path, got {type(result)}"
39+
return result
40+
41+
# Temporarily replace the function
42+
rimport.get_staging_root = wrapper
43+
yield
44+
# Restore original function
45+
rimport.get_staging_root = original_func
46+
47+
48+
class TestGetStagingRoot:
49+
"""Test suite for get_staging_root() function."""
50+
51+
def test_returns_default_when_env_not_set(self):
52+
"""Test that default staging root is returned when RIMPORT_STAGING is not set."""
53+
# Ensure RIMPORT_STAGING is not set
54+
with patch.dict(os.environ, {}, clear=True):
55+
56+
result = rimport.get_staging_root()
57+
58+
expected = Path(
59+
"/glade/campaign/collections/gdex/data/d651077/cesmdata/inputdata"
60+
)
61+
assert result == expected
62+
63+
def test_returns_env_value_when_set(self, tmp_path):
64+
"""Test that RIMPORT_STAGING environment variable is used when set."""
65+
custom_staging = tmp_path / "custom_staging"
66+
custom_staging.mkdir()
67+
68+
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
69+
result = rimport.get_staging_root()
70+
71+
assert result == custom_staging.resolve()
72+
73+
def test_expands_tilde_in_env_value(self):
74+
"""Test that ~ is expanded in RIMPORT_STAGING value."""
75+
# Use a path with ~ that will be expanded
76+
with patch.dict(os.environ, {"RIMPORT_STAGING": "~/my_staging"}):
77+
result = rimport.get_staging_root()
78+
79+
# Should be expanded and resolved
80+
assert "~" not in str(result)
81+
assert result.is_absolute()
82+
83+
def test_resolves_relative_path_in_env_value(self):
84+
"""Test that relative paths in RIMPORT_STAGING are resolved."""
85+
# Set a relative path
86+
with patch.dict(os.environ, {"RIMPORT_STAGING": "./staging"}):
87+
result = rimport.get_staging_root()
88+
89+
# Should be resolved to absolute path
90+
assert result.is_absolute()
91+
92+
def test_env_value_with_spaces(self, tmp_path):
93+
"""Test handling of RIMPORT_STAGING with spaces in path."""
94+
custom_staging = tmp_path / "staging with spaces"
95+
custom_staging.mkdir()
96+
97+
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
98+
result = rimport.get_staging_root()
99+
100+
assert result == custom_staging.resolve()
101+
102+
def test_env_value_overrides_default(self, tmp_path):
103+
"""Test that RIMPORT_STAGING overrides the default value."""
104+
custom_staging = tmp_path / "override"
105+
custom_staging.mkdir()
106+
107+
with patch.dict(os.environ, {"RIMPORT_STAGING": str(custom_staging)}):
108+
result = rimport.get_staging_root()
109+
110+
# Should NOT be the default
111+
default = Path(
112+
"/glade/campaign/collections/gdex/data/d651077/cesmdata/inputdata"
113+
)
114+
assert result != default
115+
assert result == custom_staging.resolve()

0 commit comments

Comments
 (0)