Skip to content

Commit f73d287

Browse files
committed
Move change_dir() into CliTestContext
1 parent 29287aa commit f73d287

File tree

5 files changed

+247
-159
lines changed

5 files changed

+247
-159
lines changed

src/test/mock/clitools.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
fmt::Debug,
99
fs,
1010
io::{self, Write},
11+
mem,
1112
ops::{Deref, DerefMut},
1213
path::{Path, PathBuf},
1314
process::Command,
@@ -398,6 +399,11 @@ impl CliTestContext {
398399
_self_dist: self_dist_tmp,
399400
}
400401
}
402+
403+
pub fn change_dir(&mut self, path: &Path) -> WorkDirGuard<'_> {
404+
let prev = self.config.workdir.replace(path.to_owned());
405+
WorkDirGuard { inner: self, prev }
406+
}
401407
}
402408

403409
#[must_use]
@@ -425,6 +431,32 @@ impl From<Scenario> for CliTestContext {
425431
}
426432
}
427433

434+
#[must_use]
435+
pub struct WorkDirGuard<'a> {
436+
inner: &'a mut CliTestContext,
437+
prev: PathBuf,
438+
}
439+
440+
impl Deref for WorkDirGuard<'_> {
441+
type Target = CliTestContext;
442+
443+
fn deref(&self) -> &Self::Target {
444+
&*self.inner
445+
}
446+
}
447+
448+
impl DerefMut for WorkDirGuard<'_> {
449+
fn deref_mut(&mut self) -> &mut Self::Target {
450+
self.inner
451+
}
452+
}
453+
454+
impl Drop for WorkDirGuard<'_> {
455+
fn drop(&mut self) {
456+
self.inner.config.workdir.replace(mem::take(&mut self.prev));
457+
}
458+
}
459+
428460
#[must_use]
429461
pub struct DistDirGuard<'a> {
430462
inner: &'a mut CliTestContext,
@@ -482,12 +514,6 @@ impl Config {
482514
self.workdir.borrow().clone()
483515
}
484516

