fix(upgrade): don't prompt if current version has changed (#16542)

fix https://github.com/denoland/deno/issues/16539
fix https://github.com/denoland/deno/issues/16537
This commit is contained in:
sigmaSd 2022-11-06 00:06:58 +01:00 committed by GitHub
parent 1410e4adea
commit 384e53308a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -119,6 +119,14 @@ impl<TEnvironment: UpdateCheckerEnvironment> UpdateChecker<TEnvironment> {
/// Returns the version if a new one is available and it should be prompted about.
pub fn should_prompt(&self) -> Option<String> {
let file = self.maybe_file.as_ref()?;
// If the current version saved is not the actualy current version of the binary
// It means
// - We already check for a new vesion today
// - The user have probably upgraded today
// So we should not prompt and wait for tomorrow for the latest version to be updated again
if file.current_version != self.env.current_version() {
return None;
}
if file.latest_version == self.env.current_version() {
return None;
}
@ -220,6 +228,7 @@ async fn fetch_and_store_latest_version<
.current_time()
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
last_checked: env.current_time(),
current_version: env.current_version().to_string(),
latest_version,
}
.serialize(),
@ -544,6 +553,7 @@ fn check_exe(exe_path: &Path) -> Result<(), AnyError> {
struct CheckVersionFile {
pub last_prompt: chrono::DateTime<chrono::Utc>,
pub last_checked: chrono::DateTime<chrono::Utc>,
pub current_version: String,
pub latest_version: String,
}
@ -551,7 +561,7 @@ impl CheckVersionFile {
pub fn parse(content: String) -> Option<Self> {
let split_content = content.split('!').collect::<Vec<_>>();
if split_content.len() != 3 {
if split_content.len() != 4 {
return None;
}
@ -559,6 +569,10 @@ impl CheckVersionFile {
if latest_version.is_empty() {
return None;
}
let current_version = split_content[3].trim().to_owned();
if current_version.is_empty() {
return None;
}
let last_prompt = chrono::DateTime::parse_from_rfc3339(split_content[0])
.map(|dt| dt.with_timezone(&chrono::Utc))
@ -570,16 +584,18 @@ impl CheckVersionFile {
Some(CheckVersionFile {
last_prompt,
last_checked,
current_version,
latest_version,
})
}
fn serialize(&self) -> String {
format!(
"{}!{}!{}",
"{}!{}!{}!{}",
self.last_prompt.to_rfc3339(),
self.last_checked.to_rfc3339(),
self.latest_version
self.latest_version,
self.current_version,
)
}
@ -602,7 +618,8 @@ mod test {
#[test]
fn test_parse_upgrade_check_file() {
let file = CheckVersionFile::parse(
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3".to_string(),
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3!1.2.2"
.to_string(),
)
.unwrap();
assert_eq!(
@ -614,6 +631,7 @@ mod test {
"2020-01-01T00:00:00+00:00".to_string()
);
assert_eq!(file.latest_version, "1.2.3".to_string());
assert_eq!(file.current_version, "1.2.2".to_string());
let result =
CheckVersionFile::parse("2020-01-01T00:00:00+00:00!".to_string());
@ -638,10 +656,11 @@ mod test {
.unwrap()
.with_timezone(&chrono::Utc),
latest_version: "1.2.3".to_string(),
current_version: "1.2.2".to_string(),
};
assert_eq!(
file.serialize(),
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3"
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3!1.2.2"
);
}
@ -792,6 +811,7 @@ mod test {
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
last_checked: env.current_time(),
latest_version: "1.26.2".to_string(),
current_version: "1.27.0".to_string(),
}
.serialize();
env.write_check_file(&file_content);
@ -803,4 +823,23 @@ mod test {
// propagation might be delated) we should not prompt
assert_eq!(checker.should_prompt(), None);
}
#[tokio::test]
async fn test_should_not_prompt_if_current_cli_version_has_changed() {
let env = TestUpdateCheckerEnvironment::new();
let file_content = CheckVersionFile {
last_prompt: env
.current_time()
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
last_checked: env.current_time(),
latest_version: "1.26.2".to_string(),
current_version: "1.25.0".to_string(),
}
.serialize();
env.write_check_file(&file_content);
// simulate an upgrade done to a canary version
env.set_current_version("61fbfabe440f1cfffa7b8d17426ffdece4d430d0");
let checker = UpdateChecker::new(env);
assert_eq!(checker.should_prompt(), None);
}
}