File: test_test_history.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (74 lines) | stat: -rw-r--r-- 2,183 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import itertools
import re
import shlex
import unittest
from typing import List, Optional

from tools.stats import test_history
from typing_extensions import TypedDict


class Example(TypedDict):
    cmd: str
    args: List[str]
    lines: List[str]


def parse_block(block: List[str]) -> Optional[Example]:
    if block:
        match = re.match(r"^\$ ([^ ]+) (.*)$", block[0])
        if match:
            cmd, first = match.groups()
            args = []
            for i, line in enumerate([first] + block[1:]):
                if line.endswith("\\"):
                    args.append(line[:-1])
                else:
                    args.append(line)
                    break
            return {
                "cmd": cmd,
                "args": shlex.split("".join(args)),
                "lines": block[i + 1 :],
            }
    return None


def parse_description(description: str) -> List[Example]:
    examples: List[Example] = []
    for block in description.split("\n\n"):
        matches = [re.match(r"^    (.*)$", line) for line in block.splitlines()]
        if all(matches):
            lines = []
            for match in matches:
                assert match
                (line,) = match.groups()
                lines.append(line)
            example = parse_block(lines)
            if example:
                examples.append(example)
    return examples


@unittest.skip("Skipping as this test is fragile, issue #73083")
class TestTestHistory(unittest.TestCase):
    maxDiff = None

    def test_help_examples(self) -> None:
        examples = parse_description(test_history.description())
        self.assertEqual(len(examples), 3)
        for i, example in enumerate(examples):
            with self.subTest(i=i):
                self.assertTrue(test_history.__file__.endswith(example["cmd"]))
                expected = example["lines"]
                actual = list(
                    itertools.islice(
                        test_history.run(example["args"]),
                        len(expected),
                    )
                )
                self.assertEqual(actual, expected)


if __name__ == "__main__":
    unittest.main()