From b017409dcdbcaa5d3522f0efb191522240a6e757 Mon Sep 17 00:00:00 2001 From: Lorran Rosa Date: Fri, 3 Apr 2020 23:43:49 -0300 Subject: on init create disk_cache directory if it doesn't already exists (#4617) --- cli/disk_cache.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'cli') diff --git a/cli/disk_cache.rs b/cli/disk_cache.rs index bfef8ab64..51c9d8612 100644 --- a/cli/disk_cache.rs +++ b/cli/disk_cache.rs @@ -22,7 +22,9 @@ fn with_io_context>( impl DiskCache { pub fn new(location: &Path) -> Self { - // TODO: ensure that 'location' is a directory + if !&location.is_dir() { + fs::create_dir_all(&location).ok(); + } Self { location: location.to_owned(), } @@ -131,6 +133,28 @@ impl DiskCache { #[cfg(test)] mod tests { use super::*; + use tempfile::TempDir; + + #[test] + fn test_create_cache_if_dir_exits() { + let cache_location = TempDir::new().unwrap(); + let mut cache_path = cache_location.path().to_owned(); + cache_path.push("foo"); + DiskCache::new(&cache_path); + assert!(cache_path.is_dir()); + } + + #[test] + fn test_create_cache_if_dir_not_exits() { + let cache_location = if cfg!(target_os = "windows") { + PathBuf::from(r"C:\deno_dir\foo") + } else { + PathBuf::from("~/deno_dir/foo") + }; + assert_eq!(cache_location.is_dir(), false); + DiskCache::new(&cache_location); + assert_eq!(cache_location.is_dir(), true); + } #[test] fn test_get_cache_filename() { -- cgit v1.2.3