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 #2 #10346

Merged
merged 6 commits 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
113 changes: 89 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,7 @@ pub fn run_tests(
let compilation = compile_tests(ws, options)?;

if options.no_run {
display_no_run_information(ws, test_args, &compilation, "unittests")?;
return Ok(None);
}
let (test, mut errors) = run_unit_tests(ws.config(), options, test_args, &compilation)?;
Expand Down Expand Up @@ -48,6 +50,7 @@ pub fn run_benches(
let compilation = compile_tests(ws, options)?;

if options.no_run {
display_no_run_information(ws, args, &compilation, "benches")?;
return Ok(None);
}

Expand Down Expand Up @@ -84,28 +87,16 @@ fn run_unit_tests(
script_meta,
} in compilation.tests.iter()
{
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,
"unittests",
)?;
config
.shell()
.concise(|shell| shell.status("Running", &exe_display))?;
Expand Down Expand Up @@ -264,3 +255,77 @@ fn run_doc_tests(
}
Ok((Test::Doc, errors))
}

fn display_no_run_information(
ws: &Workspace<'_>,
test_args: &[&str],
compilation: &Compilation<'_>,
exec_type: &str,
) -> CargoResult<()> {
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,
exec_type,
)?;
config
.shell()
.concise(|shell| shell.status("Executable", &exe_display))?;
config
.shell()
.verbose(|shell| shell.status("Executable", &cmd))?;
}

return Ok(());
}

fn cmd_builds(
config: &Config,
cwd: &Path,
unit: &Unit,
path: &PathBuf,
script_meta: &Option<Metadata>,
test_args: &[&str],
compilation: &Compilation<'_>,
exec_type: &str,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ehuss Let me know if you want this to be an enum.

) -> CargoResult<(String, ProcessBuilder)> {
let test_path = unit.target.src_path().path().unwrap();
let short_test_path = test_path
.strip_prefix(unit.pkg.root())
.unwrap_or(test_path)
.display();

let exe_display = match unit.target.kind() {
TargetKind::Test | TargetKind::Bench => format!(
"{} ({})",
short_test_path,
path.strip_prefix(cwd).unwrap_or(path).display()
),
_ => format!(
"{} {} ({})",
exec_type,
short_test_path,
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))
}
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] benches src/lib.rs (target/release/deps/foo-[..][EXE])
[EXECUTABLE] benches/bbaz.rs (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] unittests src/lib.rs (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] unittests src/lib.rs (target/debug/deps/foo-[..][EXE])
[EXECUTABLE] tests/foo.rs (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 @@ -836,6 +836,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] unittests src/lib.rs (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] unittests src/lib.rs (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 @@ -452,6 +452,7 @@ fn test_with_dev_profile() {
[COMPILING] foo v0.1.0 [..]
[RUNNING] `rustc --crate-name foo [..]-C debuginfo=0[..]
[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] unittests src/lib.rs (target/debug/deps/foo-[..][EXE])
",
)
.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