Skip to content

Fix #86 -- Add support for an optional regex dependency #85

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

Merged
merged 18 commits into from
Aug 9, 2024
Merged
21 changes: 20 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,28 @@ jobs:
- run: py.test --cov=.
- uses: codecov/codecov-action@v4


extras:
runs-on: ubuntu-latest
strategy:
matrix:
extras:
- "regex"

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- run: python -m pip install --upgrade pip setuptools
- run: python -m pip install -e .[test,${{ matrix.extras }}]
- run: relint --version
- run: py.test --cov=.
- uses: codecov/codecov-action@v4

analyze:
name: CodeQL Analyze
needs: [PyTest]
needs: [PyTest,extras]
runs-on: ubuntu-latest
permissions:
actions: read
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

```shell-session
python3 -m pip install relint
# or, if you have super advanced linting expressions
python3 -m pip install relint[regex]
```

## [Examples & Recipes – The reLint Cookbook](https://github.com/codingjoe/relint/blob/main/COOKBOOK.md)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ test = [
"pytest-cov",
"pytest-mock",
]
regex = [
"regex"
]

[project.scripts]
relint = "relint.__main__:main"
Expand Down
8 changes: 6 additions & 2 deletions relint/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import collections
import re
import warnings

try:
import regex as re
except ImportError:
import re

import yaml

from .exceptions import ConfigError
Expand Down Expand Up @@ -29,7 +33,7 @@ def load_config(path, fail_warnings, ignore_warnings):
file_pattern = re.compile(file_pattern)
yield Test(
name=test["name"],
pattern=re.compile(test["pattern"], re.MULTILINE),
pattern=re.compile(test["pattern"]),
hint=test.get("hint"),
file_pattern=file_pattern,
error=test.get("error", True) or fail_warnings,
Expand Down
10 changes: 7 additions & 3 deletions relint/parse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

import collections
import re

try:
import regex as re
except ImportError:
import re

from rich import print as rprint
from rich.console import Group
Expand Down Expand Up @@ -65,7 +69,7 @@ def parse_line_numbers(output):


def parse_filenames(output):
return re.findall(GIT_DIFF_FILENAME_PATTERN, output)
return GIT_DIFF_FILENAME_PATTERN.findall(output)


def split_diff_content_by_filename(output: str) -> {str: str}:
Expand All @@ -81,7 +85,7 @@ def split_diff_content_by_filename(output: str) -> {str: str}:
"""
content_by_filename = {}
filenames = parse_filenames(output)
split_content = re.split(GIT_DIFF_SPLIT_PATTERN, output)
split_content = GIT_DIFF_SPLIT_PATTERN.split(output)
split_content = filter(lambda x: x != "", split_content)

for filename, content in zip(filenames, split_content):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import pytest

from relint.__main__ import main
from relint.config import Test
from relint.exceptions import ConfigError
from relint.parse import (
lint_file,
match_with_diff_changes,
parse_diff,
parse_filenames,
Expand Down Expand Up @@ -177,3 +179,40 @@ def test_no_unicode(capsys, tmpdir, fixture_dir):
with pytest.raises(SystemExit) as exc_info:
main(["test.png"])
assert "0" in str(exc_info.value)


def test_cc_linting_rule(tmpdir, fixture_dir):
regex = pytest.importorskip("regex")
cc_file = tmpdir.join("example.cpp")
cc_file.write(
"#include <iostream>\n"
"/* This is an extremely long COMMENT that has over one hundred and twenty characters to test whether this is recognized by the regex or not. */\n"
"int main() {\n"
' std::cout << "This is an extremely long CODE that has over one hundred and twenty characters to test whether this is recognized by the regex or not."\n'
" return 0;\n"
"}\n"
)

with (fixture_dir / ".relint.yml").open() as fs:
config = fs.read()
tmpdir.join(".relint.yml").write(config)

# Load the configuration as Test named tuples

with tmpdir.as_cwd():
assert list(
lint_file(
str(cc_file),
[
Test(
name="No line longer than 120 characters",
pattern=regex.compile(
r".{120,}(?<!\s)(?=\s|$)|.{120,}(?<=\s)(?=\s)"
),
hint="There should be no line longer than 120 characters in a line.",
file_pattern=regex.compile(r".*\.(cpp|h)"),
error=True,
)
],
)
)