Skip to content

Commit 82e390f

Browse files
Merge #5561
5561: Allow running more tests at once via the module runners r=matklad a=SomeoneToIgnore Sometimes I group tests into submodules located in the root `#[cfg(test)]` crate and want to (re)run them all at once. This PR adds more test runnables to allow running all tests in the file via a module runner. Not all possible module runners are added, to avoid displaying too many code lens. before: <img width="306" alt="before" src="https://user-images.githubusercontent.com/2690773/88724886-e4c6ea00-d133-11ea-9d80-082bb610d422.png"> after (`nested_tests_0` got the runnable, `root_tests` had not): <img width="300" alt="after" src="https://user-images.githubusercontent.com/2690773/88724896-e85a7100-d133-11ea-99ee-689126679cbc.png"> Co-authored-by: Kirill Bulatov <[email protected]>
2 parents 5a81242 + bb1ae85 commit 82e390f

File tree

1 file changed

+110
-54
lines changed

1 file changed

+110
-54
lines changed

crates/ra_ide/src/runnables.rs

Lines changed: 110 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,7 @@ fn runnable_mod(
220220
module: ast::Module,
221221
file_id: FileId,
222222
) -> Option<Runnable> {
223-
let has_test_function = module
224-
.item_list()?
225-
.items()
226-
.filter_map(|it| match it {
227-
ast::ModuleItem::FnDef(it) => Some(it),
228-
_ => None,
229-
})
230-
.any(|f| has_test_related_attribute(&f));
231-
if !has_test_function {
223+
if !has_test_function_or_multiple_test_submodules(&module) {
232224
return None;
233225
}
234226
let module_def = sema.to_def(&module)?;
@@ -246,6 +238,34 @@ fn runnable_mod(
246238
Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg_exprs })
247239
}
248240

