File: linter_test_case.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (57 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# mypy: ignore-errors
import io
import json
import os
from pathlib import Path
from unittest import mock, TestCase

from tools.linter.adapters._linter import PythonFile


class LinterTestCase(TestCase):
    LinterClass = None
    rewrite_expected = "REWRITE_EXPECTED" in os.environ

    def assertExpected(self, path: Path, actual: str, suffix: str) -> None:
        expected_file = Path(f"{path}.{suffix}")
        if not self.rewrite_expected and expected_file.exists():
            self.assertEqual(actual, expected_file.read_text())
        else:
            expected_file.write_text(actual)

    def replace(self, s: str):
        linter = self.LinterClass("dummy")
        pf = PythonFile(linter.linter_name, contents=s)
        replacement, _results = linter._replace(pf)
        return replacement

    @mock.patch("sys.stdout", new_callable=io.StringIO)
    def lint_test(self, path, args, mock_stdout):
        return self._lint_test(path, args, mock_stdout)[:2]

    @mock.patch("sys.stdout", new_callable=io.StringIO)
    def lint_fix_test(self, path, args, mock_stdout):
        rep, results, linter = self._lint_test(path, args, mock_stdout)
        r = results[-1]
        path = linter.paths[0]
        self.assertEqual(r.original, path.read_text())
        self.assertEqual(rep, r.replacement)
        self.assertExpected(path, r.replacement, "python")
        return r

    def _lint_test(self, path, args, mock_stdout):
        with self.subTest("from-command-line"):
            linter = self.LinterClass([str(path), *args])
            linter.lint_all()
            self.assertExpected(path, mock_stdout.getvalue(), "lintrunner")

        with self.subTest("from-lintrunner"):
            linter = self.LinterClass(["--lintrunner", str(path), *args])
            pf = PythonFile(linter.linter_name, path)
            replacement, results = linter._replace(pf)

            actual = [json.loads(d) for d in linter._display(pf, results)]
            actual = json.dumps(actual, indent=2, sort_keys=True) + "\n"
            self.assertExpected(path, actual, "json")

        return replacement, results, linter