Skip to content

Commit f3a92f0

Browse files
committed
add tests
1 parent 335b7e1 commit f3a92f0

File tree

2 files changed

+45
-51
lines changed

2 files changed

+45
-51
lines changed
Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
import json
2-
import os
3-
import shutil
4-
import tempfile
52
from pathlib import Path
63

7-
import nf_core.pipelines.create.create
8-
import nf_core.pipelines.schema
94
from nf_core.pipelines.params_file import ParamsFileBuilder
105

6+
from ..test_pipelines import TestPipelines
117

12-
class TestParamsFileBuilder:
8+
9+
class TestParamsFileBuilder(TestPipelines):
1310
"""Class for schema tests"""
1411

15-
@classmethod
16-
def setup_class(cls):
12+
def setUp(self):
1713
"""Create a new PipelineSchema object"""
18-
cls.schema_obj = nf_core.pipelines.schema.PipelineSchema()
19-
cls.root_repo_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
20-
21-
# Create a test pipeline in temp directory
22-
cls.tmp_dir = tempfile.mkdtemp()
23-
cls.template_dir = Path(cls.tmp_dir, "wf")
24-
create_obj = nf_core.pipelines.create.create.PipelineCreate(
25-
"testpipeline", "a description", "Me", outdir=cls.template_dir, no_git=True
26-
)
27-
create_obj.init_pipeline()
28-
29-
cls.template_schema = Path(cls.template_dir, "nextflow_schema.json")
30-
cls.params_template_builder = ParamsFileBuilder(cls.template_dir)
31-
cls.invalid_template_schema = Path(cls.template_dir, "nextflow_schema_invalid.json")
32-
33-
# Remove the allOf section to make the schema invalid
34-
with open(cls.template_schema) as fh:
35-
o = json.load(fh)
36-
del o["allOf"]
37-
38-
with open(cls.invalid_template_schema, "w") as fh:
39-
json.dump(o, fh)
40-
41-
@classmethod
42-
def teardown_class(cls):
43-
if Path(cls.tmp_dir).exists():
44-
shutil.rmtree(cls.tmp_dir)
14+
super().setUp()
15+
16+
self.template_schema = Path(self.pipeline_dir, "nextflow_schema.json")
17+
self.params_template_builder = ParamsFileBuilder(self.pipeline_dir)
18+
self.outfile = Path(self.pipeline_dir, "params-file.yml")
4519

4620
def test_build_template(self):
47-
outfile = Path(self.tmp_dir, "params-file.yml")
48-
self.params_template_builder.write_params_file(str(outfile))
21+
self.params_template_builder.write_params_file(self.outfile)
4922

50-
assert outfile.exists()
23+
assert self.outfile.exists()
5124

52-
with open(outfile) as fh:
25+
with open(self.outfile) as fh:
5326
out = fh.read()
5427

5528
assert "nf-core/testpipeline" in out
5629

57-
def test_build_template_invalid_schema(self, caplog):
30+
def test_build_template_invalid_schema(self):
5831
"""Build a schema from a template"""
59-
outfile = Path(self.tmp_dir, "params-file-invalid.yml")
60-
builder = ParamsFileBuilder(self.invalid_template_schema)
61-
res = builder.write_params_file(str(outfile))
32+
schema = {}
33+
with open(self.template_schema) as fh:
34+
schema = json.load(fh)
35+
del schema["allOf"]
36+
37+
with open(self.template_schema, "w") as fh:
38+
json.dump(schema, fh)
39+
40+
builder = ParamsFileBuilder(self.template_schema)
41+
res = builder.write_params_file(self.outfile)
6242

6343
assert res is False
64-
assert "Pipeline schema file is invalid" in caplog.text
44+
assert "Pipeline schema file is invalid" in self.caplog.text
6545

66-
def test_build_template_file_exists(self, caplog):
46+
def test_build_template_file_exists(self):
6747
"""Build a schema from a template"""
6848

6949
# Creates a new empty file
70-
outfile = Path(self.tmp_dir) / "params-file.yml"
71-
with open(outfile, "w"):
72-
pass
50+
self.outfile.touch()
7351

74-
res = self.params_template_builder.write_params_file(outfile)
52+
res = self.params_template_builder.write_params_file(self.outfile)
7553

7654
assert res is False
77-
assert f"File '{outfile}' exists!" in caplog.text
55+
assert f"File '{self.outfile}' exists!" in self.caplog.text
56+
57+
self.outfile.unlink()
7858

79-
outfile.unlink()
59+
def test_build_template_content(self):
60+
"""Test that the content of the params file is correct"""
61+
self.params_template_builder.write_params_file(self.outfile)
62+
63+
with open(self.outfile) as fh:
64+
out = fh.read()
65+
66+
assert "nf-core/testpipeline" in out
67+
assert "# input = null" in out

tests/test_pipelines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import shutil
22
from unittest import TestCase
33

4+
import pytest
5+
46
from nf_core.utils import Pipeline
57

68
from .utils import create_tmp_pipeline
@@ -24,3 +26,7 @@ def _make_pipeline_copy(self):
2426
new_pipeline = self.tmp_dir / "nf-core-testpipeline-copy"
2527
shutil.copytree(self.pipeline_dir, new_pipeline)
2628
return new_pipeline
29+
30+
@pytest.fixture(autouse=True)
31+
def _use_caplog(self, caplog):
32+
self.caplog = caplog

0 commit comments

Comments
 (0)