Skip to content

Commit 862ec56

Browse files
author
Jon Gjengset
committed
Allow and test path override through env/cmd
This is needed so that proxy commands (like `cargo` -> `rustc`) will continue to use the path toolchain.
1 parent 59d9285 commit 862ec56

File tree

3 files changed

+116
-14
lines changed

3 files changed

+116
-14
lines changed

src/config.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,21 @@ impl ToolchainSection {
5050

5151
impl<T: Into<String>> From<T> for OverrideFile {
5252
fn from(channel: T) -> Self {
53-
Self {
54-
toolchain: ToolchainSection {
55-
channel: Some(channel.into()),
56-
..Default::default()
57-
},
53+
let override_ = channel.into();
54+
if Path::new(&override_).is_absolute() {
55+
Self {
56+
toolchain: ToolchainSection {
57+
channel: Some(override_),
58+
..Default::default()
59+
},
60+
}
61+
} else {
62+
Self {
63+
toolchain: ToolchainSection {
64+
path: Some(PathBuf::from(override_)),
65+
..Default::default()
66+
},
67+
}
5868
}
5969
}
6070
}

src/toolchain.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ impl<'a> Toolchain<'a> {
9191
return Err(ErrorKind::InvalidToolchainPath(path.into()).into());
9292
}
9393

94-
let base = path
95-
.components()
96-
.last()
97-
.ok_or_else(|| ErrorKind::InvalidToolchainPath(path.clone()))?
98-
.as_os_str()
99-
.to_string_lossy();
10094
Ok(Toolchain {
10195
cfg,
102-
name: base.into(),
96+
name: path
97+
.as_os_str()
98+
.to_str()
99+
.ok_or_else(|| ErrorKind::InvalidToolchainPath(path.clone().into()))?
100+
.to_owned(),
103101
path,
104102
dist_handler: Box::new(move |n| (cfg.notify_handler)(n.into())),
105103
})

tests/cli-rustup.rs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,70 @@ fn file_override() {
13841384
});
13851385
}
13861386

1387+
#[test]
1388+
fn env_override_path() {
1389+
setup(&|config| {
1390+
expect_ok(config, &["rustup", "default", "stable"]);
1391+
expect_ok(
1392+
config,
1393+
&[
1394+
"rustup",
1395+
"toolchain",
1396+
"install",
1397+
"nightly",
1398+
"--no-self-update",
1399+
],
1400+
);
1401+
1402+
let toolchain_path = config
1403+
.rustupdir
1404+
.join("toolchains")
1405+
.join(format!("nightly-{}", this_host_triple()));
1406+
1407+
let mut cmd = clitools::cmd(config, "rustc", &["--version"]);
1408+
clitools::env(config, &mut cmd);
1409+
cmd.env("RUSTUP_TOOLCHAIN", toolchain_path.to_str().unwrap());
1410+
1411+
let out = cmd.output().unwrap();
1412+
assert!(String::from_utf8(out.stdout)
1413+
.unwrap()
1414+
.contains("hash-nightly-2"));
1415+
});
1416+
}
1417+
1418+
#[test]
1419+
fn plus_override_path() {
1420+
setup(&|config| {
1421+
expect_ok(config, &["rustup", "default", "stable"]);
1422+
expect_ok(
1423+
config,
1424+
&[
1425+
"rustup",
1426+
"toolchain",
1427+
"install",
1428+
"nightly",
1429+
"--no-self-update",
1430+
],
1431+
);
1432+
1433+
let toolchain_path = config
1434+
.rustupdir
1435+
.join("toolchains")
1436+
.join(format!("nightly-{}", this_host_triple()));
1437+
expect_stdout_ok(
1438+
config,
1439+
&[
1440+
"rustup",
1441+
"run",
1442+
toolchain_path.to_str().unwrap(),
1443+
"rustc",
1444+
"--version",
1445+
],
1446+
"hash-nightly-2",
1447+
);
1448+
});
1449+
}
1450+
13871451
#[test]
13881452
fn file_override_path() {
13891453
setup(&|config| {
@@ -1406,7 +1470,7 @@ fn file_override_path() {
14061470
let toolchain_file = config.current_dir().join("rust-toolchain.toml");
14071471
raw::write_file(
14081472
&toolchain_file,
1409-
&format!("[toolchain]\npath=\"{}\"", toolchain_path.display()),
1473+
&format!("[toolchain]\npath=\"{}\"", toolchain_path.to_str().unwrap()),
14101474
)
14111475
.unwrap();
14121476

@@ -1421,6 +1485,36 @@ fn file_override_path() {
14211485
});
14221486
}
14231487

1488+
#[test]
1489+
fn proxy_override_path() {
1490+
setup(&|config| {
1491+
expect_ok(config, &["rustup", "default", "stable"]);
1492+
expect_ok(
1493+
config,
1494+
&[
1495+
"rustup",
1496+
"toolchain",
1497+
"install",
1498+
"nightly",
1499+
"--no-self-update",
1500+
],
1501+
);
1502+
1503+
let toolchain_path = config
1504+
.rustupdir
1505+
.join("toolchains")
1506+
.join(format!("nightly-{}", this_host_triple()));
1507+
let toolchain_file = config.current_dir().join("rust-toolchain.toml");
1508+
raw::write_file(
1509+
&toolchain_file,
1510+
&format!("[toolchain]\npath=\"{}\"", toolchain_path.to_str().unwrap()),
1511+
)
1512+
.unwrap();
1513+
1514+
expect_stdout_ok(config, &["cargo", "--call-rustc"], "hash-nightly-2");
1515+
});
1516+
}
1517+
14241518
#[test]
14251519
fn file_override_path_relative() {
14261520
setup(&|config| {
@@ -1472,7 +1566,7 @@ fn file_override_path_relative() {
14721566

14731567
raw::write_file(
14741568
&toolchain_file,
1475-
&format!("[toolchain]\npath=\"{}\"", relative_path.display()),
1569+
&format!("[toolchain]\npath=\"{}\"", relative_path.to_str().unwrap()),
14761570
)
14771571
.unwrap();
14781572

0 commit comments

Comments
 (0)