forked from causify-ai/kaizenflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
145 lines (125 loc) · 5.1 KB
/
conftest.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import logging
import os
from typing import Any, Generator
import helpers.hdbg as dbg
import helpers.hunit_test as hut
# Hack to workaround pytest not happy with multiple redundant conftest.py
# (bug #34).
if not hasattr(hut, "_CONFTEST_ALREADY_PARSED"):
# import helpers.hversion as hversi
# hversi.check_version()
# pylint: disable=protected-access
hut._CONFTEST_ALREADY_PARSED = True
# Store whether we are running unit test through pytest.
# pylint: disable=line-too-long
# From https://docs.pytest.org/en/latest/example/simple.html#detect-if-running-from-within-a-pytest-run
def pytest_configure(config: Any) -> None:
_ = config
# pylint: disable=protected-access
hut._CONFTEST_IN_PYTEST = True
def pytest_unconfigure(config: Any) -> None:
_ = config
# pylint: disable=protected-access
hut._CONFTEST_IN_PYTEST = False
# Create a variable to store the object used by pytest to print independently
# of the capture mode.
# https://stackoverflow.com/questions/41794888
import pytest
@pytest.fixture(autouse=True)
def populate_globals(capsys):
hut._GLOBAL_CAPSYS = capsys
# Add custom options.
def pytest_addoption(parser: Any) -> None:
parser.addoption(
"--update_outcomes",
action="store_true",
default=False,
help="Update golden outcomes of test",
)
parser.addoption(
"--incremental",
action="store_true",
default=False,
help="Reuse and not clean up test artifacts",
)
parser.addoption(
"--dbg_verbosity",
dest="log_level",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Set the logging level",
)
parser.addoption(
"--dbg",
action="store_true",
help="Set the logging level to TRACE",
)
parser.addoption(
"--image_version",
action="store",
help="Version of the image to test against",
)
parser.addoption(
"--image_stage",
action="store",
help="Stage of the image to test against",
)
def pytest_collection_modifyitems(config: Any, items: Any) -> None:
_ = items
import helpers.henv as henv
_WARNING = "\033[33mWARNING\033[0m"
try:
print(henv.get_system_signature()[0])
except:
print(f"\n{_WARNING}: Can't print system_signature")
if config.getoption("--update_outcomes"):
print(f"\n{_WARNING}: Updating test outcomes")
hut.set_update_tests(True)
if config.getoption("--incremental"):
print(f"\n{_WARNING}: Using incremental test mode")
hut.set_incremental_tests(True)
# Set the verbosity level.
level = logging.INFO
if config.getoption("--dbg_verbosity", None) or config.getoption(
"--dbg", None
):
if config.getoption("--dbg_verbosity", None):
level = config.getoption("--dbg_verbosity")
elif config.getoption("--dbg", None):
level = logging.TRACE
else:
raise ValueError("Can't get here")
print(f"\n{_WARNING}: Setting verbosity level to %s" % level)
# When we specify the debug verbosity we monkey patch the command
# line to add the '-s' option to pytest to not suppress the output.
# NOTE: monkey patching sys.argv is often fragile.
import sys
sys.argv.append("-s")
sys.argv.append("-o log_cli=true")
# TODO(gp): redirect also the stderr to file.
dbg.init_logger(level, in_pytest=True, log_filename="tmp.pytest.log")
if "PYANNOTATE" in os.environ:
print("\nWARNING: Collecting information about types through pyannotate")
# From https://github.com/dropbox/pyannotate/blob/master/example/example_conftest.py
import pytest
def pytest_collection_finish(session: Any) -> None:
"""
Handle the pytest collection finish hook: configure pyannotate.
Explicitly delay importing `collect_types` until all tests
have been collected. This gives gevent a chance to monkey
patch the world before importing pyannotate.
"""
# mypy: Cannot find module named 'pyannotate_runtime'
import pyannotate_runtime # type: ignore
_ = session
pyannotate_runtime.collect_types.init_types_collection()
@pytest.fixture(autouse=True)
def collect_types_fixture() -> Generator:
import pyannotate_runtime
pyannotate_runtime.collect_types.start()
yield
pyannotate_runtime.collect_types.stop()
def pytest_sessionfinish(session: Any, exitstatus: Any) -> None:
import pyannotate_runtime
_ = session, exitstatus
pyannotate_runtime.collect_types.dump_stats("type_info.json")
print("\n*** Collected types ***")