From ee5bc6eda76cfdb652e97949a9550f686a15fb93 Mon Sep 17 00:00:00 2001 From: Gregor Sturm Date: Fri, 29 Nov 2024 13:14:12 +0100 Subject: [PATCH] Close #62 --- CHANGELOG.md | 8 ++++++-- src/dso/lint.py | 9 +++++++++ tests/test_lint.py | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beb5439..de64276 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,15 @@ and this project adheres to [Semantic Versioning][]. [keep a changelog]: https://keepachangelog.com/en/1.0.0/ [semantic versioning]: https://semver.org/spec/v2.0.0.html -## [Unreleased] +## v0.10.1 + +### Fixes + +- Take comments into account when linting for `DSO001` ### Template updates -- Do not ignore gitignore files in output/report directories of template ([#63](https://github.com/Boehringer-Ingelheim/dso/pull/63)) +- Do not ignore the `.gitignore` files in output/report directories of template ([#63](https://github.com/Boehringer-Ingelheim/dso/pull/63)) ## v0.10.0 diff --git a/src/dso/lint.py b/src/dso/lint.py index 304e211..3dad81c 100644 --- a/src/dso/lint.py +++ b/src/dso/lint.py @@ -87,6 +87,15 @@ def check(cls, file): # .parent to remove the dvc.yaml filename stage_path_expected = str(stage_path_expected.parent.relative_to(root_path)) content = file.read_text() + + # remove comments + # TODO there are still edge cases, e.g. a `#` within a string doesn't initiate a comment + # However this is hard/impossible (?) to solve with a regular expression alone, we'd need a proper + # R parse to address all edge cases. See also https://github.com/Boehringer-Ingelheim/dso/issues/66 + pattern_is_comment = re.compile(r"#.*$") + content = "\n".join([re.sub(pattern_is_comment, "", line) for line in content.split("\n")]) + + # detect pattern pattern = r"[\s\S]*?(dso::)?read_params\s*\(([\s\S]*?)(\s*,.*)?\)" res = re.findall(pattern, content, flags=re.MULTILINE) if len(res) == 0: diff --git a/tests/test_lint.py b/tests/test_lint.py index bb3ea91..bfc6718 100644 --- a/tests/test_lint.py +++ b/tests/test_lint.py @@ -116,6 +116,21 @@ class MockQuartoRule(QuartoRule): """, LintError, ), + ( + """\ + params = read_params("quarto_stage") + # params = read_params("quarto_stage") + """, + None, + ), + # TODO no good way to cover that with regex alone, see https://github.com/Boehringer-Ingelheim/dso/issues/66 + # ( + # """\ + # params = read_params("quarto_stage") + # print(" foo # no comment"); params = read_params("quarto_stage") + # """, + # LintError, + # ), ( """\ params = read_params("wrong_path")