pytorch/tools/test/test_test_history.py
Rong Rong (AI Infra) 7e619b9588 First step to rearrange files in tools folder (#60473)
Summary:
Changes including:
- introduced `linter/`, `testing/`, `stats/` folders in `tools/`
- move appropriate scripts into these folders
- change grepped references in the pytorch/pytorch repo

Next step
- introduce `build/` folder for build scripts

Pull Request resolved: https://github.com/pytorch/pytorch/pull/60473

Test Plan:
- CI (this is important b/c pytorch/test-infra also rely on some script reference.
- tools/tests/

Reviewed By: albanD

Differential Revision: D29352716

Pulled By: walterddr

fbshipit-source-id: bad40b5ce130b35dfd9e59b8af34f9025f3285fd
2021-06-24 10:13:58 -07:00

75 lines
2.0 KiB
Python

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
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()