Skip to content

Commit d3a7f40

Browse files
authored
Merge pull request #2666 from alexwlchan/no-self-update-without-internet
Don't access the Internet in the check_updates tests
2 parents 823a5e8 + c97f490 commit d3a7f40

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

tests/cli-exact.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
pub mod mock;
55

66
use crate::mock::clitools::{
7-
self, expect_err_ex, expect_ok, expect_ok_ex, expect_stdout_ok, self_update_setup,
8-
set_current_dist_date, Config, Scenario,
7+
self, check_update_setup, expect_err_ex, expect_ok, expect_ok_ex, expect_stdout_ok,
8+
self_update_setup, set_current_dist_date, Config, Scenario,
99
};
1010
use rustup::for_host;
1111
use rustup::test::this_host_triple;
@@ -68,7 +68,7 @@ fn update_again() {
6868

6969
#[test]
7070
fn check_updates_none() {
71-
setup(&|config| {
71+
check_update_setup(&|config| {
7272
set_current_dist_date(config, "2015-01-01");
7373
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
7474
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
@@ -88,7 +88,7 @@ nightly-{0} - Up to date : 1.2.0 (hash-nightly-1)
8888

8989
#[test]
9090
fn check_updates_some() {
91-
setup(&|config| {
91+
check_update_setup(&|config| {
9292
set_current_dist_date(config, "2015-01-01");
9393
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
9494
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
@@ -151,7 +151,7 @@ fn check_updates_self_no_change() {
151151

152152
#[test]
153153
fn check_updates_with_update() {
154-
setup(&|config| {
154+
check_update_setup(&|config| {
155155
set_current_dist_date(config, "2015-01-01");
156156
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
157157
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);

tests/mock/clitools.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ 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+
206+
pub fn check_update_setup(f: &dyn Fn(&mut Config)) {
207+
let version = env!("CARGO_PKG_VERSION");
208+
209+
setup(Scenario::ArchivesV2, &|config| {
210+
let self_dist_tmp = tempfile::Builder::new()
211+
.prefix("self_dist")
212+
.tempdir()
213+
.unwrap();
214+
let self_dist = self_dist_tmp.path();
215+
216+
create_local_update_server(self_dist, config, version);
217+
218+
f(config);
219+
});
220+
}
221+
192222
pub fn self_update_setup(f: &dyn Fn(&Config, &Path), version: &str) {
193223
setup(Scenario::SimpleV2, &|config| {
194224
// Create a mock self-update server
@@ -198,21 +228,16 @@ pub fn self_update_setup(f: &dyn Fn(&Config, &Path), version: &str) {
198228
.unwrap();
199229
let self_dist = self_dist_tmp.path();
200230

231+
create_local_update_server(self_dist, config, version);
232+
201233
let trip = this_host_triple();
202234
let dist_dir = self_dist.join(&format!("archive/{}/{}", version, trip));
203235
let dist_exe = dist_dir.join(&format!("rustup-init{}", EXE_SUFFIX));
204-
let rustup_bin = config.exedir.join(&format!("rustup-init{}", EXE_SUFFIX));
205236

206-
fs::create_dir_all(dist_dir).unwrap();
207-
output_release_file(self_dist, "1", version);
208-
fs::copy(&rustup_bin, &dist_exe).unwrap();
209237
// Modify the exe so it hashes different
210238
raw::append_file(&dist_exe, "").unwrap();
211239

212-
let root_url = format!("file://{}", self_dist.display());
213-
config.rustup_update_root = Some(root_url);
214-
215-
f(config, self_dist);
240+
f(config, &self_dist);
216241
});
217242
}
218243

0 commit comments

Comments
 (0)