Skip to content

Commit f9a9bb8

Browse files
authored
Merge pull request #22 from samsrabin/fix-check-for-published-files
Fix --check for published files
2 parents efa3504 + 3b7fcd0 commit f9a9bb8

4 files changed

Lines changed: 98 additions & 21 deletions

File tree

rimport

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,15 @@ def stage_data(
210210
dst = staging_root / rel
211211

212212
if dst.exists():
213-
logger.info("File is already published but NOT linked; linking now.")
214-
replace_one_file_with_symlink(inputdata_root, staging_root, str(src))
215-
print_can_file_be_downloaded(can_file_be_downloaded(rel, staging_root))
216-
check_relink_worked(src, dst)
213+
msg = "File is already published but NOT linked"
214+
if check:
215+
logger.info("%s; would link.", msg)
216+
print_can_file_be_downloaded(can_file_be_downloaded(rel, staging_root))
217+
else:
218+
logger.info("%s; linking now.", msg)
219+
replace_one_file_with_symlink(inputdata_root, staging_root, str(src))
220+
check_relink_worked(src, dst)
221+
print_can_file_be_downloaded(can_file_be_downloaded(rel, staging_root))
217222
return
218223

219224
if check:

tests/rimport/test_can_file_be_downloaded.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ def test_existing_file_exists(self):
4343
def test_true_abspath(self):
4444
"""Test that can_file_be_downloaded() is true for an existing file given absolute path"""
4545
file_abspath = Path(os.path.join(DEFAULT_STAGING_ROOT, RELPATH_THAT_DOES_EXIST))
46-
assert rimport.can_file_be_downloaded(
46+
result = rimport.can_file_be_downloaded(
4747
file_abspath,
4848
DEFAULT_STAGING_ROOT,
4949
)
50+
assert result, f"Maybe the server is down? Try again. Result was: {result}"
5051

5152
def test_true_relpath(self):
5253
"""Test that can_file_be_downloaded() is true for an existing file given relative path"""
5354
file_relpath = Path(RELPATH_THAT_DOES_EXIST)
54-
assert rimport.can_file_be_downloaded(
55+
result = rimport.can_file_be_downloaded(
5556
file_relpath,
5657
DEFAULT_STAGING_ROOT,
5758
)
59+
assert result, f"Maybe the server is down? Try again. Result was: {result}"
5860

5961
def test_false_nonexistent(self):
6062
"""Test that can_file_be_downloaded() is false for a nonexistent file"""
6163
file_relpath = Path("weurueridniduafnea/smfnigsroerij/msdif8ernnr.nc")
62-
assert not rimport.can_file_be_downloaded(
64+
result = rimport.can_file_be_downloaded(
6365
file_relpath,
6466
DEFAULT_STAGING_ROOT,
6567
)
68+
assert not result, f"Unexpected result: {result}"

tests/rimport/test_cmdline.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,14 @@ def test_error_symlink_pointing_outside_staging(
455455
msg = "is outside staging directory"
456456
assert msg in result.stderr
457457

458-
def test_check_doesnt_copy(self, rimport_script, test_env, rimport_env):
459-
"""Test that a file is NOT copied to the staging directory if check is True."""
458+
def test_check_doesnt_copy_unpublished(self, rimport_script, test_env, rimport_env):
459+
"""Test that an unpublished file is not copied to the staging directory if check is True."""
460460
inputdata_root = test_env["inputdata_root"]
461461
staging_root = test_env["staging_root"]
462462

463463
# Create a file in inputdata
464-
test_file = inputdata_root / "test.nc"
464+
file_basename = "test.nc"
465+
test_file = inputdata_root / file_basename
465466
test_file.write_text("test data")
466467

467468
# Make sure --check skips ensure_running_as()
@@ -472,7 +473,7 @@ def test_check_doesnt_copy(self, rimport_script, test_env, rimport_env):
472473
sys.executable,
473474
rimport_script,
474475
"-file",
475-
"test.nc",
476+
file_basename,
476477
"-inputdata",
477478
str(inputdata_root),
478479
"--check",
@@ -490,11 +491,66 @@ def test_check_doesnt_copy(self, rimport_script, test_env, rimport_env):
490491
assert result.returncode == 0, f"Command failed: {result.stderr}"
491492

492493
# Verify file was not staged
493-
staged_file = staging_root / "test.nc"
494+
staged_file = staging_root / file_basename
494495
assert not staged_file.exists()
495496

496497
# Verify file was not replaced with a symlink
497498
assert not test_file.is_symlink()
498499

499500
# Verify message was printed
500501
assert "not already published" in result.stdout
502+
503+
# Verify messages weren't printed
504+
assert "already published but NOT linked".lower() not in result.stdout.lower()
505+
assert "Deleted original file".lower() not in result.stdout.lower()
506+
assert "Created symbolic link".lower() not in result.stdout.lower()
507+
assert "Error creating symlink".lower() not in result.stdout.lower()
508+
509+
def test_check_doesnt_relink_published(self, rimport_script, test_env, rimport_env):
510+
"""Test that published file is not relinked if check is True."""
511+
inputdata_root = test_env["inputdata_root"]
512+
staging_root = test_env["staging_root"]
513+
514+
# Create a file in inputdata and staging
515+
file_basename = "test.nc"
516+
test_file = inputdata_root / file_basename
517+
test_file.write_text("test data")
518+
staged_file = staging_root / file_basename
519+
staged_file.write_text("test data")
520+
521+
# Make sure --check skips ensure_running_as()
522+
del rimport_env["RIMPORT_SKIP_USER_CHECK"]
523+
524+
# Run rimport with --check option
525+
command = [
526+
sys.executable,
527+
rimport_script,
528+
"-file",
529+
file_basename,
530+
"-inputdata",
531+
str(inputdata_root),
532+
"--check",
533+
]
534+
535+
result = subprocess.run(
536+
command,
537+
capture_output=True,
538+
text=True,
539+
check=False,
540+
env=rimport_env,
541+
)
542+
543+
# Verify success
544+
assert result.returncode == 0, f"Command failed: {result.stderr}"
545+
546+
# Verify file was not replaced with a symlink
547+
assert not test_file.is_symlink()
548+
549+
# Verify message was printed
550+
assert "already published but NOT linked".lower() in result.stdout.lower()
551+
552+
# Verify messages weren't printed
553+
assert "linking now".lower() not in result.stdout.lower()
554+
assert "Deleted original file".lower() not in result.stdout.lower()
555+
assert "Created symbolic link".lower() not in result.stdout.lower()
556+
assert "Error creating symlink".lower() not in result.stdout.lower()

tests/rimport/test_stage_data.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def fixture_staging_root(tmp_path):
5454
class TestStageData:
5555
"""Test suite for stage_data() function."""
5656

57-
def test_copies_file_to_staging(self, inputdata_root, staging_root):
58-
"""Test that a file is copied to the staging directory."""
57+
def test_copies_and_relinks_unpublished(self, inputdata_root, staging_root):
58+
"""Test that an unpublished file is copied to the staging directory and relinked."""
5959
# Create file in inputdata root
6060
src = inputdata_root / "file.nc"
6161
src.write_text("data content")
@@ -72,22 +72,35 @@ def test_copies_file_to_staging(self, inputdata_root, staging_root):
7272
assert src.is_symlink()
7373
assert src.resolve() == dst
7474

75-
def test_check_doesnt_copy(self, inputdata_root, staging_root, caplog):
76-
"""Test that a file is NOT copied to the staging directory if check is True"""
75+
def test_check_doesnt_copy_unpublished(self, inputdata_root, staging_root, caplog):
76+
"""Test that an unpublished file is NOT copied to the staging directory if check is True"""
7777
# Create file in inputdata root
78-
src = inputdata_root / "file.nc"
78+
file_basename = "file.nc"
79+
src = inputdata_root / file_basename
7980
src.write_text("data content")
8081

8182
# Check the file
8283
rimport.stage_data(src, inputdata_root, staging_root, check=True)
8384

84-
# Verify file was NOT copied to staging
85-
dst = staging_root / "file.nc"
85+
# Verify file was NOT copied to staging or relinked
86+
dst = staging_root / file_basename
8687
assert not dst.exists()
8788
assert not src.is_symlink()
8889

89-
# Verify message was logged
90-
assert "not already published" in caplog.text
90+
def test_check_doesnt_relink_published(self, inputdata_root, staging_root, caplog):
91+
"""Test that a published file is NOT relinked if check is True"""
92+
# Create file in inputdata root and staging
93+
file_basename = "file.nc"
94+
src = inputdata_root / file_basename
95+
src.write_text("data content")
96+
dst = staging_root / file_basename
97+
dst.write_text("data content")
98+
99+
# Check the file
100+
rimport.stage_data(src, inputdata_root, staging_root, check=True)
101+
102+
# Verify file was NOT relinked
103+
assert not src.is_symlink()
91104

92105
def test_preserves_directory_structure(self, inputdata_root, staging_root):
93106
"""Test that directory structure is preserved in staging."""

0 commit comments

Comments
 (0)