0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-09 13:49:37 -04:00

test_extract_path_args

This commit is contained in:
Ryan Dahl 2022-01-08 11:22:36 -05:00
parent 795f2ebb6e
commit 9dca2f8715
2 changed files with 51 additions and 31 deletions

View file

@ -166,11 +166,9 @@ pub fn discover(flags: &crate::Flags) -> Result<Option<ConfigFile>, AnyError> {
Ok(Some(ConfigFile::read(config_path)?))
} else {
let mut checked = HashSet::new();
if let Some(files) = extract_path_args(flags) {
for f in files {
if let Some(cf) = discover_inner(&f, &mut checked)? {
return Ok(Some(cf));
}
for f in crate::flags::extract_path_args(flags) {
if let Some(cf) = discover_inner(&f, &mut checked)? {
return Ok(Some(cf));
}
}
@ -180,32 +178,6 @@ pub fn discover(flags: &crate::Flags) -> Result<Option<ConfigFile>, AnyError> {
}
}
/// Extract arguments from flags for config search paths.
fn extract_path_args(flags: &crate::Flags) -> Option<Vec<PathBuf>> {
use crate::flags::DenoSubcommand::*;
use crate::flags::FmtFlags;
use crate::flags::LintFlags;
use crate::flags::RunFlags;
if let Fmt(FmtFlags { files, .. }) = &flags.subcommand {
Some(files.clone())
} else if let Lint(LintFlags { files, .. }) = &flags.subcommand {
Some(files.clone())
} else if let Run(RunFlags { script }) = &flags.subcommand {
if let Ok(module_specifier) = deno_core::resolve_url_or_path(&script) {
if let Ok(p) = module_specifier.to_file_path() {
Some(vec![p])
} else {
None
}
} else {
None
}
} else {
None
}
}
fn discover_inner(
start: &Path,
checked: &mut HashSet<PathBuf>,

View file

@ -480,6 +480,28 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> {
Ok(flags)
}
/// Extract arguments from flags for config search paths.
pub fn extract_path_args(flags: &Flags) -> Vec<PathBuf> {
use DenoSubcommand::*;
if let Fmt(FmtFlags { files, .. }) = &flags.subcommand {
files.clone()
} else if let Lint(LintFlags { files, .. }) = &flags.subcommand {
files.clone()
} else if let Run(RunFlags { script }) = &flags.subcommand {
if let Ok(module_specifier) = deno_core::resolve_url_or_path(&script) {
if let Ok(p) = module_specifier.to_file_path() {
vec![p]
} else {
vec![]
}
} else {
vec![]
}
} else {
vec![]
}
}
fn clap_root<'a, 'b>(version: &'b str) -> App<'a, 'b> {
clap::App::new("deno")
.bin_name("deno")
@ -4635,4 +4657,30 @@ mod tests {
}
);
}
#[test]
fn test_extract_path_args() {
let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap();
assert_eq!(
extract_path_args(&flags),
vec![std::env::current_dir().unwrap().join("foo.js")]
);
let flags =
flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!(
extract_path_args(&flags),
vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]
);
let flags = flags_from_vec(svec!["deno", "lint"]).unwrap();
assert!(extract_path_args(&flags).is_empty());
let flags =
flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap();
assert_eq!(
extract_path_args(&flags),
vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]
);
}
}