Skip to content

Commit f1dd67d

Browse files
committed
change_dir takes mutable Config
1 parent eef953c commit f1dd67d

File tree

6 files changed

+43
-53
lines changed

6 files changed

+43
-53
lines changed

tests/cli-exact.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn remove_override_with_path() {
364364
.prefix("rustup-test")
365365
.tempdir()
366366
.unwrap();
367-
config.change_dir(dir.path(), || {
367+
config.change_dir(dir.path(), &|config| {
368368
config.expect_ok(&["rustup", "override", "add", "nightly"]);
369369
});
370370
config.expect_ok_ex(
@@ -395,7 +395,7 @@ fn remove_override_with_path_deleted() {
395395
.tempdir()
396396
.unwrap();
397397
let path = std::fs::canonicalize(dir.path()).unwrap();
398-
config.change_dir(&path, || {
398+
config.change_dir(&path, &|config| {
399399
config.expect_ok(&["rustup", "override", "add", "nightly"]);
400400
});
401401
path
@@ -429,7 +429,7 @@ fn remove_override_nonexistent() {
429429
.tempdir()
430430
.unwrap();
431431
let path = std::fs::canonicalize(dir.path()).unwrap();
432-
config.change_dir(&path, || {
432+
config.change_dir(&path, &|config| {
433433
config.expect_ok(&["rustup", "override", "add", "nightly"]);
434434
});
435435
path
@@ -483,7 +483,7 @@ fn list_overrides_with_nonexistent() {
483483
.prefix("rustup-test")
484484
.tempdir()
485485
.unwrap();
486-
config.change_dir(dir.path(), || {
486+
config.change_dir(dir.path(), &|config| {
487487
config.expect_ok(&["rustup", "override", "add", "nightly"]);
488488
});
489489
std::fs::canonicalize(dir.path()).unwrap()

tests/cli-rustup.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! for_host_and_home {
1818
};
1919
}
2020

21-
pub fn setup(f: &dyn Fn(&Config)) {
21+
pub fn setup(f: &dyn Fn(&mut Config)) {
2222
clitools::setup(Scenario::ArchivesV2, &|config| {
2323
f(config);
2424
});
@@ -851,7 +851,7 @@ fn show_toolchain_version_nested_file_override() {
851851
let subdir = cwd.join("foo");
852852

853853
fs::create_dir_all(&subdir).unwrap();
854-
config.change_dir(&subdir, &|| {
854+
config.change_dir(&subdir, &|config| {
855855
config.expect_ok_ex(
856856
&["rustup", "show"],
857857
&format!(
@@ -932,15 +932,16 @@ fn override_set_unset_with_path() {
932932
cwd_str = &cwd_str[4..];
933933
}
934934

935-
config.change_dir(&config.emptydir, &|| {
935+
let emptydir = tempfile::tempdir().unwrap();
936+
config.change_dir(emptydir.path(), &|config| {
936937
config.expect_ok(&["rustup", "override", "set", "nightly", "--path", cwd_str]);
937938
});
938939
config.expect_ok_ex(
939940
&["rustup", "override", "list"],
940941
&format!("{}\tnightly-{}\n", cwd_str, this_host_triple()),
941942
r"",
942943
);
943-
config.change_dir(&config.emptydir, &|| {
944+
config.change_dir(emptydir.path(), &|config| {
944945
config.expect_ok(&["rustup", "override", "unset", "--path", cwd_str]);
945946
});
946947
config.expect_ok_ex(&["rustup", "override", "list"], "no overrides\n", r"");
@@ -1417,7 +1418,7 @@ fn file_override_path_relative() {
14171418
// Change into an ephemeral dir so that we test that the path is relative to the override
14181419
let ephemeral = config.current_dir().join("ephemeral");
14191420
fs::create_dir_all(&ephemeral).unwrap();
1420-
config.change_dir(&ephemeral, || {
1421+
config.change_dir(&ephemeral, &|config| {
14211422
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
14221423
});
14231424
});
@@ -1505,7 +1506,7 @@ fn file_override_subdir() {
15051506

15061507
let subdir = cwd.join("subdir");
15071508
fs::create_dir_all(&subdir).unwrap();
1508-
config.change_dir(&subdir, &|| {
1509+
config.change_dir(&subdir, &|config| {
15091510
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
15101511
});
15111512
});
@@ -1708,7 +1709,7 @@ fn close_file_override_beats_far_directory_override() {
17081709
let toolchain_file = subdir.join("rust-toolchain");
17091710
raw::write_file(&toolchain_file, "nightly").unwrap();
17101711

1711-
config.change_dir(&subdir, &|| {
1712+
config.change_dir(&subdir, &|config| {
17121713
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
17131714
});
17141715
});

tests/cli-self-upd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::mock::dist::calc_hash;
2020

2121
const TEST_VERSION: &str = "1.1.1";
2222

23-
pub fn update_setup(f: &dyn Fn(&Config, &Path)) {
23+
pub fn update_setup(f: &dyn Fn(&mut Config, &Path)) {
2424
self_update_setup(f, TEST_VERSION)
2525
}
2626

tests/cli-v1.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn remove_default_toolchain_autoinstalls() {
147147
fn remove_override_toolchain_err_handling() {
148148
setup(&|config| {
149149
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
150-
config.change_dir(tempdir.path(), &|| {
150+
config.change_dir(tempdir.path(), &|config| {
151151
config.expect_ok(&["rustup", "default", "nightly"]);
152152
config.expect_ok(&["rustup", "override", "add", "beta"]);
153153
config.expect_ok(&["rustup", "toolchain", "remove", "beta"]);
@@ -222,7 +222,7 @@ fn override_overrides_default() {
222222
setup(&|config| {
223223
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
224224
config.expect_ok(&["rustup", "default", "nightly"]);
225-
config.change_dir(tempdir.path(), &|| {
225+
config.change_dir(tempdir.path(), &|config| {
226226
config.expect_ok(&["rustup", "override", "add", "beta"]);
227227
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
228228
});
@@ -236,19 +236,19 @@ fn multiple_overrides() {
236236
let tempdir2 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
237237

238238
config.expect_ok(&["rustup", "default", "nightly"]);
239-
config.change_dir(tempdir1.path(), &|| {
239+
config.change_dir(tempdir1.path(), &|config| {
240240
config.expect_ok(&["rustup", "override", "add", "beta"]);
241241
});
242-
config.change_dir(tempdir2.path(), &|| {
242+
config.change_dir(tempdir2.path(), &|config| {
243243
config.expect_ok(&["rustup", "override", "add", "stable"]);
244244
});
245245

246246
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
247247

248-
config.change_dir(tempdir1.path(), &|| {
248+
config.change_dir(tempdir1.path(), &|config| {
249249
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
250250
});
251-
config.change_dir(tempdir2.path(), &|| {
251+
config.change_dir(tempdir2.path(), &|config| {
252252
config.expect_stdout_ok(&["rustc", "--version"], "hash-stable-1.1.0");
253253
});
254254
});
@@ -258,7 +258,7 @@ fn multiple_overrides() {
258258
fn change_override() {
259259
setup(&|config| {
260260
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
261-
config.change_dir(tempdir.path(), &|| {
261+
config.change_dir(tempdir.path(), &|config| {
262262
config.expect_ok(&["rustup", "override", "add", "nightly"]);
263263
config.expect_ok(&["rustup", "override", "add", "beta"]);
264264
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
@@ -270,7 +270,7 @@ fn change_override() {
270270
fn remove_override_no_default() {
271271
setup(&|config| {
272272
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
273-
config.change_dir(tempdir.path(), &|| {
273+
config.change_dir(tempdir.path(), &|config| {
274274
config.expect_ok(&["rustup", "override", "add", "nightly"]);
275275
config.expect_ok(&["rustup", "override", "remove"]);
276276
config.expect_err(
@@ -285,7 +285,7 @@ fn remove_override_no_default() {
285285
fn remove_override_with_default() {
286286
setup(&|config| {
287287
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
288-
config.change_dir(tempdir.path(), &|| {
288+
config.change_dir(tempdir.path(), &|config| {
289289
config.expect_ok(&["rustup", "default", "nightly"]);
290290
config.expect_ok(&["rustup", "override", "add", "beta"]);
291291
config.expect_ok(&["rustup", "override", "remove"]);
@@ -300,18 +300,18 @@ fn remove_override_with_multiple_overrides() {
300300
let tempdir1 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
301301
let tempdir2 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
302302
config.expect_ok(&["rustup", "default", "nightly"]);
303-
config.change_dir(tempdir1.path(), &|| {
303+
config.change_dir(tempdir1.path(), &|config| {
304304
config.expect_ok(&["rustup", "override", "add", "beta"]);
305305
});
306-
config.change_dir(tempdir2.path(), &|| {
306+
config.change_dir(tempdir2.path(), &|config| {
307307
config.expect_ok(&["rustup", "override", "add", "stable"]);
308308
});
309309
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
310-
config.change_dir(tempdir1.path(), &|| {
310+
config.change_dir(tempdir1.path(), &|config| {
311311
config.expect_ok(&["rustup", "override", "remove"]);
312312
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
313313
});
314-
config.change_dir(tempdir2.path(), &|| {
314+
config.change_dir(tempdir2.path(), &|config| {
315315
config.expect_stdout_ok(&["rustc", "--version"], "hash-stable-1.1.0");
316316
});
317317
});

tests/cli-v2.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn remove_default_toolchain_autoinstalls() {
250250
fn remove_override_toolchain_err_handling() {
251251
setup(&|config| {
252252
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
253-
config.change_dir(tempdir.path(), &|| {
253+
config.change_dir(tempdir.path(), &|config| {
254254
config.expect_ok(&["rustup", "default", "nightly"]);
255255
config.expect_ok(&["rustup", "override", "add", "beta"]);
256256
config.expect_ok(&["rustup", "toolchain", "remove", "beta"]);
@@ -354,7 +354,7 @@ fn override_overrides_default() {
354354
setup(&|config| {
355355
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
356356
config.expect_ok(&["rustup", "default", "nightly"]);
357-
config.change_dir(tempdir.path(), &|| {
357+
config.change_dir(tempdir.path(), &|config| {
358358
config.expect_ok(&["rustup", "override", "add", "beta"]);
359359
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
360360
});
@@ -368,19 +368,19 @@ fn multiple_overrides() {
368368
let tempdir2 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
369369

370370
config.expect_ok(&["rustup", "default", "nightly"]);
371-
config.change_dir(tempdir1.path(), &|| {
371+
config.change_dir(tempdir1.path(), &|config| {
372372
config.expect_ok(&["rustup", "override", "add", "beta"]);
373373
});
374-
config.change_dir(tempdir2.path(), &|| {
374+
config.change_dir(tempdir2.path(), &|config| {
375375
config.expect_ok(&["rustup", "override", "add", "stable"]);
376376
});
377377

378378
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
379379

380-
config.change_dir(tempdir1.path(), &|| {
380+
config.change_dir(tempdir1.path(), &|config| {
381381
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
382382
});
383-
config.change_dir(tempdir2.path(), &|| {
383+
config.change_dir(tempdir2.path(), &|config| {
384384
config.expect_stdout_ok(&["rustc", "--version"], "hash-stable-1.1.0");
385385
});
386386
});
@@ -404,7 +404,7 @@ fn override_windows_root() {
404404
// Really sketchy to be messing with C:\ in a test...
405405
let prefix = prefix.as_os_str().to_str().unwrap();
406406
let prefix = format!("{prefix}\\");
407-
config.change_dir(&PathBuf::from(&prefix), &|| {
407+
config.change_dir(&PathBuf::from(&prefix), &|config| {
408408
config.expect_ok(&["rustup", "default", "stable"]);
409409
config.expect_ok(&["rustup", "override", "add", "nightly"]);
410410
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
@@ -418,7 +418,7 @@ fn override_windows_root() {
418418
fn change_override() {
419419
setup(&|config| {
420420
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
421-
config.change_dir(tempdir.path(), &|| {
421+
config.change_dir(tempdir.path(), &|config| {
422422
config.expect_ok(&["rustup", "override", "add", "nightly"]);
423423
config.expect_ok(&["rustup", "override", "add", "beta"]);
424424
config.expect_stdout_ok(&["rustc", "--version"], "hash-beta-1.2.0");
@@ -430,7 +430,7 @@ fn change_override() {
430430
fn remove_override_no_default() {
431431
setup(&|config| {
432432
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
433-
config.change_dir(tempdir.path(), &|| {
433+
config.change_dir(tempdir.path(), &|config| {
434434
config.expect_ok(&["rustup", "override", "add", "nightly"]);
435435
config.expect_ok(&["rustup", "override", "remove"]);
436436
config.expect_err(
@@ -445,7 +445,7 @@ fn remove_override_no_default() {
445445
fn remove_override_with_default() {
446446
setup(&|config| {
447447
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
448-
config.change_dir(tempdir.path(), &|| {
448+
config.change_dir(tempdir.path(), &|config| {
449449
config.expect_ok(&["rustup", "default", "nightly"]);
450450
config.expect_ok(&["rustup", "override", "add", "beta"]);
451451
config.expect_ok(&["rustup", "override", "remove"]);
@@ -460,18 +460,18 @@ fn remove_override_with_multiple_overrides() {
460460
let tempdir1 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
461461
let tempdir2 = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
462462
config.expect_ok(&["rustup", "default", "nightly"]);
463-
config.change_dir(tempdir1.path(), &|| {
463+
config.change_dir(tempdir1.path(), &|config| {
464464
config.expect_ok(&["rustup", "override", "add", "beta"]);
465465
});
466-
config.change_dir(tempdir2.path(), &|| {
466+
config.change_dir(tempdir2.path(), &|config| {
467467
config.expect_ok(&["rustup", "override", "add", "stable"]);
468468
});
469469
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
470-
config.change_dir(tempdir1.path(), &|| {
470+
config.change_dir(tempdir1.path(), &|config| {
471471
config.expect_ok(&["rustup", "override", "remove"]);
472472
config.expect_stdout_ok(&["rustc", "--version"], "hash-nightly-2");
473473
});
474-
config.change_dir(tempdir2.path(), &|| {
474+
config.change_dir(tempdir2.path(), &|config| {
475475
config.expect_stdout_ok(&["rustc", "--version"], "hash-stable-1.1.0");
476476
});
477477
});

tests/mock/clitools.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ pub struct Config {
4242
pub cargodir: PathBuf,
4343
/// ~
4444
pub homedir: PathBuf,
45-
/// An empty directory. Tests should not write to this.
46-
pub emptydir: PathBuf,
4745
/// Root for updates to rustup itself aka RUSTUP_UPDATE_ROOT
4846
pub rustup_update_root: Option<String>,
4947
/// This is cwd for the test
@@ -130,7 +128,6 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
130128
let customdir = tempdir_in_with_prefix(&test_dir, "rustup-custom");
131129
let cargodir = tempdir_in_with_prefix(&test_dir, "rustup-cargo");
132130
let homedir = tempdir_in_with_prefix(&test_dir, "rustup-home");
133-
let emptydir = tempdir_in_with_prefix(&test_dir, "rustup-empty");
134131
let workdir = tempdir_in_with_prefix(&test_dir, "rustup-workdir");
135132

136133
// The uninstall process on windows involves using the directory above
@@ -145,7 +142,6 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
145142
customdir,
146143
cargodir,
147144
homedir,
148-
emptydir,
149145
rustup_update_root: None,
150146
workdir: RefCell::new(workdir),
151147
};
@@ -218,7 +214,7 @@ pub fn check_update_setup(f: &dyn Fn(&mut Config)) {
218214
});
219215
}
220216

221-
pub fn self_update_setup(f: &dyn Fn(&Config, &Path), version: &str) {
217+
pub fn self_update_setup(f: &dyn Fn(&mut Config, &Path), version: &str) {
222218
setup(Scenario::SimpleV2, &|config| {
223219
// Create a mock self-update server
224220
let self_dist_tmp = tempfile::Builder::new()
@@ -256,16 +252,9 @@ impl Config {
256252
self.workdir.borrow().clone()
257253
}
258254

259-
pub fn change_dir<F>(&self, path: &Path, mut f: F)
260-
where
261-
F: FnMut(),
262-
{
263-
self._change_dir(path, &mut f)
264-
}
265-
266-
fn _change_dir(&self, path: &Path, f: &mut dyn FnMut()) {
255+
pub fn change_dir(&mut self, path: &Path, f: &dyn Fn(&mut Config)) {
267256
let prev = self.workdir.replace(path.to_owned());
268-
f();
257+
f(self);
269258
*self.workdir.borrow_mut() = prev;
270259
}
271260

0 commit comments

Comments
 (0)