1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

Use tokio::test for some of cli's unit tests (#3868)

This commit is contained in:
Ryan Dahl 2020-02-03 08:53:50 -05:00 committed by GitHub
parent 55063dd8e8
commit fba40d86c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 495 additions and 594 deletions

View file

@ -851,7 +851,6 @@ impl SourceCodeHeaders {
mod tests { mod tests {
use super::*; use super::*;
use crate::fs as deno_fs; use crate::fs as deno_fs;
use crate::tokio_util;
use tempfile::TempDir; use tempfile::TempDir;
fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher { fn setup_file_fetcher(dir_path: &Path) -> SourceFileFetcher {
@ -998,8 +997,8 @@ mod tests {
} }
} }
#[test] #[tokio::test]
fn test_get_source_code_1() { async fn test_get_source_code_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (temp_dir, fetcher) = test_setup(); let (temp_dir, fetcher) = test_setup();
let fetcher_1 = fetcher.clone(); let fetcher_1 = fetcher.clone();
@ -1017,9 +1016,9 @@ mod tests {
let headers_file_name_2 = headers_file_name.clone(); let headers_file_name_2 = headers_file_name.clone();
let headers_file_name_3 = headers_file_name; let headers_file_name_3 = headers_file_name;
let fut = fetcher let result = fetcher
.get_source_file_async(&module_url, true, false, false) .get_source_file_async(&module_url, true, false, false)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!( assert_eq!(
@ -1035,9 +1034,9 @@ mod tests {
&headers_file_name_1, &headers_file_name_1,
"{ \"mime_type\": \"text/javascript\" }", "{ \"mime_type\": \"text/javascript\" }",
); );
fetcher_1.get_source_file_async(&module_url, true, false, false) let result2 = fetcher_1
}) .get_source_file_async(&module_url, true, false, false)
.then(move |result2| { .await;
assert!(result2.is_ok()); assert!(result2.is_ok());
let r2 = result2.unwrap(); let r2 = result2.unwrap();
assert_eq!( assert_eq!(
@ -1063,9 +1062,9 @@ mod tests {
None, None,
None, None,
); );
fetcher_2.get_source_file_async(&module_url_1, true, false, false) let result3 = fetcher_2
}) .get_source_file_async(&module_url_1, true, false, false)
.then(move |result3| { .await;
assert!(result3.is_ok()); assert!(result3.is_ok());
let r3 = result3.unwrap(); let r3 = result3.unwrap();
assert_eq!( assert_eq!(
@ -1082,26 +1081,22 @@ mod tests {
// let's create fresh instance of DenoDir (simulating another freshh Deno process) // let's create fresh instance of DenoDir (simulating another freshh Deno process)
// and don't use cache // and don't use cache
let fetcher = setup_file_fetcher(temp_dir.path()); let fetcher = setup_file_fetcher(temp_dir.path());
fetcher.get_source_file_async(&module_url_2, false, false, false) let result4 = fetcher
}) .get_source_file_async(&module_url_2, false, false, false)
.map(move |result4| { .await;
assert!(result4.is_ok()); assert!(result4.is_ok());
let r4 = result4.unwrap(); let r4 = result4.unwrap();
let expected4 = let expected4 = &b"export { printHello } from \"./print_hello.ts\";\n"[..];
&b"export { printHello } from \"./print_hello.ts\";\n"[..];
assert_eq!(r4.source_code, expected4); assert_eq!(r4.source_code, expected4);
// Now the old .headers.json file should have gone! Resolved back to TypeScript // Now the old .headers.json file should have gone! Resolved back to TypeScript
assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r4.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name_3).is_err()); assert!(fs::read_to_string(&headers_file_name_3).is_err());
});
// http_util::fetch_sync_string requires tokio
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_2() { async fn test_get_source_code_2() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (temp_dir, fetcher) = test_setup(); let (temp_dir, fetcher) = test_setup();
let fetcher_1 = fetcher.clone(); let fetcher_1 = fetcher.clone();
@ -1116,9 +1111,9 @@ mod tests {
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let fut = fetcher let result = fetcher
.get_source_file_async(&module_url, true, false, false) .get_source_file_async(&module_url, true, false, false)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
let expected = b"export const loaded = true;\n"; let expected = b"export const loaded = true;\n";
@ -1141,24 +1136,25 @@ mod tests {
None, None,
None, None,
); );
fetcher.get_source_file_async(&module_url, true, false, false) let result2 = fetcher
}) .get_source_file_async(&module_url, true, false, false)
.then(move |result2| { .await;
assert!(result2.is_ok()); assert!(result2.is_ok());
let r2 = result2.unwrap(); let r2 = result2.unwrap();
let expected2 = b"export const loaded = true;\n"; let expected2 = b"export const loaded = true;\n";
assert_eq!(r2.source_code, expected2); assert_eq!(r2.source_code, expected2);
// If get_source_file_async does not call remote, this should be TypeScript // If get_source_file_async does not call remote, this should be TypeScript
// as we modified before! (we do not overwrite .headers.json due to no http fetch) // as we modified before! (we do not overwrite .headers.json due to no http
// fetch)
assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript); assert_eq!(&(r2.media_type), &msg::MediaType::TypeScript);
assert!(fs::read_to_string(&headers_file_name).is_err()); assert!(fs::read_to_string(&headers_file_name).is_err());
// let's create fresh instance of DenoDir (simulating another freshh Deno process) // let's create fresh instance of DenoDir (simulating another freshh Deno
// and don't use cache // process) and don't use cache
let fetcher = setup_file_fetcher(temp_dir.path()); let fetcher = setup_file_fetcher(temp_dir.path());
fetcher.get_source_file_async(&module_url_1, false, false, false) let result3 = fetcher
}) .get_source_file_async(&module_url_1, false, false, false)
.map(move |result3| { .await;
assert!(result3.is_ok()); assert!(result3.is_ok());
let r3 = result3.unwrap(); let r3 = result3.unwrap();
let expected3 = b"export const loaded = true;\n"; let expected3 = b"export const loaded = true;\n";
@ -1173,14 +1169,12 @@ mod tests {
.unwrap(), .unwrap(),
"text/javascript" "text/javascript"
); );
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_multiple_downloads_of_same_file() { async fn test_get_source_code_multiple_downloads_of_same_file() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let specifier = ModuleSpecifier::resolve_url( let specifier = ModuleSpecifier::resolve_url(
@ -1194,11 +1188,8 @@ mod tests {
); );
// first download // first download
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_ok()); assert!(r.is_ok());
},
));
let result = fs::File::open(&headers_file_name); let result = fs::File::open(&headers_file_name);
assert!(result.is_ok()); assert!(result.is_ok());
@ -1207,14 +1198,12 @@ mod tests {
let headers_file_metadata = headers_file.metadata().unwrap(); let headers_file_metadata = headers_file.metadata().unwrap();
let headers_file_modified = headers_file_metadata.modified().unwrap(); let headers_file_modified = headers_file_metadata.modified().unwrap();
// download file again, it should use already fetched file even though `use_disk_cache` is set to // download file again, it should use already fetched file even though
// false, this can be verified using source header file creation timestamp (should be // `use_disk_cache` is set to false, this can be verified using source
// the same as after first download) // header file creation timestamp (should be the same as after first
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( // download)
|r| { let r = fetcher.fetch_source_file_async(&specifier, None).await;
assert!(r.is_ok()); assert!(r.is_ok());
},
));
let result = fs::File::open(&headers_file_name); let result = fs::File::open(&headers_file_name);
assert!(result.is_ok()); assert!(result.is_ok());
@ -1227,8 +1216,8 @@ mod tests {
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_3() { async fn test_get_source_code_3() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
@ -1254,9 +1243,9 @@ mod tests {
redirect_target_filepath.to_str().unwrap().to_string(); redirect_target_filepath.to_str().unwrap().to_string();
// Test basic follow and headers recording // Test basic follow and headers recording
let fut = fetcher let result = fetcher
.get_source_file_async(&redirect_module_url, true, false, false) .get_source_file_async(&redirect_module_url, true, false, false)
.map(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let mod_meta = result.unwrap(); let mod_meta = result.unwrap();
// File that requires redirection is not downloaded. // File that requires redirection is not downloaded.
@ -1279,14 +1268,12 @@ mod tests {
// Examine the meta result. // Examine the meta result.
assert_eq!(mod_meta.url, target_module_url); assert_eq!(mod_meta.url, target_module_url);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_4() { async fn test_get_source_code_4() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let double_redirect_url = Url::parse( let double_redirect_url = Url::parse(
@ -1317,9 +1304,9 @@ mod tests {
.join("http/localhost_PORT4545/cli/tests/subdir/redirects/redirect1.js"); .join("http/localhost_PORT4545/cli/tests/subdir/redirects/redirect1.js");
// Test double redirects and headers recording // Test double redirects and headers recording
let fut = fetcher let result = fetcher
.get_source_file_async(&double_redirect_url, true, false, false) .get_source_file_async(&double_redirect_url, true, false, false)
.map(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let mod_meta = result.unwrap(); let mod_meta = result.unwrap();
assert!(fs::read_to_string(&double_redirect_path).is_err()); assert!(fs::read_to_string(&double_redirect_path).is_err());
@ -1342,20 +1329,17 @@ mod tests {
fs::read_to_string(&target_path).unwrap(), fs::read_to_string(&target_path).unwrap(),
"export const redirect = 1;\n" "export const redirect = 1;\n"
); );
let redirect_target_headers = let redirect_target_headers = fetcher.get_source_code_headers(&target_url);
fetcher.get_source_code_headers(&target_url);
assert!(redirect_target_headers.redirect_to.is_none()); assert!(redirect_target_headers.redirect_to.is_none());
// Examine the meta result. // Examine the meta result.
assert_eq!(mod_meta.url, target_url); assert_eq!(mod_meta.url, target_url);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_5() { async fn test_get_source_code_5() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
@ -1376,9 +1360,9 @@ mod tests {
let target_path_ = target_path.clone(); let target_path_ = target_path.clone();
// Test that redirect target is not downloaded twice for different redirect source. // Test that redirect target is not downloaded twice for different redirect source.
let fut = fetcher let result = fetcher
.get_source_file_async(&double_redirect_url, true, false, false) .get_source_file_async(&double_redirect_url, true, false, false)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let result = fs::File::open(&target_path); let result = fs::File::open(&target_path);
assert!(result.is_ok()); assert!(result.is_ok());
@ -1387,14 +1371,13 @@ mod tests {
let file_metadata = file.metadata().unwrap(); let file_metadata = file.metadata().unwrap();
let file_modified = file_metadata.modified().unwrap(); let file_modified = file_metadata.modified().unwrap();
// When another file is fetched that also point to redirect target, then redirect target // When another file is fetched that also point to redirect target, then
// shouldn't be downloaded again. It can be verified using source header file creation // redirect target shouldn't be downloaded again. It can be verified
// timestamp (should be the same as after first `get_source_file`) // using source header file creation timestamp (should be the same as
fetcher // after first `get_source_file`)
let result = fetcher
.get_source_file_async(&redirect_url, true, false, false) .get_source_file_async(&redirect_url, true, false, false)
.map(move |r| (r, file_modified)) .await;
})
.map(move |(result, file_modified)| {
assert!(result.is_ok()); assert!(result.is_ok());
let result = fs::File::open(&target_path_); let result = fs::File::open(&target_path_);
assert!(result.is_ok()); assert!(result.is_ok());
@ -1404,14 +1387,12 @@ mod tests {
let file_modified_2 = file_metadata_2.modified().unwrap(); let file_modified_2 = file_metadata_2.modified().unwrap();
assert_eq!(file_modified, file_modified_2); assert_eq!(file_modified, file_modified_2);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_code_6() { async fn test_get_source_code_6() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let double_redirect_url = Url::parse( let double_redirect_url = Url::parse(
@ -1420,43 +1401,40 @@ mod tests {
.unwrap(); .unwrap();
// Test that redirections can be limited // Test that redirections can be limited
let fut = fetcher let result = fetcher
.fetch_remote_source_async(&double_redirect_url, false, false, 2) .fetch_remote_source_async(&double_redirect_url, false, false, 2)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
fetcher.fetch_remote_source_async(&double_redirect_url, false, false, 1)
}) let result = fetcher
.map(move |result| { .fetch_remote_source_async(&double_redirect_url, false, false, 1)
.await;
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::Http); assert_eq!(err.kind(), ErrorKind::Http);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_no_remote() { async fn test_get_source_no_remote() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://localhost:4545/cli/tests/002_hello.ts").unwrap(); Url::parse("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
// Remote modules are not allowed // Remote modules are not allowed
let fut = fetcher let result = fetcher
.get_source_file_async(&module_url, true, true, false) .get_source_file_async(&module_url, true, true, false)
.map(move |result| { .await;
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::NotFound); assert_eq!(err.kind(), ErrorKind::NotFound);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_get_source_cached_only() { async fn test_get_source_cached_only() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let fetcher_1 = fetcher.clone(); let fetcher_1 = fetcher.clone();
@ -1465,32 +1443,32 @@ mod tests {
Url::parse("http://localhost:4545/cli/tests/002_hello.ts").unwrap(); Url::parse("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
let module_url_1 = module_url.clone(); let module_url_1 = module_url.clone();
let module_url_2 = module_url.clone(); let module_url_2 = module_url.clone();
// file hasn't been cached before // file hasn't been cached before
let fut = fetcher let result = fetcher
.get_source_file_async(&module_url, true, false, true) .get_source_file_async(&module_url, true, false, true)
.then(move |result| { .await;
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind(), ErrorKind::PermissionDenied); assert_eq!(err.kind(), ErrorKind::PermissionDenied);
// download and cache file // download and cache file
fetcher_1.get_source_file_async(&module_url_1, true, false, false) let result = fetcher_1
}) .get_source_file_async(&module_url_1, true, false, false)
.then(move |result| { .await;
assert!(result.is_ok());
// module is already cached, should be ok even with `cached_only`
let result = fetcher_2
.get_source_file_async(&module_url_2, true, false, true)
.await;
assert!(result.is_ok()); assert!(result.is_ok());
// module is already cached, should be ok even with `cached_only`
fetcher_2.get_source_file_async(&module_url_2, true, false, true)
})
.map(move |result| {
assert!(result.is_ok());
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_source_async_1() { async fn test_fetch_source_async_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
@ -1502,9 +1480,9 @@ mod tests {
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let fut = fetcher let result = fetcher
.fetch_remote_source_async(&module_url, false, false, 10) .fetch_remote_source_async(&module_url, false, false, 10)
.map(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, b"export const loaded = true;\n"); assert_eq!(r.source_code, b"export const loaded = true;\n");
@ -1525,14 +1503,12 @@ mod tests {
assert_eq!(r2.source_code, b"export const loaded = true;\n"); assert_eq!(r2.source_code, b"export const loaded = true;\n");
// Not MediaType::TypeScript due to .headers.json modification // Not MediaType::TypeScript due to .headers.json modification
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_source_1() { async fn test_fetch_source_1() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
@ -1545,9 +1521,9 @@ mod tests {
.get_cache_filename_with_extension(&module_url, "headers.json"), .get_cache_filename_with_extension(&module_url, "headers.json"),
); );
let fut = fetcher let result = fetcher
.fetch_remote_source_async(&module_url, false, false, 10) .fetch_remote_source_async(&module_url, false, false, 10)
.map(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, b"export const loaded = true;\n"); assert_eq!(r.source_code, b"export const loaded = true;\n");
@ -1569,14 +1545,12 @@ mod tests {
assert_eq!(r2.source_code, b"export const loaded = true;\n"); assert_eq!(r2.source_code, b"export const loaded = true;\n");
// Not MediaType::TypeScript due to .headers.json modification // Not MediaType::TypeScript due to .headers.json modification
assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript); assert_eq!(&(r2.media_type), &msg::MediaType::JavaScript);
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_source_2() { async fn test_fetch_source_2() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let fetcher_1 = fetcher.clone(); let fetcher_1 = fetcher.clone();
@ -1593,9 +1567,9 @@ mod tests {
.unwrap(); .unwrap();
let module_url_3_ = module_url_3.clone(); let module_url_3_ = module_url_3.clone();
let fut = fetcher let result = fetcher
.fetch_remote_source_async(&module_url, false, false, 10) .fetch_remote_source_async(&module_url, false, false, 10)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r = result.unwrap(); let r = result.unwrap();
assert_eq!(r.source_code, b"export const loaded = true;\n"); assert_eq!(r.source_code, b"export const loaded = true;\n");
@ -1608,9 +1582,9 @@ mod tests {
.unwrap(), .unwrap(),
"text/typescript" "text/typescript"
); );
fetcher_1.fetch_remote_source_async(&module_url_2, false, false, 10) let result = fetcher_1
}) .fetch_remote_source_async(&module_url_2, false, false, 10)
.then(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r2 = result.unwrap(); let r2 = result.unwrap();
assert_eq!(r2.source_code, b"export const loaded = true;\n"); assert_eq!(r2.source_code, b"export const loaded = true;\n");
@ -1624,9 +1598,9 @@ mod tests {
"text/javascript" "text/javascript"
); );
// test unknown extension // test unknown extension
fetcher_2.fetch_remote_source_async(&module_url_3, false, false, 10) let result = fetcher_2
}) .fetch_remote_source_async(&module_url_3, false, false, 10)
.map(move |result| { .await;
assert!(result.is_ok()); assert!(result.is_ok());
let r3 = result.unwrap(); let r3 = result.unwrap();
assert_eq!(r3.source_code, b"export const loaded = true;\n"); assert_eq!(r3.source_code, b"export const loaded = true;\n");
@ -1639,63 +1613,49 @@ mod tests {
.unwrap(), .unwrap(),
"text/typescript" "text/typescript"
); );
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_source_file() { async fn test_fetch_source_file() {
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test failure case. // Test failure case.
let specifier = let specifier =
ModuleSpecifier::resolve_url(file_url!("/baddir/hello.ts")).unwrap(); ModuleSpecifier::resolve_url(file_url!("/baddir/hello.ts")).unwrap();
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_err()); assert!(r.is_err());
},
));
let p = let p =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts"); std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts");
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap(); ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_ok()); assert!(r.is_ok());
},
));
} }
#[test] #[tokio::test]
fn test_fetch_source_file_1() { async fn test_fetch_source_file_1() {
/*recompile ts file*/ /*recompile ts file*/
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
// Test failure case. // Test failure case.
let specifier = let specifier =
ModuleSpecifier::resolve_url(file_url!("/baddir/hello.ts")).unwrap(); ModuleSpecifier::resolve_url(file_url!("/baddir/hello.ts")).unwrap();
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_err()); assert!(r.is_err());
},
));
let p = let p =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts"); std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("js/main.ts");
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap(); ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_ok()); assert!(r.is_ok());
},
));
} }
#[test] #[tokio::test]
fn test_fetch_source_file_2() { async fn test_fetch_source_file_2() {
/*recompile ts file*/ /*recompile ts file*/
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
@ -1703,11 +1663,8 @@ mod tests {
.join("tests/001_hello.js"); .join("tests/001_hello.js");
let specifier = let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap(); ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
tokio_util::run(fetcher.fetch_source_file_async(&specifier, None).map( let r = fetcher.fetch_source_file_async(&specifier, None).await;
|r| {
assert!(r.is_ok()); assert!(r.is_ok());
},
));
} }
#[test] #[test]
@ -1947,14 +1904,13 @@ mod tests {
assert_eq!(filter_shebang(code), b"\nconsole.log('hello');\n"); assert_eq!(filter_shebang(code), b"\nconsole.log('hello');\n");
} }
#[test] #[tokio::test]
fn test_fetch_with_etag() { async fn test_fetch_with_etag() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap(); Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
let fut = async move {
let source = fetcher let source = fetcher
.fetch_remote_source_async(&module_url, false, false, 1) .fetch_remote_source_async(&module_url, false, false, 1)
.await; .await;
@ -1990,9 +1946,7 @@ mod tests {
// Assert that the file has not been modified // Assert that the file has not been modified
assert_eq!(modified1, modified2); assert_eq!(modified1, modified2);
};
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
@ -2075,14 +2029,12 @@ mod tests {
); );
} }
#[test] #[tokio::test]
fn test_fetch_with_types_header() { async fn test_fetch_with_types_header() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.js").unwrap(); Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.js").unwrap();
let fut = async move {
let source = fetcher let source = fetcher
.fetch_remote_source_async(&module_url, false, false, 1) .fetch_remote_source_async(&module_url, false, false, 1)
.await; .await;
@ -2092,24 +2044,17 @@ mod tests {
assert_eq!(&(source.media_type), &msg::MediaType::JavaScript); assert_eq!(&(source.media_type), &msg::MediaType::JavaScript);
assert_eq!( assert_eq!(
source.types_url, source.types_url,
Some( Some(Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.d.ts").unwrap())
Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.d.ts").unwrap()
)
); );
};
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_with_types_reference() { async fn test_fetch_with_types_reference() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let (_temp_dir, fetcher) = test_setup(); let (_temp_dir, fetcher) = test_setup();
let module_url = let module_url =
Url::parse("http://127.0.0.1:4545/referenceTypes.js").unwrap(); Url::parse("http://127.0.0.1:4545/referenceTypes.js").unwrap();
let fut = async move {
let source = fetcher let source = fetcher
.fetch_remote_source_async(&module_url, false, false, 1) .fetch_remote_source_async(&module_url, false, false, 1)
.await; .await;
@ -2118,13 +2063,8 @@ mod tests {
assert_eq!(&(source.media_type), &msg::MediaType::JavaScript); assert_eq!(&(source.media_type), &msg::MediaType::JavaScript);
assert_eq!( assert_eq!(
source.types_url, source.types_url,
Some( Some(Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.d.ts").unwrap())
Url::parse("http://127.0.0.1:4545/xTypeScriptTypes.d.ts").unwrap()
)
); );
};
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
} }

View file

@ -269,37 +269,28 @@ impl AsyncRead for HttpBody {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::tokio_util;
#[test] #[tokio::test]
fn test_fetch_sync_string() { async fn test_fetch_sync_string() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py // Relies on external http server. See tools/http_server.py
let url = let url =
Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap(); Url::parse("http://127.0.0.1:4545/cli/tests/fixture.json").unwrap();
let client = create_http_client(); let client = create_http_client();
let fut = let result = fetch_string_once(client, &url, None).await;
fetch_string_once(client, &url, None).map(|result| match result { if let Ok(FetchOnceResult::Code(payload)) = result {
Ok(FetchOnceResult::Code(ResultPayload { assert!(!payload.body.is_empty());
body: code, assert_eq!(payload.content_type, Some("application/json".to_string()));
content_type: maybe_content_type, assert_eq!(payload.etag, None);
etag, assert_eq!(payload.x_typescript_types, None);
x_typescript_types, } else {
})) => { panic!();
assert!(!code.is_empty());
assert_eq!(maybe_content_type, Some("application/json".to_string()));
assert_eq!(etag, None);
assert_eq!(x_typescript_types, None);
} }
_ => panic!(),
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_gzip() { async fn test_fetch_gzip() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py // Relies on external http server. See tools/http_server.py
let url = Url::parse( let url = Url::parse(
@ -307,72 +298,53 @@ mod tests {
) )
.unwrap(); .unwrap();
let client = create_http_client(); let client = create_http_client();
let fut = let result = fetch_string_once(client, &url, None).await;
fetch_string_once(client, &url, None).map(|result| match result { if let Ok(FetchOnceResult::Code(payload)) = result {
Ok(FetchOnceResult::Code(ResultPayload { assert_eq!(payload.body, "console.log('gzip')");
body: code,
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
assert!(!code.is_empty());
assert_eq!(code, "console.log('gzip')");
assert_eq!( assert_eq!(
maybe_content_type, payload.content_type,
Some("application/javascript".to_string()) Some("application/javascript".to_string())
); );
assert_eq!(etag, None); assert_eq!(payload.etag, None);
assert_eq!(x_typescript_types, None); assert_eq!(payload.x_typescript_types, None);
} else {
panic!();
} }
_ => panic!(),
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_with_etag() { async fn test_fetch_with_etag() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap(); let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
let client = create_http_client(); let client = create_http_client();
let fut = async move { let result = fetch_string_once(client.clone(), &url, None).await;
fetch_string_once(client.clone(), &url, None) if let Ok(FetchOnceResult::Code(ResultPayload {
.map(|result| match result { body,
Ok(FetchOnceResult::Code(ResultPayload { content_type,
body: code,
content_type: maybe_content_type,
etag, etag,
x_typescript_types, x_typescript_types,
})) => { })) = result
assert!(!code.is_empty()); {
assert_eq!(code, "console.log('etag')"); assert!(!body.is_empty());
assert_eq!( assert_eq!(body, "console.log('etag')");
maybe_content_type, assert_eq!(content_type, Some("application/typescript".to_string()));
Some("application/typescript".to_string())
);
assert_eq!(etag, Some("33a64df551425fcc55e".to_string())); assert_eq!(etag, Some("33a64df551425fcc55e".to_string()));
assert_eq!(x_typescript_types, None); assert_eq!(x_typescript_types, None);
} else {
panic!();
} }
_ => panic!(),
})
.await;
let res = fetch_string_once( let res =
client, fetch_string_once(client, &url, Some("33a64df551425fcc55e".to_string()))
&url,
Some("33a64df551425fcc55e".to_string()),
)
.await; .await;
assert_eq!(res.unwrap(), FetchOnceResult::NotModified); assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
};
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_brotli() { async fn test_fetch_brotli() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py // Relies on external http server. See tools/http_server.py
let url = Url::parse( let url = Url::parse(
@ -380,32 +352,24 @@ mod tests {
) )
.unwrap(); .unwrap();
let client = create_http_client(); let client = create_http_client();
let fut = let result = fetch_string_once(client, &url, None).await;
fetch_string_once(client, &url, None).map(|result| match result { if let Ok(FetchOnceResult::Code(payload)) = result {
Ok(FetchOnceResult::Code(ResultPayload { assert!(!payload.body.is_empty());
body: code, assert_eq!(payload.body, "console.log('brotli');");
content_type: maybe_content_type,
etag,
x_typescript_types,
})) => {
assert!(!code.is_empty());
assert_eq!(code, "console.log('brotli');");
assert_eq!( assert_eq!(
maybe_content_type, payload.content_type,
Some("application/javascript".to_string()) Some("application/javascript".to_string())
); );
assert_eq!(etag, None); assert_eq!(payload.etag, None);
assert_eq!(x_typescript_types, None); assert_eq!(payload.x_typescript_types, None);
} else {
panic!();
} }
_ => panic!(),
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }
#[test] #[tokio::test]
fn test_fetch_string_once_with_redirect() { async fn test_fetch_string_once_with_redirect() {
let http_server_guard = crate::test_util::http_server(); let http_server_guard = crate::test_util::http_server();
// Relies on external http server. See tools/http_server.py // Relies on external http server. See tools/http_server.py
let url = let url =
@ -414,15 +378,12 @@ mod tests {
let target_url = let target_url =
Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap(); Url::parse("http://localhost:4545/cli/tests/fixture.json").unwrap();
let client = create_http_client(); let client = create_http_client();
let fut = let result = fetch_string_once(client, &url, None).await;
fetch_string_once(client, &url, None).map(move |result| match result { if let Ok(FetchOnceResult::Redirect(url)) = result {
Ok(FetchOnceResult::Redirect(url)) => {
assert_eq!(url, target_url); assert_eq!(url, target_url);
} else {
panic!();
} }
_ => panic!(),
});
tokio_util::run(fut);
drop(http_server_guard); drop(http_server_guard);
} }