Skip to content

Commit 1194eec

Browse files
[primer] refactor the test to use a common function
1 parent ff40caa commit 1194eec

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

tests/testutils/_primer/test_primer.py

Lines changed: 33 additions & 22 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
@@ -46,35 +48,44 @@ class TestPrimer:
4648
],
4749
)
4850
def test_compare(self, directory: Path) -> None:
49-
main = directory / "main.json"
50-
pr = directory / "pr.json"
51-
expected_file = directory / "expected.txt"
52-
new_argv = DEFAULT_ARGS + [f"--base-file={main}", f"--new-file={pr}"]
53-
with patch("sys.argv", new_argv):
54-
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
55-
with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
56-
content = f.read()
57-
with open(expected_file, encoding="utf8") as f:
58-
expected = f.read()
59-
# rstrip so the expected.txt can end with a newline
60-
assert content == expected.rstrip("\n")
51+
"""Test for the standard case.
52+
53+
Directory in 'fixtures/' with 'main.json', 'pr.json' and 'expected.txt'."""
54+
self.__assert_expected(directory)
6155

6256
def test_truncated_compare(self) -> None:
63-
FAKE_MAX_GITHUB_COMMENT_LENGTH = 500
64-
main = FIXTURES_PATH / "message_changed/main.json"
65-
pr = FIXTURES_PATH / "message_changed/pr.json"
66-
expected_file = FIXTURES_PATH / "message_changed/expected_truncated.txt"
57+
"""Test for the truncation of comment 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"
6782
new_argv = DEFAULT_ARGS + [f"--base-file={main}", f"--new-file={pr}"]
6883
with patch("sys.argv", new_argv):
69-
with patch(
70-
"pylint.testutils._primer.primer_compare_command.MAX_GITHUB_COMMENT_LENGTH",
71-
FAKE_MAX_GITHUB_COMMENT_LENGTH,
72-
):
73-
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
84+
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
7485
with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
7586
content = f.read()
76-
assert len(content) < FAKE_MAX_GITHUB_COMMENT_LENGTH
7787
with open(expected_file, encoding="utf8") as f:
7888
expected = f.read()
7989
# rstrip so the expected.txt can end with a newline
8090
assert content == expected.rstrip("\n")
91+
return content

0 commit comments

Comments
 (0)