Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump template yml to file in pipeline repo after creation #2189

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -348,6 +348,11 @@ def render_template(self):
# Update the .nf-core.yml with linting configurations
self.fix_linting()

log.debug("Dumping pipeline template yml to file")
if self.template_yaml:
with open(self.outdir / "pipeline_template.yml", "w") as fh:
yaml.safe_dump(self.template_yaml, fh)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
yaml.safe_dump(self.template_yaml, fh)
yaml.safe_dump(self.template_yaml, fh)
run_prettier_on_file(fh.name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this should only trigger when a template_yml is either supplied (which perhaps is silly) or someone customises something on the CLI.

Currently there are no tests which mock passing information in via the CLI prompts that I'm aware of. Happy to try adding them but not sure if it's best done as a unit-test or as part of one of the fuller tests defined in the GH actions?


def update_nextflow_schema(self):
"""
Removes unused parameters from the nextflow schema.
Expand Down
1 change: 1 addition & 0 deletions tests/data/pipeline_create_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prefix: testprefix
92 changes: 79 additions & 13 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@
"""
import os
import unittest
from pathlib import Path
from unittest import mock

import git

import nf_core.create

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,
Expand All @@ -30,14 +53,57 @@ 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()

@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)
with open(pipeline_template) as fh:
assert fh.read() == PIPELINE_TEMPLATE_YML.read_text()