Skip to content

Commit 7290ae7

Browse files
committed
test: migrate remaining uses of .run() to .expect() APIs
1 parent e8623b8 commit 7290ae7

File tree

4 files changed

+136
-125
lines changed

4 files changed

+136
-125
lines changed

src/test/clitools.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub struct Config {
114114
/// [`Assert::extend_redactions`] method to introduce new filters.
115115
#[derive(Clone)]
116116
pub struct Assert {
117-
output: SanitizedOutput,
117+
pub output: SanitizedOutput,
118118
redactions: Redactions,
119119
}
120120

tests/suite/cli_exact.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ async fn check_updates_self() {
210210

211211
// We are checking an update to rustup itself in this test.
212212
cx.config
213-
.run("rustup", ["set", "auto-self-update", "enable"], &[])
214-
.await;
213+
.expect(["rustup", "set", "auto-self-update", "enable"])
214+
.await
215+
.is_ok();
215216

216217
cx.config
217218
.expect(["rustup", "check"])
@@ -232,8 +233,9 @@ async fn check_updates_self_no_change() {
232233

233234
// We are checking an update to rustup itself in this test.
234235
cx.config
235-
.run("rustup", ["set", "auto-self-update", "enable"], &[])
236-
.await;
236+
.expect(["rustup", "set", "auto-self-update", "enable"])
237+
.await
238+
.is_ok();
237239

238240
cx.config
239241
.expect(["rustup", "check"])

tests/suite/cli_misc.rs

Lines changed: 127 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::fs;
55
use std::str;
66
use std::{env::consts::EXE_SUFFIX, path::Path};
77

8-
use rustup::test::{
9-
CliTestContext, Config, MULTI_ARCH1, Scenario, print_command, print_indented, this_host_triple,
10-
};
8+
use itertools::Itertools;
9+
use rustup::test::Assert;
10+
use rustup::test::{CliTestContext, MULTI_ARCH1, Scenario, this_host_triple};
1111
use rustup::utils;
1212
use rustup::utils::raw::symlink_dir;
1313

@@ -50,22 +50,24 @@ info: The currently active `rustc` version is `1.3.0 (hash-nightly-2)`
5050
#[tokio::test]
5151
async fn no_colors_in_piped_error_output() {
5252
let cx = CliTestContext::new(Scenario::SimpleV2).await;
53-
let args: Vec<&str> = vec![];
54-
let out = cx.config.run("rustc", args, &[]).await;
55-
assert!(!out.ok);
56-
assert!(!out.stderr.contains('\x1b'));
53+
cx.config
54+
.expect(["rustc"])
55+
.await
56+
.is_err()
57+
.without_stderr("\u{1b}");
5758
}
5859

5960
#[tokio::test]
6061
async fn rustc_with_bad_rustup_toolchain_env_var() {
6162
let cx = CliTestContext::new(Scenario::SimpleV2).await;
62-
let args: Vec<&str> = vec![];
63-
let out = cx
64-
.config
65-
.run("rustc", args, &[("RUSTUP_TOOLCHAIN", "bogus")])
66-
.await;
67-
assert!(!out.ok);
68-
assert!(out.stderr.contains("toolchain 'bogus' is not installed"));
63+
cx.config
64+
.expect_with_env(["rustc"], [("RUSTUP_TOOLCHAIN", "bogus")])
65+
.await
66+
.with_stderr(snapbox::str![[r#"
67+
error: override toolchain 'bogus' is not installed[..]
68+
69+
"#]])
70+
.is_err();
6971
}
7072

7173
#[tokio::test]
@@ -389,90 +391,83 @@ async fn rustup_doesnt_prepend_path_unnecessarily() {
389391
.await
390392
.is_ok();
391393

392-
async fn expect_stderr_ok_env_first_then(
393-
config: &Config,
394-
args: &[&str],
395-
env: &[(&str, &str)],
396-
first: &Path,
397-
second: Option<&Path>,
398-
) {
399-
let out = config.run(args[0], &args[1..], env).await;
400-
let first_then_second = |list: &str| -> bool {
401-
let mut saw_first = false;
402-
let mut saw_second = false;
403-
for path in std::env::split_paths(list) {
404-
if path == first {
405-
if saw_second {
406-
return false;
407-
}
408-
saw_first = true;
409-
}
410-
if Some(&*path) == second {
411-
if !saw_first {
412-
return false;
413-
}
414-
saw_second = true;
415-
}
416-
}
417-
true
418-
};
419-
if !out.ok || !first_then_second(&out.stderr) {
420-
print_command(args, &out);
421-
println!("expected.ok: true");
422-
print_indented(
423-
"expected.stderr.first_then",
424-
&format!("{} comes before {:?}", first.display(), second),
425-
);
426-
panic!();
427-
}
428-
}
394+
let assert_ok_with_paths = |assert: &Assert, data| {
395+
assert.is_ok();
396+
let stderr = std::env::split_paths(&assert.output.stderr)
397+
.format_with("\n", |p, f| f(&p.display()))
398+
.to_string();
399+
let stderr = assert.redact(&stderr);
400+
snapbox::assert_data_eq!(stderr, data);
401+
};
429402

430403
// For all of these, CARGO_HOME/bin will be auto-prepended.
431404
let cargo_home_bin = cx.config.cargodir.join("bin");
432-
expect_stderr_ok_env_first_then(
433-
&cx.config,
434-
&["cargo", "--echo-path"],
435-
&[],
436-
&cargo_home_bin,
437-
None,
438-
)
439-
.await;
440-
expect_stderr_ok_env_first_then(
441-
&cx.config,
442-
&["cargo", "--echo-path"],
443-
&[("PATH", "")],
444-
&cargo_home_bin,
445-
None,
446-
)
447-
.await;
405+
assert_ok_with_paths(
406+
cx.config
407+
.expect(["cargo", "--echo-path"])
408+
.await
409+
.extend_redactions([("[CARGO_HOME_BIN]", &cargo_home_bin)]),
410+
snapbox::str![[r#"
411+
[CARGO_HOME_BIN]
412+
...
413+
"#]],
414+
);
415+
416+
assert_ok_with_paths(
417+
cx.config
418+
.expect_with_env(["cargo", "--echo-path"], [("PATH", "")])
419+
.await
420+
.extend_redactions([("[CARGO_HOME_BIN]", &cargo_home_bin)]),
421+
snapbox::str![[r#"
422+
[CARGO_HOME_BIN]
423+
...
424+
"#]],
425+
);
448426

449427
// Check that CARGO_HOME/bin is prepended to path.
450-
let config = &cx.config;
451-
expect_stderr_ok_env_first_then(
452-
config,
453-
&["cargo", "--echo-path"],
454-
&[("PATH", &format!("{}", config.exedir.display()))],
455-
&cargo_home_bin,
456-
Some(&config.exedir),
457-
)
458-
.await;
428+
assert_ok_with_paths(
429+
cx.config
430+
.expect_with_env(
431+
["cargo", "--echo-path"],
432+
[("PATH", &*cx.config.exedir.display().to_string())],
433+
)
434+
.await
435+
.extend_redactions([
436+
("[CARGO_HOME_BIN]", &cargo_home_bin),
437+
("[EXEDIR]", &cx.config.exedir),
438+
]),
439+
snapbox::str![[r#"
440+
[CARGO_HOME_BIN]
441+
[EXEDIR]
442+
...
443+
"#]],
444+
);
459445

460446
// But if CARGO_HOME/bin is already on PATH, it will not be prepended again,
461447
// so exedir will take precedence.
462-
expect_stderr_ok_env_first_then(
463-
config,
464-
&["cargo", "--echo-path"],
465-
&[(
466-
"PATH",
467-
std::env::join_paths([&config.exedir, &cargo_home_bin])
468-
.unwrap()
469-
.to_str()
470-
.unwrap(),
471-
)],
472-
&config.exedir,
473-
Some(&cargo_home_bin),
474-
)
475-
.await;
448+
assert_ok_with_paths(
449+
cx.config
450+
.expect_with_env(
451+
["cargo", "--echo-path"],
452+
[(
453+
"PATH",
454+
std::env::join_paths([&cx.config.exedir, &cargo_home_bin])
455+
.unwrap()
456+
.to_str()
457+
.unwrap(),
458+
)],
459+
)
460+
.await
461+
.extend_redactions([
462+
("[CARGO_HOME_BIN]", &cargo_home_bin),
463+
("[EXEDIR]", &cx.config.exedir),
464+
]),
465+
snapbox::str![[r#"
466+
[EXEDIR]
467+
[CARGO_HOME_BIN]
468+
...
469+
"#]],
470+
);
476471
}
477472

478473
#[tokio::test]
@@ -832,9 +827,15 @@ async fn rename_rls_list() {
832827
.await
833828
.is_ok();
834829

835-
let out = cx.config.run("rustup", ["component", "list"], &[]).await;
836-
assert!(out.ok);
837-
assert!(out.stdout.contains(&format!("rls-{}", this_host_triple())));
830+
cx.config
831+
.expect(["rustup", "component", "list"])
832+
.await
833+
.with_stdout(snapbox::str![[r#"
834+
...
835+
rls-[HOST_TRIPLE] (installed)
836+
...
837+
"#]])
838+
.is_ok();
838839
}
839840

840841
#[tokio::test]
@@ -853,9 +854,15 @@ async fn rename_rls_preview_list() {
853854
.await
854855
.is_ok();
855856

856-
let out = cx.config.run("rustup", ["component", "list"], &[]).await;
857-
assert!(out.ok);
858-
assert!(out.stdout.contains(&format!("rls-{}", this_host_triple())));
857+
cx.config
858+
.expect(["rustup", "component", "list"])
859+
.await
860+
.with_stdout(snapbox::str![[r#"
861+
...
862+
rls-[HOST_TRIPLE] (installed)
863+
...
864+
"#]])
865+
.is_ok();
859866
}
860867

861868
#[tokio::test]
@@ -1479,27 +1486,29 @@ custom-1 [..]/custom-1
14791486
#[tokio::test]
14801487
async fn update_self_smart_guess() {
14811488
let cx = CliTestContext::new(Scenario::SimpleV2).await;
1482-
let out = cx.config.run("rustup", &["update", "self"], &[]).await;
1483-
let invalid_toolchain = out.stderr.contains("invalid toolchain name");
1484-
if !out.ok && invalid_toolchain {
1485-
assert!(
1486-
out.stderr
1487-
.contains("if you meant to update rustup itself, use `rustup self update`")
1488-
)
1489-
}
1489+
cx.config
1490+
.expect(["rustup", "update", "self"])
1491+
.await
1492+
.is_err()
1493+
.with_stderr(snapbox::str![[r#"
1494+
...
1495+
info: if you meant to update rustup itself, use `rustup self update`
1496+
...
1497+
"#]]);
14901498
}
14911499

14921500
#[tokio::test]
14931501
async fn uninstall_self_smart_guess() {
14941502
let cx = CliTestContext::new(Scenario::SimpleV2).await;
1495-
let out = cx.config.run("rustup", &["uninstall", "self"], &[]).await;
1496-
let no_toolchain_installed = out.stdout.contains("no toolchain installed");
1497-
if out.ok && no_toolchain_installed {
1498-
assert!(
1499-
out.stdout
1500-
.contains("if you meant to uninstall rustup itself, use `rustup self uninstall`")
1501-
)
1502-
}
1503+
cx.config
1504+
.expect(["rustup", "uninstall", "self"])
1505+
.await
1506+
.is_ok()
1507+
.with_stderr(snapbox::str![[r#"
1508+
...
1509+
info: if you meant to uninstall rustup itself, use `rustup self uninstall`
1510+
...
1511+
"#]]);
15031512
}
15041513

15051514
// https://github.com/rust-lang/rustup/issues/4073
@@ -1547,9 +1556,9 @@ async fn rustup_updates_cargo_env_if_proxy() {
15471556
.borrow()
15481557
.join("bin")
15491558
.join(format!("cargo{EXE_SUFFIX}"));
1550-
let real_path = cx.config.run("rustup", &["which", "cargo"], &[]).await;
1551-
assert!(real_path.ok);
1552-
let real_path = real_path.stdout;
1559+
let real_path = cx.config.expect(["rustup", "which", "cargo"]).await;
1560+
real_path.is_ok();
1561+
let real_path = &real_path.output.stdout;
15531562

15541563
fs::create_dir_all(proxy_path.parent().unwrap()).unwrap();
15551564
#[cfg(windows)]
@@ -1623,10 +1632,10 @@ async fn rust_analyzer_proxy_falls_back_external() {
16231632
// use the former.
16241633
let real_path = cx
16251634
.config
1626-
.run("rust-analyzer", &["--echo-current-exe"], &[])
1635+
.expect(["rust-analyzer", "--echo-current-exe"])
16271636
.await;
1628-
assert!(real_path.ok);
1629-
let real_path = Path::new(real_path.stderr.trim());
1637+
real_path.is_ok();
1638+
let real_path = Path::new(real_path.output.stderr.trim());
16301639

16311640
assert!(real_path.is_file());
16321641

tests/suite/cli_rustup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ async fn recursive_cargo() {
669669
// The solution here is to copy from the "mock" `cargo.exe` into
670670
// `~/.cargo/bin/cargo-foo`. This is just for convenience to avoid
671671
// needing to build another executable just for this test.
672-
let output = cx.config.run("rustup", ["which", "cargo"], &[]).await;
673-
let real_mock_cargo = output.stdout.trim();
672+
let which_cargo = cx.config.expect(["rustup", "which", "cargo"]).await;
673+
let real_mock_cargo = which_cargo.output.stdout.trim();
674674
let cargo_bin_path = cx.config.cargodir.join("bin");
675675
let cargo_subcommand = cargo_bin_path.join(format!("cargo-foo{EXE_SUFFIX}"));
676676
fs::create_dir_all(&cargo_bin_path).unwrap();

0 commit comments

Comments
 (0)