From ff58de28440be4629b2fc7264fa7e735c277675c Mon Sep 17 00:00:00 2001 From: Matt Parker Date: Sat, 23 Mar 2024 00:55:29 -0400 Subject: [PATCH] fix: graceful error handling for `grit check` in uninitialized contexts (#61) --- crates/gritcache/src/new_cache.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/gritcache/src/new_cache.rs b/crates/gritcache/src/new_cache.rs index 0b7b60a61..1217f6a3b 100644 --- a/crates/gritcache/src/new_cache.rs +++ b/crates/gritcache/src/new_cache.rs @@ -39,8 +39,8 @@ impl ThreadedCache { }; let (sender, receiver) = mpsc::channel::(); + let mut writer = Self::new_writer(&mismatches_path)?; let manager = thread::spawn(move || { - let mut writer = Self::new_writer(&mismatches_path).unwrap(); while let Ok(key) = receiver.recv() { writer.write_all(&key).unwrap(); } @@ -105,7 +105,8 @@ impl ThreadedCache { .create(true) .append(true) .open(path) - .context("Failed to open cache file".to_string())?, + .context("Failed to open cache file".to_string()) + .context("Please run `grit init` or set GRIT_CACHE_DIR to cache check results")?, ); Ok(writer) @@ -172,6 +173,7 @@ mod tests { let path = PathBuf::from("."); let mismatches_cache_path = path.join(MISMATCHES_CACHE_NAME); + let bad_path = PathBuf::from("./doesnotexist").join(MISMATCHES_CACHE_NAME); println!( "mismatches_cache_path: {}", @@ -183,6 +185,9 @@ mod tests { std::fs::remove_file(&mismatches_cache_path)?; } + // assert cache creation fails gracefully on invalid paths + assert!(ThreadedCache::new(bad_path.clone(), false).await.is_err()); + // Create an empty cache let (cache, manager) = ThreadedCache::new(path.clone(), true).await?;