From bf47a0fa4311adf7e78fba7329a81ba0951861ed Mon Sep 17 00:00:00 2001 From: nicholaslyang Date: Mon, 10 Jul 2023 16:09:50 -0400 Subject: [PATCH] Updating with CacheResponse --- crates/turborepo-cache/src/fs.rs | 33 +++++++++++++++--------------- crates/turborepo-cache/src/http.rs | 2 +- crates/turborepo-cache/src/lib.rs | 18 +++------------- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/crates/turborepo-cache/src/fs.rs b/crates/turborepo-cache/src/fs.rs index e9bb3a189bbdf..f8610c14f1a45 100644 --- a/crates/turborepo-cache/src/fs.rs +++ b/crates/turborepo-cache/src/fs.rs @@ -1,14 +1,11 @@ -use std::{ - backtrace::Backtrace, - fs::{metadata, OpenOptions}, -}; +use std::{backtrace::Backtrace, fs::OpenOptions}; use serde::{Deserialize, Serialize}; use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf, AnchoredSystemPathBuf}; use crate::{ cache_archive::{CacheReader, CacheWriter}, - CacheError, CacheSource, ItemStatus, + CacheError, CacheResponse, CacheSource, }; struct FSCache { @@ -54,7 +51,7 @@ impl FSCache { &self, anchor: &AbsoluteSystemPath, hash: &str, - ) -> Result<(ItemStatus, Vec), CacheError> { + ) -> Result<(CacheResponse, Vec), CacheError> { let uncompressed_cache_path = self .cache_directory .join_component(&format!("{}.tar", hash)); @@ -67,7 +64,7 @@ impl FSCache { } else if compressed_cache_path.exists() { compressed_cache_path } else { - return Ok((ItemStatus::Miss, vec![])); + return Err(CacheError::CacheMiss); }; let mut cache_reader = CacheReader::open(&cache_path)?; @@ -81,7 +78,7 @@ impl FSCache { )?; Ok(( - ItemStatus::Hit { + CacheResponse { time_saved: meta.duration, source: CacheSource::Local, }, @@ -89,7 +86,7 @@ impl FSCache { )) } - fn exists(&self, hash: &str) -> Result { + fn exists(&self, hash: &str) -> Result { let uncompressed_cache_path = self .cache_directory .join_component(&format!("{}.tar", hash)); @@ -98,7 +95,7 @@ impl FSCache { .join_component(&format!("{}.tar.zst", hash)); if !uncompressed_cache_path.exists() && !compressed_cache_path.exists() { - return Ok(ItemStatus::Miss); + return Err(CacheError::CacheMiss); } let duration = CacheMetadata::read( @@ -109,7 +106,7 @@ impl FSCache { .map(|meta| meta.duration) .unwrap_or(0); - Ok(ItemStatus::Hit { + Ok(CacheResponse { time_saved: duration, source: CacheSource::Local, }) @@ -144,7 +141,7 @@ impl FSCache { let mut metadata_options = OpenOptions::new(); metadata_options.create(true).write(true); - let mut metadata_file = metadata_path.open_with_options(metadata_options)?; + let metadata_file = metadata_path.open_with_options(metadata_options)?; serde_json::to_writer(metadata_file, &meta) .map_err(|e| CacheError::InvalidMetadata(e, Backtrace::capture()))?; @@ -155,6 +152,8 @@ impl FSCache { #[cfg(test)] mod test { + use std::assert_matches::assert_matches; + use anyhow::Result; use futures::future::try_join_all; use tempfile::tempdir; @@ -176,8 +175,10 @@ mod test { let cache = FSCache::new(None, &repo_root_path)?; - let expected_miss = cache.exists(&test_case.hash)?; - assert_eq!(expected_miss, ItemStatus::Miss); + let expected_miss = cache + .exists(&test_case.hash) + .expect_err("Expected cache miss"); + assert_matches!(expected_miss, CacheError::CacheMiss); cache.put( repo_root_path, @@ -189,7 +190,7 @@ mod test { let expected_hit = cache.exists(&test_case.hash)?; assert_eq!( expected_hit, - ItemStatus::Hit { + CacheResponse { time_saved: test_case.duration, source: CacheSource::Local } @@ -198,7 +199,7 @@ mod test { let (status, files) = cache.fetch(&repo_root_path, &test_case.hash)?; assert_eq!( status, - ItemStatus::Hit { + CacheResponse { time_saved: test_case.duration, source: CacheSource::Local } diff --git a/crates/turborepo-cache/src/http.rs b/crates/turborepo-cache/src/http.rs index eaed9ae477b24..3f9c474f328b6 100644 --- a/crates/turborepo-cache/src/http.rs +++ b/crates/turborepo-cache/src/http.rs @@ -180,8 +180,8 @@ mod test { use crate::{ http::HttpCache, - CacheSource test_cases::{get_test_cases, TestCase}, + CacheSource, }; #[tokio::test] diff --git a/crates/turborepo-cache/src/lib.rs b/crates/turborepo-cache/src/lib.rs index 925a1b18dfb2b..f8c5724380204 100644 --- a/crates/turborepo-cache/src/lib.rs +++ b/crates/turborepo-cache/src/lib.rs @@ -1,5 +1,6 @@ #![feature(error_generic_member_access)] #![feature(provide_any)] +#![feature(assert_matches)] pub mod cache_archive; pub mod fs; @@ -57,21 +58,8 @@ pub enum CacheError { InvalidMetadata(serde_json::Error, #[backtrace] Backtrace), #[error("Failed to write cache metadata file")] MetadataWriteFailure(serde_json::Error, #[backtrace] Backtrace), -} - -#[derive(Debug, Clone, PartialEq)] -enum CacheSource { - Local, - Remote, -} - -#[derive(Debug, Clone, PartialEq)] -enum ItemStatus { - Hit { - source: CacheSource, - time_saved: u32, - }, - Miss, + #[error("Cache miss")] + CacheMiss, } #[derive(Debug, Clone, PartialEq)]