|
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
|
|
20 | 22 |
|
21 | 23 | PRIMER_CURRENT_INTERPRETER = (3, 10)
|
22 | 24 |
|
| 25 | +DEFAULT_ARGS = ["python tests/primer/__main__.py", "compare", "--commit=v2.14.2"] |
| 26 | + |
23 | 27 |
|
24 | 28 | @pytest.mark.skipif(
|
25 | 29 | 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 | + ), |
27 | 33 | )
|
28 | 34 | @pytest.mark.skipif(
|
29 | 35 | 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 | + ), |
39 | 40 | )
|
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