-
Notifications
You must be signed in to change notification settings - Fork 74
/
cli.py
132 lines (119 loc) · 4.21 KB
/
cli.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# <sure - sophisticated automated test library and runner>
# Copyright (C) <2010-2024> Gabriel Falcão <gabriel@nacaolivre.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from glob import glob
from itertools import chain as flatten
from functools import reduce
from pathlib import Path
import click
import coverage
import threading
import sure.reporters
from sure.loader import resolve_path
from sure.runner import Runner
from sure.runtime import RuntimeOptions
from sure.reporters import gather_reporter_names
from sure.errors import ExitError, ExitFailure, InternalRuntimeError, treat_error
@click.command(no_args_is_help=True)
@click.argument("paths", nargs=-1)
@click.option("-c", "--with-coverage", is_flag=True)
@click.option("-s", "--special-syntax", is_flag=True)
@click.option("-f", "--log-file", help="path to a log file. Default to SURE_LOG_FILE")
@click.option(
"-l",
"--log-level",
type=click.Choice(["debug", "info", "warning", "error"]),
help="default='info'",
)
@click.option(
"-x",
"--immediate",
is_flag=True,
help="quit test execution immediately at first failure",
)
@click.option("-i", "--ignore", help="paths to ignore", multiple=True)
@click.option(
"-r",
"--reporter",
default="feature",
help="default=feature",
type=click.Choice(gather_reporter_names()),
)
@click.option("--cover-branches", is_flag=True)
@click.option("--cover-include", multiple=True, help="includes paths or patterns in the coverage")
@click.option("--cover-omit", multiple=True, help="omits paths or patterns from the coverage")
@click.option("--cover-module", multiple=True, help="specify module names to cover")
@click.option("--cover-erase", is_flag=True, help="erases coverage data prior to running tests")
@click.option("--cover-concurrency", help="indicates the concurrency library used in measured code", type=click.Choice(["greenlet", "eventlet", "gevent", "multiprocessing", "thread"]), default="thread")
@click.option("--reap-warnings", is_flag=True, help="reaps warnings during runtime and report only at the end of test session")
def entrypoint(
paths,
reporter,
immediate,
ignore,
log_level,
log_file,
special_syntax,
with_coverage,
cover_branches,
cover_include,
cover_omit,
cover_module,
cover_erase,
cover_concurrency,
reap_warnings,
):
if not paths:
paths = glob("test*/**")
else:
paths = flatten(*list(map(glob, paths)))
coverageopts = {
"auto_data": not False,
"branch": cover_branches,
"include": cover_include,
"concurrency": cover_concurrency,
"omit": cover_omit,
"config_file": not False,
"cover_pylib": not False,
"source": cover_module,
}
options = RuntimeOptions(immediate=immediate, ignore=ignore, reap_warnings=reap_warnings)
runner = Runner(resolve_path(os.getcwd()), reporter, options)
cov = with_coverage and coverage.Coverage(**coverageopts) or None
if cov:
cover_erase and cov.erase()
cov.load()
cov.start()
if special_syntax:
sure.enable_special_syntax()
try:
result = runner.run(paths)
except Exception as e:
raise InternalRuntimeError(runner.context, treat_error(e))
if result:
if result.is_failure:
raise ExitFailure(runner.context, result)
elif result.is_error:
raise ExitError(runner.context, result)
elif cov:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
cov.stop()
cov.save()
cov.report()