Skip to main content

ra_ap_rust_analyzer/cli/
run_tests.rs

1//! Run all tests in a project, similar to `cargo test`, but using the mir interpreter.
2
3use hir::{Crate, Module};
4use hir_ty::db::HirDatabase;
5use ide_db::{LineIndexDatabase, base_db::SourceDatabase};
6use profile::StopWatch;
7use project_model::{CargoConfig, RustLibSource};
8use syntax::TextRange;
9
10use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace_at};
11
12use crate::cli::{Result, flags, full_name_of_item};
13
14impl flags::RunTests {
15    pub fn run(self) -> Result<()> {
16        let cargo_config = CargoConfig {
17            sysroot: Some(RustLibSource::Discover),
18            all_targets: true,
19            set_test: true,
20            ..Default::default()
21        };
22        let load_cargo_config = LoadCargoConfig {
23            load_out_dirs_from_check: true,
24            with_proc_macro_server: ProcMacroServerChoice::Sysroot,
25            prefill_caches: false,
26            proc_macro_processes: 1,
27        };
28        let (ref db, _vfs, _proc_macro) =
29            load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
30
31        let tests = all_modules(db)
32            .into_iter()
33            .flat_map(|x| x.declarations(db))
34            .filter_map(|x| match x {
35                hir::ModuleDef::Function(f) => Some(f),
36                _ => None,
37            })
38            .filter(|x| x.is_test(db));
39        let span_formatter = |file_id, text_range: TextRange| {
40            let line_col = match db.line_index(file_id).try_line_col(text_range.start()) {
41                None => " (unknown line col)".to_owned(),
42                Some(x) => format!("#{}:{}", x.line + 1, x.col),
43            };
44            let source_root = db.file_source_root(file_id).source_root_id(db);
45            let source_root = db.source_root(source_root).source_root(db);
46
47            let path = source_root.path_for_file(&file_id).map(|x| x.to_string());
48            let path = path.as_deref().unwrap_or("<unknown file>");
49            format!("file://{path}{line_col}")
50        };
51        let mut pass_count = 0;
52        let mut ignore_count = 0;
53        let mut fail_count = 0;
54        let mut sw_all = StopWatch::start();
55        for test in tests {
56            let full_name = full_name_of_item(db, test.module(db), test.name(db));
57            println!("test {full_name}");
58            if test.is_ignore(db) {
59                println!("ignored");
60                ignore_count += 1;
61                continue;
62            }
63            let mut sw_one = StopWatch::start();
64            let result = test.eval(db, span_formatter);
65            match &result {
66                Ok(result) if result.trim() == "pass" => pass_count += 1,
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
78fn 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}