Skip to content

Commit 29287aa

Browse files
committed
Move with_update_server() into CliTestContext
1 parent 39a4c5f commit 29287aa

File tree

2 files changed

+86
-83
lines changed

2 files changed

+86
-83
lines changed

src/test/mock/clitools.rs

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,43 @@ impl CliTestContext {
366366
self.config.distdir = Some(CONST_TEST_STATE.dist_server_for(scenario).unwrap());
367367
DistDirGuard { inner: self }
368368
}
369+
370+
pub fn with_update_server(&mut self, version: &str) -> UpdateServerGuard {
371+
let self_dist_tmp = tempfile::Builder::new()
372+
.prefix("self_dist")
373+
.tempdir()
374+
.unwrap();
375+
let self_dist = self_dist_tmp.path();
376+
377+
let root_url = create_local_update_server(self_dist, &self.config.exedir, version);
378+
let trip = this_host_triple();
379+
let dist_dir = self_dist.join(format!("archive/{version}/{trip}"));
380+
let dist_exe = dist_dir.join(format!("rustup-init{EXE_SUFFIX}"));
381+
let dist_tmp = dist_dir.join("rustup-init-tmp");
382+
383+
// Modify the exe so it hashes different
384+
// 1) move out of the way the file
385+
fs::rename(&dist_exe, &dist_tmp).unwrap();
386+
// 2) copy it
387+
fs::copy(dist_tmp, &dist_exe).unwrap();
388+
// modify it
389+
let mut dest_file = fs::OpenOptions::new()
390+
.append(true)
391+
.create(true)
392+
.open(dist_exe)
393+
.unwrap();
394+
writeln!(dest_file).unwrap();
395+
396+
self.config.rustup_update_root = Some(root_url);
397+
UpdateServerGuard {
398+
_self_dist: self_dist_tmp,
399+
}
400+
}
401+
}
402+
403+
#[must_use]
404+
pub struct UpdateServerGuard {
405+
_self_dist: TempDir,
369406
}
370407

371408
impl From<Scenario> for CliTestContext {
@@ -384,10 +421,7 @@ impl From<Scenario> for CliTestContext {
384421
config.distdir = Some(config.test_dist_dir.path().to_path_buf());
385422
}
386423

387-
Self {
388-
config,
389-
_test_dir,
390-
}
424+
Self { config, _test_dir }
391425
}
392426
}
393427

@@ -432,37 +466,6 @@ fn create_local_update_server(self_dist: &Path, exedir: &Path, version: &str) ->
432466
root_url
433467
}
434468

