Skip to content

Commit

Permalink
chore: add upgrade prompt integration test (denoland#21273)
Browse files Browse the repository at this point in the history
1. Adds an upgrade prompt integration test.
1. Adds a test for when the upgrade check takes a long time in the repl.
  • Loading branch information
dsherret authored Nov 23, 2023
1 parent bf42467 commit eda3850
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
61 changes: 61 additions & 0 deletions cli/tests/integration/upgrade_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use std::process::Command;
use std::process::Stdio;
use std::time::Instant;
use test_util as util;
use test_util::TempDir;
use util::TestContextBuilder;

// Warning: this test requires internet access.
// TODO(#7412): reenable. test is flaky
Expand Down Expand Up @@ -192,3 +194,62 @@ fn upgrade_invalid_canary_version() {
util::strip_ansi_codes(&String::from_utf8(output.stderr).unwrap())
);
}

#[test]
fn upgrade_prompt() {
let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.env(
"DENO_DONT_USE_INTERNAL_BASE_UPGRADE_URL",
"https://localhost:4545",
)
.build();
let temp_dir = context.temp_dir();
// start a task that goes indefinitely in order to allow
// the upgrade check to occur
temp_dir.write("main.js", "setInterval(() => {}, 1_000)");
let cmd = context
.new_command()
.args("run --log-level=debug main.js")
.env_remove("DENO_NO_UPDATE_CHECK");
// run once and wait for the version to be stored
cmd.with_pty(|mut pty| {
pty.expect("Finished upgrade checker.");
});
// now check that the upgrade prompt is shown the next time this is run
temp_dir.write("main.js", "");
cmd.with_pty(|mut pty| {
// - We need to use a pty here because the upgrade prompt
// doesn't occur except when there's a pty.
// - Version comes from the test server.
pty.expect(" 99999.99.99 Run `deno upgrade` to install it.");
});
}

#[test]
fn upgrade_lsp_repl_sleeps() {
let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.env(
"DENO_DONT_USE_INTERNAL_BASE_UPGRADE_URL",
"https://localhost:4545/upgrade/sleep",
)
.build();
let start_instant = Instant::now();
// ensure this works even though the upgrade check is taking
// a long time to complete
context
.new_command()
.args("repl")
.env_remove("DENO_NO_UPDATE_CHECK")
.with_pty(|mut pty| {
pty.write_line("123 + 456\n");
pty.expect("579");
});

// the test server will sleep for 45 seconds, so ensure this is less
let elapsed_secs = start_instant.elapsed().as_secs();
assert!(elapsed_secs < 30, "elapsed_secs: {}", elapsed_secs);
}
5 changes: 4 additions & 1 deletion cli/tools/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ pub fn check_for_upgrades(
tokio::time::sleep(UPGRADE_CHECK_FETCH_DELAY).await;

fetch_and_store_latest_version(&env, &version_provider).await;

// text is used by the test suite
log::debug!("Finished upgrade checker.")
});
}

Expand Down Expand Up @@ -597,7 +600,7 @@ fn get_url(
}

fn base_upgrade_url() -> Cow<'static, str> {
// this is used to get the current version
// this is used by the test suite
if let Ok(url) = env::var("DENO_DONT_USE_INTERNAL_BASE_UPGRADE_URL") {
Cow::Owned(url)
} else {
Expand Down
3 changes: 3 additions & 0 deletions test_util/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ impl TestCommandBuilder {
if !envs.contains_key("NPM_CONFIG_REGISTRY") {
envs.insert("NPM_CONFIG_REGISTRY".to_string(), npm_registry_unset_url());
}
if !envs.contains_key("DENO_NO_UPDATE_CHECK") {
envs.insert("DENO_NO_UPDATE_CHECK".to_string(), "1".to_string());
}
for key in &self.envs_remove {
envs.remove(key);
}
Expand Down
18 changes: 18 additions & 0 deletions test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,24 @@ async fn main_server(
.unwrap(),
)
}
(&hyper::Method::GET, "/upgrade/sleep/release-latest.txt") => {
tokio::time::sleep(Duration::from_secs(45)).await;
return Ok(
Response::builder()
.status(StatusCode::OK)
.body(Body::from("99999.99.99"))
.unwrap(),
);
}
(&hyper::Method::GET, "/release-latest.txt") => {
return Ok(
Response::builder()
.status(StatusCode::OK)
// use a deno version that will never happen
.body(Body::from("99999.99.99"))
.unwrap(),
);
}
_ => {
let mut file_path = testdata_path().to_path_buf();
file_path.push(&req.uri().path()[1..].replace("%2f", "/"));
Expand Down

0 comments on commit eda3850

Please sign in to comment.