diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 446128ae77e3..939071e7119b 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -1,7 +1,5 @@ //! Tests for custom cargo commands and other global command features. -#![allow(deprecated)] - use std::env; use std::fs; use std::io::Read; @@ -12,6 +10,7 @@ use std::str; use cargo_test_support::basic_manifest; use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; +use cargo_test_support::str; use cargo_test_support::tools::echo_subcommand; use cargo_test_support::{ basic_bin_manifest, cargo_exe, cargo_process, paths, project, project_in_home, @@ -26,44 +25,46 @@ fn path() -> Vec { fn list_commands_with_descriptions() { let p = project().build(); p.cargo("--list") - .with_stdout_contains( - " build Compile a local package and all of its dependencies", - ) - // Assert that `read-manifest` prints the right one-line description followed by another - // command, indented. - .with_stdout_contains( - " read-manifest Print a JSON representation of a Cargo.toml manifest.", + .with_stdout_data( + "\ +... + b alias: build +... + build Compile a local package and all of its dependencies +... + c alias: check +... + r alias: run +... + read-manifest Print a JSON representation of a Cargo.toml manifest. +... + t alias: test +... +", ) .run(); } -#[cargo_test] -fn list_builtin_aliases_with_descriptions() { - let p = project().build(); - p.cargo("--list") - .with_stdout_contains(" b alias: build") - .with_stdout_contains(" c alias: check") - .with_stdout_contains(" r alias: run") - .with_stdout_contains(" t alias: test") - .run(); -} - #[cargo_test] fn list_custom_aliases_with_descriptions() { let p = project_in_home("proj") .file( &paths::home().join(".cargo").join("config"), r#" - [alias] - myaliasstr = "foo --bar" - myaliasvec = ["foo", "--bar"] - "#, + [alias] + myaliasstr = "foo --bar" + myaliasvec = ["foo", "--bar"] + "#, ) .build(); p.cargo("--list") - .with_stdout_contains(" myaliasstr alias: foo --bar") - .with_stdout_contains(" myaliasvec alias: foo --bar") + .with_stdout_data(str![[r#" +... + myaliasstr alias: foo --bar + myaliasvec alias: foo --bar +... +"#]]) .run(); } @@ -81,7 +82,11 @@ fn list_dedupe() { p.cargo("--list") .env("PATH", &path) - .with_stdout_contains_n(" dupe", 1) + .with_stdout_data(str![[r#" +... + dupe +... +"#]]) .run(); } @@ -132,6 +137,7 @@ fn list_command_looks_at_path_case_mismatch() { ); } +#[allow(deprecated)] #[cargo_test] fn list_command_handles_known_external_commands() { let p = project() @@ -153,7 +159,10 @@ fn list_command_handles_known_external_commands() { p.cargo("--list") .env("PATH", &path) - .with_stdout_contains(fmt_desc) + .with_stdout_data(str![[r#" +... + fmt Formats all bin and lib files of the current crate using rustfmt. +..."#]]) .run(); } @@ -182,13 +191,15 @@ fn list_command_resolves_symlinks() { fn find_closest_capital_c_to_c() { cargo_process("C") .with_status(101) - .with_stderr_contains( - "\ -error: no such command: `C` + .with_stderr_data(str![[r#" +[ERROR] no such command: `C` -Did you mean `c`? -", - ) + Did you mean `c`? + + View all installed commands with `cargo --list` + Find a package to install `C` with `cargo search cargo-C` + +"#]]) .run(); } @@ -196,13 +207,15 @@ error: no such command: `C` fn find_closest_capital_b_to_b() { cargo_process("B") .with_status(101) - .with_stderr_contains( - "\ -error: no such command: `B` + .with_stderr_data(str![[r#" +[ERROR] no such command: `B` -Did you mean `b`? -", - ) + Did you mean `b`? + + View all installed commands with `cargo --list` + Find a package to install `B` with `cargo search cargo-B` + +"#]]) .run(); } @@ -210,13 +223,15 @@ error: no such command: `B` fn find_closest_biuld_to_build() { cargo_process("biuld") .with_status(101) - .with_stderr_contains( - "\ -error: no such command: `biuld` + .with_stderr_data(str![[r#" +[ERROR] no such command: `biuld` -Did you mean `build`? -", - ) + Did you mean `build`? + + View all installed commands with `cargo --list` + Find a package to install `biuld` with `cargo search cargo-biuld` + +"#]]) .run(); // But, if we actually have `biuld`, it must work! @@ -225,22 +240,26 @@ error: no such command: `biuld` .file( "src/main.rs", r#" - fn main() { - println!("Similar, but not identical to, build"); - } - "#, + fn main() { + println!("Similar, but not identical to, build"); + } + "#, ) .publish(); cargo_process("install cargo-biuld").run(); cargo_process("biuld") - .with_stdout("Similar, but not identical to, build\n") + .with_stdout_data(str![[r#" +Similar, but not identical to, build + +"#]]) .run(); cargo_process("--list") - .with_stdout_contains( - " build Compile a local package and all of its dependencies\n", - ) - .with_stdout_contains(" biuld\n") + .with_stdout_data(str![[r#" +... + biuld + build Compile a local package and all of its dependencies +..."#]]) .run(); } @@ -252,37 +271,38 @@ fn find_closest_alias() { fs::write( &my_home.join("config"), r#" - [alias] - myalias = "build" - "#, + [alias] + myalias = "build" + "#, ) .unwrap(); cargo_process("myalais") .env("CARGO_HOME", &my_home) .with_status(101) - .with_stderr_contains( - "\ -error: no such command: `myalais` + .with_stderr_data(str![[r#" +[WARNING] `[ROOT]/my_home/config` is deprecated in favor of `config.toml` +[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml` +[ERROR] no such command: `myalais` -Did you mean `myalias`? -", - ) + Did you mean `myalias`? + + View all installed commands with `cargo --list` + Find a package to install `myalais` with `cargo search cargo-myalais` + +"#]]) .run(); // But, if no alias is defined, it must not suggest one! cargo_process("myalais") .with_status(101) - .with_stderr_contains( - "\ -error: no such command: `myalais` -", - ) - .with_stderr_does_not_contain( - "\ -Did you mean `myalias`? -", - ) + .with_stderr_data(str![[r#" +[ERROR] no such command: `myalais` + + View all installed commands with `cargo --list` + Find a package to install `myalais` with `cargo search cargo-myalais` + +"#]]) .run(); } @@ -290,31 +310,29 @@ error: no such command: `myalais` #[cargo_test] fn find_closest_dont_correct_nonsense() { cargo_process("there-is-no-way-that-there-is-a-command-close-to-this") - .cwd(&paths::root()) - .with_status(101) - .with_stderr( - "\ + .cwd(&paths::root()) + .with_status(101) + .with_stderr_data(str![[r#" [ERROR] no such command: `there-is-no-way-that-there-is-a-command-close-to-this` -View all installed commands with `cargo --list` -Find a package to install `there-is-no-way-that-there-is-a-command-close-to-this` with `cargo search cargo-there-is-no-way-that-there-is-a-command-close-to-this` -", - ) - .run(); + View all installed commands with `cargo --list` + Find a package to install `there-is-no-way-that-there-is-a-command-close-to-this` with `cargo search cargo-there-is-no-way-that-there-is-a-command-close-to-this` + +"#]]) + .run(); } #[cargo_test] fn displays_subcommand_on_error() { cargo_process("invalid-command") .with_status(101) - .with_stderr( - "\ + .with_stderr_data(str![[r#" [ERROR] no such command: `invalid-command` -View all installed commands with `cargo --list` -Find a package to install `invalid-command` with `cargo search cargo-invalid-command` -", - ) + View all installed commands with `cargo --list` + Find a package to install `invalid-command` with `cargo search cargo-invalid-command` + +"#]]) .run(); } @@ -326,9 +344,9 @@ fn override_cargo_home() { fs::write( &my_home.join("config"), r#" - [cargo-new] - vcs = "none" - "#, + [cargo-new] + vcs = "none" + "#, ) .unwrap(); @@ -365,14 +383,16 @@ fn cargo_subcommand_env() { p.cargo("build").run(); assert!(p.bin("cargo-envtest").is_file()); - let cargo = cargo_exe().canonicalize().unwrap(); let mut path = path(); path.push(target_dir.clone()); let path = env::join_paths(path.iter()).unwrap(); cargo_process("envtest") .env("PATH", &path) - .with_stdout(cargo.to_str().unwrap()) + .with_stdout_data(str![[r#" +[CARGO] + +"#]]) .run(); // Check that subcommands inherit an overridden $CARGO @@ -381,11 +401,13 @@ fn cargo_subcommand_env() { .with_extension(std::env::consts::EXE_EXTENSION) .canonicalize() .unwrap(); - let envtest_bin = envtest_bin.to_str().unwrap(); cargo_process("envtest") .env("PATH", &path) .env(cargo::CARGO_ENV, &envtest_bin) - .with_stdout(envtest_bin) + .with_stdout_data(str![[r#" +[ROOT]/cargo-envtest/target/debug/cargo-envtest[EXE] + +"#]]) .run(); } @@ -424,7 +446,12 @@ fn cargo_cmd_bins_vs_explicit_path() { // If `$CARGO_HOME/bin` is not in a path, prefer it over anything in `$PATH`. // // This is the historical behavior we don't want to break. - cargo_process("foo").with_stdout_contains("INSIDE").run(); + cargo_process("foo") + .with_stdout_data(str![[r#" +INSIDE + +"#]]) + .run(); // When `$CARGO_HOME/bin` is in the `$PATH` // use only `$PATH` so the user-defined ordering is respected. @@ -434,7 +461,10 @@ fn cargo_cmd_bins_vs_explicit_path() { "PATH", join_paths(&[&inside_dir, &outside_dir], "PATH").unwrap(), ) - .with_stdout_contains("INSIDE") + .with_stdout_data(str![[r#" +INSIDE + +"#]]) .run(); cargo_process("foo") @@ -443,7 +473,10 @@ fn cargo_cmd_bins_vs_explicit_path() { "PATH", join_paths(&[inside_dir.join(""), outside_dir.join("")], "PATH").unwrap(), ) - .with_stdout_contains("INSIDE") + .with_stdout_data(str![[r#" +INSIDE + +"#]]) .run(); cargo_process("foo") @@ -451,7 +484,10 @@ fn cargo_cmd_bins_vs_explicit_path() { "PATH", join_paths(&[&outside_dir, &inside_dir], "PATH").unwrap(), ) - .with_stdout_contains("OUTSIDE") + .with_stdout_data(str![[r#" +OUTSIDE + +"#]]) .run(); cargo_process("foo") @@ -460,7 +496,10 @@ fn cargo_cmd_bins_vs_explicit_path() { "PATH", join_paths(&[outside_dir.join(""), inside_dir.join("")], "PATH").unwrap(), ) - .with_stdout_contains("OUTSIDE") + .with_stdout_data(str![[r#" +OUTSIDE + +"#]]) .run(); } } @@ -477,16 +516,43 @@ fn cargo_subcommand_args() { cargo_process("echo bar -v --help") .env("PATH", &path) - .with_stdout("echo bar -v --help") + .with_stdout_data(str![[r#" +echo bar -v --help + +"#]]) .run(); } #[cargo_test] fn explain() { cargo_process("--explain E0001") - .with_stdout_contains( - "This error suggests that the expression arm corresponding to the noted pattern", - ) + .with_stdout_data(str![[r#" +#### Note: this error code is no longer emitted by the compiler. + +This error suggests that the expression arm corresponding to the noted pattern +will never be reached as for all possible values of the expression being +matched, one of the preceding patterns will match. + +This means that perhaps some of the preceding patterns are too general, this +one is too specific or the ordering is incorrect. + +For example, the following `match` block has too many arms: + +``` +match Some(0) { + Some(bar) => {/* ... */} + x => {/* ... */} // This handles the `None` case + _ => {/* ... */} // All possible cases have already been handled +} +``` + +`match` blocks have their patterns matched in order, so, for example, putting +a wildcard arm above a more specific arm will make the latter arm irrelevant. + +Ensure the ordering of the match arm is correct and remove any superfluous +arms. + +"#]]) .run(); } @@ -515,13 +581,13 @@ fn closed_output_ok() { fn subcommand_leading_plus_output_contains() { cargo_process("+nightly") .with_status(101) - .with_stderr( - "\ -error: no such command: `+nightly` + .with_stderr_data(str![[r#" +[ERROR] no such command: `+nightly` -Cargo does not handle `+toolchain` directives. -Did you mean to invoke `cargo` through `rustup` instead?", - ) + Cargo does not handle `+toolchain` directives. + Did you mean to invoke `cargo` through `rustup` instead? + +"#]]) .run(); } @@ -529,15 +595,14 @@ error: no such command: `+nightly` fn full_did_you_mean() { cargo_process("bluid") .with_status(101) - .with_stderr( - "\ -error: no such command: `bluid` + .with_stderr_data(str![[r#" +[ERROR] no such command: `bluid` -Did you mean `build`? + Did you mean `build`? -View all installed commands with `cargo --list` -Find a package to install `bluid` with `cargo search cargo-bluid` -", - ) + View all installed commands with `cargo --list` + Find a package to install `bluid` with `cargo search cargo-bluid` + +"#]]) .run(); }