0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-30 09:08:00 -04:00

fix(deno install): support file: scheme URLs (#10562)

This commit is contained in:
Satya Rohith 2021-05-19 17:10:23 +05:30 committed by Bert Belder
parent 44cd0b1ef6
commit 7a751b8135
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
2 changed files with 54 additions and 39 deletions

View file

@ -3,7 +3,7 @@ use crate::flags::Flags;
use crate::fs_util::canonicalize_path;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::resolve_url_or_path;
use deno_core::url::Url;
use log::Level;
use regex::Regex;
@ -29,11 +29,6 @@ lazy_static::lazy_static! {
).case_insensitive(true).build().unwrap();
}
pub fn is_remote_url(module_url: &str) -> bool {
let lower = module_url.to_lowercase();
lower.starts_with("http://") || lower.starts_with("https://")
}
fn validate_name(exec_name: &str) -> Result<(), AnyError> {
if EXEC_NAME_RE.is_match(exec_name) {
Ok(())
@ -144,26 +139,6 @@ pub fn infer_name_from_url(url: &Url) -> Option<String> {
Some(stem)
}
/// Get a valid URL from the provided value.
/// When the provided value is a URL with 'http(s)' scheme,
/// it ensures it is valid by parsing it and if not, it will
/// construct a URL of 'file' scheme from the provided value.
fn get_valid_url(module_url: &str) -> Result<Url, AnyError> {
if is_remote_url(module_url) {
Ok(Url::parse(module_url).expect("Should be valid url"))
} else {
let module_path = PathBuf::from(module_url);
let module_path = if module_path.is_absolute() {
module_path
} else {
let cwd = env::current_dir()
.context("Failed to get current working directory")?;
cwd.join(module_path)
};
Ok(Url::from_file_path(module_path).expect("Path should be absolute"))
}
}
pub fn install(
flags: Flags,
module_url: &str,
@ -189,7 +164,7 @@ pub fn install(
};
// Check if module_url is remote
let module_url = get_valid_url(module_url)?;
let module_url = resolve_url_or_path(module_url)?;
let name = name.or_else(|| infer_name_from_url(&module_url));
@ -279,7 +254,7 @@ pub fn install(
}
if let Some(import_map_path) = flags.import_map_path {
let import_map_url = get_valid_url(&import_map_path)?;
let import_map_url = resolve_url_or_path(&import_map_path)?;
executable_args.push("--import-map".to_string());
executable_args.push(import_map_url.to_string());
}
@ -345,21 +320,12 @@ mod tests {
use std::process::Command;
use std::sync::Mutex;
use tempfile::TempDir;
use test_util::tests_path;
lazy_static::lazy_static! {
pub static ref ENV_LOCK: Mutex<()> = Mutex::new(());
}
#[test]
fn test_is_remote_url() {
assert!(is_remote_url("https://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("http://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("HTTP://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("HTTp://deno.land/std/http/file_server.ts"));
assert!(!is_remote_url("file:///dev/deno_std/http/file_server.ts"));
assert!(!is_remote_url("./dev/deno_std/http/file_server.ts"));
}
#[test]
fn install_infer_name_from_url() {
assert_eq!(
@ -923,4 +889,39 @@ mod tests {
let content = fs::read_to_string(file_path).unwrap();
assert!(content.contains(&expected_string));
}
// Regression test for https://github.com/denoland/deno/issues/10556.
#[test]
fn install_file_url() {
let temp_dir = TempDir::new().expect("tempdir fail");
let bin_dir = temp_dir.path().join("bin");
let module_path = fs::canonicalize(tests_path().join("cat.ts")).unwrap();
let file_module_string =
Url::from_file_path(module_path).unwrap().to_string();
assert!(file_module_string.starts_with("file:///"));
let result = install(
Flags::default(),
&file_module_string,
vec![],
Some("echo_test".to_string()),
Some(temp_dir.path().to_path_buf()),
true,
);
assert!(result.is_ok());
let mut file_path = bin_dir.join("echo_test");
if cfg!(windows) {
file_path = file_path.with_extension("cmd");
}
assert!(file_path.exists());
let mut expected_string = format!("run '{}'", &file_module_string);
if cfg!(windows) {
expected_string = format!("\"run\" \"{}\"", &file_module_string);
}
let content = fs::read_to_string(file_path).unwrap();
assert!(content.contains(&expected_string));
}
}

View file

@ -11,7 +11,6 @@ use crate::module_graph;
use crate::program_state::ProgramState;
use crate::tokio_util;
use crate::tools::coverage::CoverageCollector;
use crate::tools::installer::is_remote_url;
use deno_core::error::AnyError;
use deno_core::futures::future;
use deno_core::futures::stream;
@ -226,6 +225,11 @@ pub(crate) fn is_supported(p: &Path) -> bool {
}
}
pub fn is_remote_url(module_url: &str) -> bool {
let lower = module_url.to_lowercase();
lower.starts_with("http://") || lower.starts_with("https://")
}
pub fn collect_test_module_specifiers<P>(
include: Vec<String>,
root_path: &Path,
@ -642,4 +646,14 @@ mod tests {
.collect();
assert_eq!(matched_urls, expected);
}
#[test]
fn test_is_remote_url() {
assert!(is_remote_url("https://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("http://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("HTTP://deno.land/std/http/file_server.ts"));
assert!(is_remote_url("HTTp://deno.land/std/http/file_server.ts"));
assert!(!is_remote_url("file:///dev/deno_std/http/file_server.ts"));
assert!(!is_remote_url("./dev/deno_std/http/file_server.ts"));
}
}