485-
pub fn change_dir(&mut self, path: &Path, f: &dyn Fn(&mut Config)) {
486-
let prev = self.workdir.replace(path.to_owned());
487-
f(self);
488-
*self.workdir.borrow_mut() = prev;
489-
}
490-
491517
pub fn create_rustup_sh_metadata(&self) {
492518
let rustup_dir = self.homedir.join(".rustup");
493519
fs::create_dir_all(&rustup_dir).unwrap();

tests/suite/cli_exact.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,13 @@ fn remove_override_with_path() {
337337
.prefix("rustup-test")
338338
.tempdir()
339339
.unwrap();
340-
cx.config.change_dir(dir.path(), &|config| {
341-
config.expect_ok(&["rustup", "override", "add", "nightly"]);
342-
});
340+
341+
{
342+
let mut cx = cx.change_dir(dir.path());
343+
cx.config
344+
.expect_ok(&["rustup", "override", "add", "nightly"]);
345+
}
346+
343347
cx.config.expect_ok_ex(
344348
&[
345349
"rustup",
@@ -367,9 +371,9 @@ fn remove_override_with_path_deleted() {
367371
.tempdir()
368372
.unwrap();
369373
let path = std::fs::canonicalize(dir.path()).unwrap();
370-
cx.config.change_dir(&path, &|config| {
371-
config.expect_ok(&["rustup", "override", "add", "nightly"]);
372-
});
374+
let mut cx = cx.change_dir(&path);
375+
cx.config
376+
.expect_ok(&["rustup", "override", "add", "nightly"]);
373377
path
374378
};
375379
cx.config.expect_ok_ex(
@@ -400,9 +404,9 @@ fn remove_override_nonexistent() {
400404
.tempdir()
401405
.unwrap();
402406
let path = std::fs::canonicalize(dir.path()).unwrap();
403-
cx.config.change_dir(&path, &|config| {
404-
config.expect_ok(&["rustup", "override", "add", "nightly"]);
405-
});
407+
let mut cx = cx.change_dir(&path);
408+
cx.config
409+
.expect_ok(&["rustup", "override", "add", "nightly"]);
406410
path
407411
};
408412
// FIXME TempDir seems to succumb to difficulties removing dirs on windows
@@ -453,9 +457,9 @@ fn list_overrides_with_nonexistent() {
453457
.prefix("rustup-test")
454458
.tempdir()
455459
.unwrap();
456-
cx.config.change_dir(dir.path(), &|config| {
457-
config.expect_ok(&["rustup", "override", "add", "nightly"]);
458-
});
460+
let mut cx = cx.change_dir(dir.path());
461+
cx.config
462+
.expect_ok(&["rustup", "override", "add", "nightly"]);
459463
std::fs::canonicalize(dir.path()).unwrap()
460464
};
461465
// FIXME TempDir seems to succumb to difficulties removing dirs on windows

tests/suite/cli_rustup.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,11 @@ fn show_toolchain_version_nested_file_override() {
961961
let subdir = cwd.join("foo");
962962

963963
fs::create_dir_all(&subdir).unwrap();
964-
cx.config.change_dir(&subdir, &|config| {
965-
config.expect_ok_ex(
966-
&["rustup", "show"],
967-
&format!(
968-
r"Default host: {0}
964+
let mut cx = cx.change_dir(&subdir);
965+
cx.config.expect_ok_ex(
966+
&["rustup", "show"],
967+
&format!(
968+
r"Default host: {0}
969969
970970
installed toolchains
971971
--------------------
@@ -980,12 +980,11 @@ nightly-{0} (overridden by '{1}')
980980
1.3.0 (hash-nightly-2)
981981
982982
",
983-
this_host_triple(),
984-
toolchain_file.display()
985-
),
986-
r"",
987-
);
988-
});
983+
this_host_triple(),
984+
toolchain_file.display()
985+
),
986+
r"",
987+
);
989988
}
990989

991990
#[test]
@@ -1039,17 +1038,24 @@ fn override_set_unset_with_path() {
10391038
}
10401039

10411040
let emptydir = tempfile::tempdir().unwrap();
1042-
cx.config.change_dir(emptydir.path(), &|config| {
1043-
config.expect_ok(&["rustup", "override", "set", "nightly", "--path", cwd_str]);
1044-
});
1041+
{
1042+
let mut cx = cx.change_dir(emptydir.path());
1043+
cx.config
1044+
.expect_ok(&["rustup", "override", "set", "nightly", "--path", cwd_str]);
1045+
}
1046+
10451047
cx.config.expect_ok_ex(
10461048
&["rustup", "override", "list"],
10471049
&format!("{}\tnightly-{}\n", cwd_str, this_host_triple()),
10481050
r"",
10491051
);
1050-
cx.config.change_dir(emptydir.path(), &|config| {
1051-
config.expect_ok(&["rustup", "override", "unset", "--path", cwd_str]);
1052-
});
1052+
1053+
{
1054+
let mut cx = cx.change_dir(emptydir.path());
1055+
cx.config
1056+
.expect_ok(&["rustup", "override", "unset", "--path", cwd_str]);
1057+
}
1058+
10531059
cx.config
10541060
.expect_ok_ex(&["rustup", "override", "list"], "no overrides\n", r"");
10551061
}
@@ -1728,9 +1734,10 @@ fn file_override_path_relative_not_supported() {
17281734
// Change into an ephemeral dir so that we test that the path is relative to the override
17291735
let ephemeral = cx.config.current_dir().join("ephemeral");
17301736
fs::create_dir_all(&ephemeral).unwrap();
1731-
cx.config.change_dir(&ephemeral, &|config| {
1732-
config.expect_err(&["rustc", "--version"], "relative path toolchain");
1733-
});
1737+
1738+
let cx = cx.change_dir(&ephemeral);
1739+
cx.config
1740+
.expect_err(&["rustc", "--version"], "relative path toolchain");
17341741
}
17351742

17361743
#[test]
@@ -1815,9 +1822,9 @@ fn file_override_subdir() {
18151822

18161823
let subdir = cwd.join("subdir");
18171824
fs::create_dir_all(&subdir).unwrap();
1818-
cx.config.change_dir(&subdir, &|config| {
1819-
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
1820-
});
1825+
let cx = cx.change_dir(&subdir);
1826+
cx.config
1827+
.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
18211828
}
18221829

18231830
#[test]
@@ -2026,9 +2033,9 @@ fn close_file_override_beats_far_directory_override() {
20262033
let toolchain_file = subdir.join("rust-toolchain");
20272034
raw::write_file(&toolchain_file, "nightly").unwrap();
20282035

2029-
cx.config.change_dir(&subdir, &|config| {
2030-
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
2031-
});
2036+
let cx = cx.change_dir(&subdir);
2037+
cx.config
2038+
.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
20322039
}
20332040

20342041
#[test]

0 commit comments

Comments
 (0)