435-
pub fn with_update_server(config: &mut Config, version: &str, f: &dyn Fn(&mut Config)) {
436-
let self_dist_tmp = tempfile::Builder::new()
437-
.prefix("self_dist")
438-
.tempdir()
439-
.unwrap();
440-
let self_dist = self_dist_tmp.path();
441-
442-
let root_url = create_local_update_server(self_dist, &config.exedir, version);
443-
let trip = this_host_triple();
444-
let dist_dir = self_dist.join(format!("archive/{version}/{trip}"));
445-
let dist_exe = dist_dir.join(format!("rustup-init{EXE_SUFFIX}"));
446-
let dist_tmp = dist_dir.join("rustup-init-tmp");
447-
448-
// Modify the exe so it hashes different
449-
// 1) move out of the way the file
450-
fs::rename(&dist_exe, &dist_tmp).unwrap();
451-
// 2) copy it
452-
fs::copy(dist_tmp, &dist_exe).unwrap();
453-
// modify it
454-
let mut dest_file = fs::OpenOptions::new()
455-
.append(true)
456-
.create(true)
457-
.open(dist_exe)
458-
.unwrap();
459-
writeln!(dest_file).unwrap();
460-
461-
config.rustup_update_root = Some(root_url);
462-
f(config);
463-
config.rustup_update_root = None;
464-
}
465-
466469
pub fn output_release_file(dist_dir: &Path, schema: &str, version: &str) {
467470
let contents = format!(
468471
r#"

tests/suite/cli_exact.rs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustup::for_host;
55
use rustup::test::{
6-
mock::clitools::{self, set_current_dist_date, with_update_server, CliTestContext, Scenario},
6+
mock::clitools::{self, set_current_dist_date, CliTestContext, Scenario},
77
this_host_triple,
88
};
99

@@ -39,25 +39,27 @@ info: default toolchain set to 'nightly-{0}'
3939
fn update_once_and_check_self_update() {
4040
let test_version = "2.0.0";
4141
let mut cx = CliTestContext::from(Scenario::SimpleV2);
42-
with_update_server(&mut cx.config, test_version, &|config| {
43-
config.expect_ok(&["rustup-init", "-y", "--no-modify-path"]);
44-
config.expect_ok(&["rustup", "set", "auto-self-update", "check-only"]);
45-
let current_version = env!("CARGO_PKG_VERSION");
42+
let _dist_guard = cx.with_update_server(test_version);
43+
cx.config
44+
.expect_ok(&["rustup-init", "-y", "--no-modify-path"]);
45+
cx.config
46+
.expect_ok(&["rustup", "set", "auto-self-update", "check-only"]);
47+
let current_version = env!("CARGO_PKG_VERSION");
4648

47-
config.expect_ok_ex(
48-
&["rustup", "update", "nightly"],
49-
&format!(
50-
r"
49+
cx.config.expect_ok_ex(
50+
&["rustup", "update", "nightly"],
51+
&format!(
52+
r"
5153
nightly-{} installed - 1.3.0 (hash-nightly-2)
5254
5355
rustup - Update available : {} -> {}
5456
",
55-
&this_host_triple(),
56-
current_version,
57-
test_version
58-
),
59-
for_host!(
60-
r"info: syncing channel updates for 'nightly-{0}'
57+
&this_host_triple(),
58+
current_version,
59+
test_version
60+
),
61+
for_host!(
62+
r"info: syncing channel updates for 'nightly-{0}'
6163
info: latest update on 2015-01-02, rust version 1.3.0 (hash-nightly-2)
6264
info: downloading component 'cargo'
6365
info: downloading component 'rust-docs'
@@ -68,28 +70,29 @@ info: installing component 'rust-docs'
6870
info: installing component 'rust-std'
6971
info: installing component 'rustc'
7072
"
71-
),
72-
);
73-
})
73+
),
74+
);
7475
}
7576

7677
#[test]
7778
fn update_once_and_self_update() {
7879
let test_version = "2.0.0";
7980
let mut cx = CliTestContext::from(Scenario::SimpleV2);
80-
with_update_server(&mut cx.config, test_version, &|config| {
81-
config.expect_ok(&["rustup-init", "-y", "--no-modify-path"]);
82-
config.expect_ok(&["rustup", "set", "auto-self-update", "enable"]);
83-
config.expect_ok_ex(
84-
&["rustup", "update", "nightly"],
85-
for_host!(
86-
r"
81+
let _dist_guard = cx.with_update_server(test_version);
82+
cx.config
83+
.expect_ok(&["rustup-init", "-y", "--no-modify-path"]);
84+
cx.config
85+
.expect_ok(&["rustup", "set", "auto-self-update", "enable"]);
86+
cx.config.expect_ok_ex(
87+
&["rustup", "update", "nightly"],
88+
for_host!(
89+
r"
8790
nightly-{0} installed - 1.3.0 (hash-nightly-2)
8891
8992
"
90-
),
91-
for_host!(
92-
r"info: syncing channel updates for 'nightly-{0}'
93+
),
94+
for_host!(
95+
r"info: syncing channel updates for 'nightly-{0}'
9396
info: latest update on 2015-01-02, rust version 1.3.0 (hash-nightly-2)
9497
info: downloading component 'cargo'
9598
info: downloading component 'rust-docs'
@@ -102,9 +105,8 @@ info: installing component 'rustc'
102105
info: checking for self-update
103106
info: downloading self-update
104107
"
105-
),
106-
);
107-
});
108+
),
109+
);
108110
}
109111

110112
#[test]
@@ -182,32 +184,30 @@ nightly-{0} - Update available : 1.2.0 (hash-nightly-1) -> 1.3.0 (hash-nightly-2
182184
fn check_updates_self() {
183185
let test_version = "2.0.0";
184186
let mut cx = CliTestContext::from(Scenario::SimpleV2);
185-
with_update_server(&mut cx.config, test_version, &|config| {
186-
let current_version = env!("CARGO_PKG_VERSION");
187+
let _dist_guard = cx.with_update_server(test_version);
188+
let current_version = env!("CARGO_PKG_VERSION");
187189

188-
config.expect_stdout_ok(
189-
&["rustup", "check"],
190-
&format!(
191-
r"rustup - Update available : {current_version} -> {test_version}
190+
cx.config.expect_stdout_ok(
191+
&["rustup", "check"],
192+
&format!(
193+
r"rustup - Update available : {current_version} -> {test_version}
192194
"
193-
),
194-
);
195-
});
195+
),
196+
);
196197
}
197198

198199
#[test]
199200
fn check_updates_self_no_change() {
200201
let current_version = env!("CARGO_PKG_VERSION");
201202
let mut cx = CliTestContext::from(Scenario::SimpleV2);
202-
with_update_server(&mut cx.config, current_version, &|config| {
203-
config.expect_stdout_ok(
204-
&["rustup", "check"],
205-
&format!(
206-
r"rustup - Up to date : {current_version}
203+
let _dist_guard = cx.with_update_server(current_version);
204+
cx.config.expect_stdout_ok(
205+
&["rustup", "check"],
206+
&format!(
207+
r"rustup - Up to date : {current_version}
207208
"
208-
),
209-
);
210-
});
209+
),
210+
);
211211
}
212212

213213
#[test]

0 commit comments

Comments
 (0)