Skip to content

Commit

Permalink
[primer] refactor the test to use a common function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jun 20, 2022
1 parent ff40caa commit 1194eec
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions tests/testutils/_primer/test_primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,35 +48,44 @@ class TestPrimer:
],
)
def test_compare(self, directory: Path) -> None:
main = directory / "main.json"
pr = directory / "pr.json"
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")
"""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:
FAKE_MAX_GITHUB_COMMENT_LENGTH = 500
main = FIXTURES_PATH / "message_changed/main.json"
pr = FIXTURES_PATH / "message_changed/pr.json"
expected_file = FIXTURES_PATH / "message_changed/expected_truncated.txt"
"""Test for the truncation of comment 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):
with patch(
"pylint.testutils._primer.primer_compare_command.MAX_GITHUB_COMMENT_LENGTH",
FAKE_MAX_GITHUB_COMMENT_LENGTH,
):
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
Primer(PRIMER_DIRECTORY, PACKAGES_TO_PRIME_PATH).run()
with open(PRIMER_DIRECTORY / "comment.txt", encoding="utf8") as f:
content = f.read()
assert len(content) < FAKE_MAX_GITHUB_COMMENT_LENGTH
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

0 comments on commit 1194eec

Please sign in to comment.