|
11 | 11 | import collections |
12 | 12 | import io |
13 | 13 | import os |
14 | | -import re |
15 | 14 | import shutil |
16 | 15 | import sys |
17 | 16 |
|
@@ -121,66 +120,48 @@ def setup_parser(subparser): |
121 | 120 |
|
122 | 121 | def do_list(args, extra_args): |
123 | 122 | """Print a lists of tests than what pytest offers.""" |
124 | | - # Run test collection and get the tree out. |
| 123 | + |
| 124 | + def colorize(c, prefix): |
| 125 | + if isinstance(prefix, tuple): |
| 126 | + return "::".join(color.colorize(f"@{c}{{{p}}}") for p in prefix if p != "()") |
| 127 | + return color.colorize(f"@{c}{{{prefix}}}") |
| 128 | + |
| 129 | + # Run test collection with quiet mode, to extract a list of <path>::<test_name>. |
125 | 130 | old_output = sys.stdout |
126 | 131 | try: |
127 | 132 | sys.stdout = output = io.StringIO() |
128 | 133 | try: |
129 | 134 | import pytest |
130 | 135 |
|
131 | | - pytest.main(["--collect-only"] + extra_args) |
| 136 | + pytest.main(["--collect-only", "-q"] + extra_args) |
132 | 137 | except ImportError: |
133 | 138 | logger.die("Pytest python module not found. Ensure requirements.txt are installed.") |
134 | 139 | finally: |
135 | 140 | sys.stdout = old_output |
136 | 141 |
|
137 | 142 | lines = output.getvalue().split("\n") |
138 | 143 | tests = collections.defaultdict(set) |
139 | | - prefix = [] |
140 | | - |
141 | | - print("All lines =") |
142 | | - print(lines) |
143 | | - |
144 | 144 | # collect tests into sections |
145 | 145 | for line in lines: |
146 | | - match = re.match(r"(\s*)<([^ ]*) '([^']*)'", line) |
147 | | - if not match: |
| 146 | + if "::" not in line: |
148 | 147 | continue |
149 | | - indent, nodetype, name = match.groups() |
150 | | - |
151 | | - # strip parametrized tests |
152 | | - if "[" in name: |
153 | | - name = name[: name.index("[")] |
154 | | - |
155 | | - depth = len(indent) // 2 |
156 | | - |
157 | | - if nodetype.endswith("Function"): |
158 | | - key = tuple(prefix) |
159 | | - tests[key].add(name) |
160 | | - print(f"added test {key}={name} from {nodetype}") |
161 | | - else: |
162 | | - prefix = prefix[:depth] |
163 | | - prefix.append(name) |
164 | | - print(f"added prefix {name}") |
165 | | - |
166 | | - def colorize(c, prefix): |
167 | | - if isinstance(prefix, tuple): |
168 | | - return "::".join(color.colorize(f"@{c}{{{p}}}") for p in prefix if p != "()") |
169 | | - return color.colorize(f"@{c}{{{prefix}}}") |
| 148 | + [path, test_name] = line.split("::", 1) |
| 149 | + # dedupe parametrized tests |
| 150 | + if "[" in test_name: |
| 151 | + test_name = test_name[: test_name.index("[")] |
| 152 | + tests[path].add(test_name) |
170 | 153 |
|
171 | 154 | if args.list == "list": |
172 | | - files = {prefix[0] for prefix in tests} |
| 155 | + files = tests.keys() |
173 | 156 | color_files = [colorize("B", file) for file in sorted(files)] |
174 | 157 | colify(color_files) |
175 | | - |
176 | 158 | elif args.list == "long": |
177 | 159 | for prefix, functions in sorted(tests.items()): |
178 | 160 | path = colorize("*B", prefix) + "::" |
179 | 161 | functions = [colorize("c", f) for f in sorted(functions)] |
180 | 162 | color.cprint(path) |
181 | 163 | colify(functions, indent=4) |
182 | 164 | print() |
183 | | - |
184 | 165 | else: # args.list == "names" |
185 | 166 | all_functions = [ |
186 | 167 | colorize("*B", prefix) + "::" + colorize("c", f) |
|
0 commit comments