Skip to content

Commit 3f37d13

Browse files
committed
refactor(test): implement new testing DSL and simple showcase
1 parent a92432f commit 3f37d13

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ otel = [
3737
]
3838

3939
# Exports code dependent on private interfaces for the integration test suite
40-
test = ["dep:walkdir"]
40+
test = ["dep:snapbox", "dep:walkdir"]
4141

4242
# Sorted by alphabetic order
4343
[dependencies]
@@ -77,6 +77,7 @@ semver = "1.0"
7777
serde = { version = "1.0", features = ["derive"] }
7878
sha2 = "0.10"
7979
sharded-slab = "0.1.1"
80+
snapbox = { version = "0.6.21", optional = true }
8081
strsim = "0.11"
8182
tar = "0.4.26"
8283
tempfile = "3.8"

src/test/clitools.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818
};
1919

2020
use enum_map::{Enum, EnumMap, enum_map};
21+
use snapbox::{IntoData, assert_data_eq};
2122
use tempfile::TempDir;
2223
use url::Url;
2324

@@ -61,6 +62,33 @@ pub struct Config {
6162
pub test_root_dir: PathBuf,
6263
}
6364

65+
#[must_use]
66+
pub struct Assert {
67+
output: SanitizedOutput,
68+
}
69+
70+
impl Assert {
71+
pub fn is_ok(&self) -> &Self {
72+
assert!(self.output.ok);
73+
self
74+
}
75+
76+
pub fn is_err(&self) -> &Self {
77+
assert!(!self.output.ok);
78+
self
79+
}
80+
81+
pub fn with_stdout(&self, expected: impl IntoData) -> &Self {
82+
assert_data_eq!(&self.output.stdout, expected);
83+
self
84+
}
85+
86+
pub fn with_stderr(&self, expected: impl IntoData) -> &Self {
87+
assert_data_eq!(&self.output.stderr, expected);
88+
self
89+
}
90+
}
91+
6492
impl Config {
6593
pub fn current_dir(&self) -> PathBuf {
6694
self.workdir.borrow().clone()
@@ -136,6 +164,20 @@ impl Config {
136164
}
137165
}
138166

167+
pub async fn expect_with_env(
168+
&self,
169+
args: impl AsRef<[&str]>,
170+
env: impl AsRef<[(&str, &str)]>,
171+
) -> Assert {
172+
let args = args.as_ref();
173+
let output = self.run(args[0], &args[1..], env.as_ref()).await;
174+
Assert { output }
175+
}
176+
177+
pub async fn expect(&self, args: impl AsRef<[&str]>) -> Assert {
178+
self.expect_with_env(args, &[]).await
179+
}
180+
139181
/// Expect an ok status
140182
pub async fn expect_ok(&mut self, args: &[&str]) {
141183
self.expect_ok_env(args, &[]).await

tests/suite/cli_v1.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use std::fs;
55

66
use rustup::for_host;
77
use rustup::test::{CliTestContext, Scenario};
8+
use snapbox::str;
89

910
#[tokio::test]
1011
async fn rustc_no_default_toolchain() {
1112
let cx = CliTestContext::new(Scenario::SimpleV1).await;
1213
cx.config
13-
.expect_err(
14-
&["rustc"],
15-
"rustup could not choose a version of rustc to run",
16-
)
17-
.await;
14+
.expect(["rustc"])
15+
.await
16+
.is_err()
17+
.with_stderr(str!["rustup could not choose a version of rustc to run"]);
1818
}
1919

2020
#[tokio::test]

0 commit comments

Comments
 (0)