diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-08-01 20:49:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-02 00:49:09 +0000 |
commit | 1cefa831fd74b14121494045a347024502d74e34 (patch) | |
tree | cc7791cf674e427fe4165262db416e6c537e99a3 /cli/cache/disk_cache.rs | |
parent | 36ae37604a0ddab4349df6eb6fafb8ae39fd20fc (diff) |
feat(unstable): optional `deno_modules` directory (#19977)
Closes #15633
Diffstat (limited to 'cli/cache/disk_cache.rs')
-rw-r--r-- | cli/cache/disk_cache.rs | 63 |
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] |