1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

fix(cli): Fix panic when providing invalid urls to --reload (#15784)

This commit is contained in:
Alexander Sage 2022-09-06 09:30:42 -07:00 committed by Yoshiya Hinosawa
parent f00dd96187
commit 83dcf6ede3
No known key found for this signature in database
GPG key ID: 0E8BFAA8A5B4E92B

View file

@ -1937,6 +1937,7 @@ fn reload_arg<'a>() -> Arg<'a> {
Reloads specific modules",
)
.value_hint(ValueHint::FilePath)
.validator(reload_arg_validate)
}
fn ca_file_arg<'a>() -> Arg<'a> {
@ -3046,6 +3047,16 @@ fn inspect_arg_validate(val: &str) -> Result<(), String> {
}
}
fn reload_arg_validate(urlstr: &str) -> Result<(), String> {
if urlstr.is_empty() {
return Err(String::from("Missing url. Check for extra commas."));
}
match Url::from_str(urlstr) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}
fn watch_arg_parse(
flags: &mut Flags,
matches: &clap::ArgMatches,
@ -4280,6 +4291,78 @@ mod tests {
assert!(r.is_err());
}
#[test]
fn reload_validator() {
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=http://deno.land/",
"script.ts"
]);
assert!(r.is_ok(), "should accept valid urls");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=http://deno.land/a,http://deno.land/b",
"script.ts"
]);
assert!(r.is_ok(), "should accept accept multiple valid urls");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=./relativeurl/",
"script.ts"
]);
assert!(r.is_err(), "Should reject relative urls that start with ./");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=relativeurl/",
"script.ts"
]);
assert!(r.is_err(), "Should reject relative urls");
let r =
flags_from_vec(svec!["deno", "run", "--reload=/absolute", "script.ts"]);
assert!(r.is_err(), "Should reject absolute urls");
let r = flags_from_vec(svec!["deno", "run", "--reload=/", "script.ts"]);
assert!(r.is_err(), "Should reject absolute root url");
let r = flags_from_vec(svec!["deno", "run", "--reload=", "script.ts"]);
assert!(r.is_err(), "Should reject when nothing is provided");
let r = flags_from_vec(svec!["deno", "run", "--reload=,", "script.ts"]);
assert!(r.is_err(), "Should reject when a single comma is provided");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=,http://deno.land/a",
"script.ts"
]);
assert!(r.is_err(), "Should reject a leading comma");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=http://deno.land/a,",
"script.ts"
]);
assert!(r.is_err(), "Should reject a trailing comma");
let r = flags_from_vec(svec![
"deno",
"run",
"--reload=http://deno.land/a,,http://deno.land/b",
"script.ts"
]);
assert!(r.is_err(), "Should reject adjacent commas");
}
#[test]
fn bundle() {
let r = flags_from_vec(svec!["deno", "bundle", "source.ts"]);