From 534f2b637678c22a8d8ab6cf1118950d488fea4a Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Thu, 16 Mar 2023 11:18:30 -0700 Subject: [PATCH] feat: Add two more rust tests (#82) --- .vscode/settings.json | 3 ++- updater/library/src/cache.rs | 19 +++++++++++++++++-- updater/library/src/network.rs | 4 +++- updater/library/src/updater.rs | 20 ++++++++++++++------ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 887ac650..5756759a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ "reqwest", "rollouts", "rustup", - "struct" + "struct", + "tempdir" ] } \ No newline at end of file diff --git a/updater/library/src/cache.rs b/updater/library/src/cache.rs index 3f3ba521..cd5271ba 100644 --- a/updater/library/src/cache.rs +++ b/updater/library/src/cache.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; use crate::network::{download_file_to_path, PatchCheckResponse}; +#[derive(PartialEq, Debug)] pub struct PatchInfo { pub path: String, pub version: String, @@ -103,8 +104,7 @@ impl UpdaterState { } pub fn current_patch(&self) -> Option { - // If there is no state, return None. - if self.slots.is_empty() { + if self.slots.is_empty() || self.current_slot_index >= self.slots.len() { return None; } let slot = &self.slots[self.current_slot_index]; @@ -187,3 +187,18 @@ fn download_into_slot( return Ok(()); } + +#[cfg(test)] +mod tests { + + #[test] + fn current_patch_does_not_crash() { + let mut state = super::UpdaterState::default(); + assert_eq!(state.current_patch(), None); + state.current_slot_index = 3; + assert_eq!(state.current_patch(), None); + state.slots.push(super::Slot::default()); + // This used to crash, where index was bad, but slots were not empty. + assert_eq!(state.current_patch(), None); + } +} diff --git a/updater/library/src/network.rs b/updater/library/src/network.rs index 8322844b..f2d07998 100644 --- a/updater/library/src/network.rs +++ b/updater/library/src/network.rs @@ -63,7 +63,9 @@ pub fn download_file_to_path(url: &str, path: &PathBuf) -> anyhow::Result<()> { let mut bytes = response.bytes()?; // Ensure the download directory exists. - std::fs::create_dir_all(path.parent().unwrap())?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } let mut file = File::create(path)?; file.write_all(&mut bytes)?; diff --git a/updater/library/src/updater.rs b/updater/library/src/updater.rs index dfb018ef..f816f429 100644 --- a/updater/library/src/updater.rs +++ b/updater/library/src/updater.rs @@ -1,6 +1,5 @@ // This file's job is to be the Rust API for the updater. -use std::fmt; use std::fmt::{Display, Formatter}; use crate::cache::{download_into_unused_slot, PatchInfo, UpdaterState}; @@ -38,7 +37,7 @@ pub enum UpdateError { impl std::error::Error for UpdateError {} impl Display for UpdateError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { match self { UpdateError::InvalidArgument(name, value) => { write!(f, "Invalid Argument: {} -> {}", name, value) @@ -131,13 +130,16 @@ pub fn report_failed_launch() -> Result<(), UpdateError> { }); } -pub fn report_successful_launch() { - with_config(|config| { +pub fn report_successful_launch() -> Result<(), UpdateError> { + return with_config(|config| { let mut state = UpdaterState::load(&config.cache_dir).unwrap_or_default(); - let patch = state.current_patch().unwrap(); + let patch = state + .current_patch() + .ok_or(UpdateError::InvalidState("No current patch".to_string()))?; state.mark_patch_as_good(&patch); state.save(&config.cache_dir).unwrap(); + Ok(()) }); } @@ -197,7 +199,7 @@ mod tests { } #[test] - fn report_failure_with_no_current() { + fn report_launch_result_with_no_current_patch() { init_for_testing(); assert_eq!( crate::report_failed_launch(), @@ -205,5 +207,11 @@ mod tests { "No current patch".to_string() )) ); + assert_eq!( + crate::report_successful_launch(), + Err(crate::UpdateError::InvalidState( + "No current patch".to_string() + )) + ); } }