2021-11-28 14:06:08 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
|
|
|
|
|
|
|
|
import csv
|
|
|
|
import os
|
2021-11-28 18:50:59 +00:00
|
|
|
from typing import List, Optional
|
2021-11-28 14:06:08 +00:00
|
|
|
|
2021-11-28 17:05:29 +00:00
|
|
|
from _pytest.config import Config
|
|
|
|
|
|
|
|
from pylint.constants import PY38_PLUS
|
|
|
|
from pylint.testutils.functional.test_file import FunctionalTestFile
|
2021-11-28 18:50:59 +00:00
|
|
|
from pylint.testutils.lint_module_test import LintModuleTest, MessageCounter
|
|
|
|
from pylint.testutils.output_line import OutputLine
|
2021-11-28 14:06:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LintModuleOutputUpdate(LintModuleTest):
|
2021-11-28 18:50:59 +00:00
|
|
|
"""Class to be used if expected output files should be updated instead of checked."""
|
2021-11-28 14:06:08 +00:00
|
|
|
|
|
|
|
class TestDialect(csv.excel):
|
2021-11-28 18:50:59 +00:00
|
|
|
"""Dialect used by the csv writer."""
|
|
|
|
|
2021-11-28 14:06:08 +00:00
|
|
|
delimiter = ":"
|
|
|
|
lineterminator = "\n"
|
|
|
|
|
|
|
|
csv.register_dialect("test", TestDialect)
|
|
|
|
|
2021-11-28 17:05:29 +00:00
|
|
|
def __init__(
|
|
|
|
self, test_file: FunctionalTestFile, config: Optional[Config] = None
|
|
|
|
) -> None:
|
|
|
|
if not PY38_PLUS:
|
|
|
|
raise RuntimeError(
|
|
|
|
"You need at least python 3.8 for the functional test updater to work. "
|
|
|
|
"This is because python 3.8 includes a new AST parser, which amongst others "
|
|
|
|
"returns the end line and end column of most nodes."
|
|
|
|
)
|
|
|
|
super().__init__(test_file, config)
|
|
|
|
|
2021-11-28 18:50:59 +00:00
|
|
|
def _check_output_text(
|
|
|
|
self,
|
|
|
|
_: MessageCounter,
|
|
|
|
expected_output: List[OutputLine],
|
|
|
|
actual_output: List[OutputLine],
|
|
|
|
) -> None:
|
|
|
|
"""Overwrite or remove the expected output file based on actual output."""
|
|
|
|
# Remove the file if no output is actually expected and a file exists
|
2021-11-28 14:06:08 +00:00
|
|
|
if not expected_output and not actual_output:
|
|
|
|
if os.path.exists(self._test_file.expected_output):
|
|
|
|
os.remove(self._test_file.expected_output)
|
|
|
|
return
|
2021-11-28 18:50:59 +00:00
|
|
|
# Write file with expected output
|
2021-11-28 14:06:08 +00:00
|
|
|
with open(self._test_file.expected_output, "w", encoding="utf-8") as f:
|
|
|
|
writer = csv.writer(f, dialect="test")
|
|
|
|
for line in actual_output:
|
|
|
|
writer.writerow(line.to_csv())
|