Skip to content

Commit 5b352fb

Browse files
authored
Merge pull request #1014 from linsword13/test-cov
Fix up unit-test listing
2 parents 3160c58 + 4ad561a commit 5b352fb

File tree

2 files changed

+78
-34
lines changed

2 files changed

+78
-34
lines changed

lib/ramble/ramble/cmd/unit_test.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import collections
1212
import io
1313
import os
14-
import re
1514
import shutil
1615
import sys
1716

@@ -121,66 +120,48 @@ def setup_parser(subparser):
121120

122121
def do_list(args, extra_args):
123122
"""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>.
125130
old_output = sys.stdout
126131
try:
127132
sys.stdout = output = io.StringIO()
128133
try:
129134
import pytest
130135

131-
pytest.main(["--collect-only"] + extra_args)
136+
pytest.main(["--collect-only", "-q"] + extra_args)
132137
except ImportError:
133138
logger.die("Pytest python module not found. Ensure requirements.txt are installed.")
134139
finally:
135140
sys.stdout = old_output
136141

137142
lines = output.getvalue().split("\n")
138143
tests = collections.defaultdict(set)
139-
prefix = []
140-
141-
print("All lines =")
142-
print(lines)
143-
144144
# collect tests into sections
145145
for line in lines:
146-
match = re.match(r"(\s*)<([^ ]*) '([^']*)'", line)
147-
if not match:
146+
if "::" not in line:
148147
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)
170153

171154
if args.list == "list":
172-
files = {prefix[0] for prefix in tests}
155+
files = tests.keys()
173156
color_files = [colorize("B", file) for file in sorted(files)]
174157
colify(color_files)
175-
176158
elif args.list == "long":
177159
for prefix, functions in sorted(tests.items()):
178160
path = colorize("*B", prefix) + "::"
179161
functions = [colorize("c", f) for f in sorted(functions)]
180162
color.cprint(path)
181163
colify(functions, indent=4)
182164
print()
183-
184165
else: # args.list == "names"
185166
all_functions = [
186167
colorize("*B", prefix) + "::" + colorize("c", f)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2022-2025 The Ramble Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
# option. This file may not be copied, modified, or distributed
7+
# except according to those terms.
8+
9+
import pytest
10+
11+
from ramble import main
12+
13+
pytestmark = pytest.mark.maybeslow
14+
15+
ramble_test = main.RambleCommand("unit-test")
16+
17+
18+
def test_unit_test_list():
19+
output = ramble_test("--list")
20+
# Surely this file should show up in the list
21+
assert "ramble/test/cmd/unit_test.py" in output
22+
# But individual test should not show up
23+
assert "test_unit_test_list" not in output
24+
25+
26+
def test_long_list():
27+
output = ramble_test("-L")
28+
assert "lib/ramble/ramble/test/cmd/unit_test.py::\n" in output
29+
assert " test_long_list" in output
30+
31+
32+
def test_full_list():
33+
output = ramble_test("-N")
34+
assert "lib/ramble/ramble/test/cmd/unit_test.py::test_full_list\n" in output
35+
36+
37+
def test_pytest_help():
38+
output = ramble_test("-H")
39+
assert "-k EXPRESSION" in output
40+
assert "pytest-warnings:" in output
41+
assert "--collect-only" in output
42+
43+
44+
def test_failed_import():
45+
with pytest.raises(main.RambleCommandError):
46+
ramble_test("-p", "fake-mod")
47+
48+
49+
def test_list_only_lib():
50+
output = ramble_test("--lib", "-l")
51+
assert "var/ramble/repos" not in output
52+
assert "lib/ramble/ramble/test" in output
53+
54+
55+
def test_list_only_objects():
56+
output = ramble_test("--obj", "-l")
57+
assert "var/ramble/repos" in output
58+
assert "lib/ramble/ramble/test" not in output
59+
60+
61+
def test_external_repo():
62+
with pytest.raises(FileNotFoundError):
63+
ramble_test("--repo-path", "non-existent-path")

0 commit comments

Comments
 (0)