minimal adjustments
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-python / pythonFiles / testing_tools / adapter / report.py
1 # Copyright (c) Microsoft Corporation. All rights reserved.
2 # Licensed under the MIT License.
3
4 from __future__ import print_function
5
6 import json
7
8
9 def report_discovered(tests, parents, pretty=False, simple=False,
10                       _send=print, **_ignored):
11     """Serialize the discovered tests and write to stdout."""
12     if simple:
13         data = [{
14             'id': test.id,
15             'name': test.name,
16             'testroot': test.path.root,
17             'relfile': test.path.relfile,
18             'lineno': test.lineno,
19             'testfunc': test.path.func,
20             'subtest': test.path.sub or None,
21             'markers': test.markers or [],
22             } for test in tests]
23     else:
24         byroot = {}
25         for parent in parents:
26             rootdir = parent.name if parent.root is None else parent.root
27             try:
28                 root = byroot[rootdir]
29             except KeyError:
30                 root = byroot[rootdir] = {
31                         'id': rootdir,
32                         'parents': [],
33                         'tests': [],
34                         }
35             if not parent.root:
36                 root['id'] = parent.id
37                 continue
38             root['parents'].append({
39                 'id': parent.id,
40                 'kind': parent.kind,
41                 'name': parent.name,
42                 'parentid': parent.parentid,
43                 })
44         for test in tests:
45             # We are guaranteed that the parent was added.
46             root = byroot[test.path.root]
47             testdata = {
48                 'id': test.id,
49                 'name': test.name,
50                 # TODO: Add a "kind" field
51                 #  (e.g. "unittest", "function", "doctest")
52                 'source': test.source,
53                 'markers': test.markers or [],
54                 'parentid': test.parentid,
55                 }
56             root['tests'].append(testdata)
57         data = [{
58             'rootid': byroot[root]['id'],
59             'root': root,
60             'parents': byroot[root]['parents'],
61             'tests': byroot[root]['tests'],
62             } for root in sorted(byroot)]
63
64     kwargs = {}
65     if pretty:
66         # human-formatted
67         kwargs = dict(
68             sort_keys=True,
69             indent=4,
70             separators=(',', ': '),
71             )
72     serialized = json.dumps(data, **kwargs)
73
74     _send(serialized)