Skip to content

Commit 2d3f9ab

Browse files
committed
Implement tests for 11036 cargo changes
1 parent cc2855c commit 2d3f9ab

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

tests/testsuite/rustup.rs

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use std::path::{Path, PathBuf};
1010

1111
use crate::prelude::*;
1212
use crate::utils::cargo_process;
13-
use cargo_test_support::paths::{home, root};
14-
use cargo_test_support::{process, project, str};
13+
use cargo_test_support::install::assert_has_installed_exe;
14+
use cargo_test_support::paths::{cargo_home, home, root};
15+
use cargo_test_support::registry::Package;
16+
use cargo_test_support::{execs, process, project, str};
1517

1618
/// Helper to generate an executable.
1719
fn make_exe(dest: &Path, name: &str, contents: &str, env: &[(&str, PathBuf)]) -> PathBuf {
@@ -299,3 +301,111 @@ custom toolchain rustc running
299301
"#]])
300302
.run();
301303
}
304+
305+
/// Performs a `cargo install` with a non-default toolchain in a simulated
306+
/// rustup environment. The purpose is to verify the warning that is emitted.
307+
#[cargo_test]
308+
fn cargo_install_with_toolchain_source_unset() {
309+
cargo_install_with_toolchain_source(None, &warning_with_optional_rust_toolchain_source(None));
310+
}
311+
312+
#[cargo_test]
313+
fn cargo_install_with_toolchain_source_default() {
314+
cargo_install_with_toolchain_source(
315+
Some("default"),
316+
&warning_with_optional_rust_toolchain_source(None),
317+
);
318+
}
319+
320+
#[cargo_test]
321+
fn cargo_install_with_toolchain_source_cli() {
322+
cargo_install_with_toolchain_source(
323+
Some("cli"),
324+
&warning_with_optional_rust_toolchain_source(None),
325+
);
326+
}
327+
328+
#[cargo_test]
329+
fn cargo_install_with_toolchain_source_env() {
330+
cargo_install_with_toolchain_source(
331+
Some("env"),
332+
&warning_with_optional_rust_toolchain_source(None),
333+
);
334+
}
335+
336+
#[cargo_test]
337+
fn cargo_install_with_toolchain_source_path_override() {
338+
cargo_install_with_toolchain_source(
339+
Some("path-override"),
340+
&warning_with_optional_rust_toolchain_source(None),
341+
);
342+
}
343+
344+
#[cargo_test]
345+
fn cargo_install_with_toolchain_source_toolchain_file() {
346+
cargo_install_with_toolchain_source(
347+
Some("toolchain-file"),
348+
&warning_with_optional_rust_toolchain_source(None),
349+
);
350+
}
351+
352+
#[cargo_test]
353+
fn cargo_install_with_toolchain_source_unrecognized() {
354+
cargo_install_with_toolchain_source(
355+
Some("unrecognized"),
356+
&warning_with_optional_rust_toolchain_source(None),
357+
);
358+
}
359+
360+
fn cargo_install_with_toolchain_source(source: Option<&str>, stderr: &str) {
361+
let mut builder = RustupEnvironmentBuilder::new();
362+
builder.call_cargo_under_test();
363+
builder.env("RUSTUP_TOOLCHAIN", "test-toolchain");
364+
if let Some(source) = source {
365+
builder.env("RUSTUP_TOOLCHAIN_SOURCE", source);
366+
};
367+
let RustupEnvironment {
368+
cargo_bin,
369+
rustup_home: _,
370+
cargo_toolchain_exe: _,
371+
} = builder.build();
372+
373+
Package::new("foo", "0.0.1")
374+
.file("src/main.rs", "fn main() {{}}")
375+
.publish();
376+
377+
let mut p = process(cargo_bin.join("cargo"));
378+
p.arg_line("install foo");
379+
execs()
380+
.with_process_builder(p)
381+
.with_stderr_data(stderr)
382+
.run();
383+
assert_has_installed_exe(cargo_home(), "foo");
384+
}
385+
386+
fn warning_with_optional_rust_toolchain_source(source: Option<&str>) -> String {
387+
let maybe_warning = if let Some(source) = source {
388+
format!(
389+
r#"[WARNING] using non-default toolchain `test-toolchain` overridden by {}
390+
|
391+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
392+
"#,
393+
source
394+
)
395+
} else {
396+
String::new()
397+
};
398+
format!(
399+
r#"`[..]/cargo[EXE]` proxy running
400+
[UPDATING] `dummy-registry` index
401+
[DOWNLOADING] crates ...
402+
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
403+
[INSTALLING] foo v0.0.1
404+
{maybe_warning}[COMPILING] foo v0.0.1
405+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
406+
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
407+
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
408+
[WARNING] be sure to add `[ROOT]/home/.cargo/bin` to your PATH to be able to run the installed binaries
409+
"#
410+
)
411+
}

0 commit comments

Comments
 (0)