Skip to main content

cargo/ops/
cargo_test.rs

1use crate::core::compiler::{Compilation, Doctest, Unit, UnitHash, UnitOutput};
2use crate::core::profiles::PanicStrategy;
3use crate::core::shell::ColorChoice;
4use crate::core::shell::Verbosity;
5use crate::core::{TargetKind, Workspace};
6use crate::ops;
7use crate::util::errors::CargoResult;
8use crate::util::{CliError, CliResult, GlobalContext, add_path_args};
9use anyhow::format_err;
10use cargo_util::{ProcessBuilder, ProcessError};
11use std::collections::HashMap;
12use std::ffi::OsString;
13use std::fmt::Write;
14use std::path::{Path, PathBuf};
15
16pub struct TestOptions {
17    pub compile_opts: ops::CompileOptions,
18    pub no_run: bool,
19    pub no_fail_fast: bool,
20}
21
22/// The kind of test.
23///
24/// This is needed because `Unit` does not track whether or not something is a
25/// benchmark.
26#[derive(Copy, Clone)]
27enum TestKind {
28    Test,
29    Bench,
30    Doctest,
31}
32
33/// A unit that failed to run.
34struct UnitTestError {
35    unit: Unit,
36    kind: TestKind,
37}
38
39impl UnitTestError {
40    /// Returns the CLI args needed to target this unit.
41    fn cli_args(&self, ws: &Workspace<'_>, opts: &ops::CompileOptions) -> String {
42        let mut args = if opts.spec.needs_spec_flag(ws) {
43            format!("-p {} ", self.unit.pkg.name())
44        } else {
45            String::new()
46        };
47        let mut add = |which| write!(args, "--{which} {}", self.unit.target.name()).unwrap();
48
49        match self.kind {
50            TestKind::Test | TestKind::Bench => match self.unit.target.kind() {
51                TargetKind::Lib(_) => args.push_str("--lib"),
52                TargetKind::Bin => add("bin"),
53                TargetKind::Test => add("test"),
54                TargetKind::Bench => add("bench"),
55                TargetKind::ExampleLib(_) | TargetKind::ExampleBin => add("example"),
56                TargetKind::CustomBuild => panic!("unexpected CustomBuild kind"),
57            },
58            TestKind::Doctest => args.push_str("--doc"),
59        }
60        args
61    }
62}
63
64/// Compiles and runs tests.
65///
66/// On error, the returned [`CliError`] will have the appropriate process exit
67/// code that Cargo should use.
68pub fn run_tests(ws: &Workspace<'_>, options: &TestOptions, test_args: &[&str]) -> CliResult {
69    let compilation = compile_tests(ws, options)?;
70
71    if options.no_run {
72        if !options.compile_opts.build_config.emit_json() {
73            display_no_run_information(ws, test_args, &compilation, "unittests")?;
74        }
75        return Ok(());
76    }
77    let mut errors = run_unit_tests(ws, options, test_args, &compilation, TestKind::Test)?;
78
79    let doctest_errors = run_doc_tests(ws, options, test_args, &compilation)?;
80    errors.extend(doctest_errors);
81    no_fail_fast_err(ws, &options.compile_opts, &errors)
82}
83
84/// Compiles and runs benchmarks.
85///
86/// On error, the returned [`CliError`] will have the appropriate process exit
87/// code that Cargo should use.
88pub fn run_benches(ws: &Workspace<'_>, options: &TestOptions, args: &[&str]) -> CliResult {
89    let compilation = compile_tests(ws, options)?;
90
91    if options.no_run {
92        if !options.compile_opts.build_config.emit_json() {
93            display_no_run_information(ws, args, &compilation, "benches")?;
94        }
95        return Ok(());
96    }
97
98    let mut args = args.to_vec();
99    args.push("--bench");
100
101    let errors = run_unit_tests(ws, options, &args, &compilation, TestKind::Bench)?;
102    no_fail_fast_err(ws, &options.compile_opts, &errors)
103}
104
105fn compile_tests<'a>(ws: &Workspace<'a>, options: &TestOptions) -> CargoResult<Compilation<'a>> {
106    let mut compilation = ops::compile(ws, &options.compile_opts)?;
107    compilation.tests.sort_by_key(|u| u.unit.clone());
108    Ok(compilation)
109}
110
111/// Runs the unit and integration tests of a package.
112///
113/// Returns a `Vec` of tests that failed when `--no-fail-fast` is used.
114/// If `--no-fail-fast` is *not* used, then this returns an `Err`.
115fn run_unit_tests(
116    ws: &Workspace<'_>,
117    options: &TestOptions,
118    test_args: &[&str],
119    compilation: &Compilation<'_>,
120    test_kind: TestKind,
121) -> Result<Vec<UnitTestError>, CliError> {
122    let gctx = ws.gctx();
123    let cwd = gctx.cwd();
124    let mut errors = Vec::new();
125
126    for UnitOutput {
127        unit,
128        path,
129        script_metas,
130        env,
131    } in compilation.tests.iter()
132    {
133        let (exe_display, mut cmd) = cmd_builds(
134            gctx,
135            cwd,
136            unit,
137            path,
138            script_metas.as_ref(),
139            env,
140            test_args,
141            compilation,
142            "unittests",
143        )?;
144
145        if gctx.extra_verbose() {
146            cmd.display_env_vars();
147        }
148
149        gctx.shell()
150            .concise(|shell| shell.status("Running", &exe_display))?;
151        gctx.shell()
152            .verbose(|shell| shell.status("Running", &cmd))?;
153
154        if let Err(e) = cmd.exec() {
155            let code = fail_fast_code(&e);
156            let unit_err = UnitTestError {
157                unit: unit.clone(),
158                kind: test_kind,
159            };
160            report_test_error(ws, test_args, &options.compile_opts, &unit_err, e);
161            errors.push(unit_err);
162            if !options.no_fail_fast {
163                return Err(CliError::code(code));
164            }
165        }
166    }
167    Ok(errors)
168}
169
170/// Runs doc tests.
171///
172/// Returns a `Vec` of tests that failed when `--no-fail-fast` is used.
173/// If `--no-fail-fast` is *not* used, then this returns an `Err`.
174fn run_doc_tests(
175    ws: &Workspace<'_>,
176    options: &TestOptions,
177    test_args: &[&str],
178    compilation: &Compilation<'_>,
179) -> Result<Vec<UnitTestError>, CliError> {
180    let gctx = ws.gctx();
181    let mut errors = Vec::new();
182    let color = gctx.shell().color_choice();
183
184    for doctest_info in &compilation.to_doc_test {
185        let Doctest {
186            args,
187            unstable_opts,
188            unit,
189            linker,
190            script_metas,
191            env,
192        } = doctest_info;
193
194        gctx.shell().status("Doc-tests", unit.target.name())?;
195        let mut p = compilation.rustdoc_process(unit, script_metas.as_ref())?;
196
197        for (var, value) in env {
198            p.env(var, value);
199        }
200
201        let color_arg = match color {
202            ColorChoice::Always => "always",
203            ColorChoice::Never => "never",
204            ColorChoice::CargoAuto => "auto",
205        };
206        p.arg("--color").arg(color_arg);
207
208        p.arg("--crate-name").arg(&unit.target.crate_name());
209        p.arg("--test");
210
211        add_path_args(ws, unit, &mut p);
212        p.arg("--test-run-directory").arg(unit.pkg.root());
213
214        unit.kind.add_target_arg(&mut p);
215
216        if let Some((runtool, runtool_args)) = compilation.target_runner(unit.kind) {
217            p.arg("--test-runtool").arg(runtool);
218            for arg in runtool_args {
219                p.arg("--test-runtool-arg").arg(arg);
220            }
221        }
222        if let Some(linker) = linker {
223            let mut joined = OsString::from("linker=");
224            joined.push(linker);
225            p.arg("-C").arg(joined);
226        }
227
228        if unit.profile.panic != PanicStrategy::Unwind {
229            p.arg("-C").arg(format!("panic={}", unit.profile.panic));
230        }
231
232        for native_dep in compilation.native_dirs.iter() {
233            p.arg("-L").arg(native_dep);
234        }
235
236        for arg in test_args {
237            p.arg("--test-args").arg(arg);
238        }
239
240        if gctx.shell().verbosity() == Verbosity::Quiet {
241            p.arg("--test-args").arg("--quiet");
242        }
243
244        p.args(unit.pkg.manifest().lint_rustflags());
245
246        p.args(args);
247
248        if *unstable_opts {
249            p.arg("-Zunstable-options");
250        }
251
252        if gctx.extra_verbose() {
253            p.display_env_vars();
254        }
255
256        gctx.shell()
257            .verbose(|shell| shell.status("Running", p.to_string()))?;
258
259        if let Err(e) = p.exec() {
260            let code = fail_fast_code(&e);
261            let unit_err = UnitTestError {
262                unit: unit.clone(),
263                kind: TestKind::Doctest,
264            };
265            report_test_error(ws, test_args, &options.compile_opts, &unit_err, e);
266            errors.push(unit_err);
267            if !options.no_fail_fast {
268                return Err(CliError::code(code));
269            }
270        }
271    }
272    Ok(errors)
273}
274
275/// Displays human-readable descriptions of the test executables.
276///
277/// This is used when `cargo test --no-run` is used.
278fn display_no_run_information(
279    ws: &Workspace<'_>,
280    test_args: &[&str],
281    compilation: &Compilation<'_>,
282    exec_type: &str,
283) -> CargoResult<()> {
284    let gctx = ws.gctx();
285    let cwd = gctx.cwd();
286    for UnitOutput {
287        unit,
288        path,
289        script_metas,
290        env,
291    } in compilation.tests.iter()
292    {
293        let (exe_display, cmd) = cmd_builds(
294            gctx,
295            cwd,
296            unit,
297            path,
298            script_metas.as_ref(),
299            env,
300            test_args,
301            compilation,
302            exec_type,
303        )?;
304        gctx.shell()
305            .concise(|shell| shell.status("Executable", &exe_display))?;
306        gctx.shell()
307            .verbose(|shell| shell.status("Executable", &cmd))?;
308    }
309
310    return Ok(());
311}
312
313/// Creates a [`ProcessBuilder`] for executing a single test.
314///
315/// Returns a tuple `(exe_display, process)` where `exe_display` is a string
316/// to display that describes the executable path in a human-readable form.
317/// `process` is the `ProcessBuilder` to use for executing the test.
318fn cmd_builds(
319    gctx: &GlobalContext,
320    cwd: &Path,
321    unit: &Unit,
322    path: &PathBuf,
323    script_metas: Option<&Vec<UnitHash>>,
324    env: &HashMap<String, OsString>,
325    test_args: &[&str],
326    compilation: &Compilation<'_>,
327    exec_type: &str,
328) -> CargoResult<(String, ProcessBuilder)> {
329    let test_path = unit.target.src_path().path().unwrap();
330    let short_test_path = test_path
331        .strip_prefix(unit.pkg.root())
332        .unwrap_or(test_path)
333        .display();
334
335    let exe_display = match unit.target.kind() {
336        TargetKind::Test | TargetKind::Bench => format!(
337            "{} ({})",
338            short_test_path,
339            path.strip_prefix(cwd).unwrap_or(path).display()
340        ),
341        _ => format!(
342            "{} {} ({})",
343            exec_type,
344            short_test_path,
345            path.strip_prefix(cwd).unwrap_or(path).display()
346        ),
347    };
348
349    let mut cmd = compilation.target_process(path, unit.kind, &unit.pkg, script_metas)?;
350    cmd.args(test_args);
351    if unit.target.harness() && gctx.shell().verbosity() == Verbosity::Quiet {
352        cmd.arg("--quiet");
353    }
354    for (key, val) in env.iter() {
355        cmd.env(key, val);
356    }
357
358    Ok((exe_display, cmd))
359}
360
361/// Returns the error code to use when *not* using `--no-fail-fast`.
362///
363/// Cargo will return the error code from the test process itself. If some
364/// other error happened (like a failure to launch the process), then it will
365/// return a standard 101 error code.
366///
367/// When using `--no-fail-fast`, Cargo always uses the 101 exit code (since
368/// there may not be just one process to report).
369fn fail_fast_code(error: &anyhow::Error) -> i32 {
370    if let Some(proc_err) = error.downcast_ref::<ProcessError>() {
371        if let Some(code) = proc_err.code {
372            return code;
373        }
374    }
375    101
376}
377
378/// Returns the `CliError` when using `--no-fail-fast` and there is at least
379/// one error.
380fn no_fail_fast_err(
381    ws: &Workspace<'_>,
382    opts: &ops::CompileOptions,
383    errors: &[UnitTestError],
384) -> CliResult {
385    // TODO: This could be improved by combining the flags on a single line when feasible.
386    let args: Vec<_> = errors
387        .iter()
388        .map(|unit_err| format!("    `{}`", unit_err.cli_args(ws, opts)))
389        .collect();
390    let message = match errors.len() {
391        0 => return Ok(()),
392        1 => format!("1 target failed:\n{}", args.join("\n")),
393        n => format!("{n} targets failed:\n{}", args.join("\n")),
394    };
395    Err(anyhow::Error::msg(message).into())
396}
397
398/// Displays an error on the console about a test failure.
399fn report_test_error(
400    ws: &Workspace<'_>,
401    test_args: &[&str],
402    opts: &ops::CompileOptions,
403    unit_err: &UnitTestError,
404    test_error: anyhow::Error,
405) {
406    let which = match unit_err.kind {
407        TestKind::Test => "test failed",
408        TestKind::Bench => "bench failed",
409        TestKind::Doctest => "doctest failed",
410    };
411
412    let mut err = format_err!("{}, to rerun pass `{}`", which, unit_err.cli_args(ws, opts));
413    // Don't show "process didn't exit successfully" for simple errors.
414    // libtest exits with 101 for normal errors.
415    let (is_simple, executed) = test_error
416        .downcast_ref::<ProcessError>()
417        .and_then(|proc_err| proc_err.code)
418        .map_or((false, false), |code| (code == 101, true));
419
420    if !is_simple {
421        err = test_error.context(err);
422    }
423
424    crate::display_error(&err, &mut ws.gctx().shell());
425
426    let harness: bool = unit_err.unit.target.harness();
427    let nocapture: bool = test_args.contains(&"--nocapture") || test_args.contains(&"--no-capture");
428
429    if !is_simple && executed && harness && !nocapture {
430        drop(ws.gctx().shell().note(
431            "test exited abnormally; to see the full output pass --no-capture to the harness.",
432        ));
433    }
434}