Skip to content

Commit 593a860

Browse files
[primer] Add a test for truncating comments
1 parent 69ce1ce commit 593a860

File tree

4 files changed

+83
-30
lines changed

4 files changed

+83
-30
lines changed

.pyenchant_pylint_custom_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ iterables
154154
iteritems
155155
jn
156156
jpg
157+
json
157158
jx
158159
jython
159160
# class is a reserved word

pylint/testutils/_primer/primer_command.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616

1717
class PrimerCommand:
18-
1918
"""Generic primer action with required arguments."""
2019

2120
def __init__(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
🤖 **Effect of this PR on checked open source code:** 🤖
2+
3+
4+
5+
**Effect on [astroid](https://github.com/PyCQA/astroid):**
6+
The following messages are now emitted:
7+
8+
<details>
9+
10+
1) locally-disabled:
11+
*Locally disabling redefined-builtin [we added some text in the message] (W0622)*
12+
https://github.com/PyCQA/astroid/blob/main/astroid/__init__.py#L91
13+
14+
</details>
15+
16+
The fol...
17+
18+
*This comment was truncated because GitHub allows only 500 characters in a comment.*
19+
20+
*This comment was generated for commit v2.14.2*

tests/testutils/_primer/test_primer.py

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

55
"""Test the primer commands. """
6+
from __future__ import annotations
7+
68
import sys
79
from pathlib import Path
810
from unittest.mock import patch
@@ -20,39 +22,70 @@
2022

2123
PRIMER_CURRENT_INTERPRETER = (3, 10)
2224

25+
DEFAULT_ARGS = ["python tests/primer/__main__.py", "compare", "--commit=v2.14.2"]
26+
2327

2428
@pytest.mark.skipif(
2529
sys.platform in {"win32", "darwin"},
26-
reason="Primers are internal will never be run on costly github action (mac or windows)",
30+
reason=(
31+
"Primers are internal and will never be run on costly github action (mac or windows)"
32+
),
2733
)
2834
@pytest.mark.skipif(
2935
sys.version_info[:2] != PRIMER_CURRENT_INTERPRETER or IS_PYPY,
30-
reason=f"Primers are internal will always be run for only one interpreter (currently {PRIMER_CURRENT_INTERPRETER})",
31-
)
32-
@pytest.mark.parametrize(
33-
"directory",
34-
[
35-
pytest.param(p, id=str(p.relative_to(FIXTURES_PATH)))
36-
for p in FIXTURES_PATH.iterdir()
37-
if p.is_dir()
38-
],
36+
reason=(
37+
"Primers are internal and will always be run for only one interpreter (currently"
38+
f" {PRIMER_CURRENT_INTERPRETER})"
39+
),
3940
)
40-
def test_compare(directory: Path) -> None:
41-
main = directory / "main.json"
42-
pr = directory / "pr.json"
43-
expected_file = directory / "expected.txt"
44-
new_argv = [
45-
"python tests/primer/__main__.py",
46-
"compare",
47-
"--commit=v2.14.2",
48-
f"--base-file={main}",
49-
f"--new-file={pr}",
50-
]
51-
with patch("sys.argv", new_argv):
52-
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
53-
with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
54-
content = f.read()
55-
with open(expected_file, encoding="utf8") as f:
56-
expected = f.read()
57-
# rstrip so the expected.txt can end with a newline
58-
assert content == expected.rstrip("\n")
41+
class TestPrimer:
42+
@pytest.mark.parametrize(
43+
"directory",
44+
[
45+
pytest.param(p, id=str(p.relative_to(FIXTURES_PATH)))
46+
for p in FIXTURES_PATH.iterdir()
47+
if p.is_dir()
48+
],
49+
)
50+
def test_compare(self, directory: Path) -> None:
51+
"""Test for the standard case.
52+
53+
Directory in 'fixtures/' with 'main.json', 'pr.json' and 'expected.txt'."""
54+
self.__assert_expected(directory)
55+
56+
def test_truncated_compare(self) -> None:
57+
"""Test for the truncation of comments that are too long."""
58+
max_comment_length = 500
59+
directory = FIXTURES_PATH / "message_changed"
60+
with patch(
61+
"pylint.testutils._primer.primer_compare_command.MAX_GITHUB_COMMENT_LENGTH",
62+
max_comment_length,
63+
):
64+
content = self.__assert_expected(
65+
directory, expected_file=directory / "expected_truncated.txt"
66+
)
67+
assert len(content) < max_comment_length
68+
69+
@staticmethod
70+
def __assert_expected(
71+
directory: Path,
72+
main: Path | None = None,
73+
pr: Path | None = None,
74+
expected_file: Path | None = None,
75+
) -> str:
76+
if main is None:
77+
main = directory / "main.json"
78+
if pr is None:
79+
pr = directory / "pr.json"
80+
if expected_file is None:
81+
expected_file = directory / "expected.txt"
82+
new_argv = DEFAULT_ARGS + [f"--base-file={main}", f"--new-file={pr}"]
83+
with patch("sys.argv", new_argv):
84+
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
85+
with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
86+
content = f.read()
87+
with open(expected_file, encoding="utf8") as f:
88+
expected = f.read()
89+
# rstrip so the expected.txt can end with a newline
90+
assert content == expected.rstrip("\n")
91+
return content

0 commit comments

Comments
 (0)