241+
// We could create runnables for modules with number_of_test_submodules > 0,
242+
// but that bloats the runnables for no real benefit, since all tests can be run by the submodule already
243+
fn has_test_function_or_multiple_test_submodules(module: &ast::Module) -> bool {
244+
if let Some(item_list) = module.item_list() {
245+
let mut number_of_test_submodules = 0;
246+
247+
for item in item_list.items() {
248+
match item {
249+
ast::ModuleItem::FnDef(f) => {
250+
if has_test_related_attribute(&f) {
251+
return true;
252+
}
253+
}
254+
ast::ModuleItem::Module(submodule) => {
255+
if has_test_function_or_multiple_test_submodules(&submodule) {
256+
number_of_test_submodules += 1;
257+
}
258+
}
259+
_ => (),
260+
}
261+
}
262+
263+
number_of_test_submodules > 1
264+
} else {
265+
false
266+
}
267+
}
268+
249269
#[cfg(test)]
250270
mod tests {
251271
use expect::{expect, Expect};
@@ -571,38 +591,52 @@ mod test_mod {
571591
}
572592

573593
#[test]
574-
fn test_runnables_one_depth_layer_module() {
594+
fn only_modules_with_test_functions_or_more_than_one_test_submodule_have_runners() {
575595
check(
576596
r#"
577597
//- /lib.rs
578598
<|>
579-
mod foo {
580-
mod test_mod {
581-
#[test]
582-
fn test_foo1() {}
599+
mod root_tests {
600+
mod nested_tests_0 {
601+
mod nested_tests_1 {
602+
#[test]
603+
fn nested_test_11() {}
604+
605+
#[test]
606+
fn nested_test_12() {}
607+
}
608+
609+
mod nested_tests_2 {
610+
#[test]
611+
fn nested_test_2() {}
612+
}
613+
614+
mod nested_tests_3 {}
583615
}
616+
617+
mod nested_tests_4 {}
584618
}
585619
"#,
586-
&[&TEST, &TEST],
620+
&[&TEST, &TEST, &TEST, &TEST, &TEST, &TEST],
587621
expect![[r#"
588622
[
589623
Runnable {
590624
nav: NavigationTarget {
591625
file_id: FileId(
592626
1,
593627
),
594-
full_range: 15..77,
628+
full_range: 22..323,
595629
focus_range: Some(
596-
19..27,
630+
26..40,
597631
),
598-
name: "test_mod",
632+
name: "nested_tests_0",
599633
kind: MODULE,
600634
container_name: None,
601635
description: None,
602636
docs: None,
603637
},
604638
kind: TestMod {
605-
path: "foo::test_mod",
639+
path: "root_tests::nested_tests_0",
606640
},
607641
cfg_exprs: [],
608642
},
@@ -611,66 +645,88 @@ mod foo {
611645
file_id: FileId(
612646
1,
613647
),
614-
full_range: 38..71,
648+
full_range: 51..192,
615649
focus_range: Some(
616-
57..66,
650+
55..69,
617651
),
618-
name: "test_foo1",
652+
name: "nested_tests_1",
653+
kind: MODULE,
654+
container_name: None,
655+
description: None,
656+
docs: None,
657+
},
658+
kind: TestMod {
659+
path: "root_tests::nested_tests_0::nested_tests_1",
660+
},
661+
cfg_exprs: [],
662+
},
663+
Runnable {
664+
nav: NavigationTarget {
665+
file_id: FileId(
666+
1,
667+
),
668+
full_range: 84..126,
669+
focus_range: Some(
670+
107..121,
671+
),
672+
name: "nested_test_11",
619673
kind: FN_DEF,
620674
container_name: None,
621675
description: None,
622676
docs: None,
623677
},
624678
kind: Test {
625679
test_id: Path(
626-
"foo::test_mod::test_foo1",
680+
"root_tests::nested_tests_0::nested_tests_1::nested_test_11",
627681
),
628682
attr: TestAttr {
629683
ignore: false,
630684
},
631685
},
632686
cfg_exprs: [],
633687
},
634-
]
635-
"#]],
636-
);
637-
}
638-
639-
#[test]
640-
fn test_runnables_multiple_depth_module() {
641-
check(
642-
r#"
643-
//- /lib.rs
644-
<|>
645-
mod foo {
646-
mod bar {
647-
mod test_mod {
648-
#[test]
649-
fn test_foo1() {}
650-
}
651-
}
652-
}
653-
"#,
654-
&[&TEST, &TEST],
655-
expect![[r#"
656-
[
657688
Runnable {
658689
nav: NavigationTarget {
659690
file_id: FileId(
660691
1,
661692
),
662-
full_range: 33..107,
693+
full_range: 140..182,
663694
focus_range: Some(
664-
37..45,
695+
163..177,
665696
),
666-
name: "test_mod",
697+
name: "nested_test_12",
698+
kind: FN_DEF,
699+
container_name: None,
700+
description: None,
701+
docs: None,
702+
},
703+
kind: Test {
704+
test_id: Path(
705+
"root_tests::nested_tests_0::nested_tests_1::nested_test_12",
706+
),
707+
attr: TestAttr {
708+
ignore: false,
709+
},
710+
},
711+
cfg_exprs: [],
712+
},
713+
Runnable {
714+
nav: NavigationTarget {
715+
file_id: FileId(
716+
1,
717+
),
718+
full_range: 202..286,
719+
focus_range: Some(
720+
206..220,
721+
),
722+
name: "nested_tests_2",
667723
kind: MODULE,
668724
container_name: None,
669725
description: None,
670726
docs: None,
671727
},
672728
kind: TestMod {
673-
path: "foo::bar::test_mod",
729+
path: "root_tests::nested_tests_0::nested_tests_2",
674730
},
675731
cfg_exprs: [],
676732
},
@@ -679,19 +735,19 @@ mod foo {
679735
file_id: FileId(
680736
1,
681737
),
682-
full_range: 60..97,
738+
full_range: 235..276,
683739
focus_range: Some(
684-
83..92,
740+
258..271,
685741
),
686-
name: "test_foo1",
742+
name: "nested_test_2",
687743
kind: FN_DEF,
688744
container_name: None,
689745
description: None,
690746
docs: None,
691747
},
692748
kind: Test {
693749
test_id: Path(
694-
"foo::bar::test_mod::test_foo1",
750+
"root_tests::nested_tests_0::nested_tests_2::nested_test_2",
695751
),
696752
attr: TestAttr {
697753
ignore: false,

0 commit comments

Comments
 (0)