Skip to content

Commit d7c9b30

Browse files
committed
move common test path into a property
1 parent cae7443 commit d7c9b30

File tree

2 files changed

+26
-193
lines changed

2 files changed

+26
-193
lines changed

tests/modules/test_lint.py

Lines changed: 23 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -394,35 +394,10 @@ def test_modules_lint_snapshot_file_not_needed(self):
394394

395395
def test_modules_environment_yml_file_doesnt_exists(self):
396396
"""Test linting a module with an environment.yml file"""
397-
Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test", "environment.yml").rename(
398-
Path(
399-
self.nfcore_modules,
400-
"modules",
401-
"nf-core",
402-
"bpipe",
403-
"test",
404-
"environment.yml.bak",
405-
)
406-
)
397+
(self.bpipe_test_module_path / "environment.yml").rename(self.bpipe_test_module_path / "environment.yml.bak")
407398
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
408399
module_lint.lint(print_results=False, module="bpipe/test")
409-
Path(
410-
self.nfcore_modules,
411-
"modules",
412-
"nf-core",
413-
"bpipe",
414-
"test",
415-
"environment.yml.bak",
416-
).rename(
417-
Path(
418-
self.nfcore_modules,
419-
"modules",
420-
"nf-core",
421-
"bpipe",
422-
"test",
423-
"environment.yml",
424-
)
425-
)
400+
(self.bpipe_test_module_path / "environment.yml.bak").rename(self.bpipe_test_module_path / "environment.yml")
426401
assert len(module_lint.failed) == 1, f"Linting failed with {[x.__dict__ for x in module_lint.failed]}"
427402
assert len(module_lint.passed) > 0
428403
assert len(module_lint.warned) >= 0
@@ -438,32 +413,13 @@ def test_modules_environment_yml_file_sorted_correctly(self):
438413

439414
def test_modules_environment_yml_file_sorted_incorrectly(self):
440415
"""Test linting a module with an incorrectly sorted environment.yml file"""
441-
with open(
442-
Path(
443-
self.nfcore_modules,
444-
"modules",
445-
"nf-core",
446-
"bpipe",
447-
"test",
448-
"environment.yml",
449-
)
450-
) as fh:
416+
with open(self.bpipe_test_module_path / "environment.yml") as fh:
451417
yaml_content = yaml.safe_load(fh)
452418
# Add a new dependency to the environment.yml file and reverse the order
453419
yaml_content["dependencies"].append("z=0.0.0")
454420
yaml_content["dependencies"].reverse()
455421
yaml_content = yaml.dump(yaml_content)
456-
with open(
457-
Path(
458-
self.nfcore_modules,
459-
"modules",
460-
"nf-core",
461-
"bpipe",
462-
"test",
463-
"environment.yml",
464-
),
465-
"w",
466-
) as fh:
422+
with open(self.bpipe_test_module_path / "environment.yml", "w") as fh:
467423
fh.write(yaml_content)
468424
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
469425
module_lint.lint(print_results=False, module="bpipe/test")
@@ -474,29 +430,10 @@ def test_modules_environment_yml_file_sorted_incorrectly(self):
474430

475431
def test_modules_environment_yml_file_not_array(self):
476432
"""Test linting a module with an incorrectly formatted environment.yml file"""
477-
with open(
478-
Path(
479-
self.nfcore_modules,
480-
"modules",
481-
"nf-core",
482-
"bpipe",
483-
"test",
484-
"environment.yml",
485-
)
486-
) as fh:
433+
with open(self.bpipe_test_module_path / "environment.yml") as fh:
487434
yaml_content = yaml.safe_load(fh)
488435
yaml_content["dependencies"] = "z"
489-
with open(
490-
Path(
491-
self.nfcore_modules,
492-
"modules",
493-
"nf-core",
494-
"bpipe",
495-
"test",
496-
"environment.yml",
497-
),
498-
"w",
499-
) as fh:
436+
with open(self.bpipe_test_module_path / "environment.yml", "w") as fh:
500437
fh.write(yaml.dump(yaml_content))
501438
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
502439
module_lint.lint(print_results=False, module="bpipe/test")
@@ -507,16 +444,7 @@ def test_modules_environment_yml_file_not_array(self):
507444

