1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

refactor: clean up cli/file_fetcher.rs (#12772)

This commit is contained in:
Ryan Dahl 2021-11-15 19:59:19 -05:00 committed by GitHub
parent cd9193f126
commit 01644488ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,7 +35,6 @@ use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
static DENO_AUTH_TOKENS: &str = "DENO_AUTH_TOKENS";
pub const SUPPORTED_SCHEMES: [&str; 5] = pub const SUPPORTED_SCHEMES: [&str; 5] =
["data", "blob", "file", "http", "https"]; ["data", "blob", "file", "http", "https"];
@ -247,7 +246,7 @@ impl FileFetcher {
unsafely_ignore_certificate_errors: Option<Vec<String>>, unsafely_ignore_certificate_errors: Option<Vec<String>>,
) -> Result<Self, AnyError> { ) -> Result<Self, AnyError> {
Ok(Self { Ok(Self {
auth_tokens: AuthTokens::new(env::var(DENO_AUTH_TOKENS).ok()), auth_tokens: AuthTokens::new(env::var("DENO_AUTH_TOKENS").ok()),
allow_remote, allow_remote,
cache: Default::default(), cache: Default::default(),
cache_setting, cache_setting,
@ -639,9 +638,8 @@ mod tests {
cache_setting: CacheSetting, cache_setting: CacheSetting,
maybe_temp_dir: Option<Rc<TempDir>>, maybe_temp_dir: Option<Rc<TempDir>>,
) -> (FileFetcher, Rc<TempDir>, BlobStore) { ) -> (FileFetcher, Rc<TempDir>, BlobStore) {
let temp_dir = maybe_temp_dir.unwrap_or_else(|| { let temp_dir =
Rc::new(TempDir::new().expect("failed to create temp directory")) maybe_temp_dir.unwrap_or_else(|| Rc::new(TempDir::new().unwrap()));
});
let location = temp_dir.path().join("deps"); let location = temp_dir.path().join("deps");
let blob_store = BlobStore::default(); let blob_store = BlobStore::default();
let file_fetcher = FileFetcher::new( let file_fetcher = FileFetcher::new(
@ -652,7 +650,7 @@ mod tests {
blob_store.clone(), blob_store.clone(),
None, None,
) )
.expect("setup failed"); .unwrap();
(file_fetcher, temp_dir, blob_store) (file_fetcher, temp_dir, blob_store)
} }
@ -1097,7 +1095,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("setup failed"); .unwrap();
let result = file_fetcher let result = file_fetcher
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await; .await;
@ -1113,10 +1111,8 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_fetch_uses_cache() { async fn test_fetch_uses_cache() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let temp_dir = TempDir::new() let temp_dir = TempDir::new().unwrap();
.expect("could not create temp dir") let location = temp_dir.path().join("deps");
.into_path();
let location = temp_dir.join("deps");
let file_fetcher_01 = FileFetcher::new( let file_fetcher_01 = FileFetcher::new(
HttpCache::new(&location), HttpCache::new(&location),
CacheSetting::Use, CacheSetting::Use,
@ -1125,7 +1121,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let specifier = let specifier =
resolve_url("http://localhost:4545/subdir/mismatch_ext.ts").unwrap(); resolve_url("http://localhost:4545/subdir/mismatch_ext.ts").unwrap();
let cache_filename = file_fetcher_01 let cache_filename = file_fetcher_01
@ -1140,8 +1136,7 @@ mod tests {
let metadata_filename = let metadata_filename =
crate::http_cache::Metadata::filename(&cache_filename); crate::http_cache::Metadata::filename(&cache_filename);
let metadata_file = let metadata_file = fs::File::open(metadata_filename).unwrap();
fs::File::open(metadata_filename).expect("could not open metadata file");
let metadata_file_metadata = metadata_file.metadata().unwrap(); let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap(); let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
@ -1153,7 +1148,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let result = file_fetcher_02 let result = file_fetcher_02
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await; .await;
@ -1161,15 +1156,11 @@ mod tests {
let metadata_filename = let metadata_filename =
crate::http_cache::Metadata::filename(&cache_filename); crate::http_cache::Metadata::filename(&cache_filename);
let metadata_file = let metadata_file = fs::File::open(metadata_filename).unwrap();
fs::File::open(metadata_filename).expect("could not open metadata file");
let metadata_file_metadata = metadata_file.metadata().unwrap(); let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap(); let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();
assert_eq!(metadata_file_modified_01, metadata_file_modified_02); assert_eq!(metadata_file_modified_01, metadata_file_modified_02);
// because we converted to a "fixed" directory, we need to cleanup after
// ourselves.
let _ = fs::remove_dir_all(temp_dir);
} }
#[tokio::test] #[tokio::test]
@ -1203,10 +1194,7 @@ mod tests {
"", "",
"redirected files should have empty cached contents" "redirected files should have empty cached contents"
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher.http_cache.get(&specifier).unwrap();
.http_cache
.get(&specifier)
.expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
"http://localhost:4545/subdir/redirects/redirect1.js" "http://localhost:4545/subdir/redirects/redirect1.js"
@ -1216,10 +1204,8 @@ mod tests {
fs::read_to_string(redirected_cached_filename).unwrap(), fs::read_to_string(redirected_cached_filename).unwrap(),
"export const redirect = 1;\n" "export const redirect = 1;\n"
); );
let (_, headers) = file_fetcher let (_, headers) =
.http_cache file_fetcher.http_cache.get(&redirected_specifier).unwrap();
.get(&redirected_specifier)
.expect("could not get file");
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
@ -1261,10 +1247,7 @@ mod tests {
"", "",
"redirected files should have empty cached contents" "redirected files should have empty cached contents"
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher.http_cache.get(&specifier).unwrap();
.http_cache
.get(&specifier)
.expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
"http://localhost:4546/subdir/redirects/redirect1.js" "http://localhost:4546/subdir/redirects/redirect1.js"
@ -1278,7 +1261,7 @@ mod tests {
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(&redirected_01_specifier) .get(&redirected_01_specifier)
.expect("could not get file"); .unwrap();
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
"http://localhost:4545/subdir/redirects/redirect1.js" "http://localhost:4545/subdir/redirects/redirect1.js"
@ -1291,17 +1274,15 @@ mod tests {
let (_, headers) = file_fetcher let (_, headers) = file_fetcher
.http_cache .http_cache
.get(&redirected_02_specifier) .get(&redirected_02_specifier)
.expect("could not get file"); .unwrap();
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
#[tokio::test] #[tokio::test]
async fn test_fetch_uses_cache_with_redirects() { async fn test_fetch_uses_cache_with_redirects() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let temp_dir = TempDir::new() let temp_dir = TempDir::new().unwrap();
.expect("could not create temp dir") let location = temp_dir.path().join("deps");
.into_path();
let location = temp_dir.join("deps");
let file_fetcher_01 = FileFetcher::new( let file_fetcher_01 = FileFetcher::new(
HttpCache::new(&location), HttpCache::new(&location),
CacheSetting::Use, CacheSetting::Use,
@ -1310,7 +1291,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let specifier = let specifier =
resolve_url("http://localhost:4548/subdir/mismatch_ext.ts").unwrap(); resolve_url("http://localhost:4548/subdir/mismatch_ext.ts").unwrap();
let redirected_specifier = let redirected_specifier =
@ -1327,8 +1308,7 @@ mod tests {
let metadata_filename = let metadata_filename =
crate::http_cache::Metadata::filename(&redirected_cache_filename); crate::http_cache::Metadata::filename(&redirected_cache_filename);
let metadata_file = let metadata_file = fs::File::open(metadata_filename).unwrap();
fs::File::open(metadata_filename).expect("could not open metadata file");
let metadata_file_metadata = metadata_file.metadata().unwrap(); let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap(); let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
@ -1340,7 +1320,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let result = file_fetcher_02 let result = file_fetcher_02
.fetch(&redirected_specifier, &mut Permissions::allow_all()) .fetch(&redirected_specifier, &mut Permissions::allow_all())
.await; .await;
@ -1348,15 +1328,11 @@ mod tests {
let metadata_filename = let metadata_filename =
crate::http_cache::Metadata::filename(&redirected_cache_filename); crate::http_cache::Metadata::filename(&redirected_cache_filename);
let metadata_file = let metadata_file = fs::File::open(metadata_filename).unwrap();
fs::File::open(metadata_filename).expect("could not open metadata file");
let metadata_file_metadata = metadata_file.metadata().unwrap(); let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap(); let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();
assert_eq!(metadata_file_modified_01, metadata_file_modified_02); assert_eq!(metadata_file_modified_01, metadata_file_modified_02);
// because we converted to a "fixed" directory, we need to cleanup after
// ourselves.
let _ = fs::remove_dir_all(temp_dir);
} }
#[tokio::test] #[tokio::test]
@ -1416,10 +1392,7 @@ mod tests {
"", "",
"redirected files should have empty cached contents" "redirected files should have empty cached contents"
); );
let (_, headers) = file_fetcher let (_, headers) = file_fetcher.http_cache.get(&specifier).unwrap();
.http_cache
.get(&specifier)
.expect("could not get file");
assert_eq!( assert_eq!(
headers.get("location").unwrap(), headers.get("location").unwrap(),
"/subdir/redirects/redirect1.js" "/subdir/redirects/redirect1.js"
@ -1429,17 +1402,15 @@ mod tests {
fs::read_to_string(redirected_cached_filename).unwrap(), fs::read_to_string(redirected_cached_filename).unwrap(),
"export const redirect = 1;\n" "export const redirect = 1;\n"
); );
let (_, headers) = file_fetcher let (_, headers) =
.http_cache file_fetcher.http_cache.get(&redirected_specifier).unwrap();
.get(&redirected_specifier)
.expect("could not get file");
assert!(headers.get("location").is_none()); assert!(headers.get("location").is_none());
} }
#[tokio::test] #[tokio::test]
async fn test_fetch_no_remote() { async fn test_fetch_no_remote() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let temp_dir = TempDir::new().expect("could not create temp dir"); let temp_dir = TempDir::new().unwrap();
let location = temp_dir.path().join("deps"); let location = temp_dir.path().join("deps");
let file_fetcher = FileFetcher::new( let file_fetcher = FileFetcher::new(
HttpCache::new(&location), HttpCache::new(&location),
@ -1449,7 +1420,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap(); let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap();
let result = file_fetcher let result = file_fetcher
@ -1464,10 +1435,8 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_fetch_cache_only() { async fn test_fetch_cache_only() {
let _http_server_guard = test_util::http_server(); let _http_server_guard = test_util::http_server();
let temp_dir = TempDir::new() let temp_dir = TempDir::new().unwrap();
.expect("could not create temp dir") let location = temp_dir.path().join("deps");
.into_path();
let location = temp_dir.join("deps");
let file_fetcher_01 = FileFetcher::new( let file_fetcher_01 = FileFetcher::new(
HttpCache::new(&location), HttpCache::new(&location),
CacheSetting::Only, CacheSetting::Only,
@ -1476,7 +1445,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let file_fetcher_02 = FileFetcher::new( let file_fetcher_02 = FileFetcher::new(
HttpCache::new(&location), HttpCache::new(&location),
CacheSetting::Use, CacheSetting::Use,
@ -1485,7 +1454,7 @@ mod tests {
BlobStore::default(), BlobStore::default(),
None, None,
) )
.expect("could not create file fetcher"); .unwrap();
let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap(); let specifier = resolve_url("http://localhost:4545/002_hello.ts").unwrap();
let result = file_fetcher_01 let result = file_fetcher_01
@ -1505,10 +1474,6 @@ mod tests {
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
// because we converted to a "fixed" directory, we need to cleanup after
// ourselves.
let _ = fs::remove_dir_all(temp_dir);
} }
#[tokio::test] #[tokio::test]
@ -1517,8 +1482,7 @@ mod tests {
let fixture_path = temp_dir.path().join("mod.ts"); let fixture_path = temp_dir.path().join("mod.ts");
let specifier = let specifier =
resolve_url_or_path(&fixture_path.to_string_lossy()).unwrap(); resolve_url_or_path(&fixture_path.to_string_lossy()).unwrap();
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#) fs::write(fixture_path.clone(), r#"console.log("hello deno");"#).unwrap();
.expect("could not write file");
let result = file_fetcher let result = file_fetcher
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await; .await;
@ -1526,8 +1490,7 @@ mod tests {
let file = result.unwrap(); let file = result.unwrap();
assert_eq!(file.source.as_str(), r#"console.log("hello deno");"#); assert_eq!(file.source.as_str(), r#"console.log("hello deno");"#);
fs::write(fixture_path, r#"console.log("goodbye deno");"#) fs::write(fixture_path, r#"console.log("goodbye deno");"#).unwrap();
.expect("could not write file");
let result = file_fetcher let result = file_fetcher
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await; .await;