|
1 | 1 | import json |
2 | | -import os |
3 | | -import shutil |
4 | | -import tempfile |
5 | 2 | from pathlib import Path |
6 | 3 |
|
7 | | -import nf_core.pipelines.create.create |
8 | | -import nf_core.pipelines.schema |
9 | 4 | from nf_core.pipelines.params_file import ParamsFileBuilder |
10 | 5 |
|
| 6 | +from ..test_pipelines import TestPipelines |
11 | 7 |
|
12 | | -class TestParamsFileBuilder: |
| 8 | + |
| 9 | +class TestParamsFileBuilder(TestPipelines): |
13 | 10 | """Class for schema tests""" |
14 | 11 |
|
15 | | - @classmethod |
16 | | - def setup_class(cls): |
| 12 | + def setUp(self): |
17 | 13 | """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") |
45 | 19 |
|
46 | 20 | 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) |
49 | 22 |
|
50 | | - assert outfile.exists() |
| 23 | + assert self.outfile.exists() |
51 | 24 |
|
52 | | - with open(outfile) as fh: |
| 25 | + with open(self.outfile) as fh: |
53 | 26 | out = fh.read() |
54 | 27 |
|
55 | 28 | assert "nf-core/testpipeline" in out |
56 | 29 |
|
57 | | - def test_build_template_invalid_schema(self, caplog): |
| 30 | + def test_build_template_invalid_schema(self): |
58 | 31 | """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) |
62 | 42 |
|
63 | 43 | 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 |
65 | 45 |
|
66 | | - def test_build_template_file_exists(self, caplog): |
| 46 | + def test_build_template_file_exists(self): |
67 | 47 | """Build a schema from a template""" |
68 | 48 |
|
69 | 49 | # 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() |
73 | 51 |
|
74 | | - res = self.params_template_builder.write_params_file(outfile) |
| 52 | + res = self.params_template_builder.write_params_file(self.outfile) |
75 | 53 |
|
76 | 54 | 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() |
78 | 58 |
|
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 |
0 commit comments