forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
98 lines (78 loc) · 2.56 KB
/
__main__.py
File metadata and controls
98 lines (78 loc) · 2.56 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from __future__ import absolute_import
import argparse
import sys
from . import pytest, report
from .errors import UnsupportedToolError, UnsupportedCommandError
TOOLS = {
'pytest': {
'_add_subparser': pytest.add_cli_subparser,
'discover': pytest.discover,
},
}
REPORTERS = {
'discover': report.report_discovered,
}
def parse_args(
argv=sys.argv[1:],
prog=sys.argv[0],
):
"""
Return the subcommand & tool to run, along with its args.
This defines the standard CLI for the different testing frameworks.
"""
parser = argparse.ArgumentParser(
description='Run Python testing operations.',
prog=prog,
)
cmdsubs = parser.add_subparsers(dest='cmd')
# Add "run" and "debug" subcommands when ready.
for cmdname in ['discover']:
sub = cmdsubs.add_parser(cmdname)
subsubs = sub.add_subparsers(dest='tool')
for toolname in sorted(TOOLS):
try:
add_subparser = TOOLS[toolname]['_add_subparser']
except KeyError:
continue
subsub = add_subparser(cmdname, toolname, subsubs)
if cmdname == 'discover':
subsub.add_argument('--simple', action='store_true')
subsub.add_argument('--no-hide-stdio', dest='hidestdio',
action='store_false')
subsub.add_argument('--pretty', action='store_true')
# Parse the args!
if '--' in argv:
seppos = argv.index('--')
toolargs = argv[seppos + 1:]
argv = argv[:seppos]
else:
toolargs = []
args = parser.parse_args(argv)
ns = vars(args)
cmd = ns.pop('cmd')
if not cmd:
parser.error('missing command')
tool = ns.pop('tool')
if not tool:
parser.error('missing tool')
return tool, cmd, ns, toolargs
def main(toolname, cmdname, subargs, toolargs,
_tools=TOOLS, _reporters=REPORTERS):
try:
tool = _tools[toolname]
except KeyError:
raise UnsupportedToolError(toolname)
try:
run = tool[cmdname]
report_result = _reporters[cmdname]
except KeyError:
raise UnsupportedCommandError(cmdname)
parents, result = run(toolargs, **subargs)
report_result(result, parents,
**subargs
)
if __name__ == '__main__':
tool, cmd, subargs, toolargs = parse_args()
main(tool, cmd, subargs, toolargs)