From 1bf5eea5e358651db54f4be46e41c982c1ae1b99 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Thu, 20 Jun 2024 19:46:20 +0800 Subject: [PATCH] test: migrate cargo_command to snapbox --- tests/testsuite/cargo_command.rs | 347 ++++++++++++++++++++----------- 1 file changed, 227 insertions(+), 120 deletions(-) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index 446128ae77e3..befd01197fb1 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,26 +25,65 @@ 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.", - ) - .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(); + .with_stdout_data(str![[r#" +Installed Commands: + add Add dependencies to a Cargo.toml manifest file + b alias: build + bench Execute all benchmarks of a local package + build Compile a local package and all of its dependencies + c alias: check + check Check a local package and all of its dependencies for errors + clean Remove artifacts that cargo has generated in the past + clippy Checks a package to catch common mistakes and improve your Rust code. + config Inspect configuration values + d alias: doc + date + dl + doc Build a package's documentation + fetch Fetch dependencies of a package from the network + fix Automatically fix lint warnings reported by rustc + fmt Formats all bin and lib files of the current crate using rustfmt. + fuzz + generate-lockfile Generate the lockfile for a package + git-checkout This command has been removed + help Displays help for a cargo subcommand + init Create a new cargo package in an existing directory + install Install a Rust binary + llvm-cov + locate-project Print a JSON representation of a Cargo.toml file's location + login Log in to a registry. + logout Remove an API token from the registry locally + metadata Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format + miri + new Create a new cargo package at + nextest + owner Manage the owners of a crate on the registry + package Assemble the local package into a distributable tarball + pkgid Print a fully qualified package specification + publish Upload a package to the registry + r alias: run + read-manifest Print a JSON representation of a Cargo.toml manifest. + remove Remove dependencies from a Cargo.toml manifest file + report Generate and display various kinds of reports + rm alias: remove + run Run a binary or example of the local package + rustc Compile a package, and pass extra options to the compiler + rustdoc Build a package's documentation, using specified custom flags. + search Search packages in the registry. Default registry is crates.io + set-version + t alias: test + test Execute all unit and integration tests and build examples of a local package + tree Display a tree visualization of a dependency graph + uninstall Remove a Rust binary + update Update dependencies as recorded in the local lock file + upgrade + vendor Vendor all dependencies for a project locally + verify-project Check correctness of crate manifest + version Show version information + yank Remove a pushed crate from the index + +"#]]) + .run(); } #[cargo_test] @@ -54,16 +92,20 @@ fn list_custom_aliases_with_descriptions() { .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 +123,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 +178,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 +200,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 +232,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 +248,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 +264,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 +281,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 +312,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 +351,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 +385,9 @@ fn override_cargo_home() { fs::write( &my_home.join("config"), r#" - [cargo-new] - vcs = "none" - "#, + [cargo-new] + vcs = "none" + "#, ) .unwrap(); @@ -365,14 +424,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 @@ -385,7 +446,10 @@ fn cargo_subcommand_env() { 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 + +"#]]) .run(); } @@ -424,7 +488,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 +503,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 +515,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 +526,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 +538,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 +558,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 +623,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 +637,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(); }