Skip to content

Commit 4e0fe3f

Browse files
committed
Don't access the Internet in the check_updates tests
Previously, three of the tests in `cli-exact.rs` would try to connect to the Internet, or fail if the Internet wasn't available: * `check_updates_with_update` * `check_updates_none` * `check_updates_some` This is because all three tests would run `rustup check`, and because the tests weren't overriding the value of `RUSTUP_UPDATE_ROOT`, they'd go out to the Internet to try to download self-updates. The new helper mimics the behaviour of the `self_update_setup()` helper in the same file. It creates a mock self-update server in a local directory, and points rustup to the directory using a `file://` URL. When it tries to get self-updates as part of `rustup check`, it only fetches from that directory.
1 parent 90c8401 commit 4e0fe3f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ pub fn setup(s: Scenario, f: &dyn Fn(&mut Config)) {
189189
assert!(!PathBuf::from("./bogus-cargo-home").exists());
190190
}
191191

192+
pub fn check_update_setup(f: &dyn Fn(&mut Config)) {
193+
let version = env!("CARGO_PKG_VERSION");
194+
195+
setup(Scenario::ArchivesV2, &|config| {
196+
// Create a mock self-update server
197+
let self_dist_tmp = tempfile::Builder::new()
198+
.prefix("self_dist")
199+
.tempdir()
200+
.unwrap();
201+
let self_dist = self_dist_tmp.path();
202+
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);
214+
215+
f(config);
216+
});
217+
}
218+
192219
pub fn self_update_setup(f: &dyn Fn(&Config, &Path), version: &str) {
193220
setup(Scenario::SimpleV2, &|config| {
194221
// Create a mock self-update server

0 commit comments

Comments
 (0)