From 6d68f11d47a4603c5ca484d43154f068981008b2 Mon Sep 17 00:00:00 2001 From: Arthur Gymer <24782660+awgymer@users.noreply.github.com> Date: Wed, 22 Feb 2023 13:01:06 +0000 Subject: [PATCH 1/5] Dump template yml to file in pipeline repo after creation --- nf_core/create.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 74c9df1b87..1c6a88bc50 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -56,7 +56,7 @@ def __init__( plain=False, default_branch=None, ): - self.template_params, skip_paths_keys = self.create_param_dict( + self.template_params, skip_paths_keys, self.template_yaml = self.create_param_dict( name, description, author, version, template_yaml_path, plain ) @@ -178,7 +178,7 @@ def create_param_dict(self, name, description, author, version, template_yaml_pa if not re.match(r"^[a-z]+$", param_dict["short_name"]): raise UserWarning("[red]Invalid workflow name: must be lowercase without punctuation.") - return param_dict, skip_paths + return param_dict, skip_paths, template_yaml def customize_template(self, template_areas): """Customizes the template parameters. @@ -348,6 +348,10 @@ def render_template(self): # Update the .nf-core.yml with linting configurations self.fix_linting() + log.debug("Dumping pipeline template yml to file") + with open(self.outdir / "pipeline_template.yml", "w") as fh: + yaml.safe_dump(self.template_yaml, fh) + def update_nextflow_schema(self): """ Removes unused parameters from the nextflow schema. From 834c0ecb6d5a6d0c160ee2aed53c0bd21d3fc2c1 Mon Sep 17 00:00:00 2001 From: Arthur Gymer <24782660+awgymer@users.noreply.github.com> Date: Wed, 22 Feb 2023 13:04:20 +0000 Subject: [PATCH 2/5] Make the dump conditional on template yaml having content --- nf_core/create.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 1c6a88bc50..0e9edf59c9 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -349,8 +349,9 @@ def render_template(self): self.fix_linting() log.debug("Dumping pipeline template yml to file") - with open(self.outdir / "pipeline_template.yml", "w") as fh: - yaml.safe_dump(self.template_yaml, fh) + if self.template_yaml: + with open(self.outdir / "pipeline_template.yml", "w") as fh: + yaml.safe_dump(self.template_yaml, fh) def update_nextflow_schema(self): """ From d88756bba22d4ccd7e5fb8d4e0c69120d8866ce8 Mon Sep 17 00:00:00 2001 From: Arthur Gymer <24782660+awgymer@users.noreply.github.com> Date: Mon, 27 Mar 2023 10:35:36 +0100 Subject: [PATCH 3/5] Add test for pipeline init with template yml --- tests/data/pipeline_create_template.yml | 1 + tests/test_create.py | 65 ++++++++++++++++++++----- 2 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 tests/data/pipeline_create_template.yml diff --git a/tests/data/pipeline_create_template.yml b/tests/data/pipeline_create_template.yml new file mode 100644 index 0000000000..12e48e9c27 --- /dev/null +++ b/tests/data/pipeline_create_template.yml @@ -0,0 +1 @@ +prefix: testprefix diff --git a/tests/test_create.py b/tests/test_create.py index baac509d74..2e709ef94b 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -2,6 +2,7 @@ """ import os import unittest +from pathlib import Path import git @@ -9,17 +10,38 @@ from .utils import with_temporary_folder +TEST_DATA_DIR = Path(__file__).parent / "data" +PIPELINE_TEMPLATE_YML = TEST_DATA_DIR / "pipeline_create_template.yml" + class NfcoreCreateTest(unittest.TestCase): - @with_temporary_folder - def setUp(self, tmp_path): + def setUp(self): self.pipeline_name = "nf-core/test" self.pipeline_description = "just for 4w3s0m3 tests" self.pipeline_author = "Chuck Norris" self.pipeline_version = "1.0.0" self.default_branch = "default" - self.pipeline = nf_core.create.PipelineCreate( + def test_pipeline_creation(self): + pipeline = nf_core.create.PipelineCreate( + name=self.pipeline_name, + description=self.pipeline_description, + author=self.pipeline_author, + version=self.pipeline_version, + no_git=False, + force=True, + plain=True, + default_branch=self.default_branch, + ) + + assert pipeline.template_params["name"] == self.pipeline_name + assert pipeline.template_params["description"] == self.pipeline_description + assert pipeline.template_params["author"] == self.pipeline_author + assert pipeline.template_params["version"] == self.pipeline_version + + @with_temporary_folder + def test_pipeline_creation_initiation(self, tmp_path): + pipeline = nf_core.create.PipelineCreate( name=self.pipeline_name, description=self.pipeline_description, author=self.pipeline_author, @@ -30,14 +52,31 @@ def setUp(self, tmp_path): plain=True, default_branch=self.default_branch, ) + pipeline.init_pipeline() + assert os.path.isdir(os.path.join(pipeline.outdir, ".git")) + assert f" {self.default_branch}\n" in git.Repo.init(pipeline.outdir).git.branch() + assert not os.path.exists(os.path.join(pipeline.outdir, "pipeline_template.yml")) - def test_pipeline_creation(self): - assert self.pipeline.template_params["name"] == self.pipeline_name - assert self.pipeline.template_params["description"] == self.pipeline_description - assert self.pipeline.template_params["author"] == self.pipeline_author - assert self.pipeline.template_params["version"] == self.pipeline_version - - def test_pipeline_creation_initiation(self): - self.pipeline.init_pipeline() - assert os.path.isdir(os.path.join(self.pipeline.outdir, ".git")) - assert f" {self.default_branch}\n" in git.Repo.init(self.pipeline.outdir).git.branch() + @with_temporary_folder + def test_pipeline_creation_initiation_with_yml(self, tmp_path): + pipeline = nf_core.create.PipelineCreate( + name=self.pipeline_name, + description=self.pipeline_description, + author=self.pipeline_author, + version=self.pipeline_version, + no_git=False, + force=True, + outdir=tmp_path, + template_yaml_path=PIPELINE_TEMPLATE_YML, + plain=True, + default_branch=self.default_branch, + ) + pipeline.init_pipeline() + assert os.path.isdir(os.path.join(pipeline.outdir, ".git")) + assert f" {self.default_branch}\n" in git.Repo.init(pipeline.outdir).git.branch() + + # Check pipeline yml has been dumped and matches input + pipeline_template = os.path.join(pipeline.outdir, "pipeline_template.yml") + assert os.path.exists(pipeline_template) + with open(pipeline_template) as fh: + assert fh.read() == PIPELINE_TEMPLATE_YML.read_text() From 637e9678db8265e1ced0a90b9c2e248b2e24d9fd Mon Sep 17 00:00:00 2001 From: Arthur Gymer <24782660+awgymer@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:38:16 +0100 Subject: [PATCH 4/5] Add test mocking cli input --- tests/test_create.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_create.py b/tests/test_create.py index 2e709ef94b..e454f0f85d 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -3,6 +3,7 @@ import os import unittest from pathlib import Path +from unittest import mock import git @@ -80,3 +81,27 @@ def test_pipeline_creation_initiation_with_yml(self, tmp_path): assert os.path.exists(pipeline_template) with open(pipeline_template) as fh: assert fh.read() == PIPELINE_TEMPLATE_YML.read_text() + + @mock.patch.object(nf_core.create.PipelineCreate, "customize_template") + @mock.patch.object(nf_core.create.questionary, "confirm") + @with_temporary_folder + def test_pipeline_creation_initiation_customize_template(self, mock_questionary, mock_customize, tmp_path): + mock_questionary.unsafe_ask.return_value = True + mock_customize.return_value = {"prefix": "testprefix"} + pipeline = nf_core.create.PipelineCreate( + name=self.pipeline_name, + description=self.pipeline_description, + author=self.pipeline_author, + version=self.pipeline_version, + no_git=False, + force=True, + outdir=tmp_path, + default_branch=self.default_branch, + ) + pipeline.init_pipeline() + assert os.path.isdir(os.path.join(pipeline.outdir, ".git")) + assert f" {self.default_branch}\n" in git.Repo.init(pipeline.outdir).git.branch() + + # Check pipeline yml has been dumped and matches input + pipeline_template = os.path.join(pipeline.outdir, "pipeline_template.yml") + assert os.path.exists(pipeline_template) From 326e600d8ddbd5fb5914bf6781f141ce91716186 Mon Sep 17 00:00:00 2001 From: Arthur Gymer <24782660+awgymer@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:38:54 +0100 Subject: [PATCH 5/5] Add check to ensure output yml matches expected --- tests/test_create.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_create.py b/tests/test_create.py index e454f0f85d..cc6bf8ba47 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -105,3 +105,5 @@ def test_pipeline_creation_initiation_customize_template(self, mock_questionary, # Check pipeline yml has been dumped and matches input pipeline_template = os.path.join(pipeline.outdir, "pipeline_template.yml") assert os.path.exists(pipeline_template) + with open(pipeline_template) as fh: + assert fh.read() == PIPELINE_TEMPLATE_YML.read_text()