forked from causify-ai/helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_pytest.py
More file actions
executable file
·155 lines (138 loc) · 5.17 KB
/
main_pytest.py
File metadata and controls
executable file
·155 lines (138 loc) · 5.17 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
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
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
"""
Main script used for running tests in runnable directories.
"""
import argparse
import logging
import os
import subprocess
import sys
import helpers.hdbg as hdbg
import helpers.hparser as hparser
_LOG = logging.getLogger(__name__)
def _parse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command", help="Sub-command help")
# Add command for running fast tests.
run_fast_tests_parser = subparsers.add_parser(
"run_fast_tests", help="Run fast tests"
)
run_fast_tests_parser.add_argument(
"--dir",
action="store",
required=False,
type=str,
help="Name of runnable dir",
)
# Add command for running slow tests.
run_slow_tests_parser = subparsers.add_parser(
"run_slow_tests", help="Run slow tests"
)
run_slow_tests_parser.add_argument(
"--dir",
action="store",
required=False,
type=str,
help="Name of runnable dir",
)
# Add command for running superslow tests.
run_superslow_tests_parser = subparsers.add_parser(
"run_superslow_tests", help="Run superslow tests"
)
run_superslow_tests_parser.add_argument(
"--dir",
action="store",
required=False,
type=str,
help="Name of runnable dir",
)
parser = hparser.add_verbosity_arg(parser)
return parser
def _is_runnable_dir(runnable_dir: str) -> bool:
"""
Check if the specified directory is a runnable directory.
Each directory that is runnable contains the files:
- changelog.txt: store the changelog
- devops: dir with all the Docker files needed to build and run a container
:param runnable_dir: nme of the runnable directory
:return: True if the directory is a runnable directory, False otherwise
"""
changelog_path = os.path.join(runnable_dir, "changelog.txt")
devops_path = os.path.join(runnable_dir, "devops")
if not os.path.exists(changelog_path) or not os.path.isdir(devops_path):
_LOG.warning(f"{runnable_dir} is not a runnable directory")
return False
return True
def _run_test(runnable_dir: str, command: str) -> None:
"""
Run test in for specified runnable directory.
:param runnable_dir: directory to run tests in
:param command: command to run tests (e.g. run_fast_tests,
run_slow_tests, run_superslow_tests)
"""
is_runnable_dir = _is_runnable_dir(runnable_dir)
hdbg.dassert(is_runnable_dir, f"{runnable_dir} is not a runnable dir.")
_LOG.info(f"Running tests in {runnable_dir}")
# Make sure the `invoke` command is referencing to the correct
# devops and helpers directory.
env = os.environ.copy()
env["HELPERS_ROOT_DIR"] = os.path.join(os.getcwd(), "helpers_root")
# Give priority to the current runnable directory over helpers.
env["PYTHONPATH"] = (
f"{os.path.join(os.getcwd(), runnable_dir)}:{env['HELPERS_ROOT_DIR']}"
)
# TODO(heanh): Use hsystem.
# We cannot use `hsystem.system` because it does not support passing of env
# variables yet.
result = subprocess.run(
f"invoke {command}", shell=True, env=env, cwd=runnable_dir
)
# Error code is not propagated upward to the parent process causing the
# GH actions to not fail the pipeline (See CmampTask11449).
# We need to explicitly exit with the return code of the subprocess.
if result.returncode != 0:
sys.exit(result.returncode)
def _main(parser: argparse.ArgumentParser) -> None:
args = parser.parse_args()
hdbg.init_logger(verbosity=args.log_level, use_exec_path=True)
command = args.command
runnable_dirs = [
"ck.infra",
]
if command == "run_fast_tests":
runnable_dir = args.dir
if runnable_dir:
# Run tests for the specified runnable directory.
_run_test(runnable_dir, command)
else:
# Run tests for all runnable directories.
_LOG.info("Running fast tests for all runnable directories.")
for runnable_dir in runnable_dirs:
_run_test(runnable_dir, command)
elif command == "run_slow_tests":
runnable_dir = args.dir
if runnable_dir:
# Run tests for the specified runnable directory.
_run_test(runnable_dir, command)
else:
# Run tests for all runnable directories.
_LOG.info("Running slow tests for all runnable directories.")
for runnable_dir in runnable_dirs:
_run_test(runnable_dir, command)
elif command == "run_superslow_tests":
runnable_dir = args.dir
if runnable_dir:
# Run tests for the specified runnable directory.
_run_test(runnable_dir, command)
else:
# Run tests for all runnable directories.
_LOG.info("Running superslow tests for all runnable directories.")
for runnable_dir in runnable_dirs:
_run_test(runnable_dir, command)
else:
_LOG.error("Invalid command.")
if __name__ == "__main__":
_main(_parse())