Skip to content

Commit 126bf84

Browse files
committed
Implement test for 11036 cargo changes
1 parent 80d3a10 commit 126bf84

File tree

1 file changed

+88
-21
lines changed

1 file changed

+88
-21
lines changed

tests/testsuite/rustup.rs

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ use std::path::{Path, PathBuf};
88

99
use crate::prelude::*;
1010
use crate::utils::cargo_process;
11-
use cargo_test_support::paths::{home, root};
12-
use cargo_test_support::{process, project, str};
11+
use cargo_test_support::install::assert_has_installed_exe;
12+
use cargo_test_support::paths::{cargo_home, home, root};
13+
use cargo_test_support::registry::Package;
14+
use cargo_test_support::{execs, process, project, str};
15+
16+
fn pkg(name: &str, vers: &str) {
17+
Package::new(name, vers)
18+
.file("src/main.rs", "fn main() {{}}")
19+
.publish();
20+
}
1321

1422
/// Helper to generate an executable.
1523
fn make_exe(dest: &Path, name: &str, contents: &str, env: &[(&str, PathBuf)]) -> PathBuf {
@@ -99,7 +107,13 @@ fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
99107
/// Creates a simulation of a rustup environment with `~/.cargo/bin` and
100108
/// `~/.rustup` directories populated with some executables that simulate
101109
/// rustup.
102-
fn simulated_rustup_environment() -> RustupEnvironment {
110+
///
111+
/// Arguments
112+
///
113+
/// - `proxy_calls_cargo`: if true, the cargo proxy calls the cargo under test;
114+
/// otherwise, the cargo proxy calls an executable that panics immediately
115+
/// - `env_setup`: environment variable setup the proxy should perform
116+
fn simulated_rustup_environment(proxy_calls_cargo: bool, env_setup: &str) -> RustupEnvironment {
103117
// Set up ~/.rustup/toolchains/test-toolchain/bin with a custom rustc and cargo.
104118
let rustup_home = home().join(".rustup");
105119
let toolchain_bin = rustup_home
@@ -108,42 +122,58 @@ fn simulated_rustup_environment() -> RustupEnvironment {
108122
.join("bin");
109123
toolchain_bin.mkdir_p();
110124
let rustc_toolchain_exe = real_rustc_wrapper(&toolchain_bin, "real rustc running");
111-
let cargo_toolchain_exe = make_exe(
112-
&toolchain_bin,
113-
"cargo",
114-
r#"panic!("cargo toolchain should not be called");"#,
115-
&[],
116-
);
125+
let cargo_toolchain_exe = if proxy_calls_cargo {
126+
crate::utils::cargo_exe()
127+
} else {
128+
make_exe(
129+
&toolchain_bin,
130+
"cargo",
131+
r#"panic!("cargo toolchain should not be called");"#,
132+
&[],
133+
)
134+
};
117135

118136
// Set up ~/.cargo/bin with a typical set of rustup proxies.
119137
let cargo_bin = home().join(".cargo").join("bin");
120138
cargo_bin.mkdir_p();
121139

122-
let rustc_proxy = make_exe(
140+
let proxy = make_exe(
123141
&cargo_bin,
124142
"rustc",
125143
&format!(
126144
r#"
127-
match std::env::args().next().unwrap().as_ref() {{
128-
"rustc" => {{}}
129-
arg => panic!("proxy only supports rustc, got {{arg:?}}"),
130-
}}
131-
eprintln!("rustc proxy running");
132-
let r = std::process::Command::new(env!("CARGO_RUSTUP_TEST_rustc_toolchain_exe"))
145+
let file_stem = std::path::PathBuf::from(std::env::args().next().unwrap())
146+
.file_stem()
147+
.map(ToOwned::to_owned)
148+
.unwrap();
149+
let program = match file_stem.to_str().unwrap() {{
150+
"cargo" => env!("CARGO_RUSTUP_TEST_cargo_toolchain_exe"),
151+
"rustc" => env!("CARGO_RUSTUP_TEST_rustc_toolchain_exe"),
152+
arg => panic!("proxy only supports cargo and rustc, got {{arg:?}}"),
153+
}};
154+
eprintln!("`{{program}}` proxy running");
155+
let r = std::process::Command::new(program)
133156
.args(std::env::args_os().skip(1))
157+
{env_setup}
134158
.status();
135159
std::process::exit(r.unwrap().code().unwrap_or(2));
136160
"#
137161
),
138-
&[("CARGO_RUSTUP_TEST_rustc_toolchain_exe", rustc_toolchain_exe)],
162+
&[
163+
("CARGO_RUSTUP_TEST_rustc_toolchain_exe", rustc_toolchain_exe),
164+
(
165+
"CARGO_RUSTUP_TEST_cargo_toolchain_exe",
166+
cargo_toolchain_exe.clone(),
167+
),
168+
],
139169
);
140170
fs::hard_link(
141-
&rustc_proxy,
171+
&proxy,
142172
cargo_bin.join("cargo").with_extension(EXE_EXTENSION),
143173
)
144174
.unwrap();
145175
fs::hard_link(
146-
&rustc_proxy,
176+
&proxy,
147177
cargo_bin.join("rustup").with_extension(EXE_EXTENSION),
148178
)
149179
.unwrap();
@@ -162,7 +192,7 @@ fn typical_rustup() {
162192
cargo_bin,
163193
rustup_home,
164194
cargo_toolchain_exe,
165-
} = simulated_rustup_environment();
195+
} = simulated_rustup_environment(false, "");
166196

167197
// Set up a project and run a normal cargo build.
168198
let p = project().file("src/lib.rs", "").build();
@@ -212,7 +242,7 @@ fn custom_calls_other_cargo() {
212242
cargo_bin,
213243
rustup_home,
214244
cargo_toolchain_exe: _,
215-
} = simulated_rustup_environment();
245+
} = simulated_rustup_environment(false, "");
216246

217247
// Create a directory with a custom toolchain (outside of the rustup universe).
218248
let custom_bin = root().join("custom-bin");
@@ -271,3 +301,40 @@ custom toolchain rustc running
271301
"#]])
272302
.run();
273303
}
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_non_default_toolchain() {
309+
let RustupEnvironment {
310+
cargo_bin,
311+
rustup_home: _,
312+
cargo_toolchain_exe: _,
313+
} = simulated_rustup_environment(
314+
true,
315+
".env(\"RUSTUP_TOOLCHAIN_SOURCE\", \"env\")
316+
.env(\"RUSTUP_TOOLCHAIN\", \"test-toolchain\")",
317+
);
318+
319+
pkg("foo", "0.0.1");
320+
321+
let mut p = process(cargo_bin.join("cargo"));
322+
p.arg_line("install foo");
323+
execs()
324+
.with_process_builder(p)
325+
.with_stderr_data(str![[r#"
326+
`[..]/cargo[EXE]` proxy running
327+
[UPDATING] `dummy-registry` index
328+
[DOWNLOADING] crates ...
329+
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
330+
[INSTALLING] foo v0.0.1
331+
[COMPILING] foo v0.0.1
332+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
333+
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
334+
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
335+
[WARNING] be sure to add `[ROOT]/home/.cargo/bin` to your PATH to be able to run the installed binaries
336+
337+
"#]])
338+
.run();
339+
assert_has_installed_exe(cargo_home(), "foo");
340+
}

0 commit comments

Comments
 (0)