Skip to content

Commit a41eb75

Browse files
committed
rimport: Safer check of whether file is already in staging root.
1 parent 2343909 commit a41eb75

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

rimport

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ def stage_data(src: Path, inputdata_root: Path, staging_root: Path) -> None:
103103
try:
104104
rel = src.resolve().relative_to(inputdata_root.resolve())
105105
except ValueError:
106-
# TODO: Do not hard-code string here
107-
# TODO: Check whether it's IN THE DIRECTORY, not whether the path contains a string
108-
if "d651077" in str(src):
106+
if src.resolve().is_relative_to(staging_root.resolve()):
109107
raise RuntimeError(
110108
f"Source file '{src.name}' is already under staging directory '{staging_root}'."
111109
)

tests/rimport/test_stage_data.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,28 @@ def test_raises_error_for_file_outside_inputdata_root(
119119
with pytest.raises(RuntimeError, match="not under inputdata root"):
120120
rimport.stage_data(src, inputdata_root, staging_root)
121121

122+
def test_raises_error_for_file_outside_inputdata_root_with_special_str(
123+
self, tmp_path, inputdata_root, staging_root
124+
):
125+
"""
126+
Test that staging a file outside inputdata root raises RuntimeError, even if a certain
127+
special string is included in its path.
128+
"""
129+
# Create a file outside inputdata root
130+
src = tmp_path / "outside" / "file_d651077.nc"
131+
src.parent.mkdir()
132+
src.write_text("data")
133+
134+
# Should raise RuntimeError
135+
with pytest.raises(RuntimeError, match="not under inputdata root"):
136+
rimport.stage_data(src, inputdata_root, staging_root)
137+
122138
def test_raises_error_for_already_published_file(
123139
self, tmp_path, inputdata_root, staging_root
124140
):
125141
"""Test that staging an already published file raises RuntimeError."""
126-
# Create a file with "d651077" in the path (published indicator)
127-
src = tmp_path / "d651077" / "data" / "file.nc"
128-
src.parent.mkdir(parents=True)
142+
# Create a file in staging_root
143+
src = staging_root / "file.nc"
129144
src.write_text("data")
130145

131146
# Should raise RuntimeError for already published file

0 commit comments

Comments
 (0)