Skip to content

Commit 83f8b16

Browse files
author
Jon Gjengset
committed
Make path tests OS-agnostic
1 parent 6c429e9 commit 83f8b16

File tree

1 file changed

+70
-39
lines changed

1 file changed

+70
-39
lines changed

tests/cli-rustup.rs

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,64 +1385,95 @@ fn file_override() {
13851385
}
13861386

13871387
#[test]
1388-
#[cfg_attr(
1389-
not(unix),
1390-
ignore = "TODO: Figure out how to write a wrapper toolchain on Windows"
1391-
)]
13921388
fn file_override_path() {
13931389
setup(&|config| {
1394-
let cwd = config.current_dir();
1395-
let toolchain_path = cwd.join("ephemeral");
1396-
let toolchain_bin = toolchain_path.join("bin");
1397-
fs::create_dir_all(&toolchain_bin).unwrap();
1398-
1399-
// Inject a wrapper binary for rustc.
1400-
let rustc = toolchain_bin.join("rustc");
1401-
#[cfg(unix)]
1402-
{
1403-
use std::os::unix::fs::PermissionsExt;
1404-
raw::write_file(&rustc, "#!/bin/sh\necho custom-toolchain").unwrap();
1405-
fs::set_permissions(rustc, fs::Permissions::from_mode(0o755)).unwrap();
1406-
}
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+
);
14071401

1408-
let toolchain_file = cwd.join("rust-toolchain.toml");
1402+
let toolchain_path = config
1403+
.rustupdir
1404+
.join("toolchains")
1405+
.join(format!("nightly-{}", this_host_triple()));
1406+
let toolchain_file = config.current_dir().join("rust-toolchain.toml");
14091407
raw::write_file(
14101408
&toolchain_file,
14111409
&format!("[toolchain]\npath=\"{}\"", toolchain_path.display()),
14121410
)
14131411
.unwrap();
14141412

1415-
expect_stdout_ok(config, &["rustc", "--version"], "custom-toolchain");
1413+
expect_stdout_ok(config, &["rustc", "--version"], "hash-nightly-2");
14161414
});
14171415
}
14181416

14191417
#[test]
1420-
#[cfg_attr(
1421-
not(unix),
1422-
ignore = "TODO: Figure out how to write a wrapper toolchain on Windows"
1423-
)]
14241418
fn file_override_path_relative() {
14251419
setup(&|config| {
1426-
let cwd = config.current_dir();
1427-
let toolchain_path = cwd.join("ephemeral");
1428-
let toolchain_bin = toolchain_path.join("bin");
1429-
fs::create_dir_all(&toolchain_bin).unwrap();
1420+
expect_ok(config, &["rustup", "default", "stable"]);
1421+
expect_ok(
1422+
config,
1423+
&[
1424+
"rustup",
1425+
"toolchain",
1426+
"install",
1427+
"nightly",
1428+
"--no-self-update",
1429+
],
1430+
);
14301431

1431-
// Inject a wrapper binary for rustc.
1432-
let rustc = toolchain_bin.join("rustc");
1433-
#[cfg(unix)]
1434-
{
1435-
use std::os::unix::fs::PermissionsExt;
1436-
raw::write_file(&rustc, "#!/bin/sh\necho custom-toolchain").unwrap();
1437-
fs::set_permissions(rustc, fs::Permissions::from_mode(0o755)).unwrap();
1432+
let toolchain_path = config
1433+
.rustupdir
1434+
.join("toolchains")
1435+
.join(format!("nightly-{}", this_host_triple()))
1436+
.canonicalize()
1437+
.unwrap();
1438+
let toolchain_file = config
1439+
.current_dir()
1440+
.canonicalize()
1441+
.unwrap()
1442+
.join("rust-toolchain.toml");
1443+
1444+
// Find shared prefix so we can determine a relative path
1445+
let mut p1 = dbg!(&toolchain_path).components().peekable();
1446+
let mut p2 = dbg!(&toolchain_file).components().peekable();
1447+
while let (Some(p1p), Some(p2p)) = (p1.peek(), p2.peek()) {
1448+
if p1p == p2p {
1449+
let _ = p1.next();
1450+
let _ = p2.next();
1451+
} else {
1452+
// The two paths diverge here
1453+
break;
1454+
}
14381455
}
1456+
let mut relative_path = PathBuf::new();
1457+
// NOTE: We skip 1 since we don't need to .. across the .toml file at the end of the path
1458+
for _ in p2.skip(1) {
1459+
relative_path.push("..");
1460+
}
1461+
for p in p1 {
1462+
relative_path.push(p);
1463+
}
1464+
assert!(relative_path.is_relative());
14391465

1440-
let toolchain_file = cwd.join("rust-toolchain.toml");
1441-
raw::write_file(&toolchain_file, "[toolchain]\npath=\"ephemeral\"").unwrap();
1466+
raw::write_file(
1467+
&toolchain_file,
1468+
&format!("[toolchain]\npath=\"{}\"", relative_path.display()),
1469+
)
1470+
.unwrap();
14421471

1443-
// Change into ephemeral so that we actually test that the path is relative to the override
1444-
config.change_dir(&toolchain_path, || {
1445-
expect_stdout_ok(config, &["rustc", "--version"], "custom-toolchain")
1472+
// Change into an ephemeral dir so that we test that the path is relative to the override
1473+
let ephemeral = config.current_dir().join("ephemeral");
1474+
fs::create_dir_all(&ephemeral).unwrap();
1475+
config.change_dir(&ephemeral, || {
1476+
expect_stdout_ok(config, &["rustc", "--version"], "hash-nightly-2");
14461477
});
14471478
});
14481479
}

0 commit comments

Comments
 (0)