Skip to content

refactor(test): add new APIs for easier snapshot testing #4334

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

Merged
merged 3 commits into from
May 20, 2025
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ otel = [
]

# Exports code dependent on private interfaces for the integration test suite
test = ["dep:similar-asserts", "dep:walkdir"]
test = ["dep:similar-asserts", "dep:snapbox", "dep:walkdir"]

# Sorted by alphabetic order
[dependencies]
Expand Down Expand Up @@ -77,8 +77,6 @@ semver = "1.0"
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.10"
sharded-slab = "0.1.1"
# test only (depends on `test` feature)
similar-asserts = { version = "1.7", optional = true }
strsim = "0.11"
tar = "0.4.26"
tempfile = "3.8"
Expand All @@ -95,10 +93,14 @@ tracing-opentelemetry = { version = "0.30", optional = true }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
url = "2.4"
wait-timeout = "0.2"
walkdir = { version = "2", optional = true }
xz2 = "0.1.3"
zstd = "0.13"

# test only (depends on `test` feature)
similar-asserts = { version = "1.7", optional = true }
snapbox = { version = "0.6.21", optional = true }
walkdir = { version = "2", optional = true }

[target."cfg(windows)".dependencies]
cc = "1"
scopeguard = "1"
Expand Down
112 changes: 111 additions & 1 deletion src/test/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{

use enum_map::{Enum, EnumMap, enum_map};
use similar_asserts::SimpleDiff;
use snapbox::{IntoData, RedactedValue, Redactions, assert_data_eq};
use tempfile::TempDir;
use url::Url;

Expand Down Expand Up @@ -62,6 +63,78 @@ pub struct Config {
pub test_root_dir: PathBuf,
}

#[derive(Clone)]
pub struct Assert {
output: SanitizedOutput,
redactions: Redactions,
}

impl Assert {
/// Creates a new [`Assert`] object with the given command [`SanitizedOutput`].
pub fn new(output: SanitizedOutput) -> Self {
let mut redactions = Redactions::new();
redactions
.extend([("[HOST_TRIPLE]", this_host_triple())])
.expect("invalid redactions detected");
Self { output, redactions }
}

/// Extend the redaction rules used in the currrent assertion with new values.
pub fn extend_redactions(
&mut self,
vars: impl IntoIterator<Item = (&'static str, impl Into<RedactedValue>)>,
) -> &mut Self {
self.redactions
.extend(vars)
.expect("invalid redactions detected");
self
}

/// Asserts that the command exited with an ok status.
pub fn is_ok(&self) -> &Self {
assert!(self.output.ok);
self
}

/// Asserts that the command exited with an error.
pub fn is_err(&self) -> &Self {
assert!(!self.output.ok);
self
}

/// Asserts that the command exited with the given `expected` stdout pattern.
pub fn with_stdout(&self, expected: impl IntoData) -> &Self {
let stdout = self.redactions.redact(&self.output.stdout);
assert_data_eq!(&stdout, expected);
self
}

/// Asserts that the command exited without the given `unexpected` stdout pattern.
pub fn without_stdout(&self, unexpected: &str) -> &Self {
if self.output.stdout.contains(unexpected) {
print_indented("expected.stdout.does_not_contain", unexpected);
panic!();
}
self
}

/// Asserts that the command exited with the given `expected` stderr pattern.
pub fn with_stderr(&self, expected: impl IntoData) -> &Self {
let stderr = self.redactions.redact(&self.output.stderr);
assert_data_eq!(&stderr, expected);
self
}

/// Asserts that the command exited without the given `unexpected` stderr pattern.
pub fn without_stderr(&self, unexpected: &str) -> &Self {
if self.output.stderr.contains(unexpected) {
print_indented("expected.stderr.does_not_contain", unexpected);
panic!();
}
self
}
}

impl Config {
pub fn current_dir(&self) -> PathBuf {
self.workdir.borrow().clone()
Expand Down Expand Up @@ -137,12 +210,35 @@ impl Config {
}
}

/// Returns an [`Assert`] object to check the output of running the command
/// specified by `args` under the default environment.
#[must_use]
pub async fn expect(&self, args: impl AsRef<[&str]>) -> Assert {
self.expect_with_env(args, &[]).await
}

/// Returns an [`Assert`] object to check the output of running the command
/// specified by `args` and under the environment specified by `env`.
#[must_use]
pub async fn expect_with_env(
&self,
args: impl AsRef<[&str]>,
env: impl AsRef<[(&str, &str)]>,
) -> Assert {
let args = args.as_ref();
let output = self.run(args[0], &args[1..], env.as_ref()).await;
Assert::new(output)
}

/// Expect an ok status
#[deprecated(note = "use `.expect().await.is_ok()` instead")]
#[allow(deprecated)]
pub async fn expect_ok(&mut self, args: &[&str]) {
self.expect_ok_env(args, &[]).await
}

/// Expect an ok status with extra environment variables
#[deprecated(note = "use `.expect_with_env().await.is_ok()` instead")]
pub async fn expect_ok_env(&self, args: &[&str], env: &[(&str, &str)]) {
let out = self.run(args[0], &args[1..], env).await;
if !out.ok {
Expand All @@ -153,11 +249,14 @@ impl Config {
}

/// Expect an err status and a string in stderr
#[deprecated(note = "use `.expect().await.is_err()` instead")]
#[allow(deprecated)]
pub async fn expect_err(&self, args: &[&str], expected: &str) {
self.expect_err_env(args, &[], expected).await
}

/// Expect an err status and a string in stderr, with extra environment variables
#[deprecated(note = "use `.expect_with_env().await.is_err()` instead")]
pub async fn expect_err_env(&self, args: &[&str], env: &[(&str, &str)], expected: &str) {
let out = self.run(args[0], &args[1..], env).await;
if out.ok || !out.stderr.contains(expected) {
Expand All @@ -169,6 +268,7 @@ impl Config {
}

/// Expect an ok status and a string in stdout
#[deprecated(note = "use `.expect().await.is_ok().with_stdout()` instead")]
pub async fn expect_stdout_ok(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if !out.ok || !out.stdout.contains(expected) {
Expand All @@ -179,6 +279,7 @@ impl Config {
}
}

#[deprecated(note = "use `.expect().await.is_ok().without_stdout()` instead")]
pub async fn expect_not_stdout_ok(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if !out.ok || out.stdout.contains(expected) {
Expand All @@ -189,6 +290,7 @@ impl Config {
}
}

#[deprecated(note = "use `.expect().await.is_ok().without_stderr()` instead")]
pub async fn expect_not_stderr_ok(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if !out.ok || out.stderr.contains(expected) {
Expand All @@ -199,6 +301,7 @@ impl Config {
}
}

#[deprecated(note = "use `.expect().await.is_err().without_stderr()` instead")]
pub async fn expect_not_stderr_err(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if out.ok || out.stderr.contains(expected) {
Expand All @@ -210,6 +313,7 @@ impl Config {
}

/// Expect an ok status and a string in stderr
#[deprecated(note = "use `.expect().await.is_ok().with_stderr()` instead")]
pub async fn expect_stderr_ok(&self, args: &[&str], expected: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if !out.ok || !out.stderr.contains(expected) {
Expand All @@ -221,12 +325,17 @@ impl Config {
}

/// Expect an exact strings on stdout/stderr with an ok status code
#[deprecated(note = "use `.expect().await.is_ok().with_stdout().with_stderr()` instead")]
#[allow(deprecated)]
pub async fn expect_ok_ex(&mut self, args: &[&str], stdout: &str, stderr: &str) {
self.expect_ok_ex_env(args, &[], stdout, stderr).await;
}

/// Expect an exact strings on stdout/stderr with an ok status code,
/// with extra environment variables
#[deprecated(
note = "use `.expect_with_env().await.is_ok().with_stdout().with_stderr()` instead"
)]
pub async fn expect_ok_ex_env(
&mut self,
args: &[&str],
Expand All @@ -249,6 +358,7 @@ impl Config {
}

/// Expect an exact strings on stdout/stderr with an error status code
#[deprecated(note = "use `.expect().await.is_err().with_stdout().with_stderr()` instead")]
pub async fn expect_err_ex(&self, args: &[&str], stdout: &str, stderr: &str) {
let out = self.run(args[0], &args[1..], &[]).await;
if out.ok || out.stdout != stdout || out.stderr != stderr {
Expand Down Expand Up @@ -1045,7 +1155,7 @@ pub struct Output {
pub stderr: Vec<u8>,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SanitizedOutput {
pub ok: bool,
pub stdout: String,
Expand Down
31 changes: 16 additions & 15 deletions tests/suite/cli_exact.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Yet more cli test cases. These are testing that the output
//! is exactly as expected.

#![allow(deprecated)]

use rustup::for_host;
use rustup::test::{
CROSS_ARCH1, CROSS_ARCH2, CliTestContext, MULTI_ARCH1, Scenario, this_host_triple,
};
use rustup::utils::raw;
use snapbox::str;

#[tokio::test]
async fn update_once() {
Expand Down Expand Up @@ -309,23 +312,21 @@ info: default toolchain set to 'nightly-{0}'

#[tokio::test]
async fn override_again() {
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
let cwd = cx.config.current_dir();
let cx = &CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_ok(&["rustup", "override", "add", "nightly"])
.await;
.expect(["rustup", "override", "add", "nightly"])
.await
.is_ok();
cx.config
.expect_ok_ex(
&["rustup", "override", "add", "nightly"],
"",
&format!(
r"info: override toolchain for '{}' set to 'nightly-{1}'
",
cwd.display(),
&this_host_triple()
),
)
.await;
.expect(["rustup", "override", "add", "nightly"])
.await
.extend_redactions([("[CWD]", cx.config.current_dir().display().to_string())])
.is_ok()
.with_stdout("")
.with_stderr(str![[r#"
info: override toolchain for '[CWD]' set to 'nightly-[HOST_TRIPLE]'

"#]]);
}

#[tokio::test]
Expand Down
2 changes: 2 additions & 0 deletions tests/suite/cli_inst_interactive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tests of the interactive console installer
#![allow(deprecated)]

use std::env::consts::EXE_SUFFIX;
use std::io::Write;
use std::process::Stdio;
Expand Down
20 changes: 11 additions & 9 deletions tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Test cases of the rustup command that do not depend on the
//! dist server, mostly derived from multirust/test-v2.sh

#![allow(deprecated)]

use std::fs;
use std::str;
use std::{env::consts::EXE_SUFFIX, path::Path};
Expand All @@ -11,6 +13,7 @@ use rustup::test::{
};
use rustup::utils;
use rustup::utils::raw::symlink_dir;
use snapbox::str;

#[tokio::test]
async fn smoke_test() {
Expand Down Expand Up @@ -113,16 +116,15 @@ async fn custom_invalid_names_with_archive_dates() {
async fn update_all_no_update_whitespace() {
let cx = CliTestContext::new(Scenario::SimpleV2).await;
cx.config
.expect_stdout_ok(
&["rustup", "update", "nightly"],
for_host!(
r"
nightly-{} installed - 1.3.0 (hash-nightly-2)
.expect(["rustup", "update", "nightly"])
.await
.is_ok()
.with_stdout(str![[r#"

"
),
)
.await;
nightly-[HOST_TRIPLE] installed - 1.3.0 (hash-nightly-2)


"#]]);
}

// Issue #145
Expand Down
2 changes: 2 additions & 0 deletions tests/suite/cli_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! It depends on self-update working, so if absolutely everything here breaks,
//! check those tests as well.

#![allow(deprecated)]

// Prefer omitting actually unpacking content while just testing paths.
const INIT_NONE: [&str; 4] = ["rustup-init", "-y", "--default-toolchain", "none"];

Expand Down
2 changes: 2 additions & 0 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Test cases for new rustup UI

#![allow(deprecated)]

use std::fs;
use std::path::{MAIN_SEPARATOR, PathBuf};
use std::{env::consts::EXE_SUFFIX, path::Path};
Expand Down
Loading
Loading