-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathconftest.py
More file actions
46 lines (31 loc) · 1.32 KB
/
conftest.py
File metadata and controls
46 lines (31 loc) · 1.32 KB
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
# mypy: ignore-errors
import asyncio
import pathlib
import expecttest
import ghstack.async_script
import ghstack.test_prelude
import pytest
# Adapted from https://stackoverflow.com/questions/56807698/how-to-run-script-as-pytest-test
def pytest_collect_file(file_path: pathlib.Path, parent):
# NB: script name must not end with py, due to doctest picking it
# up in that case
if file_path.suffixes == [".py", ".test"]:
return Script.from_parent(parent, path=file_path)
class Script(pytest.File):
def collect(self):
yield ScriptItem.from_parent(self, name="default", direct=False)
if self.path.parent.name in ["submit", "unlink", "log", "sync"]:
yield ScriptItem.from_parent(self, name="direct", direct=True)
class ScriptItem(pytest.Item):
def __init__(self, *, direct, **kwargs):
super().__init__(**kwargs)
self.direct = direct
def runtest(self):
asyncio.run(self.aruntest())
async def aruntest(self):
async with ghstack.test_prelude.scoped_test(direct=self.direct):
expecttest.EDIT_HISTORY.reload_file(self.fspath)
await ghstack.async_script.run_path(str(self.fspath))
def repr_failure(self, excinfo):
excinfo.traceback = excinfo.traceback.cut(path=self.fspath)
return super().repr_failure(excinfo)