diff --git a/tests/components/generate_snapshot.py b/tests/components/generate_snapshot.py deleted file mode 100644 index a5a8eaba39..0000000000 --- a/tests/components/generate_snapshot.py +++ /dev/null @@ -1,143 +0,0 @@ -"""Test generate a snapshot""" - -import json -from pathlib import Path -from unittest.mock import MagicMock - -import pytest - -from nf_core.components.components_test import ComponentsTest -from nf_core.utils import set_wd - -from ..utils import GITLAB_NFTEST_BRANCH, GITLAB_URL - - -def test_generate_snapshot_module(self): - """Generate the snapshot for a module in nf-core/modules clone""" - with set_wd(self.nfcore_modules): - snap_generator = ComponentsTest( - component_type="modules", - component_name="fastqc", - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - ) - snap_generator.run() - - snap_path = Path("modules", "nf-core-test", "fastqc", "tests", "main.nf.test.snap") - assert snap_path.exists() - - with open(snap_path) as fh: - snap_content = json.load(fh) - assert "versions" in snap_content - assert "content" in snap_content["versions"] - assert "versions.yml:md5,e1cc25ca8af856014824abd842e93978" in snap_content["versions"]["content"][0] - - -def test_generate_snapshot_subworkflow(self): - """Generate the snapshot for a subworkflows in nf-core/modules clone""" - with set_wd(self.nfcore_modules): - snap_generator = ComponentsTest( - component_type="subworkflows", - component_name="bam_sort_stats_samtools", - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - ) - snap_generator.run() - - snap_path = Path("subworkflows", "nf-core-test", "bam_sort_stats_samtools", "tests", "main.nf.test.snap") - assert snap_path.exists() - - with open(snap_path) as fh: - snap_content = json.load(fh) - assert "test_bam_sort_stats_samtools_paired_end_flagstats" in snap_content - assert ( - "test.flagstat:md5,4f7ffd1e6a5e85524d443209ac97d783" - in snap_content["test_bam_sort_stats_samtools_paired_end_flagstats"]["content"][0][0] - ) - assert "test_bam_sort_stats_samtools_paired_end_idxstats" in snap_content - assert ( - "test.idxstats:md5,df60a8c8d6621100d05178c93fb053a2" - in snap_content["test_bam_sort_stats_samtools_paired_end_idxstats"]["content"][0][0] - ) - - -def test_generate_snapshot_once( - self, -): - """Generate the snapshot for a module in nf-core/modules clone only once""" - with set_wd(self.nfcore_modules): - snap_generator = ComponentsTest( - component_type="modules", - component_name="fastqc", - once=True, - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - ) - snap_generator.repo_type = "modules" - snap_generator.generate_snapshot = MagicMock() - snap_generator.run() - snap_generator.generate_snapshot.assert_called_once() - - -def test_update_snapshot_module(self): - """Update the snapshot of a module in nf-core/modules clone""" - - with set_wd(self.nfcore_modules): - snap_path = Path("modules", "nf-core-test", "bwa", "mem", "tests", "main.nf.test.snap") - with open(snap_path) as fh: - snap_content = json.load(fh) - original_timestamp = snap_content["Single-End"]["timestamp"] - # delete the timestamp in json - snap_content["Single-End"]["timestamp"] = "" - with open(snap_path, "w") as fh: - json.dump(snap_content, fh) - snap_generator = ComponentsTest( - component_type="modules", - component_name="bwa/mem", - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - update=True, - ) - snap_generator.run() - - with open(snap_path) as fh: - snap_content = json.load(fh) - assert "Single-End" in snap_content - assert snap_content["Single-End"]["timestamp"] != original_timestamp - - -def test_test_not_found(self): - """Generate the snapshot for a module in nf-core/modules clone which doesn't contain tests""" - with set_wd(self.nfcore_modules): - snap_generator = ComponentsTest( - component_type="modules", - component_name="fastp", - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - ) - test_file = Path("modules", "nf-core-test", "fastp", "tests", "main.nf.test") - test_file.rename(test_file.parent / "main.nf.test.bak") - with pytest.raises(UserWarning) as e: - snap_generator.run() - assert "Test file 'main.nf.test' not found" in str(e.value) - Path(test_file.parent / "main.nf.test.bak").rename(test_file) - - -def test_unstable_snapshot(self): - """Generate the snapshot for a module in nf-core/modules clone with unstable snapshots""" - with set_wd(self.nfcore_modules): - snap_generator = ComponentsTest( - component_type="modules", - component_name="kallisto/quant", - no_prompts=True, - remote_url=GITLAB_URL, - branch=GITLAB_NFTEST_BRANCH, - ) - with pytest.raises(UserWarning) as e: - snap_generator.run() - assert "nf-test snapshot is not stable" in str(e.value) diff --git a/tests/components/snapshot_test.py b/tests/components/snapshot_test.py deleted file mode 100644 index b3fc259770..0000000000 --- a/tests/components/snapshot_test.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Test the 'modules test' or 'subworkflows test' command which runs nf-test test.""" - -import shutil -from pathlib import Path - -import pytest - -from nf_core.components.components_test import ComponentsTest -from nf_core.utils import set_wd - - -def test_components_test_check_inputs(self): - """Test the check_inputs() function - raise UserWarning because module doesn't exist""" - with set_wd(self.nfcore_modules): - meta_builder = ComponentsTest(component_type="modules", component_name="none", no_prompts=True) - with pytest.raises(UserWarning) as excinfo: - meta_builder.check_inputs() - assert "Cannot find directory" in str(excinfo.value) - - -def test_components_test_no_name_no_prompts(self): - """Test the check_inputs() function - raise UserWarning prompts are deactivated and module name is not provided.""" - with set_wd(self.nfcore_modules): - meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=True) - with pytest.raises(UserWarning) as excinfo: - meta_builder.check_inputs() - assert "Module name not provided and prompts deactivated." in str(excinfo.value) - - -def test_components_test_no_installed_modules(self): - """Test the check_inputs() function - raise UserWarning because installed modules were not found""" - with set_wd(self.nfcore_modules): - module_dir = Path(self.nfcore_modules, "modules") - shutil.rmtree(module_dir) - module_dir.mkdir() - meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=False) - meta_builder.repo_type = "modules" - with pytest.raises(LookupError) as excinfo: - meta_builder.check_inputs() - assert "Nothing installed from" in str(excinfo.value) diff --git a/tests/components/test_components_generate_snapshot.py b/tests/components/test_components_generate_snapshot.py new file mode 100644 index 0000000000..265842f5d0 --- /dev/null +++ b/tests/components/test_components_generate_snapshot.py @@ -0,0 +1,140 @@ +"""Test generate a snapshot""" + +import json +from pathlib import Path +from unittest.mock import MagicMock + +import pytest + +from nf_core.components.components_test import ComponentsTest +from nf_core.utils import set_wd + +from ..test_components import TestComponents +from ..utils import GITLAB_NFTEST_BRANCH, GITLAB_URL + + +class TestTestComponentsUtils(TestComponents): + def test_generate_snapshot_module(self): + """Generate the snapshot for a module in nf-core/modules clone""" + with set_wd(self.nfcore_modules): + snap_generator = ComponentsTest( + component_type="modules", + component_name="fastqc", + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + ) + snap_generator.run() + + snap_path = Path("modules", "nf-core-test", "fastqc", "tests", "main.nf.test.snap") + assert snap_path.exists() + + with open(snap_path) as fh: + snap_content = json.load(fh) + assert "versions" in snap_content + assert "content" in snap_content["versions"] + assert "versions.yml:md5,e1cc25ca8af856014824abd842e93978" in snap_content["versions"]["content"][0] + + def test_generate_snapshot_subworkflow(self): + """Generate the snapshot for a subworkflows in nf-core/modules clone""" + with set_wd(self.nfcore_modules): + snap_generator = ComponentsTest( + component_type="subworkflows", + component_name="bam_sort_stats_samtools", + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + ) + snap_generator.run() + + snap_path = Path("subworkflows", "nf-core-test", "bam_sort_stats_samtools", "tests", "main.nf.test.snap") + assert snap_path.exists() + + with open(snap_path) as fh: + snap_content = json.load(fh) + assert "test_bam_sort_stats_samtools_paired_end_flagstats" in snap_content + assert ( + "test.flagstat:md5,4f7ffd1e6a5e85524d443209ac97d783" + in snap_content["test_bam_sort_stats_samtools_paired_end_flagstats"]["content"][0][0] + ) + assert "test_bam_sort_stats_samtools_paired_end_idxstats" in snap_content + assert ( + "test.idxstats:md5,df60a8c8d6621100d05178c93fb053a2" + in snap_content["test_bam_sort_stats_samtools_paired_end_idxstats"]["content"][0][0] + ) + + def test_generate_snapshot_once( + self, + ): + """Generate the snapshot for a module in nf-core/modules clone only once""" + with set_wd(self.nfcore_modules): + snap_generator = ComponentsTest( + component_type="modules", + component_name="fastqc", + once=True, + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + ) + snap_generator.repo_type = "modules" + snap_generator.generate_snapshot = MagicMock() + snap_generator.run() + snap_generator.generate_snapshot.assert_called_once() + + def test_update_snapshot_module(self): + """Update the snapshot of a module in nf-core/modules clone""" + + with set_wd(self.nfcore_modules): + snap_path = Path("modules", "nf-core-test", "bwa", "mem", "tests", "main.nf.test.snap") + with open(snap_path) as fh: + snap_content = json.load(fh) + original_timestamp = snap_content["Single-End"]["timestamp"] + # delete the timestamp in json + snap_content["Single-End"]["timestamp"] = "" + with open(snap_path, "w") as fh: + json.dump(snap_content, fh) + snap_generator = ComponentsTest( + component_type="modules", + component_name="bwa/mem", + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + update=True, + ) + snap_generator.run() + + with open(snap_path) as fh: + snap_content = json.load(fh) + assert "Single-End" in snap_content + assert snap_content["Single-End"]["timestamp"] != original_timestamp + + def test_test_not_found(self): + """Generate the snapshot for a module in nf-core/modules clone which doesn't contain tests""" + with set_wd(self.nfcore_modules): + snap_generator = ComponentsTest( + component_type="modules", + component_name="fastp", + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + ) + test_file = Path("modules", "nf-core-test", "fastp", "tests", "main.nf.test") + test_file.rename(test_file.parent / "main.nf.test.bak") + with pytest.raises(UserWarning) as e: + snap_generator.run() + assert "Test file 'main.nf.test' not found" in str(e.value) + Path(test_file.parent / "main.nf.test.bak").rename(test_file) + + def test_unstable_snapshot(self): + """Generate the snapshot for a module in nf-core/modules clone with unstable snapshots""" + with set_wd(self.nfcore_modules): + snap_generator = ComponentsTest( + component_type="modules", + component_name="kallisto/quant", + no_prompts=True, + remote_url=GITLAB_URL, + branch=GITLAB_NFTEST_BRANCH, + ) + with pytest.raises(UserWarning) as e: + snap_generator.run() + assert "nf-test snapshot is not stable" in str(e.value) diff --git a/tests/components/test_components_snapshot_test.py b/tests/components/test_components_snapshot_test.py new file mode 100644 index 0000000000..8f0f2c0bd1 --- /dev/null +++ b/tests/components/test_components_snapshot_test.py @@ -0,0 +1,41 @@ +"""Test the 'modules test' or 'subworkflows test' command which runs nf-test test.""" + +import shutil +from pathlib import Path + +import pytest + +from nf_core.components.components_test import ComponentsTest +from nf_core.utils import set_wd + +from ..test_components import TestComponents + + +class TestTestComponentsUtils(TestComponents): + def test_components_test_check_inputs(self): + """Test the check_inputs() function - raise UserWarning because module doesn't exist""" + with set_wd(self.nfcore_modules): + meta_builder = ComponentsTest(component_type="modules", component_name="none", no_prompts=True) + with pytest.raises(UserWarning) as excinfo: + meta_builder.check_inputs() + assert "Cannot find directory" in str(excinfo.value) + + def test_components_test_no_name_no_prompts(self): + """Test the check_inputs() function - raise UserWarning prompts are deactivated and module name is not provided.""" + with set_wd(self.nfcore_modules): + meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=True) + with pytest.raises(UserWarning) as excinfo: + meta_builder.check_inputs() + assert "Module name not provided and prompts deactivated." in str(excinfo.value) + + def test_components_test_no_installed_modules(self): + """Test the check_inputs() function - raise UserWarning because installed modules were not found""" + with set_wd(self.nfcore_modules): + module_dir = Path(self.nfcore_modules, "modules") + shutil.rmtree(module_dir) + module_dir.mkdir() + meta_builder = ComponentsTest(component_type="modules", component_name=None, no_prompts=False) + meta_builder.repo_type = "modules" + with pytest.raises(LookupError) as excinfo: + meta_builder.check_inputs() + assert "Nothing installed from" in str(excinfo.value) diff --git a/tests/test_components.py b/tests/test_components.py index b77046df2b..a6b22f6686 100644 --- a/tests/test_components.py +++ b/tests/test_components.py @@ -36,21 +36,3 @@ def tearDown(self): @pytest.fixture(autouse=True) def _use_caplog(self, caplog): self.caplog = caplog - - ############################################ - # Test of the individual components commands. # - ############################################ - - from .components.generate_snapshot import ( # type: ignore[misc] - test_generate_snapshot_module, - test_generate_snapshot_once, - test_generate_snapshot_subworkflow, - test_test_not_found, - test_unstable_snapshot, - test_update_snapshot_module, - ) - from .components.snapshot_test import ( # type: ignore[misc] - test_components_test_check_inputs, - test_components_test_no_installed_modules, - test_components_test_no_name_no_prompts, - )