summaryrefslogtreecommitdiff
path: root/cli/cache/disk_cache.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/cache/disk_cache.rs')
-rw-r--r--cli/cache/disk_cache.rs63
1 files changed, 7 insertions, 56 deletions
diff --git a/cli/cache/disk_cache.rs b/cli/cache/disk_cache.rs
index 456b59912..6950c056a 100644
--- a/cli/cache/disk_cache.rs
+++ b/cli/cache/disk_cache.rs
@@ -8,7 +8,6 @@ use deno_core::url::Host;
use deno_core::url::Url;
use std::ffi::OsStr;
use std::fs;
-use std::io;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
@@ -20,13 +19,6 @@ pub struct DiskCache {
pub location: PathBuf,
}
-fn with_io_context<T: AsRef<str>>(
- e: &std::io::Error,
- context: T,
-) -> std::io::Error {
- std::io::Error::new(e.kind(), format!("{} (for '{}')", e, context.as_ref()))
-}
-
impl DiskCache {
/// `location` must be an absolute path.
pub fn new(location: &Path) -> Self {
@@ -36,27 +28,6 @@ impl DiskCache {
}
}
- /// Ensures the location of the cache.
- pub fn ensure_dir_exists(&self, path: &Path) -> io::Result<()> {
- if path.is_dir() {
- return Ok(());
- }
- fs::create_dir_all(path).map_err(|e| {
- io::Error::new(
- e.kind(),
- format!(
- concat!(
- "Could not create TypeScript compiler cache location: {}\n",
- "Check the permission of the directory.\n",
- "{:#}",
- ),
- path.display(),
- e
- ),
- )
- })
- }
-
fn get_cache_filename(&self, url: &Url) -> Option<PathBuf> {
let mut out = PathBuf::new();
@@ -78,7 +49,7 @@ impl DiskCache {
out.push(path_seg);
}
}
- "http" | "https" | "data" | "blob" => out = url_to_filename(url)?,
+ "http" | "https" | "data" | "blob" => out = url_to_filename(url).ok()?,
"file" => {
let path = match url.to_file_path() {
Ok(path) => path,
@@ -149,12 +120,7 @@ impl DiskCache {
pub fn set(&self, filename: &Path, data: &[u8]) -> std::io::Result<()> {
let path = self.location.join(filename);
- match path.parent() {
- Some(parent) => self.ensure_dir_exists(parent),
- None => Ok(()),
- }?;
atomic_write_file(&path, data, CACHE_PERM)
- .map_err(|e| with_io_context(&e, format!("{:#?}", &path)))
}
}
@@ -164,28 +130,13 @@ mod tests {
use test_util::TempDir;
#[test]
- fn test_create_cache_if_dir_exits() {
- let cache_location = TempDir::new();
- let cache_path = cache_location.path().join("foo");
- let cache = DiskCache::new(cache_path.as_path());
- cache
- .ensure_dir_exists(&cache.location)
- .expect("Testing expect:");
- assert!(cache_path.is_dir());
- }
-
- #[test]
- fn test_create_cache_if_dir_not_exits() {
+ fn test_set_get_cache_file() {
let temp_dir = TempDir::new();
- let cache_location = temp_dir.path();
- cache_location.remove_dir_all();
- let cache_location = cache_location.join("foo");
- assert!(!cache_location.is_dir());
- let cache = DiskCache::new(cache_location.as_path());
- cache
- .ensure_dir_exists(&cache.location)
- .expect("Testing expect:");
- assert!(cache_location.is_dir());
+ let sub_dir = temp_dir.path().join("sub_dir");
+ let cache = DiskCache::new(&sub_dir.to_path_buf());
+ let path = PathBuf::from("foo/bar.txt");
+ cache.set(&path, b"hello").unwrap();
+ assert_eq!(cache.get(&path).unwrap(), b"hello");
}
#[test]