minimal adjustments
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-python / pythonFiles / testing_tools / adapter / __main__.py
1 # Copyright (c) Microsoft Corporation. All rights reserved.
2 # Licensed under the MIT License.
3
4 from __future__ import absolute_import
5
6 import argparse
7 import sys
8
9 from . import pytest, report
10 from .errors import UnsupportedToolError, UnsupportedCommandError
11
12
13 TOOLS = {
14     'pytest': {
15         '_add_subparser': pytest.add_cli_subparser,
16         'discover': pytest.discover,
17         },
18     }
19 REPORTERS = {
20     'discover': report.report_discovered,
21     }
22
23
24
25 def parse_args(
26         argv=sys.argv[1:],
27         prog=sys.argv[0],
28         ):
29     """
30     Return the subcommand & tool to run, along with its args.
31
32     This defines the standard CLI for the different testing frameworks.
33     """
34     parser = argparse.ArgumentParser(
35             description='Run Python testing operations.',
36             prog=prog,
37             )
38     cmdsubs = parser.add_subparsers(dest='cmd')
39
40     # Add "run" and "debug" subcommands when ready.
41     for cmdname in ['discover']:
42         sub = cmdsubs.add_parser(cmdname)
43         subsubs = sub.add_subparsers(dest='tool')
44         for toolname in sorted(TOOLS):
45             try:
46                 add_subparser = TOOLS[toolname]['_add_subparser']
47             except KeyError:
48                 continue
49             subsub = add_subparser(cmdname, toolname, subsubs)
50             if cmdname == 'discover':
51                 subsub.add_argument('--simple', action='store_true')
52                 subsub.add_argument('--no-hide-stdio', dest='hidestdio',
53                                     action='store_false')
54                 subsub.add_argument('--pretty', action='store_true')
55
56     # Parse the args!
57     if '--' in argv:
58         seppos = argv.index('--')
59         toolargs = argv[seppos + 1:]
60         argv = argv[:seppos]
61     else:
62         toolargs = []
63     args = parser.parse_args(argv)
64     ns = vars(args)
65
66     cmd = ns.pop('cmd')
67     if not cmd:
68         parser.error('missing command')
69
70     tool = ns.pop('tool')
71     if not tool:
72         parser.error('missing tool')
73
74     return tool, cmd, ns, toolargs
75
76
77 def main(toolname, cmdname, subargs, toolargs,
78          _tools=TOOLS, _reporters=REPORTERS):
79     try:
80         tool = _tools[toolname]
81     except KeyError:
82         raise UnsupportedToolError(toolname)
83
84     try:
85         run = tool[cmdname]
86         report_result = _reporters[cmdname]
87     except KeyError:
88         raise UnsupportedCommandError(cmdname)
89
90     parents, result = run(toolargs, **subargs)
91     report_result(result, parents,
92                   **subargs
93                   )
94
95
96 if __name__ == '__main__':
97     tool, cmd, subargs, toolargs = parse_args()
98     main(tool, cmd, subargs, toolargs)