Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print executable name on cargo test --no-run. #9959

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn substitute_macros(input: &str) -> String {
("[YANK]", " Yank"),
("[OWNER]", " Owner"),
("[MIGRATING]", " Migrating"),
("[EXECUTABLE]", " Executable"),
];
let mut result = input.to_owned();
for &(pat, subst) in &macros {
Expand Down
105 changes: 81 additions & 24 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::core::compiler::{Compilation, CompileKind, Doctest, UnitOutput};
use crate::core::compiler::{Compilation, CompileKind, Doctest, Metadata, Unit, UnitOutput};
use crate::core::shell::Verbosity;
use crate::core::{TargetKind, Workspace};
use crate::ops;
use crate::util::errors::CargoResult;
use crate::util::{add_path_args, CargoTestError, Config, Test};
use cargo_util::ProcessError;
use cargo_util::{ProcessBuilder, ProcessError};
use std::ffi::OsString;
use std::path::{Path, PathBuf};

pub struct TestOptions {
pub compile_opts: ops::CompileOptions,
Expand All @@ -21,6 +22,30 @@ pub fn run_tests(
let compilation = compile_tests(ws, options)?;

if options.no_run {
let config = ws.config();
let cwd = config.cwd();
for UnitOutput {
unit,
path,
script_meta,
} in compilation.tests.iter()
{
let (exe_display, cmd) = cmd_builds(
config,
cwd,
unit,
path,
script_meta,
test_args,
&compilation,
)?;
config
.shell()
.concise(|shell| shell.status("Executable", &exe_display))?;
config
.shell()
.verbose(|shell| shell.status("Executable", &cmd))?;
}
Comment on lines +25 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is quite a lot of duplicated code with run_benches, can this be extracted out to a function to deduplicate it?

return Ok(None);
}
let (test, mut errors) = run_unit_tests(ws.config(), options, test_args, &compilation)?;
Expand Down Expand Up @@ -48,6 +73,23 @@ pub fn run_benches(
let compilation = compile_tests(ws, options)?;

if options.no_run {
let config = ws.config();
let cwd = config.cwd();
for UnitOutput {
unit,
path,
script_meta,
} in compilation.tests.iter()
{
let (exe_display, cmd) =
cmd_builds(config, cwd, unit, path, script_meta, args, &compilation)?;
config
.shell()
.concise(|shell| shell.status("Executable", &exe_display))?;
config
.shell()
.verbose(|shell| shell.status("Executable", &cmd))?;
}
return Ok(None);
}

Expand Down Expand Up @@ -86,28 +128,8 @@ fn run_unit_tests(
{
let test = unit.target.name().to_string();

let test_path = unit.target.src_path().path().unwrap();
let exe_display = if let TargetKind::Test = unit.target.kind() {
format!(
"{} ({})",
test_path
.strip_prefix(unit.pkg.root())
.unwrap_or(test_path)
.display(),
path.strip_prefix(cwd).unwrap_or(path).display()
)
} else {
format!(
"unittests ({})",
path.strip_prefix(cwd).unwrap_or(path).display()
)
};

let mut cmd = compilation.target_process(path, unit.kind, &unit.pkg, *script_meta)?;
cmd.args(test_args);
if unit.target.harness() && config.shell().verbosity() == Verbosity::Quiet {
cmd.arg("--quiet");
}
let (exe_display, cmd) =
cmd_builds(config, cwd, unit, path, script_meta, test_args, compilation)?;
config
.shell()
.concise(|shell| shell.status("Running", &exe_display))?;
Expand Down Expand Up @@ -152,6 +174,41 @@ fn run_unit_tests(
}
}

fn cmd_builds(
config: &Config,
cwd: &Path,
unit: &Unit,
path: &PathBuf,
script_meta: &Option<Metadata>,
test_args: &[&str],
compilation: &Compilation<'_>,
) -> CargoResult<(String, ProcessBuilder)> {
let test_path = unit.target.src_path().path().unwrap();
let exe_display = if let TargetKind::Test = unit.target.kind() {
format!(
"{} ({})",
test_path
.strip_prefix(unit.pkg.root())
.unwrap_or(test_path)
.display(),
path.strip_prefix(cwd).unwrap_or(path).display()
)
} else {
format!(
"unittests ({})",
path.strip_prefix(cwd).unwrap_or(path).display()
)
};

let mut cmd = compilation.target_process(path, unit.kind, &unit.pkg, *script_meta)?;
cmd.args(test_args);
if unit.target.harness() && config.shell().verbosity() == Verbosity::Quiet {
cmd.arg("--quiet");
}

Ok((exe_display, cmd))
}

fn run_doc_tests(
ws: &Workspace<'_>,
options: &TestOptions,
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,8 @@ fn test_bench_no_run() {
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] bench [optimized] target(s) in [..]
[EXECUTABLE] [..] (target/release/deps/foo-[..][EXE])
[EXECUTABLE] [..] (target/release/deps/bbaz-[..][EXE])
",
)
.run();
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,7 @@ fn minimal_download() {
[COMPILING] dev_dep v1.0.0
[COMPILING] foo v0.1.0 [..]
[FINISHED] [..]
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
",
)
.run();
Expand Down
4 changes: 4 additions & 0 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ fn no_rebuild_transitive_target_deps() {
[COMPILING] b v0.0.1 ([..])
[COMPILING] foo v0.0.1 ([..])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
",
)
.run();
Expand Down Expand Up @@ -1125,6 +1127,7 @@ fn reuse_workspace_lib() {
[COMPILING] baz v0.1.1 ([..])
[RUNNING] `rustc[..] --test [..]`
[FINISHED] [..]
[EXECUTABLE] `[..]/target/debug/deps/baz-[..][EXE]`
",
)
.run();
Expand Down Expand Up @@ -1376,6 +1379,7 @@ fn reuse_panic_build_dep_test() {
[RUNNING] [..]build-script-build`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--test[..]
[FINISHED] [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
",
)
.run();
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ fn fresh_swapping_commands() {
[FRESH] bar v1.0.0
[FRESH] foo [..]
[FINISHED] [..]
[EXECUTABLE] `[..]/target/release/deps/foo-[..][EXE]`
",
)
.run();
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn deduplicate_messages_basic() {
warning: `foo` (lib) generated 1 warning
warning: `foo` (lib test) generated 1 warning (1 duplicate)
[FINISHED] [..]
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
",
rustc_message
);
Expand Down Expand Up @@ -106,6 +107,7 @@ warning: `foo` (lib) generated 1 warning
{}\
warning: `foo` (lib test) generated 2 warnings (1 duplicate)
[FINISHED] [..]
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
",
lib_output, lib_test_output
);
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/profile_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ fn test_with_dev_profile() {
[COMPILING] foo v0.1.0 [..]
[RUNNING] `rustc --crate-name foo [..]-C debuginfo=2[..]
[FINISHED] [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
",
)
.run();
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ fn panic_unwind_does_not_build_twice() {
[RUNNING] `rustc --crate-name foo src/main.rs [..] --test [..]
[RUNNING] `rustc --crate-name t1 tests/t1.rs [..]
[FINISHED] [..]
[EXECUTABLE] `[..]/target/debug/deps/t1-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
",
)
.run();
Expand Down
10 changes: 10 additions & 0 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,9 @@ fn cfg_rustflags_normal_source() {
[RUNNING] `rustc [..] --cfg bar[..]`
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/a-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/c-[..][EXE]`
",
)
.run();
Expand All @@ -1111,6 +1114,8 @@ fn cfg_rustflags_normal_source() {
[RUNNING] `rustc [..] --cfg bar[..]`
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] bench [optimized] target(s) in [..]
[EXECUTABLE] `[..]/target/release/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/release/deps/a-[..][EXE]`
",
)
.run();
Expand Down Expand Up @@ -1181,6 +1186,9 @@ fn cfg_rustflags_precedence() {
[RUNNING] `rustc [..] --cfg bar[..]`
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/a-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/c-[..][EXE]`
",
)
.run();
Expand All @@ -1193,6 +1201,8 @@ fn cfg_rustflags_precedence() {
[RUNNING] `rustc [..] --cfg bar[..]`
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] bench [optimized] target(s) in [..]
[EXECUTABLE] `[..]/target/release/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/release/deps/a-[..][EXE]`
",
)
.run();
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ fn test_no_run() {
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] [..] (target/debug/deps/foo-[..][EXE])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason to use [..] here to ignore part of the output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually looked at other tests such as test_run_simple where the [RUNNING] part was tested like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yea I'm not sure why it does that since in that case it is just the word "unittests". I think it would be good to include the actual output here so that it is clearer what is being emitted and checked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a chance, I would appreciate if you can remove the [..] here and the other tests just so that the tests are verifying the actual output.

",
)
.run();
Expand Down Expand Up @@ -1963,6 +1964,7 @@ fn example_bin_same_name() {
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
",
)
.run();
Expand Down Expand Up @@ -2498,6 +2500,9 @@ fn bin_does_not_rebuild_tests() {
[RUNNING] `rustc [..] src/main.rs [..]`
[RUNNING] `rustc [..] src/main.rs [..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
[EXECUTABLE] `[..]/target/debug/deps/foo-[..][EXE]`
",
)
.run();
Expand Down Expand Up @@ -2556,6 +2561,7 @@ fn selective_test_optional_dep() {
[RUNNING] `rustc [..] a/src/lib.rs [..]`
[RUNNING] `rustc [..] a/src/lib.rs [..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[EXECUTABLE] `[..]/target/debug/deps/a-[..][EXE]`
",
)
.run();
Expand Down