Skip to content

Commit

Permalink
Finally a comprehensive test for symlink_singularity_images()
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasZepper committed Feb 21, 2024
1 parent 4651f85 commit aacde33
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions nf_core/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,15 +1021,15 @@ def symlink_singularity_images(self, image_out_path):

for registry in self.registry_set:
if not os.path.basename(image_out_path).startswith(registry):
symlink_name = f"{registry}-{os.path.basename(image_out_path)}"
symlink_name = os.path.join("./", f"{registry}-{os.path.basename(image_out_path)}")
else:
trimmed_name = re.sub(f"^{trim_pattern}", "", os.path.basename(image_out_path))
symlink_name = f"{registry}-{trimmed_name}"
symlink_name = os.path.join("./", f"{registry}-{trimmed_name}")

symlink_full = os.path.join(os.path.dirname(image_out_path), symlink_name)
target_name = os.path.join("./", os.path.basename(image_out_path))

if not os.path.exists(symlink_full):
if not os.path.exists(symlink_full) and target_name != symlink_name:
os.makedirs(os.path.dirname(symlink_full), exist_ok=True)
image_dir = os.open(os.path.dirname(image_out_path), os.O_RDONLY)
try:
Expand Down
56 changes: 56 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,60 @@ def test_get_singularity_images(self, tmp_path, mock_fetch_wf_config):
# Test that they are all caught inside get_singularity_images().
download_obj.get_singularity_images()

@with_temporary_folder
@mock.patch("os.makedirs")
@mock.patch("os.symlink")
@mock.patch("os.open")
@mock.patch("os.close")
@mock.patch("re.sub")
@mock.patch("os.path.basename")
@mock.patch("os.path.dirname")
def test_symlink_singularity_images(
self,
tmp_path,
mock_dirname,
mock_basename,
mock_resub,
mock_close,
mock_open,
mock_symlink,
mock_makedirs,
):
# Setup
mock_resub.return_value = "singularity-image.img"
mock_dirname.return_value = f"{tmp_path}/path/to"
mock_basename.return_value = "quay.io-singularity-image.img"
mock_open.return_value = 12 # file descriptor
mock_close.return_value = 12 # file descriptor

download_obj = DownloadWorkflow(
pipeline="dummy",
outdir=tmp_path,
container_library=("mirage-the-imaginative-registry.io", "quay.io"),
)

# Call the method
download_obj.symlink_singularity_images(f"{tmp_path}/path/to/quay.io-singularity-image.img")
print(mock_resub.call_args)

# Check that os.makedirs was called with the correct arguments
mock_makedirs.assert_any_call(f"{tmp_path}/path/to", exist_ok=True)

# Check that os.open was called with the correct arguments
mock_open.assert_called_once_with(f"{tmp_path}/path/to", os.O_RDONLY)

# Check that os.symlink was called with the correct arguments
mock_symlink.assert_any_call(
"./quay.io-singularity-image.img",
"./mirage-the-imaginative-registry.io-quay.io-singularity-image.img",
dir_fd=12,
)
# Check that there is no attempt to symlink to itself (test parameters would result in that behavior if not checked in the function)
assert (
unittest.mock.call("./quay.io-singularity-image.img", "./quay.io-singularity-image.img", dir_fd=12)
not in mock_symlink.call_args_list
)

#
# Test for gather_registries'
#
Expand Down Expand Up @@ -384,7 +438,9 @@ def test_gather_registries(self, tmp_path, mock_fetch_wf_config):
# it should only pull the apptainer, docker, podman and singularity registry from the config, but not any registry.
assert "fake-registry.io" not in download_obj.registry_set

#
# If Singularity is not installed, it raises a OSError because the singularity command can't be found.
#
@pytest.mark.skipif(
shutil.which("singularity") is not None,
reason="Can't test how the code behaves when singularity is not installed if it is.",
Expand Down

0 comments on commit aacde33

Please sign in to comment.