Skip to content

Commit adda6db

Browse files
Merge #2899
2899: Provide more runners for potential tests r=matklad a=SomeoneToIgnore Based on the https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Runners.20for.20custom.20test.20annotations discussion. Adds a test runner for every method that has an annotation that contains `test` word in it, allowing to run tests annotated with custom testing annotations such as `#[tokio::test]`, `#[test_case(...)]` and others at costs of potentially emitting some false-positives. Co-authored-by: Kirill Bulatov <[email protected]>
2 parents fb81726 + 3137215 commit adda6db

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

crates/ra_ide/src/runnables.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
4343
let name = fn_def.name()?.text().clone();
4444
let kind = if name == "main" {
4545
RunnableKind::Bin
46-
} else if fn_def.has_atom_attr("test") {
46+
} else if has_test_related_attribute(&fn_def) {
4747
RunnableKind::Test { name: name.to_string() }
4848
} else if fn_def.has_atom_attr("bench") {
4949
RunnableKind::Bench { name: name.to_string() }
@@ -53,6 +53,20 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
5353
Some(Runnable { range: fn_def.syntax().text_range(), kind })
5454
}
5555

56+
/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
57+
/// `#[test_case(...)]`, `#[tokio::test]` and similar.
58+
/// Also a regular `#[test]` annotation is supported.
59+
///
60+
/// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
61+
/// but it's better than not to have the runnables for the tests at all.
62+
fn has_test_related_attribute(fn_def: &ast::FnDef) -> bool {
63+
fn_def
64+
.attrs()
65+
.filter_map(|attr| attr.path())
66+
.map(|path| path.syntax().to_string().to_lowercase())
67+
.any(|attribute_text| attribute_text.contains("test"))
68+
}
69+
5670
fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option<Runnable> {
5771
let has_test_function = module
5872
.item_list()?

0 commit comments

Comments
 (0)