Skip to content

Commit

Permalink
Auto merge of #14091 - henry40408:issue-14039, r=weihanglo
Browse files Browse the repository at this point in the history
test: migrate testsuites to snapbox

### What does this PR try to resolve?

Part of #14039.

Migrate the following testsuites to snapbox:

- tests/testsuite/death.rs
- ~~tests/testsuite/locate_project.rs~~ see #14091 (comment)
- tests/testsuite/message_format.rs
- tests/testsuite/precise_pre_release.rs
- tests/testsuite/verify_project.rs
- tests/testsuite/version.rs

### How should we test and review this PR?

N/A

### Additional information

N/A
  • Loading branch information
bors committed Jun 20, 2024
2 parents 361a488 + 2c517d9 commit 96fc12c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 57 deletions.
2 changes: 0 additions & 2 deletions tests/testsuite/death.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Tests for ctrl-C handling.

#![allow(deprecated)]

use cargo_test_support::{project, slow_cpu_multiplier};
use std::fs;
use std::io::{self, Read};
Expand Down
46 changes: 32 additions & 14 deletions tests/testsuite/message_format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Tests for --message-format flag.

#![allow(deprecated)]

use cargo_test_support::{basic_lib_manifest, basic_manifest, project};
use cargo_test_support::{basic_lib_manifest, basic_manifest, project, str};

