Skip to content

Commit 4eef740

Browse files
yangchtcodingjoe
andauthored
Fix #86 -- Add support for an optional regex dependency (#85)
Co-authored-by: Johannes Maron <[email protected]>
1 parent 34cd939 commit 4eef740

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,28 @@ jobs:
6363
- run: py.test --cov=.
6464
- uses: codecov/codecov-action@v4
6565

66+
67+
extras:
68+
runs-on: ubuntu-latest
69+
strategy:
70+
matrix:
71+
extras:
72+
- "regex"
73+
74+
steps:
75+
- uses: actions/checkout@v4
76+
- uses: actions/setup-python@v5
77+
with:
78+
python-version: "3.x"
79+
- run: python -m pip install --upgrade pip setuptools
80+
- run: python -m pip install -e .[test,${{ matrix.extras }}]
81+
- run: relint --version
82+
- run: py.test --cov=.
83+
- uses: codecov/codecov-action@v4
84+
6685
analyze:
6786
name: CodeQL Analyze
68-
needs: [PyTest]
87+
needs: [PyTest,extras]
6988
runs-on: ubuntu-latest
7089
permissions:
7190
actions: read

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
```shell-session
1616
python3 -m pip install relint
17+
# or, if you have super advanced linting expressions
18+
python3 -m pip install relint[regex]
1719
```
1820

1921
## [Examples & Recipes – The reLint Cookbook](https://github.com/codingjoe/relint/blob/main/COOKBOOK.md)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ test = [
3535
"pytest-cov",
3636
"pytest-mock",
3737
]
38+
regex = [
39+
"regex"
40+
]
3841

3942
[project.scripts]
4043
relint = "relint.__main__:main"

relint/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import collections
2-
import re
32
import warnings
43

4+
try:
5+
import regex as re
6+
except ImportError:
7+
import re
8+
59
import yaml
610

711
from .exceptions import ConfigError
@@ -29,7 +33,7 @@ def load_config(path, fail_warnings, ignore_warnings):
2933
file_pattern = re.compile(file_pattern)
3034
yield Test(
3135
name=test["name"],
32-
pattern=re.compile(test["pattern"], re.MULTILINE),
36+
pattern=re.compile(test["pattern"]),
3337
hint=test.get("hint"),
3438
file_pattern=file_pattern,
3539
error=test.get("error", True) or fail_warnings,

relint/parse.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

33
import collections
4-
import re
4+
5+
try:
6+
import regex as re
7+
except ImportError:
8+
import re
59

610
from rich import print as rprint
711
from rich.console import Group
@@ -65,7 +69,7 @@ def parse_line_numbers(output):
6569

6670

6771
def parse_filenames(output):
68-
return re.findall(GIT_DIFF_FILENAME_PATTERN, output)
72+
return GIT_DIFF_FILENAME_PATTERN.findall(output)
6973

7074

7175
def split_diff_content_by_filename(output: str) -> {str: str}:
@@ -81,7 +85,7 @@ def split_diff_content_by_filename(output: str) -> {str: str}:
8185
"""
8286
content_by_filename = {}
8387
filenames = parse_filenames(output)
84-
split_content = re.split(GIT_DIFF_SPLIT_PATTERN, output)
88+
split_content = GIT_DIFF_SPLIT_PATTERN.split(output)
8589
split_content = filter(lambda x: x != "", split_content)
8690

8791
for filename, content in zip(filenames, split_content):

tests/test_parse.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import pytest
55

66
from relint.__main__ import main
7+
from relint.config import Test
78
from relint.exceptions import ConfigError
89
from relint.parse import (
10+
lint_file,
911
match_with_diff_changes,
1012
parse_diff,
1113
parse_filenames,
@@ -177,3 +179,40 @@ def test_no_unicode(capsys, tmpdir, fixture_dir):
177179
with pytest.raises(SystemExit) as exc_info:
178180
main(["test.png"])
179181
assert "0" in str(exc_info.value)
182+
183+
184+
def test_cc_linting_rule(tmpdir, fixture_dir):
185+
regex = pytest.importorskip("regex")
186+
cc_file = tmpdir.join("example.cpp")
187+
cc_file.write(
188+
"#include <iostream>\n"
189+
"/* 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"
190+
"int main() {\n"
191+
' 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'
192+
" return 0;\n"
193+
"}\n"
194+
)
195+
196+
with (fixture_dir / ".relint.yml").open() as fs:
197+
config = fs.read()
198+
tmpdir.join(".relint.yml").write(config)
199+
200+
# Load the configuration as Test named tuples
201+
202+
with tmpdir.as_cwd():
203+
assert list(
204+
lint_file(
205+
str(cc_file),
206+
[
207+
Test(
208+
name="No line longer than 120 characters",
209+
pattern=regex.compile(
210+
r".{120,}(?<!\s)(?=\s|$)|.{120,}(?<=\s)(?=\s)"
211+
),
212+
hint="There should be no line longer than 120 characters in a line.",
213+
file_pattern=regex.compile(r".*\.(cpp|h)"),
214+
error=True,
215+
)
216+
],
217+
)
218+
)

0 commit comments

Comments
 (0)