Repository: nf-core/tools

Issue:
Running nf-core lint -k still runs other module tests

I tried to run specific tests with `-k pipeline_todos` but for some reason I got an API error from the module tests which shouldn't have been run?

```bash
$ nf-core lint -k pipeline_todos

INFO     Testing pipeline: .                              __init__.py:212
INFO     Only running tests: 'pipeline_todos'             __init__.py:237

╭────────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────────╮
<TRUNCATED>
│ nf-core-tools/lib/python3.7/site-packages/nf_core/modules/module_utils.py:79 in get_module_git_log                                                                     │
│                                                                                                                                                                        │
│    78 │   │   raise LookupError(                                                                                                                                       │
│ ❱  79 │   │   │   f"Unable to fetch commit SHA for module {module_name}. API responded with                                                                            │
│       '{response.status_code}'"                                                                                                                                        │
│    80 │   │   )                                                                                                                                                        │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

LookupError: Unable to fetch commit SHA for module cat/fastq. API responded with '403'
```


Discussion:

Resolving patch:
diff --git a/nf_core/lint/__init__.py b/nf_core/lint/__init__.py
--- a/nf_core/lint/__init__.py
+++ b/nf_core/lint/__init__.py
@@ -42,8 +42,23 @@ def run_linting(
         An object of type :class:`PipelineLint` that contains all the linting results.
     """
 
+    # Verify that the requested tests exist
+    if key:
+        all_tests = set(PipelineLint._get_all_lint_tests(release_mode)).union(set(ModuleLint._get_all_lint_tests()))
+        bad_keys = [k for k in key if k not in all_tests]
+        if len(bad_keys) > 0:
+            raise AssertionError(
+                "Test name{} not recognised: '{}'".format(
+                    "s" if len(bad_keys) > 1 else "",
+                    "', '".join(bad_keys),
+                )
+            )
+        log.info("Only running tests: '{}'".format("', '".join(key)))
+
     # Create the lint object
-    lint_obj = PipelineLint(pipeline_dir, release_mode, fix, key, fail_ignored)
+    pipeline_keys = list(set(key).intersection(set(PipelineLint._get_all_lint_tests(release_mode)))) if key else []
+
+    lint_obj = PipelineLint(pipeline_dir, release_mode, fix, pipeline_keys, fail_ignored)
 
     # Load the various pipeline configs
     lint_obj._load_lint_config()
@@ -54,7 +69,12 @@ def run_linting(
     module_lint_obj = ModuleLint(pipeline_dir)
 
     # Run only the tests we want
-    module_lint_tests = ("module_changes", "module_version")
+    if key:
+        # Select only the module lint tests
+        module_lint_tests = list(set(key).intersection(set(ModuleLint._get_all_lint_tests())))
+    else:
+        # If no key is supplied, run the default modules tests
+        module_lint_tests = ("module_changes", "module_version")
     module_lint_obj.filter_tests_by_key(module_lint_tests)
 
     # Set up files for modules linting test
@@ -154,7 +174,14 @@ def __init__(self, wf_path, release_mode=False, fix=(), key=(), fail_ignored=Fal
         self.passed = []
         self.warned = []
         self.could_fix = []
-        self.lint_tests = [
+        self.lint_tests = self._get_all_lint_tests(self.release_mode)
+        self.fix = fix
+        self.key = key
+        self.progress_bar = None
+
+    @staticmethod
+    def _get_all_lint_tests(release_mode):
+        return [
             "files_exist",
             "nextflow_config",
             "files_unchanged",
@@ -171,12 +198,7 @@ def __init__(self, wf_path, release_mode=False, fix=(), key=(), fail_ignored=Fal
             "actions_schema_validation",
             "merge_markers",
             "modules_json",
-        ]
-        if self.release_mode:
-            self.lint_tests.extend(["version_consistency"])
-        self.fix = fix
-        self.key = key
-        self.progress_bar = None
+        ] + (["version_consistency"] if release_mode else [])
 
     def _load(self):
         """Load information about the pipeline into the PipelineLint object"""
@@ -234,7 +256,6 @@ def _lint_pipeline(self):
 
         # If -k supplied, only run these tests
         if self.key:
-            log.info("Only running tests: '{}'".format("', '".join(self.key)))
             self.lint_tests = [k for k in self.lint_tests if k in self.key]
 
         # Check that the pipeline_dir is a clean git repo
diff --git a/nf_core/modules/lint/__init__.py b/nf_core/modules/lint/__init__.py
--- a/nf_core/modules/lint/__init__.py
+++ b/nf_core/modules/lint/__init__.py
@@ -79,8 +79,7 @@ def __init__(self, dir):
         self.warned = []
         self.failed = []
         self.modules_repo = ModulesRepo()
-        self.lint_tests = ["main_nf", "functions_nf", "meta_yml", "module_changes", "module_todos"]
-
+        self.lint_tests = self._get_all_lint_tests()
         # Get lists of modules install in directory
         self.all_local_modules, self.all_nfcore_modules = self.get_installed_modules()
 
@@ -95,6 +94,10 @@ def __init__(self, dir):
             # Add as first test to load git_sha before module_changes
             self.lint_tests.insert(0, "module_version")
 
+    @staticmethod
+    def _get_all_lint_tests():
+        return ["main_nf", "functions_nf", "meta_yml", "module_changes", "module_todos"]
+
     def lint(self, module=None, key=(), all_modules=False, print_results=True, show_passed=False, local=False):
         """
         Lint all or one specific module


Test patch: