summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/deno_dir.rs1098
1 files changed, 552 insertions, 546 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs
index 5bcbf312e..2510fb056 100644
--- a/src/deno_dir.rs
+++ b/src/deno_dir.rs
@@ -15,8 +15,6 @@ use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
-#[cfg(test)]
-use tempfile::TempDir;
use url;
use url::Url;
@@ -393,65 +391,6 @@ fn get_cache_filename(basedir: &Path, url: &Url) -> PathBuf {
out
}
-#[test]
-fn test_get_cache_filename() {
- let url = Url::parse("http://example.com:1234/path/to/file.ts").unwrap();
- let basedir = Path::new("/cache/dir/");
- let cache_file = get_cache_filename(&basedir, &url);
- assert_eq!(
- cache_file,
- Path::new("/cache/dir/example.com_PORT1234/path/to/file.ts")
- );
-}
-
-#[cfg(test)]
-pub fn test_setup() -> (TempDir, DenoDir) {
- let temp_dir = TempDir::new().expect("tempdir fail");
- let deno_dir = DenoDir::new(false, Some(temp_dir.path().to_path_buf()))
- .expect("setup fail");
- (temp_dir, deno_dir)
-}
-
-#[test]
-fn test_cache_path() {
- let (temp_dir, deno_dir) = test_setup();
- assert_eq!(
- (
- temp_dir
- .path()
- .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js"),
- temp_dir
- .path()
- .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js.map")
- ),
- deno_dir.cache_path("hello.ts", "1+2")
- );
-}
-
-#[test]
-fn test_code_cache() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let filename = "hello.js";
- let source_code = "1+2";
- let output_code = "1+2 // output code";
- let source_map = "{}";
- let (cache_path, source_map_path) =
- deno_dir.cache_path(filename, source_code);
- assert!(
- cache_path.ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js")
- );
- assert!(
- source_map_path
- .ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js.map")
- );
-
- let r = deno_dir.code_cache(filename, source_code, output_code, source_map);
- r.expect("code_cache error");
- assert!(cache_path.exists());
- assert_eq!(output_code, fs::read_to_string(&cache_path).unwrap());
-}
-
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30
fn source_code_hash(filename: &str, source_code: &str) -> String {
let mut ctx = ring::digest::Context::new(&ring::digest::SHA1);
@@ -466,368 +405,6 @@ fn source_code_hash(filename: &str, source_code: &str) -> String {
out
}
-#[test]
-fn test_source_code_hash() {
- assert_eq!(
- "a3e29aece8d35a19bf9da2bb1c086af71fb36ed5",
- source_code_hash("hello.ts", "1+2")
- );
- // Different source_code should result in different hash.
- assert_eq!(
- "914352911fc9c85170908ede3df1128d690dda41",
- source_code_hash("hello.ts", "1")
- );
- // Different filename should result in different hash.
- assert_eq!(
- "2e396bc66101ecc642db27507048376d972b1b70",
- source_code_hash("hi.ts", "1+2")
- );
-}
-
-// The `add_root` macro prepends "C:" to a string if on windows; on posix
-// systems it returns the input string untouched. This is necessary because
-// `Url::from_file_path()` fails if the input path isn't an absolute path.
-#[cfg(test)]
-macro_rules! add_root {
- ($path:expr) => {
- if cfg!(target_os = "windows") {
- concat!("C:", $path)
- } else {
- $path
- }
- };
-}
-
-#[test]
-fn test_fetch_remote_source_1() {
- use tokio_util;
- // http_util::fetch_sync_string requires tokio
- tokio_util::init(|| {
- let (_temp_dir, deno_dir) = test_setup();
- let module_name = "http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts";
- let filename = deno_fs::normalize_path(
- deno_dir
- .deps_http
- .join("localhost_PORT4545/tests/subdir/mt_video_mp2t.t3.ts")
- .as_ref(),
- );
- let mime_file_name = format!("{}.mime", &filename);
-
- let result = deno_dir.fetch_remote_source(module_name, &filename);
- assert!(result.is_ok());
- let r = result.unwrap();
- assert_eq!(&(r.0), "export const loaded = true;\n");
- assert_eq!(&(r.1), &msg::MediaType::TypeScript);
- assert_eq!(fs::read_to_string(&mime_file_name).unwrap(), "video/mp2t");
-
- // Modify .mime, make sure still read from local
- let _ = fs::write(&mime_file_name, "text/javascript");
- let result2 = deno_dir.fetch_remote_source(module_name, &filename);
- assert!(result2.is_ok());
- let r2 = result2.unwrap();
- assert_eq!(&(r2.0), "export const loaded = true;\n");
- // Not MediaType::TypeScript due to .mime modification
- assert_eq!(&(r2.1), &msg::MediaType::JavaScript);
- });
-}
-
-#[test]
-fn test_fetch_remote_source_2() {
- // only local, no http_util::fetch_sync_string called
- let (_temp_dir, deno_dir) = test_setup();
- let cwd = std::env::current_dir().unwrap();
- let cwd_string = cwd.to_str().unwrap();
- let module_name = "http://example.com/mt_text_typescript.t1.ts"; // not used
- let filename =
- format!("{}/tests/subdir/mt_text_typescript.t1.ts", &cwd_string);
-
- let result = deno_dir.fetch_remote_source(module_name, &filename);
- assert!(result.is_ok());
- let r = result.unwrap();
- assert_eq!(&(r.0), "export const loaded = true;\n");
- assert_eq!(&(r.1), &msg::MediaType::TypeScript);
-}
-
-#[test]
-fn test_code_fetch() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let cwd = std::env::current_dir().unwrap();
- let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
-
- // Test failure case.
- let specifier = "hello.ts";
- let referrer = add_root!("/baddir/badfile.ts");
- let r = deno_dir.code_fetch(specifier, referrer);
- assert!(r.is_err());
-
- // Assuming cwd is the deno repo root.
- let specifier = "./js/main.ts";
- let referrer = cwd_string.as_str();
- let r = deno_dir.code_fetch(specifier, referrer);
- assert!(r.is_ok());
- //let code_fetch_output = r.unwrap();
- //println!("code_fetch_output {:?}", code_fetch_output);
-}
-
-#[test]
-fn test_code_fetch_no_ext() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let cwd = std::env::current_dir().unwrap();
- let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
-
- // Assuming cwd is the deno repo root.
- let specifier = "./js/main";
- let referrer = cwd_string.as_str();
- let r = deno_dir.code_fetch(specifier, referrer);
- assert!(r.is_ok());
-
- // Test .ts extension
- // Assuming cwd is the deno repo root.
- let specifier = "./js/main";
- let referrer = cwd_string.as_str();
- let r = deno_dir.code_fetch(specifier, referrer);
- assert!(r.is_ok());
- let code_fetch_output = r.unwrap();
- // could only test .ends_with to avoid include local abs path
- assert!(code_fetch_output.module_name.ends_with("/js/main.ts"));
- assert!(code_fetch_output.filename.ends_with("/js/main.ts"));
- assert!(code_fetch_output.source_code.len() > 10);
-
- // Test .js extension
- // Assuming cwd is the deno repo root.
- let specifier = "./js/mock_builtin";
- let referrer = cwd_string.as_str();
- let r = deno_dir.code_fetch(specifier, referrer);
- assert!(r.is_ok());
- let code_fetch_output = r.unwrap();
- // could only test .ends_with to avoid include local abs path
- assert!(
- code_fetch_output
- .module_name
- .ends_with("/js/mock_builtin.js")
- );
- assert!(code_fetch_output.filename.ends_with("/js/mock_builtin.js"));
- assert!(code_fetch_output.source_code.len() > 10);
-}
-
-#[test]
-fn test_src_file_to_url_1() {
- let (_temp_dir, deno_dir) = test_setup();
- assert_eq!("hello", deno_dir.src_file_to_url("hello"));
- assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
- let x = deno_dir.deps_http.join("hello/world.txt");
- assert_eq!(
- "http://hello/world.txt",
- deno_dir.src_file_to_url(x.to_str().unwrap())
- );
-}
-
-#[test]
-fn test_src_file_to_url_2() {
- let (_temp_dir, deno_dir) = test_setup();
- assert_eq!("hello", deno_dir.src_file_to_url("hello"));
- assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
- let x = deno_dir.deps_https.join("hello/world.txt");
- assert_eq!(
- "https://hello/world.txt",
- deno_dir.src_file_to_url(x.to_str().unwrap())
- );
-}
-
-#[test]
-fn test_src_file_to_url_3() {
- let (_temp_dir, deno_dir) = test_setup();
- let x = deno_dir.deps_http.join("localhost_PORT4545/world.txt");
- assert_eq!(
- "http://localhost:4545/world.txt",
- deno_dir.src_file_to_url(x.to_str().unwrap())
- );
-}
-
-#[test]
-fn test_src_file_to_url_4() {
- let (_temp_dir, deno_dir) = test_setup();
- let x = deno_dir.deps_https.join("localhost_PORT4545/world.txt");
- assert_eq!(
- "https://localhost:4545/world.txt",
- deno_dir.src_file_to_url(x.to_str().unwrap())
- );
-}
-
-// https://github.com/denoland/deno/blob/golang/os_test.go#L16-L87
-#[test]
-fn test_resolve_module_1() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let test_cases = [
- (
- "./subdir/print_hello.ts",
- add_root!(
- "/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"
- ),
- add_root!(
- "/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
- ),
- add_root!(
- "/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
- ),
- ),
- (
- "testdata/001_hello.js",
- add_root!("/Users/rld/go/src/github.com/denoland/deno/"),
- add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
- add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
- ),
- (
- add_root!("/Users/rld/src/deno/hello.js"),
- ".",
- add_root!("/Users/rld/src/deno/hello.js"),
- add_root!("/Users/rld/src/deno/hello.js"),
- ),
- (
- add_root!("/this/module/got/imported.js"),
- add_root!("/that/module/did/it.js"),
- add_root!("/this/module/got/imported.js"),
- add_root!("/this/module/got/imported.js"),
- ),
- ];
- for &test in test_cases.iter() {
- let specifier = String::from(test.0);
- let referrer = String::from(test.1);
- let (module_name, filename) =
- deno_dir.resolve_module(&specifier, &referrer).unwrap();
- assert_eq!(module_name, test.2);
- assert_eq!(filename, test.3);
- }
-}
-
-#[test]
-fn test_resolve_module_2() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier = "http://localhost:4545/testdata/subdir/print_hello.ts";
- let referrer = add_root!("/deno/testdata/006_url_imports.ts");
-
- let expected_module_name =
- "http://localhost:4545/testdata/subdir/print_hello.ts";
- let expected_filename = deno_fs::normalize_path(
- deno_dir
- .deps_http
- .join("localhost_PORT4545/testdata/subdir/print_hello.ts")
- .as_ref(),
- );
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
-#[test]
-fn test_resolve_module_3() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier_ = deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
- let specifier = specifier_.to_str().unwrap();
- let referrer = ".";
-
- let expected_module_name = "http://unpkg.com/liltest@0.0.5/index.ts";
- let expected_filename = deno_fs::normalize_path(
- deno_dir
- .deps_http
- .join("unpkg.com/liltest@0.0.5/index.ts")
- .as_ref(),
- );
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
-#[test]
-fn test_resolve_module_4() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier = "./util";
- let referrer_ = deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
- let referrer = referrer_.to_str().unwrap();
-
- // http containing files -> load relative import with http
- let expected_module_name = "http://unpkg.com/liltest@0.0.5/util";
- let expected_filename = deno_fs::normalize_path(
- deno_dir
- .deps_http
- .join("unpkg.com/liltest@0.0.5/util")
- .as_ref(),
- );
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
-#[test]
-fn test_resolve_module_5() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier = "./util";
- let referrer_ = deno_dir.deps_https.join("unpkg.com/liltest@0.0.5/index.ts");
- let referrer = referrer_.to_str().unwrap();
-
- // https containing files -> load relative import with https
- let expected_module_name = "https://unpkg.com/liltest@0.0.5/util";
- let expected_filename = deno_fs::normalize_path(
- deno_dir
- .deps_https
- .join("unpkg.com/liltest@0.0.5/util")
- .as_ref(),
- );
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
-#[test]
-fn test_resolve_module_6() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier = "http://localhost:4545/tests/subdir/mod2.ts";
- let referrer = add_root!("/deno/tests/006_url_imports.ts");
- let expected_module_name = "http://localhost:4545/tests/subdir/mod2.ts";
- let expected_filename = deno_fs::normalize_path(
- deno_dir
- .deps_http
- .join("localhost_PORT4545/tests/subdir/mod2.ts")
- .as_ref(),
- );
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
-#[test]
-fn test_resolve_module_7() {
- let (_temp_dir, deno_dir) = test_setup();
-
- let specifier = "http_test.ts";
- let referrer = add_root!("/Users/rld/src/deno_net/");
- let expected_module_name = add_root!("/Users/rld/src/deno_net/http_test.ts");
- let expected_filename = add_root!("/Users/rld/src/deno_net/http_test.ts");
-
- let (module_name, filename) =
- deno_dir.resolve_module(specifier, referrer).unwrap();
- assert_eq!(module_name, expected_module_name);
- assert_eq!(filename, expected_filename);
-}
-
const ASSET_PREFIX: &str = "/$asset$/";
fn is_remote(module_name: &str) -> bool {
@@ -854,34 +431,6 @@ fn map_file_extension(path: &Path) -> msg::MediaType {
}
}
-#[test]
-fn test_map_file_extension() {
- assert_eq!(
- map_file_extension(Path::new("foo/bar.ts")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_file_extension(Path::new("foo/bar.d.ts")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_file_extension(Path::new("foo/bar.js")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_file_extension(Path::new("foo/bar.json")),
- msg::MediaType::Json
- );
- assert_eq!(
- map_file_extension(Path::new("foo/bar.txt")),
- msg::MediaType::Unknown
- );
- assert_eq!(
- map_file_extension(Path::new("foo/bar")),
- msg::MediaType::Unknown
- );
-}
-
// convert a ContentType string into a enumerated MediaType
fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
match content_type {
@@ -914,93 +463,6 @@ fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
}
}
-#[test]
-fn test_map_content_type() {
- // Extension only
- assert_eq!(
- map_content_type(Path::new("foo/bar.ts"), None),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.d.ts"), None),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.js"), None),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.json"), None),
- msg::MediaType::Json
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.txt"), None),
- msg::MediaType::Unknown
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), None),
- msg::MediaType::Unknown
- );
-
- // Media Type
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/typescript")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("text/typescript")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("video/vnd.dlna.mpeg-tts")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("video/mp2t")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/x-typescript")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/javascript")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("text/javascript")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/ecmascript")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("text/ecmascript")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/x-javascript")),
- msg::MediaType::JavaScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("application/json")),
- msg::MediaType::Json
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar"), Some("text/json")),
- msg::MediaType::Json
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.ts"), Some("text/plain")),
- msg::MediaType::TypeScript
- );
- assert_eq!(
- map_content_type(Path::new("foo/bar.ts"), Some("foo/bar")),
- msg::MediaType::Unknown
- );
-}
-
fn filter_shebang(code: String) -> String {
if !code.starts_with("#!") {
return code;
@@ -1013,12 +475,556 @@ fn filter_shebang(code: String) -> String {
}
}
-#[test]
-fn test_filter_shebang() {
- assert_eq!(filter_shebang("".to_string()), "");
- assert_eq!(filter_shebang("#".to_string()), "#");
- assert_eq!(filter_shebang("#!".to_string()), "");
- assert_eq!(filter_shebang("#!\n\n".to_string()), "\n\n");
- let code = "#!/usr/bin/env deno\nconsole.log('hello');\n".to_string();
- assert_eq!(filter_shebang(code), "\nconsole.log('hello');\n");
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use tempfile::TempDir;
+
+ fn test_setup() -> (TempDir, DenoDir) {
+ let temp_dir = TempDir::new().expect("tempdir fail");
+ let deno_dir = DenoDir::new(false, Some(temp_dir.path().to_path_buf()))
+ .expect("setup fail");
+ (temp_dir, deno_dir)
+ }
+
+ // The `add_root` macro prepends "C:" to a string if on windows; on posix
+ // systems it returns the input string untouched. This is necessary because
+ // `Url::from_file_path()` fails if the input path isn't an absolute path.
+ macro_rules! add_root {
+ ($path:expr) => {
+ if cfg!(target_os = "windows") {
+ concat!("C:", $path)
+ } else {
+ $path
+ }
+ };
+ }
+
+ #[test]
+ fn test_get_cache_filename() {
+ let url = Url::parse("http://example.com:1234/path/to/file.ts").unwrap();
+ let basedir = Path::new("/cache/dir/");
+ let cache_file = get_cache_filename(&basedir, &url);
+ assert_eq!(
+ cache_file,
+ Path::new("/cache/dir/example.com_PORT1234/path/to/file.ts")
+ );
+ }
+
+ #[test]
+ fn test_cache_path() {
+ let (temp_dir, deno_dir) = test_setup();
+ assert_eq!(
+ (
+ temp_dir
+ .path()
+ .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js"),
+ temp_dir
+ .path()
+ .join("gen/a3e29aece8d35a19bf9da2bb1c086af71fb36ed5.js.map")
+ ),
+ deno_dir.cache_path("hello.ts", "1+2")
+ );
+ }
+
+ #[test]
+ fn test_code_cache() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let filename = "hello.js";
+ let source_code = "1+2";
+ let output_code = "1+2 // output code";
+ let source_map = "{}";
+ let (cache_path, source_map_path) =
+ deno_dir.cache_path(filename, source_code);
+ assert!(
+ cache_path.ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js")
+ );
+ assert!(
+ source_map_path
+ .ends_with("gen/e8e3ee6bee4aef2ec63f6ec3db7fc5fdfae910ae.js.map")
+ );
+
+ let r = deno_dir.code_cache(filename, source_code, output_code, source_map);
+ r.expect("code_cache error");
+ assert!(cache_path.exists());
+ assert_eq!(output_code, fs::read_to_string(&cache_path).unwrap());
+ }
+
+ #[test]
+ fn test_source_code_hash() {
+ assert_eq!(
+ "a3e29aece8d35a19bf9da2bb1c086af71fb36ed5",
+ source_code_hash("hello.ts", "1+2")
+ );
+ // Different source_code should result in different hash.
+ assert_eq!(
+ "914352911fc9c85170908ede3df1128d690dda41",
+ source_code_hash("hello.ts", "1")
+ );
+ // Different filename should result in different hash.
+ assert_eq!(
+ "2e396bc66101ecc642db27507048376d972b1b70",
+ source_code_hash("hi.ts", "1+2")
+ );
+ }
+
+ #[test]
+ fn test_fetch_remote_source_1() {
+ use tokio_util;
+ // http_util::fetch_sync_string requires tokio
+ tokio_util::init(|| {
+ let (_temp_dir, deno_dir) = test_setup();
+ let module_name =
+ "http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts";
+ let filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_http
+ .join("localhost_PORT4545/tests/subdir/mt_video_mp2t.t3.ts")
+ .as_ref(),
+ );
+ let mime_file_name = format!("{}.mime", &filename);
+
+ let result = deno_dir.fetch_remote_source(module_name, &filename);
+ assert!(result.is_ok());
+ let r = result.unwrap();
+ assert_eq!(&(r.0), "export const loaded = true;\n");
+ assert_eq!(&(r.1), &msg::MediaType::TypeScript);
+ assert_eq!(fs::read_to_string(&mime_file_name).unwrap(), "video/mp2t");
+
+ // Modify .mime, make sure still read from local
+ let _ = fs::write(&mime_file_name, "text/javascript");
+ let result2 = deno_dir.fetch_remote_source(module_name, &filename);
+ assert!(result2.is_ok());
+ let r2 = result2.unwrap();
+ assert_eq!(&(r2.0), "export const loaded = true;\n");
+ // Not MediaType::TypeScript due to .mime modification
+ assert_eq!(&(r2.1), &msg::MediaType::JavaScript);
+ });
+ }
+
+ #[test]
+ fn test_fetch_remote_source_2() {
+ // only local, no http_util::fetch_sync_string called
+ let (_temp_dir, deno_dir) = test_setup();
+ let cwd = std::env::current_dir().unwrap();
+ let cwd_string = cwd.to_str().unwrap();
+ let module_name = "http://example.com/mt_text_typescript.t1.ts"; // not used
+ let filename =
+ format!("{}/tests/subdir/mt_text_typescript.t1.ts", &cwd_string);
+
+ let result = deno_dir.fetch_remote_source(module_name, &filename);
+ assert!(result.is_ok());
+ let r = result.unwrap();
+ assert_eq!(&(r.0), "export const loaded = true;\n");
+ assert_eq!(&(r.1), &msg::MediaType::TypeScript);
+ }
+
+ #[test]
+ fn test_code_fetch() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let cwd = std::env::current_dir().unwrap();
+ let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
+
+ // Test failure case.
+ let specifier = "hello.ts";
+ let referrer = add_root!("/baddir/badfile.ts");
+ let r = deno_dir.code_fetch(specifier, referrer);
+ assert!(r.is_err());
+
+ // Assuming cwd is the deno repo root.
+ let specifier = "./js/main.ts";
+ let referrer = cwd_string.as_str();
+ let r = deno_dir.code_fetch(specifier, referrer);
+ assert!(r.is_ok());
+ //let code_fetch_output = r.unwrap();
+ //println!("code_fetch_output {:?}", code_fetch_output);
+ }
+
+ #[test]
+ fn test_code_fetch_no_ext() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let cwd = std::env::current_dir().unwrap();
+ let cwd_string = String::from(cwd.to_str().unwrap()) + "/";
+
+ // Assuming cwd is the deno repo root.
+ let specifier = "./js/main";
+ let referrer = cwd_string.as_str();
+ let r = deno_dir.code_fetch(specifier, referrer);
+ assert!(r.is_ok());
+
+ // Test .ts extension
+ // Assuming cwd is the deno repo root.
+ let specifier = "./js/main";
+ let referrer = cwd_string.as_str();
+ let r = deno_dir.code_fetch(specifier, referrer);
+ assert!(r.is_ok());
+ let code_fetch_output = r.unwrap();
+ // could only test .ends_with to avoid include local abs path
+ assert!(code_fetch_output.module_name.ends_with("/js/main.ts"));
+ assert!(code_fetch_output.filename.ends_with("/js/main.ts"));
+ assert!(code_fetch_output.source_code.len() > 10);
+
+ // Test .js extension
+ // Assuming cwd is the deno repo root.
+ let specifier = "./js/mock_builtin";
+ let referrer = cwd_string.as_str();
+ let r = deno_dir.code_fetch(specifier, referrer);
+ assert!(r.is_ok());
+ let code_fetch_output = r.unwrap();
+ // could only test .ends_with to avoid include local abs path
+ assert!(
+ code_fetch_output
+ .module_name
+ .ends_with("/js/mock_builtin.js")
+ );
+ assert!(code_fetch_output.filename.ends_with("/js/mock_builtin.js"));
+ assert!(code_fetch_output.source_code.len() > 10);
+ }
+
+ #[test]
+ fn test_src_file_to_url_1() {
+ let (_temp_dir, deno_dir) = test_setup();
+ assert_eq!("hello", deno_dir.src_file_to_url("hello"));
+ assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
+ let x = deno_dir.deps_http.join("hello/world.txt");
+ assert_eq!(
+ "http://hello/world.txt",
+ deno_dir.src_file_to_url(x.to_str().unwrap())
+ );
+ }
+
+ #[test]
+ fn test_src_file_to_url_2() {
+ let (_temp_dir, deno_dir) = test_setup();
+ assert_eq!("hello", deno_dir.src_file_to_url("hello"));
+ assert_eq!("/hello", deno_dir.src_file_to_url("/hello"));
+ let x = deno_dir.deps_https.join("hello/world.txt");
+ assert_eq!(
+ "https://hello/world.txt",
+ deno_dir.src_file_to_url(x.to_str().unwrap())
+ );
+ }
+
+ #[test]
+ fn test_src_file_to_url_3() {
+ let (_temp_dir, deno_dir) = test_setup();
+ let x = deno_dir.deps_http.join("localhost_PORT4545/world.txt");
+ assert_eq!(
+ "http://localhost:4545/world.txt",
+ deno_dir.src_file_to_url(x.to_str().unwrap())
+ );
+ }
+
+ #[test]
+ fn test_src_file_to_url_4() {
+ let (_temp_dir, deno_dir) = test_setup();
+ let x = deno_dir.deps_https.join("localhost_PORT4545/world.txt");
+ assert_eq!(
+ "https://localhost:4545/world.txt",
+ deno_dir.src_file_to_url(x.to_str().unwrap())
+ );
+ }
+
+ // https://github.com/denoland/deno/blob/golang/os_test.go#L16-L87
+ #[test]
+ fn test_resolve_module_1() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let test_cases = [
+ (
+ "./subdir/print_hello.ts",
+ add_root!(
+ "/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"
+ ),
+ add_root!(
+ "/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
+ ),
+ add_root!(
+ "/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"
+ ),
+ ),
+ (
+ "testdata/001_hello.js",
+ add_root!("/Users/rld/go/src/github.com/denoland/deno/"),
+ add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
+ add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"),
+ ),
+ (
+ add_root!("/Users/rld/src/deno/hello.js"),
+ ".",
+ add_root!("/Users/rld/src/deno/hello.js"),
+ add_root!("/Users/rld/src/deno/hello.js"),
+ ),
+ (
+ add_root!("/this/module/got/imported.js"),
+ add_root!("/that/module/did/it.js"),
+ add_root!("/this/module/got/imported.js"),
+ add_root!("/this/module/got/imported.js"),
+ ),
+ ];
+ for &test in test_cases.iter() {
+ let specifier = String::from(test.0);
+ let referrer = String::from(test.1);
+ let (module_name, filename) =
+ deno_dir.resolve_module(&specifier, &referrer).unwrap();
+ assert_eq!(module_name, test.2);
+ assert_eq!(filename, test.3);
+ }
+ }
+
+ #[test]
+ fn test_resolve_module_2() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier = "http://localhost:4545/testdata/subdir/print_hello.ts";
+ let referrer = add_root!("/deno/testdata/006_url_imports.ts");
+
+ let expected_module_name =
+ "http://localhost:4545/testdata/subdir/print_hello.ts";
+ let expected_filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_http
+ .join("localhost_PORT4545/testdata/subdir/print_hello.ts")
+ .as_ref(),
+ );
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_resolve_module_3() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier_ =
+ deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
+ let specifier = specifier_.to_str().unwrap();
+ let referrer = ".";
+
+ let expected_module_name = "http://unpkg.com/liltest@0.0.5/index.ts";
+ let expected_filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_http
+ .join("unpkg.com/liltest@0.0.5/index.ts")
+ .as_ref(),
+ );
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_resolve_module_4() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier = "./util";
+ let referrer_ = deno_dir.deps_http.join("unpkg.com/liltest@0.0.5/index.ts");
+ let referrer = referrer_.to_str().unwrap();
+
+ // http containing files -> load relative import with http
+ let expected_module_name = "http://unpkg.com/liltest@0.0.5/util";
+ let expected_filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_http
+ .join("unpkg.com/liltest@0.0.5/util")
+ .as_ref(),
+ );
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_resolve_module_5() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier = "./util";
+ let referrer_ =
+ deno_dir.deps_https.join("unpkg.com/liltest@0.0.5/index.ts");
+ let referrer = referrer_.to_str().unwrap();
+
+ // https containing files -> load relative import with https
+ let expected_module_name = "https://unpkg.com/liltest@0.0.5/util";
+ let expected_filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_https
+ .join("unpkg.com/liltest@0.0.5/util")
+ .as_ref(),
+ );
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_resolve_module_6() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier = "http://localhost:4545/tests/subdir/mod2.ts";
+ let referrer = add_root!("/deno/tests/006_url_imports.ts");
+ let expected_module_name = "http://localhost:4545/tests/subdir/mod2.ts";
+ let expected_filename = deno_fs::normalize_path(
+ deno_dir
+ .deps_http
+ .join("localhost_PORT4545/tests/subdir/mod2.ts")
+ .as_ref(),
+ );
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_resolve_module_7() {
+ let (_temp_dir, deno_dir) = test_setup();
+
+ let specifier = "http_test.ts";
+ let referrer = add_root!("/Users/rld/src/deno_net/");
+ let expected_module_name =
+ add_root!("/Users/rld/src/deno_net/http_test.ts");
+ let expected_filename = add_root!("/Users/rld/src/deno_net/http_test.ts");
+
+ let (module_name, filename) =
+ deno_dir.resolve_module(specifier, referrer).unwrap();
+ assert_eq!(module_name, expected_module_name);
+ assert_eq!(filename, expected_filename);
+ }
+
+ #[test]
+ fn test_map_file_extension() {
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar.ts")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar.d.ts")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar.js")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar.json")),
+ msg::MediaType::Json
+ );
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar.txt")),
+ msg::MediaType::Unknown
+ );
+ assert_eq!(
+ map_file_extension(Path::new("foo/bar")),
+ msg::MediaType::Unknown
+ );
+ }
+
+ #[test]
+ fn test_map_content_type() {
+ // Extension only
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.ts"), None),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.d.ts"), None),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.js"), None),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.json"), None),
+ msg::MediaType::Json
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.txt"), None),
+ msg::MediaType::Unknown
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), None),
+ msg::MediaType::Unknown
+ );
+
+ // Media Type
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/typescript")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("text/typescript")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("video/vnd.dlna.mpeg-tts")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("video/mp2t")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/x-typescript")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/javascript")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("text/javascript")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/ecmascript")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("text/ecmascript")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/x-javascript")),
+ msg::MediaType::JavaScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("application/json")),
+ msg::MediaType::Json
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar"), Some("text/json")),
+ msg::MediaType::Json
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.ts"), Some("text/plain")),
+ msg::MediaType::TypeScript
+ );
+ assert_eq!(
+ map_content_type(Path::new("foo/bar.ts"), Some("foo/bar")),
+ msg::MediaType::Unknown
+ );
+ }
+
+ #[test]
+ fn test_filter_shebang() {
+ assert_eq!(filter_shebang("".to_string()), "");
+ assert_eq!(filter_shebang("#".to_string()), "#");
+ assert_eq!(filter_shebang("#!".to_string()), "");
+ assert_eq!(filter_shebang("#!\n\n".to_string()), "\n\n");
+ let code = "#!/usr/bin/env deno\nconsole.log('hello');\n".to_string();
+ assert_eq!(filter_shebang(code), "\nconsole.log('hello');\n");
+ }
}