|
| 1 | +//! Run all tests in a project, similar to `cargo test`, but using the mir interpreter. |
| 2 | +
|
| 3 | +use hir::{Crate, Module}; |
| 4 | +use hir_ty::db::HirDatabase; |
| 5 | +use ide_db::{base_db::SourceDatabaseExt, LineIndexDatabase}; |
| 6 | +use profile::StopWatch; |
| 7 | +use project_model::{CargoConfig, RustLibSource}; |
| 8 | +use syntax::TextRange; |
| 9 | + |
| 10 | +use crate::cli::{ |
| 11 | + flags, full_name_of_item, |
| 12 | + load_cargo::load_workspace_at, |
| 13 | + load_cargo::{LoadCargoConfig, ProcMacroServerChoice}, |
| 14 | + Result, |
| 15 | +}; |
| 16 | + |
| 17 | +impl flags::RunTests { |
| 18 | + pub fn run(self) -> Result<()> { |
| 19 | + let mut cargo_config = CargoConfig::default(); |
| 20 | + cargo_config.sysroot = Some(RustLibSource::Discover); |
| 21 | + let load_cargo_config = LoadCargoConfig { |
| 22 | + load_out_dirs_from_check: true, |
| 23 | + with_proc_macro_server: ProcMacroServerChoice::Sysroot, |
| 24 | + prefill_caches: false, |
| 25 | + }; |
| 26 | + let (host, _vfs, _proc_macro) = |
| 27 | + load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; |
| 28 | + let db = host.raw_database(); |
| 29 | + |
| 30 | + let tests = all_modules(db) |
| 31 | + .into_iter() |
| 32 | + .flat_map(|x| x.declarations(db)) |
| 33 | + .filter_map(|x| match x { |
| 34 | + hir::ModuleDef::Function(f) => Some(f), |
| 35 | + _ => None, |
| 36 | + }) |
| 37 | + .filter(|x| x.is_test(db)); |
| 38 | + let span_formatter = |file_id, text_range: TextRange| { |
| 39 | + let line_col = match db.line_index(file_id).try_line_col(text_range.start()) { |
| 40 | + None => " (unknown line col)".to_string(), |
| 41 | + Some(x) => format!("#{}:{}", x.line + 1, x.col), |
| 42 | + }; |
| 43 | + let path = &db |
| 44 | + .source_root(db.file_source_root(file_id)) |
| 45 | + .path_for_file(&file_id) |
| 46 | + .map(|x| x.to_string()); |
| 47 | + let path = path.as_deref().unwrap_or("<unknown file>"); |
| 48 | + format!("file://{path}{line_col}") |
| 49 | + }; |
| 50 | + let mut pass_count = 0; |
| 51 | + let mut ignore_count = 0; |
| 52 | + let mut fail_count = 0; |
| 53 | + let mut sw_all = StopWatch::start(); |
| 54 | + for test in tests { |
| 55 | + let full_name = full_name_of_item(db, test.module(db), test.name(db)); |
| 56 | + println!("test {}", full_name); |
| 57 | + if test.is_ignore(db) { |
| 58 | + println!("ignored"); |
| 59 | + ignore_count += 1; |
| 60 | + continue; |
| 61 | + } |
| 62 | + let mut sw_one = StopWatch::start(); |
| 63 | + let result = test.eval(db, span_formatter); |
| 64 | + if result.trim() == "pass" { |
| 65 | + pass_count += 1; |
| 66 | + } else { |
| 67 | + fail_count += 1; |
| 68 | + } |
| 69 | + println!("{}", result); |
| 70 | + eprintln!("{:<20} {}", format!("test {}", full_name), sw_one.elapsed()); |
| 71 | + } |
| 72 | + println!("{pass_count} passed, {fail_count} failed, {ignore_count} ignored"); |
| 73 | + eprintln!("{:<20} {}", "All tests", sw_all.elapsed()); |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn all_modules(db: &dyn HirDatabase) -> Vec<Module> { |
| 79 | + let mut worklist: Vec<_> = Crate::all(db) |
| 80 | + .into_iter() |
| 81 | + .filter(|x| x.origin(db).is_local()) |
| 82 | + .map(|krate| krate.root_module(db)) |
| 83 | + .collect(); |
| 84 | + let mut modules = Vec::new(); |
| 85 | + |
| 86 | + while let Some(module) = worklist.pop() { |
| 87 | + modules.push(module); |
| 88 | + worklist.extend(module.children(db)); |
| 89 | + } |
| 90 | + |
| 91 | + modules |
| 92 | +} |
0 commit comments