Skip to content

Commit 975d11b

Browse files
committed
Port cli_self_upd to CliTestContext
1 parent c27a39f commit 975d11b

File tree

2 files changed

+636
-606
lines changed

2 files changed

+636
-606
lines changed

src/test/mock/clitools.rs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,56 @@ pub fn setup_test_state(test_dist_dir: tempfile::TempDir) -> (tempfile::TempDir,
303303
(test_dir, config)
304304
}
305305

306+
pub struct SelfUpdateTestContext {
307+
pub config: Config,
308+
_test_dir: TempDir,
309+
self_dist_tmp: TempDir,
310+
}
311+
312+
impl SelfUpdateTestContext {
313+
pub fn new(version: &str) -> Self {
314+
let mut cx = CliTestContext::from(Scenario::SimpleV2);
315+
316+
// Create a mock self-update server
317+
let self_dist_tmp = tempfile::Builder::new()
318+
.prefix("self_dist")
319+
.tempdir_in(&cx.config.test_root_dir)
320+
.unwrap();
321+
let self_dist = self_dist_tmp.path();
322+
323+
let root_url = create_local_update_server(self_dist, &cx.config.exedir, version);
324+
cx.config.rustup_update_root = Some(root_url);
325+
326+
let trip = this_host_triple();
327+
let dist_dir = self_dist.join(format!("archive/{version}/{trip}"));
328+
let dist_exe = dist_dir.join(format!("rustup-init{EXE_SUFFIX}"));
329+
let dist_tmp = dist_dir.join("rustup-init-tmp");
330+
331+
// Modify the exe so it hashes different
332+
// 1) move out of the way the file
333+
fs::rename(&dist_exe, &dist_tmp).unwrap();
334+
// 2) copy it
335+
fs::copy(dist_tmp, &dist_exe).unwrap();
336+
// modify it
337+
let mut dest_file = fs::OpenOptions::new()
338+
.append(true)
339+
.create(true)
340+
.open(dist_exe)
341+
.unwrap();
342+
writeln!(dest_file).unwrap();
343+
344+
Self {
345+
config: cx.config,
346+
_test_dir: cx._test_dir,
347+
self_dist_tmp,
348+
}
349+
}
350+
351+
pub fn path(&self) -> &Path {
352+
self.self_dist_tmp.path()
353+
}
354+
}
355+
306356
pub fn test(s: Scenario, f: &dyn Fn(&mut Config)) {
307357
let mut cx = CliTestContext::from(s);
308358
f(&mut cx.config);
@@ -349,40 +399,6 @@ fn create_local_update_server(self_dist: &Path, exedir: &Path, version: &str) ->
349399
root_url
350400
}
351401

352-
pub fn self_update_setup(f: &dyn Fn(&mut Config, &Path), version: &str) {
353-
let mut cx = CliTestContext::from(Scenario::SimpleV2);
354-
355-
// Create a mock self-update server
356-
let self_dist_tmp = tempfile::Builder::new()
357-
.prefix("self_dist")
358-
.tempdir_in(&cx.config.test_root_dir)
359-
.unwrap();
360-
let self_dist = self_dist_tmp.path();
361-
362-
let root_url = create_local_update_server(self_dist, &cx.config.exedir, version);
363-
cx.config.rustup_update_root = Some(root_url);
364-
365-
let trip = this_host_triple();
366-
let dist_dir = self_dist.join(format!("archive/{version}/{trip}"));
367-
let dist_exe = dist_dir.join(format!("rustup-init{EXE_SUFFIX}"));
368-
let dist_tmp = dist_dir.join("rustup-init-tmp");
369-
370-
// Modify the exe so it hashes different
371-
// 1) move out of the way the file
372-
fs::rename(&dist_exe, &dist_tmp).unwrap();
373-
// 2) copy it
374-
fs::copy(dist_tmp, &dist_exe).unwrap();
375-
// modify it
376-
let mut dest_file = fs::OpenOptions::new()
377-
.append(true)
378-
.create(true)
379-
.open(dist_exe)
380-
.unwrap();
381-
writeln!(dest_file).unwrap();
382-
383-
f(&mut cx.config, self_dist);
384-
}
385-
386402
pub fn with_update_server(config: &mut Config, version: &str, f: &dyn Fn(&mut Config)) {
387403
let self_dist_tmp = tempfile::Builder::new()
388404
.prefix("self_dist")

0 commit comments

Comments
 (0)