Skip to content

Commit 604b936

Browse files
bors[bot]hdevalke
andauthored
Merge #4092
4092: feat: run ignored tests r=matklad a=hdevalke I started making some exercices on https://exercism.io/ and a lot of test have the `#[ignore]` attribute. The `Run Test|Debug` code lens show up, but running the test results in: ``` running 1 test test test_one_piece ... ignored test result: ok. 0 passed; 0 failed; 1 ignored; 0 measured; 5 filtered out ``` This pull request adds the `--ignored` flag if needed. Co-authored-by: Hannes De Valkeneer <[email protected]>
2 parents 1101e74 + 380a287 commit 604b936

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

crates/ra_ide/src/runnables.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Display for TestId {
3434

3535
#[derive(Debug)]
3636
pub enum RunnableKind {
37-
Test { test_id: TestId },
37+
Test { test_id: TestId, attr: TestAttr },
3838
TestMod { path: String },
3939
Bench { test_id: TestId },
4040
Bin,
@@ -77,7 +77,8 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, fn_def: ast::FnDef) -> Option<Run
7777
};
7878

7979
if has_test_related_attribute(&fn_def) {
80-
RunnableKind::Test { test_id }
80+
let attr = TestAttr::from_fn(&fn_def);
81+
RunnableKind::Test { test_id, attr }
8182
} else if fn_def.has_atom_attr("bench") {
8283
RunnableKind::Bench { test_id }
8384
} else {
@@ -87,6 +88,21 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, fn_def: ast::FnDef) -> Option<Run
8788
Some(Runnable { range: fn_def.syntax().text_range(), kind })
8889
}
8990

91+
#[derive(Debug)]
92+
pub struct TestAttr {
93+
pub ignore: bool,
94+
}
95+
96+
impl TestAttr {
97+
fn from_fn(fn_def: &ast::FnDef) -> TestAttr {
98+
let ignore = fn_def
99+
.attrs()
100+
.filter_map(|attr| attr.simple_name())
101+
.any(|attribute_text| attribute_text == "ignore");
102+
TestAttr { ignore }
103+
}
104+
}
105+
90106
/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
91107
/// `#[test_case(...)]`, `#[tokio::test]` and similar.
92108
/// Also a regular `#[test]` annotation is supported.
@@ -157,6 +173,9 @@ mod tests {
157173
test_id: Path(
158174
"test_foo",
159175
),
176+
attr: TestAttr {
177+
ignore: false,
178+
},
160179
},
161180
},
162181
Runnable {
@@ -165,6 +184,9 @@ mod tests {
165184
test_id: Path(
166185
"test_foo",
167186
),
187+
attr: TestAttr {
188+
ignore: true,
189+
},
168190
},
169191
},
170192
]
@@ -200,6 +222,9 @@ mod tests {
200222
test_id: Path(
201223
"test_mod::test_foo1",
202224
),
225+
attr: TestAttr {
226+
ignore: false,
227+
},
203228
},
204229
},
205230
]
@@ -237,6 +262,9 @@ mod tests {
237262
test_id: Path(
238263
"foo::test_mod::test_foo1",
239264
),
265+
attr: TestAttr {
266+
ignore: false,
267+
},
240268
},
241269
},
242270
]
@@ -276,6 +304,9 @@ mod tests {
276304
test_id: Path(
277305
"foo::bar::test_mod::test_foo1",
278306
),
307+
attr: TestAttr {
308+
ignore: false,
309+
},
279310
},
280311
},
281312
]

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl CargoTargetSpec {
2323
let mut args = Vec::new();
2424
let mut extra_args = Vec::new();
2525
match kind {
26-
RunnableKind::Test { test_id } => {
26+
RunnableKind::Test { test_id, attr } => {
2727
args.push("test".to_string());
2828
if let Some(spec) = spec {
2929
spec.push_to(&mut args);
@@ -33,6 +33,9 @@ impl CargoTargetSpec {
3333
extra_args.push("--exact".to_string());
3434
}
3535
extra_args.push("--nocapture".to_string());
36+
if attr.ignore {
37+
extra_args.push("--ignored".to_string())
38+
}
3639
}
3740
RunnableKind::TestMod { path } => {
3841
args.push("test".to_string());

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ fn to_lsp_runnable(
968968
let (args, extra_args) = CargoTargetSpec::runnable_args(spec, &runnable.kind)?;
969969
let line_index = world.analysis().file_line_index(file_id)?;
970970
let label = match &runnable.kind {
971-
RunnableKind::Test { test_id } => format!("test {}", test_id),
971+
RunnableKind::Test { test_id, .. } => format!("test {}", test_id),
972972
RunnableKind::TestMod { path } => format!("test-mod {}", path),
973973
RunnableKind::Bench { test_id } => format!("bench {}", test_id),
974974
RunnableKind::Bin => "run binary".to_string(),

0 commit comments

Comments
 (0)