fix(deno_dir): better error message (#5120)

Add better error messages when a cache subdirectory in 
`DENO_DIR` cannot be created.
This commit is contained in:
Yoshiya Hinosawa 2020-05-07 21:32:57 +09:00 committed by GitHub
parent 761b7efb3b
commit dabe88f854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 15 deletions

View file

@ -31,6 +31,7 @@ impl DenoDir {
root,
gen_cache: DiskCache::new(&gen_path),
};
deno_dir.gen_cache.ensure_location()?;
Ok(deno_dir)
}

View file

@ -1,6 +1,7 @@
use crate::fs as deno_fs;
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
@ -22,14 +23,24 @@ fn with_io_context<T: AsRef<str>>(
impl DiskCache {
pub fn new(location: &Path) -> Self {
if !&location.is_dir() {
fs::create_dir_all(&location).ok();
}
Self {
location: location.to_owned(),
}
}
/// Ensures the location of the cache.
pub fn ensure_location(&self) -> io::Result<()> {
if self.location.is_dir() {
return Ok(());
}
fs::create_dir_all(&self.location).map_err(|e| {
io::Error::new(e.kind(), format!(
"Could not create TypeScript compiler cache location: {:?}\nCheck the permission of the directory.",
self.location
))
})
}
pub fn get_cache_filename(&self, url: &Url) -> PathBuf {
let mut out = PathBuf::new();
@ -140,7 +151,8 @@ mod tests {
let cache_location = TempDir::new().unwrap();
let mut cache_path = cache_location.path().to_owned();
cache_path.push("foo");
DiskCache::new(&cache_path);
let cache = DiskCache::new(&cache_path);
cache.ensure_location().expect("Testing expect:");
assert!(cache_path.is_dir());
}
@ -151,7 +163,8 @@ mod tests {
assert!(fs::remove_dir(&cache_location).is_ok());
cache_location.push("foo");
assert_eq!(cache_location.is_dir(), false);
DiskCache::new(&cache_location);
let cache = DiskCache::new(&cache_location);
cache.ensure_location().expect("Testing expect:");
assert_eq!(cache_location.is_dir(), true);
}

View file

@ -661,7 +661,7 @@ mod tests {
fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher {
SourceFileFetcher::new(
HttpCache::new(&dir_path.to_path_buf().join("deps")).unwrap(),
HttpCache::new(&dir_path.to_path_buf().join("deps")),
true,
vec![],
false,

View file

@ -54,7 +54,8 @@ impl GlobalState {
let custom_root = env::var("DENO_DIR").map(String::into).ok();
let dir = deno_dir::DenoDir::new(custom_root)?;
let deps_cache_location = dir.root.join("deps");
let http_cache = http_cache::HttpCache::new(&deps_cache_location)?;
let http_cache = http_cache::HttpCache::new(&deps_cache_location);
http_cache.ensure_location()?;
let file_fetcher = SourceFileFetcher::new(
http_cache,

View file

@ -11,6 +11,7 @@ use serde::Serialize;
use serde_derive::Deserialize;
use std::fs;
use std::fs::File;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use url::Url;
@ -101,12 +102,26 @@ impl Metadata {
}
impl HttpCache {
/// Returns error if unable to create directory
/// at specified location.
pub fn new(location: &Path) -> Result<Self, ErrBox> {
fs::create_dir_all(&location)?;
Ok(Self {
/// Returns a new instance.
pub fn new(location: &Path) -> Self {
Self {
location: location.to_owned(),
}
}
/// Ensures the location of the cache.
pub fn ensure_location(&self) -> io::Result<()> {
if self.location.is_dir() {
return Ok(());
}
fs::create_dir_all(&self.location).map_err(|e| {
io::Error::new(
e.kind(),
format!(
"Could not create remote modules cache location: {:?}\nCheck the permission of the directory.",
self.location
),
)
})
}
@ -169,15 +184,15 @@ mod tests {
let dir = TempDir::new().unwrap();
let mut cache_path = dir.path().to_owned();
cache_path.push("foobar");
let r = HttpCache::new(&cache_path);
assert!(r.is_ok());
let cache = HttpCache::new(&cache_path);
assert!(cache.ensure_location().is_ok());
assert!(cache_path.is_dir());
}
#[test]
fn test_get_set() {
let dir = TempDir::new().unwrap();
let cache = HttpCache::new(dir.path()).unwrap();
let cache = HttpCache::new(dir.path());
let url = Url::parse("https://deno.land/x/welcome.ts").unwrap();
let mut headers = HashMap::new();
headers.insert(