diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-06-19 20:46:19 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-06-20 10:22:47 +0200 |
commit | 593a86077d2f1abd25f637db42aafa284dd0c77f (patch) | |
tree | 7882d5bcccb5dfb1d3667c91611aec3d3f6b29bb | |
parent | 69ce1ce5f0f8c6cee7ef4bafa354304dc9aeeee4 (diff) | |
download | pylint-git-593a86077d2f1abd25f637db42aafa284dd0c77f.tar.gz |
[primer] Add a test for truncating comments
-rw-r--r-- | .pyenchant_pylint_custom_dict.txt | 1 | ||||
-rw-r--r-- | pylint/testutils/_primer/primer_command.py | 1 | ||||
-rw-r--r-- | tests/testutils/_primer/fixtures/message_changed/expected_truncated.txt | 20 | ||||
-rw-r--r-- | tests/testutils/_primer/test_primer.py | 91 |
4 files changed, 83 insertions, 30 deletions
diff --git a/.pyenchant_pylint_custom_dict.txt b/.pyenchant_pylint_custom_dict.txt index 80918ac5a..825ffe1f8 100644 --- a/.pyenchant_pylint_custom_dict.txt +++ b/.pyenchant_pylint_custom_dict.txt @@ -154,6 +154,7 @@ iterables iteritems jn jpg +json jx jython # class is a reserved word diff --git a/pylint/testutils/_primer/primer_command.py b/pylint/testutils/_primer/primer_command.py index 0d09e7b0a..b3a39445f 100644 --- a/pylint/testutils/_primer/primer_command.py +++ b/pylint/testutils/_primer/primer_command.py @@ -15,7 +15,6 @@ PackageMessages = Dict[str, List[Dict[str, Union[str, int]]]] class PrimerCommand: - """Generic primer action with required arguments.""" def __init__( diff --git a/tests/testutils/_primer/fixtures/message_changed/expected_truncated.txt b/tests/testutils/_primer/fixtures/message_changed/expected_truncated.txt new file mode 100644 index 000000000..e8db96a3e --- /dev/null +++ b/tests/testutils/_primer/fixtures/message_changed/expected_truncated.txt @@ -0,0 +1,20 @@ +🤖 **Effect of this PR on checked open source code:** 🤖 + + + +**Effect on [astroid](https://github.com/PyCQA/astroid):** +The following messages are now emitted: + +<details> + +1) locally-disabled: +*Locally disabling redefined-builtin [we added some text in the message] (W0622)* +https://github.com/PyCQA/astroid/blob/main/astroid/__init__.py#L91 + +</details> + +The fol... + +*This comment was truncated because GitHub allows only 500 characters in a comment.* + +*This comment was generated for commit v2.14.2* diff --git a/tests/testutils/_primer/test_primer.py b/tests/testutils/_primer/test_primer.py index 99ef4bcbd..3093fa98e 100644 --- a/tests/testutils/_primer/test_primer.py +++ b/tests/testutils/_primer/test_primer.py @@ -3,6 +3,8 @@ # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt """Test the primer commands. """ +from __future__ import annotations + import sys from pathlib import Path from unittest.mock import patch @@ -20,39 +22,70 @@ FIXTURES_PATH = HERE / "fixtures" PRIMER_CURRENT_INTERPRETER = (3, 10) +DEFAULT_ARGS = ["python tests/primer/__main__.py", "compare", "--commit=v2.14.2"] + @pytest.mark.skipif( sys.platform in {"win32", "darwin"}, - reason="Primers are internal will never be run on costly github action (mac or windows)", + reason=( + "Primers are internal and will never be run on costly github action (mac or windows)" + ), ) @pytest.mark.skipif( sys.version_info[:2] != PRIMER_CURRENT_INTERPRETER or IS_PYPY, - reason=f"Primers are internal will always be run for only one interpreter (currently {PRIMER_CURRENT_INTERPRETER})", -) -@pytest.mark.parametrize( - "directory", - [ - pytest.param(p, id=str(p.relative_to(FIXTURES_PATH))) - for p in FIXTURES_PATH.iterdir() - if p.is_dir() - ], + reason=( + "Primers are internal and will always be run for only one interpreter (currently" + f" {PRIMER_CURRENT_INTERPRETER})" + ), ) -def test_compare(directory: Path) -> None: - main = directory / "main.json" - pr = directory / "pr.json" - expected_file = directory / "expected.txt" - new_argv = [ - "python tests/primer/__main__.py", - "compare", - "--commit=v2.14.2", - f"--base-file={main}", - f"--new-file={pr}", - ] - with patch("sys.argv", new_argv): - Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run() - with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f: - content = f.read() - with open(expected_file, encoding="utf8") as f: - expected = f.read() - # rstrip so the expected.txt can end with a newline - assert content == expected.rstrip("\n") +class TestPrimer: + @pytest.mark.parametrize( + "directory", + [ + pytest.param(p, id=str(p.relative_to(FIXTURES_PATH))) + for p in FIXTURES_PATH.iterdir() + if p.is_dir() + ], + ) + def test_compare(self, directory: Path) -> None: + """Test for the standard case. + + Directory in 'fixtures/' with 'main.json', 'pr.json' and 'expected.txt'.""" + self.__assert_expected(directory) + + def test_truncated_compare(self) -> None: + """Test for the truncation of comments that are too long.""" + max_comment_length = 500 + directory = FIXTURES_PATH / "message_changed" + with patch( + "pylint.testutils._primer.primer_compare_command.MAX_GITHUB_COMMENT_LENGTH", + max_comment_length, + ): + content = self.__assert_expected( + directory, expected_file=directory / "expected_truncated.txt" + ) + assert len(content) < max_comment_length + + @staticmethod + def __assert_expected( + directory: Path, + main: Path | None = None, + pr: Path | None = None, + expected_file: Path | None = None, + ) -> str: + if main is None: + main = directory / "main.json" + if pr is None: + pr = directory / "pr.json" + if expected_file is None: + expected_file = directory / "expected.txt" + new_argv = DEFAULT_ARGS + [f"--base-file={main}", f"--new-file={pr}"] + with patch("sys.argv", new_argv): + Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run() + with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f: + content = f.read() + with open(expected_file, encoding="utf8") as f: + expected = f.read() + # rstrip so the expected.txt can end with a newline + assert content == expected.rstrip("\n") + return content |