#[cargo_test]
fn cannot_specify_two() {
Expand All @@ -13,12 +11,14 @@ fn cannot_specify_two() {

let formats = ["human", "json", "short"];

let two_kinds = "error: cannot specify two kinds of `message-format` arguments\n";
for a in formats.iter() {
for b in formats.iter() {
p.cargo(&format!("build --message-format {},{}", a, b))
.with_status(101)
.with_stderr(two_kinds)
.with_stderr_data(str![[r#"
[ERROR] cannot specify two kinds of `message-format` arguments
"#]])
.run();
}
}
Expand All @@ -45,6 +45,7 @@ fn double_json_works() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn cargo_renders() {
let p = project()
Expand Down Expand Up @@ -89,17 +90,17 @@ fn cargo_renders_short() {

p.cargo("check --message-format json-render-diagnostics,json-diagnostic-short")
.with_status(101)
.with_stderr_contains(
"\
[CHECKING] foo [..]
error[..]`main`[..]
",
)
.with_stderr_does_not_contain("note:")
.with_stderr_data(str![[r#"
[CHECKING] foo v0.1.0 ([ROOT]/foo)
error[E0601]: `main` function not found in crate `foo`
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
"#]])
.run();
}

#[cargo_test]
#[allow(deprecated)]
fn cargo_renders_ansi() {
let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
Expand Down Expand Up @@ -129,7 +130,24 @@ fn cargo_renders_doctests() {

p.cargo("test --doc --message-format short")
.with_status(101)
.with_stdout_contains("src/lib.rs:2:1: error[E0425]:[..]")
.with_stdout_contains("[..]src/lib.rs - bar (line 1)[..]")
.with_stdout_data(str![[r#"
running 1 test
test src/lib.rs - bar (line 1) ... FAILED
failures:
---- src/lib.rs - bar (line 1) stdout ----
src/lib.rs:2:1: error[E0425]: cannot find function `bar` in this scope
[ERROR] aborting due to 1 previous error
Couldn't compile the test.
failures:
src/lib.rs - bar (line 1)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s
"#]])
.run();
}
37 changes: 18 additions & 19 deletions tests/testsuite/precise_pre_release.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Tests for selecting pre-release versions with `update --precise`.

#![allow(deprecated)]

use cargo_test_support::project;
use cargo_test_support::{project, str};

#[cargo_test]
fn requires_nightly_cargo() {
Expand All @@ -29,16 +27,17 @@ fn requires_nightly_cargo() {
.with_status(101)
// This error is suffering from #12579 but still demonstrates that updating to
// a pre-release does not work on stable
.with_stderr(
r#"[UPDATING] `dummy-registry` index
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[ERROR] failed to select a version for the requirement `my-dependency = "^0.1.1"`
candidate versions found which didn't match: 0.1.2-pre.0
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
required by package `package v0.0.0 ([ROOT]/foo)`
if you are looking for the prerelease package it needs to be specified explicitly
my-dependency = { version = "0.1.2-pre.0" }
perhaps a crate was updated and forgotten to be re-vendored?"#,
)
perhaps a crate was updated and forgotten to be re-vendored?
"#]])
.run()
}

Expand All @@ -65,11 +64,11 @@ fn update_pre_release() {

p.cargo("update my-dependency --precise 0.1.2-pre.0 -Zunstable-options")
.masquerade_as_nightly_cargo(&["precise-pre-release"])
.with_stderr(
r#"[UPDATING] `dummy-registry` index
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[UPDATING] my-dependency v0.1.1 -> v0.1.2-pre.0
"#,
)
"#]])
.run();
let lockfile = p.read_lockfile();
assert!(lockfile.contains("\nname = \"my-dependency\"\nversion = \"0.1.2-pre.0\""));
Expand Down Expand Up @@ -98,20 +97,20 @@ fn update_pre_release_differ() {

p.cargo("update -p my-dependency --precise 0.1.2-pre.0 -Zunstable-options")
.masquerade_as_nightly_cargo(&["precise-pre-release"])
.with_stderr(
r#"[UPDATING] `dummy-registry` index
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[DOWNGRADING] my-dependency v0.1.2 -> v0.1.2-pre.0
"#,
)
"#]])
.run();

p.cargo("update -p my-dependency --precise 0.1.2-pre.1 -Zunstable-options")
.masquerade_as_nightly_cargo(&["precise-pre-release"])
.with_stderr(
r#"[UPDATING] `dummy-registry` index
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[UPDATING] my-dependency v0.1.2-pre.0 -> v0.1.2-pre.1
"#,
)
"#]])
.run();

let lockfile = p.read_lockfile();
Expand Down
33 changes: 21 additions & 12 deletions tests/testsuite/verify_project.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//! Tests for the `cargo verify-project` command.

#![allow(deprecated)]

use cargo_test_support::{basic_bin_manifest, main_file, project};

fn verify_project_success_output() -> String {
r#"{"success":"true"}"#.into()
}
use cargo_test_support::{basic_bin_manifest, main_file, project, str};

#[cargo_test]
fn cargo_verify_project_path_to_cargo_toml_relative() {
Expand All @@ -17,7 +11,10 @@ fn cargo_verify_project_path_to_cargo_toml_relative() {

p.cargo("verify-project --manifest-path foo/Cargo.toml")
.cwd(p.root().parent().unwrap())
.with_stdout(verify_project_success_output())
.with_stdout_data(str![[r#"
{"success":"true"}
"#]])
.run();
}

Expand All @@ -31,7 +28,10 @@ fn cargo_verify_project_path_to_cargo_toml_absolute() {
p.cargo("verify-project --manifest-path")
.arg(p.root().join("Cargo.toml"))
.cwd(p.root().parent().unwrap())
.with_stdout(verify_project_success_output())
.with_stdout_data(str![[r#"
{"success":"true"}
"#]])
.run();
}

Expand All @@ -43,7 +43,10 @@ fn cargo_verify_project_cwd() {
.build();

p.cargo("verify-project")
.with_stdout(verify_project_success_output())
.with_stdout_data(str![[r#"
{"success":"true"}
"#]])
.run();
}

Expand All @@ -65,11 +68,17 @@ fn cargo_verify_project_honours_unstable_features() {

p.cargo("verify-project")
.masquerade_as_nightly_cargo(&["test-dummy-unstable"])
.with_stdout(verify_project_success_output())
.with_stdout_data(str![[r#"
{"success":"true"}
"#]])
.run();

p.cargo("verify-project")
.with_status(1)
.with_json(r#"{"invalid":"failed to parse manifest at `[CWD]/Cargo.toml`"}"#)
.with_stdout_data(str![[r#"
{"invalid":"failed to parse manifest at `[..]`"}
"#]])
.run();
}
27 changes: 17 additions & 10 deletions tests/testsuite/version.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
//! Tests for displaying the cargo version.

#![allow(deprecated)]

use cargo_test_support::{cargo_process, project};

#[cargo_test]
fn simple() {
let p = project().build();

p.cargo("version")
.with_stdout(&format!("cargo {}\n", cargo::version()))
.with_stdout_data(&format!("cargo {}\n", cargo::version()))
.run();

p.cargo("--version")
.with_stdout(&format!("cargo {}\n", cargo::version()))
.with_stdout_data(&format!("cargo {}\n", cargo::version()))
.run();

p.cargo("-V")
.with_stdout(&format!("cargo {}\n", cargo::version()))
.with_stdout_data(&format!("cargo {}\n", cargo::version()))
.run();
}

Expand Down Expand Up @@ -53,10 +51,19 @@ fn version_works_with_bad_target_dir() {
fn verbose() {
// This is mainly to check that it doesn't explode.
cargo_process("-vV")
.with_stdout_contains(&format!("cargo {}", cargo::version()))
.with_stdout_contains("host: [..]")
.with_stdout_contains("libgit2: [..]")
.with_stdout_contains("libcurl: [..]")
.with_stdout_contains("os: [..]")
.with_stdout_data(format!(
"\
cargo {}
release: [..]
commit-hash: [..]
commit-date: [..]
host: [HOST_TARGET]
libgit2: [..] (sys:[..] [..])
libcurl: [..] (sys:[..] [..])
...
os: [..]
",
cargo::version()
))
.run();
}

0 comments on commit 96fc12c

Please sign in to comment.