pytorch/tools/test/test_test_history.py
Sam Estep a95abc4648 Test tools/test_history.py (#54259)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/54259

Test Plan:
The main point of this is to be run in our "Test tools" GitHub Actions workflow. To test locally:
```
mypy --config=mypy-strict.ini
python tools/test/test_test_history.py
```

Reviewed By: seemethere

Differential Revision: D27164519

Pulled By: samestep

fbshipit-source-id: 46f90e62e2d4d0c413b202419e509d471bad43de
2021-03-18 14:05:42 -07:00

75 lines
2.0 KiB
Python

import itertools
import re
import shlex
import unittest
from typing import List, Optional
from tools 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
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()