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

resolve_module: handle "." and ".." referrers (#1454)

This commit is contained in:
Ryan Dahl 2019-01-03 12:51:56 -05:00 committed by GitHub
parent e809673cec
commit a8d326b431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -376,16 +376,20 @@ impl DenoDir {
let filename;
let specifier = self.src_file_to_url(specifier);
let referrer = self.src_file_to_url(referrer);
let mut referrer = self.src_file_to_url(referrer);
debug!(
"resolve_module specifier {} referrer {}",
specifier, referrer
);
let j: Url = if referrer == "."
|| is_remote(&specifier)
|| Path::new(&specifier).is_absolute()
if referrer.starts_with(".") {
let cwd = std::env::current_dir().unwrap();
let referrer_path = cwd.join(referrer);
referrer = referrer_path.to_str().unwrap().to_string() + "/";
}
let j: Url = if is_remote(&specifier) || Path::new(&specifier).is_absolute()
{
parse_local_or_remote(&specifier)?
} else if referrer.ends_with('/') {
@ -1121,6 +1125,50 @@ mod tests {
assert_eq!(filename, expected_filename);
}
#[test]
fn test_resolve_module_referrer_dot() {
let (_temp_dir, deno_dir) = test_setup();
let specifier = "tests/001_hello.js";
let cwd = std::env::current_dir().unwrap();
let expected_path = cwd.join(specifier);
let expected_module_name = deno_fs::normalize_path(&expected_path);
let expected_filename = expected_module_name.clone();
let (module_name, filename) =
deno_dir.resolve_module(specifier, ".").unwrap();
assert_eq!(module_name, expected_module_name);
assert_eq!(filename, expected_filename);
let (module_name, filename) =
deno_dir.resolve_module(specifier, "./").unwrap();
assert_eq!(module_name, expected_module_name);
assert_eq!(filename, expected_filename);
}
#[test]
fn test_resolve_module_referrer_dotdot() {
let (_temp_dir, deno_dir) = test_setup();
let specifier = "tests/001_hello.js";
let cwd = std::env::current_dir().unwrap();
let expected_path = cwd.join("..").join(specifier);
let expected_module_name = deno_fs::normalize_path(&expected_path);
let expected_filename = expected_module_name.clone();
let (module_name, filename) =
deno_dir.resolve_module(specifier, "..").unwrap();
assert_eq!(module_name, expected_module_name);
assert_eq!(filename, expected_filename);
let (module_name, filename) =
deno_dir.resolve_module(specifier, "../").unwrap();
assert_eq!(module_name, expected_module_name);
assert_eq!(filename, expected_filename);
}
#[test]
fn test_map_file_extension() {
assert_eq!(