508445
def test_modules_environment_yml_file_mixed_dependencies(self):
509446
"""Test linting a module with mixed-type dependencies (strings and pip dict)"""
510-
with open(
511-
Path(
512-
self.nfcore_modules,
513-
"modules",
514-
"nf-core",
515-
"bpipe",
516-
"test",
517-
"environment.yml",
518-
)
519-
) as fh:
447+
with open(self.bpipe_test_module_path / "environment.yml") as fh:
520448
yaml_content = yaml.safe_load(fh)
521449

522450
# Create mixed dependencies with strings and pip dict in wrong order
@@ -528,33 +456,14 @@ def test_modules_environment_yml_file_mixed_dependencies(self):
528456
"pip=23.3.1",
529457
]
530458

531-
with open(
532-
Path(
533-
self.nfcore_modules,
534-
"modules",
535-
"nf-core",
536-
"bpipe",
537-
"test",
538-
"environment.yml",
539-
),
540-
"w",
541-
) as fh:
459+
with open(self.bpipe_test_module_path / "environment.yml", "w") as fh:
542460
fh.write(yaml.dump(yaml_content))
543461

544462
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
545463
module_lint.lint(print_results=False, module="bpipe/test")
546464

547465
# Check that the dependencies were sorted correctly
548-
with open(
549-
Path(
550-
self.nfcore_modules,
551-
"modules",
552-
"nf-core",
553-
"bpipe",
554-
"test",
555-
"environment.yml",
556-
)
557-
) as fh:
466+
with open(self.bpipe_test_module_path / "environment.yml") as fh:
558467
sorted_yaml = yaml.safe_load(fh)
559468

