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

Add lint test for merge markers #861

Merged
merged 3 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

### Linting

* Added lint check for merge markers [[#321]](https://github.com/nf-core/tools/issues/321)
* Added new option `--fix` to automatically correct some problems detected by linting
* Added validation of default params to `nf-core schema lint` [[#823](https://github.com/nf-core/tools/issues/823)]
* Added schema validation of GitHub action workflows to lint function [[#795](https://github.com/nf-core/tools/issues/795)]
Expand Down
4 changes: 4 additions & 0 deletions docs/api/_src/lint_tests/merge_markers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
merge_markers
==============

.. automethod:: nf_core.lint.PipelineLint.merge_markers
2 changes: 2 additions & 0 deletions nf_core/lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class PipelineLint(nf_core.utils.Pipeline):
from .schema_lint import schema_lint
from .schema_params import schema_params
from .actions_schema_validation import actions_schema_validation
from .merge_markers import merge_markers

def __init__(self, wf_path, release_mode=False, fix=()):
""" Initialise linting object """
Expand Down Expand Up @@ -144,6 +145,7 @@ def __init__(self, wf_path, release_mode=False, fix=()):
"schema_lint",
"schema_params",
"actions_schema_validation",
"merge_markers",
]
if self.release_mode:
self.lint_tests.extend(["version_consistency"])
Expand Down
43 changes: 43 additions & 0 deletions nf_core/lint/merge_markers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

import logging
import os
import io
import fnmatch

log = logging.getLogger(__name__)


def merge_markers(self):
"""Check for remaining merge markers.

This test looks for remaining merge markers in the code, e.g.:
>>>>>>> or <<<<<<<


"""
passed = []
warned = []
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
failed = []

ignore = [".git"]
if os.path.isfile(os.path.join(self.wf_path, ".gitignore")):
with io.open(os.path.join(self.wf_path, ".gitignore"), "rt", encoding="latin1") as fh:
for l in fh:
ignore.append(os.path.basename(l.strip().rstrip("/")))
for root, dirs, files in os.walk(self.wf_path):
# Ignore files
for i in ignore:
dirs = [d for d in dirs if not fnmatch.fnmatch(os.path.join(root, d), i)]
files = [f for f in files if not fnmatch.fnmatch(os.path.join(root, f), i)]
for fname in files:
try:
with io.open(os.path.join(root, fname), "rt", encoding="latin1") as fh:
for l in fh:
if ">>>>>>>" in l:
warned.append(f"Merge marker in `{fname}`: {l}")
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
if "<<<<<<<" in l:
warned.append(f"Merge marker in `{fname}`: {l}")
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
except FileNotFoundError:
log.debug(f"Could not open file {fname} in merge_markers lint test")
return {"passed": passed, "warned": warned, "failed": failed}
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions tests/lint/merge_markers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

import os
import yaml
import nf_core.lint


def test_merge_markers_found(self):
"""Missing 'jobs' field should result in failure"""
new_pipeline = self._make_pipeline_copy()

with open(os.path.join(new_pipeline, "main.nf"), "r") as fh:
main_nf_content = fh.read()
main_nf_content = ">>>>>>>\n" + main_nf_content
with open(os.path.join(new_pipeline, "main.nf"), "w") as fh:
fh.write(main_nf_content)

lint_obj = nf_core.lint.PipelineLint(new_pipeline)
lint_obj._load()

results = lint_obj.merge_markers()
assert len(results["warned"]) > 0
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
assert "Merge marker in `main.nf`: >>>>>>>\n" == results["warned"][0]
KevinMenden marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def test_sphinx_rst_files(self):
test_actions_schema_validation_missing_on,
)

from lint.merge_markers import test_merge_markers_found


# def test_critical_missingfiles_example(self):
# """Tests for missing nextflow config and main.nf files"""
Expand Down