feat: Add two more rust tests (#82)
This commit is contained in:
Vendored
+2
-1
@@ -13,6 +13,7 @@
|
||||
"reqwest",
|
||||
"rollouts",
|
||||
"rustup",
|
||||
"struct"
|
||||
"struct",
|
||||
"tempdir"
|
||||
]
|
||||
}
|
||||
@@ -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<PatchInfo> {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -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()
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user