Skip to content

Commit c97f490

Browse files
committed
Consolidate the creation of the local self-update server
This reduces the repetition in the two "setup" methods. Unfortunately, we can't move the creation of the temp directory into the helper: * If it's created in the helper and only `self_dist` is returned, then `self_dist_tmp` will be dropped and the directory will be deleted. * If it's created in the helper and we use `into_path()` to persist it, then it won't be cleaned up when the function exits.
1 parent 4e0fe3f commit c97f490

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

tests/mock/clitools.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,31 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
189189
assert!(!PathBuf::from("./bogus-cargo-home").exists());
190190
}
191191

192+
fn create_local_update_server(self_dist: &Path, config: &mut Config, version: &str) {
193+
let trip = this_host_triple();
194+
let dist_dir = self_dist.join(&format!("archive/{}/{}", version, trip));
195+
let dist_exe = dist_dir.join(&format!("rustup-init{}", EXE_SUFFIX));
196+
let rustup_bin = config.exedir.join(&format!("rustup-init{}", EXE_SUFFIX));
197+
198+
fs::create_dir_all(dist_dir).unwrap();
199+
output_release_file(&self_dist, "1", version);
200+
fs::copy(&rustup_bin, &dist_exe).unwrap();
201+
202+
let root_url = format!("file://{}", self_dist.display());
203+
config.rustup_update_root = Some(root_url);
204+
}
205+
192206
pub fn check_update_setup(f: &dyn Fn(&mut Config)) {
193207
let version = env!("CARGO_PKG_VERSION");
194208

195209
setup(Scenario::ArchivesV2, &|config| {
196-
// Create a mock self-update server
197210
let self_dist_tmp = tempfile::Builder::new()
198211
.prefix("self_dist")
199212
.tempdir()
200213
.unwrap();
201214
let self_dist = self_dist_tmp.path();
202215

203-
let trip = this_host_triple();
204-
let dist_dir = self_dist.join(&format!("archive/{}/{}", version, trip));
205-
let dist_exe = dist_dir.join(&format!("rustup-init{}", EXE_SUFFIX));
206-
let rustup_bin = config.exedir.join(&format!("rustup-init{}", EXE_SUFFIX));
207-
208-
fs::create_dir_all(dist_dir).unwrap();
209-
output_release_file(self_dist, "1", version);
210-
fs::copy(&rustup_bin, &dist_exe).unwrap();
211-
212-
let root_url = format!("file://{}", self_dist.display());
213-
config.rustup_update_root = Some(root_url);
216+
create_local_update_server(self_dist, config, version);
214217

215218
f(config);
216219
});
@@ -225,21 +228,16 @@ pub fn self_update_setup(f: &dyn Fn(&Config, &Path), version: &str) {
225228
.unwrap();
226229
let self_dist = self_dist_tmp.path();
227230

231+
create_local_update_server(self_dist, config, version);
232+
228233
let trip = this_host_triple();
229234
let dist_dir = self_dist.join(&format!("archive/{}/{}", version, trip));
230235
let dist_exe = dist_dir.join(&format!("rustup-init{}", EXE_SUFFIX));
231-
let rustup_bin = config.exedir.join(&format!("rustup-init{}", EXE_SUFFIX));
232236

233-
fs::create_dir_all(dist_dir).unwrap();
234-
output_release_file(self_dist, "1", version);
235-
fs::copy(&rustup_bin, &dist_exe).unwrap();
236237
// Modify the exe so it hashes different
237238
raw::append_file(&dist_exe, "").unwrap();
238239

239-
let root_url = format!("file://{}", self_dist.display());
240-
config.rustup_update_root = Some(root_url);
241-
242-
f(config, self_dist);
240+
f(config, &self_dist);
243241
});
244242
}
245243

0 commit comments

Comments
 (0)