|
3 | 3 | # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
|
4 | 4 |
|
5 | 5 | """Test the primer commands. """
|
| 6 | +from __future__ import annotations |
| 7 | + |
6 | 8 | import sys
|
7 | 9 | from pathlib import Path
|
8 | 10 | from unittest.mock import patch
|
@@ -46,35 +48,44 @@ class TestPrimer:
|
46 | 48 | ],
|
47 | 49 | )
|
48 | 50 | 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) |
61 | 55 |
|
62 | 56 | 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" |
67 | 82 | new_argv = DEFAULT_ARGS + [f"--base-file={main}", f"--new-file={pr}"]
|
68 | 83 | 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() |
74 | 85 | with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
|
75 | 86 | content = f.read()
|
76 |
| - assert len(content) < FAKE_MAX_GITHUB_COMMENT_LENGTH |
77 | 87 | with open(expected_file, encoding="utf8") as f:
|
78 | 88 | expected = f.read()
|
79 | 89 | # rstrip so the expected.txt can end with a newline
|
80 | 90 | assert content == expected.rstrip("\n")
|
| 91 | + return content |
0 commit comments