Skip to content

Commit 2fd0dac

Browse files
bonzinidcbaker
authored andcommitted
mtest: rust: allow parsing doctest output
Doctests have a slightly different output compared to what "protocol: rust" supports: running 2 tests test ../doctest1.rs - my_func (line 7) ... ignored test ../doctest1.rs - (line 3) ... ok test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.12s Add a little more parsing in order to accept this; a simple minded split() fails to unpack the tuple. I plan to contribute an extension of the rust module to invoke doctests, for now this allows running rustdoc --test with "protocol: 'rust'" and get information about the subtests: ▶ 4/8 ../doctest1.rs:my_func:7 SKIP ▶ 4/8 ../doctest1.rs:3 OK 4/8 rust_unit_tests:doctests / rust doctest OK 0.28s 1 subtests passed Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent f9f69d8 commit 2fd0dac

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

mesonbuild/mtest.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
UNENCODABLE_XML_CHR_RANGES = [fr'{chr(low)}-{chr(high)}' for (low, high) in UNENCODABLE_XML_UNICHRS]
8282
UNENCODABLE_XML_CHRS_RE = re.compile('([' + ''.join(UNENCODABLE_XML_CHR_RANGES) + '])')
8383

84+
RUST_TEST_RE = re.compile(r'^test (?!result)(.*) \.\.\. (.*)$')
85+
RUST_DOCTEST_RE = re.compile(r'^(.*?) - (.*? |)\(line (\d+)\)')
86+
8487

8588
def is_windows() -> bool:
8689
platname = platform.system().lower()
@@ -1157,8 +1160,14 @@ def parse_res(n: int, name: str, result: str) -> TAPParser.Test:
11571160

11581161
n = 1
11591162
async for line in lines:
1160-
if line.startswith('test ') and not line.startswith('test result'):
1161-
_, name, _, result = line.rstrip().split(' ')
1163+
match = RUST_TEST_RE.match(line)
1164+
if match:
1165+
name, result = match.groups()
1166+
doctest = RUST_DOCTEST_RE.match(name)
1167+
if doctest:
1168+
name = ':'.join((x.rstrip() for x in doctest.groups() if x))
1169+
else:
1170+
name = name.rstrip()
11621171
name = name.replace('::', '.')
11631172
t = parse_res(n, name, result)
11641173
self.results.append(t)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! This is a doctest
2+
//!
3+
//! ```
4+
//! assert_eq!(2+2, 4)
5+
//! ```
6+
7+
/// ```ignore
8+
/// this one will be skipped
9+
/// ```
10+
fn my_func()
11+
{
12+
}

test cases/rust/9 unit tests/meson.build

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ test(
3131
suite : ['foo'],
3232
)
3333

34+
rustdoc = find_program('rustdoc', required: false)
35+
if rustdoc.found()
36+
# rustdoc is invoked mostly like rustc. This is a simple example
37+
# where it is easy enough to invoke it by hand.
38+
test(
39+
'rust doctest',
40+
rustdoc,
41+
args : ['--test', '--crate-name', 'doctest1', '--crate-type', 'lib', files('doctest1.rs')],
42+
protocol : 'rust',
43+
suite : ['doctests'],
44+
)
45+
endif
46+
3447
exe = executable('rust_exe', ['test2.rs', 'test.rs'], build_by_default : false)
3548

3649
rust = import('rust')

0 commit comments

Comments
 (0)