560469
expected_deps = [
@@ -572,10 +481,10 @@ def test_modules_environment_yml_file_mixed_dependencies(self):
572481

573482
def test_modules_environment_yml_file_default_channel_fails(self):
574483
"""Test linting a module with a default channel set in the environment.yml file, which should fail"""
575-
with open(Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test", "environment.yml")) as fh:
484+
with open(self.bpipe_test_module_path / "environment.yml") as fh:
576485
yaml_content = yaml.safe_load(fh)
577486
yaml_content["channels"] = ["bioconda", "default"]
578-
with open(Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test", "environment.yml"), "w") as fh:
487+
with open(self.bpipe_test_module_path / "environment.yml", "w") as fh:
579488
fh.write(yaml.dump(yaml_content))
580489
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
581490
module_lint.lint(print_results=False, module="bpipe/test")
@@ -587,11 +496,11 @@ def test_modules_environment_yml_file_default_channel_fails(self):
587496

588497
def test_modules_meta_yml_incorrect_licence_field(self):
589498
"""Test linting a module with an incorrect Licence field in meta.yml"""
590-
with open(Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test", "meta.yml")) as fh:
499+
with open(self.bpipe_test_module_path / "meta.yml") as fh:
591500
meta_yml = yaml.safe_load(fh)
592501
meta_yml["tools"][0]["bpipe"]["licence"] = "[MIT]"
593502
with open(
594-
Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test", "meta.yml"),
503+
self.bpipe_test_module_path / "meta.yml",
595504
"w",
596505
) as fh:
597506
fh.write(yaml.dump(meta_yml))
@@ -669,45 +578,13 @@ def test_modules_missing_test_dir(self):
669578

670579
def test_modules_missing_test_main_nf(self):
671580
"""Test linting a module with a missing test/main.nf file"""
672-
Path(
673-
self.nfcore_modules,
674-
"modules",
675-
"nf-core",
676-
"bpipe",
677-
"test",
678-
"tests",
679-
"main.nf.test",
680-
).rename(
681-
Path(
682-
self.nfcore_modules,
683-
"modules",
684-
"nf-core",
685-
"bpipe",
686-
"test",
687-
"tests",
688-
"main.nf.test.bak",
689-
)
581+
(self.bpipe_test_module_path / "tests" / "main.nf.test").rename(
582+
self.bpipe_test_module_path / "tests" / "main.nf.test.bak"
690583
)
691584
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
692585
module_lint.lint(print_results=False, module="bpipe/test")
693-
Path(
694-
self.nfcore_modules,
695-
"modules",
696-
"nf-core",
697-
"bpipe",
698-
"test",
699-
"tests",
700-
"main.nf.test.bak",
701-
).rename(
702-
Path(
703-
self.nfcore_modules,
704-
"modules",
705-
"nf-core",
706-
"bpipe",
707-
"test",
708-
"tests",
709-
"main.nf.test",
710-
)
586+
(self.bpipe_test_module_path / "tests" / "main.nf.test.bak").rename(
587+
self.bpipe_test_module_path / "tests" / "main.nf.test"
711588
)
712589
assert len(module_lint.failed) == 1, f"Linting failed with {[x.__dict__ for x in module_lint.failed]}"
713590
assert len(module_lint.passed) >= 0
@@ -746,46 +623,15 @@ def test_nftest_failing_linting(self):
746623

747624
def test_modules_absent_version(self):
748625
"""Test linting a nf-test module if the versions is absent in the snapshot file `"""
749-
with open(
750-
Path(
751-
self.nfcore_modules,
752-
"modules",
753-
"nf-core",
754-
"bpipe",
755-
"test",
756-
"tests",
757-
"main.nf.test.snap",
758-
)
759-
) as fh:
626+
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
627+
with open(snap_file) as fh:
760628
content = fh.read()
761629
new_content = content.replace("versions", "foo")
762-
with open(
763-
Path(
764-
self.nfcore_modules,
765-
"modules",
766-
"nf-core",
767-
"bpipe",
768-
"test",
769-
"tests",
770-
"main.nf.test.snap",
771-
),
772-
"w",
773-
) as fh:
630+
with open(snap_file, "w") as fh:
774631
fh.write(new_content)
775632
module_lint = nf_core.modules.lint.ModuleLint(directory=self.nfcore_modules)
776633
module_lint.lint(print_results=False, module="bpipe/test")
777-
with open(
778-
Path(
779-
self.nfcore_modules,
780-
"modules",
781-
"nf-core",
782-
"bpipe",
783-
"test",
784-
"tests",
785-
"main.nf.test.snap",
786-
),
787-
"w",
788-
) as fh:
634+
with open(snap_file, "w") as fh:
789635
fh.write(content)
790636
assert len(module_lint.failed) == 1, f"Linting failed with {[x.__dict__ for x in module_lint.failed]}"
791637
assert len(module_lint.passed) >= 0
@@ -794,15 +640,7 @@ def test_modules_absent_version(self):
794640

795641
def test_modules_empty_file_in_snapshot(self):
796642
"""Test linting a nf-test module with an empty file sha sum in the test snapshot, which should make it fail (if it is not a stub)"""
797-
snap_file = Path(
798-
self.nfcore_modules,
799-
"modules",
800-
"nf-core",
801-
"bpipe",
802-
"test",
803-
"tests",
804-
"main.nf.test.snap",
805-
)
643+
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
806644
snap = json.load(snap_file.open())
807645
content = snap_file.read_text()
808646
snap["my test"]["content"][0]["0"] = "test:md5,d41d8cd98f00b204e9800998ecf8427e"
@@ -823,15 +661,7 @@ def test_modules_empty_file_in_snapshot(self):
823661

824662
def test_modules_empty_file_in_stub_snapshot(self):
825663
"""Test linting a nf-test module with an empty file sha sum in the stub test snapshot, which should make it not fail"""
826-
snap_file = Path(
827-
self.nfcore_modules,
828-
"modules",
829-
"nf-core",
830-
"bpipe",
831-
"test",
832-
"tests",
833-
"main.nf.test.snap",
834-
)
664+
snap_file = self.bpipe_test_module_path / "tests" / "main.nf.test.snap"
835665
snap = json.load(snap_file.open())
836666
content = snap_file.read_text()
837667
snap["my_test_stub"] = {"content": [{"0": "test:md5,d41d8cd98f00b204e9800998ecf8427e", "versions": {}}]}

tests/test_modules.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def setUp(self):
160160
# Set up the nf-core/modules repo dummy
161161
self.nfcore_modules = create_modules_repo_dummy(self.tmp_dir)
162162

163+
# Common path to the bpipe/test module used in tests
164+
self.bpipe_test_module_path = Path(self.nfcore_modules, "modules", "nf-core", "bpipe", "test")
165+
163166
def test_modulesrepo_class(self):
164167
"""Initialise a modules repo object"""
165168
modrepo = nf_core.modules.modules_repo.ModulesRepo()

0 commit comments

Comments
 (0)