diff options
Diffstat (limited to 'cli/flags.rs')
-rw-r--r-- | cli/flags.rs | 160 |
1 files changed, 147 insertions, 13 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index dfa617462..9a292b177 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -36,6 +36,13 @@ static LONG_VERSION: Lazy<String> = Lazy::new(|| { }); #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct BenchFlags { + pub ignore: Vec<PathBuf>, + pub include: Option<Vec<String>>, + pub filter: Option<String>, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct BundleFlags { pub source_file: String, pub out_file: Option<PathBuf>, @@ -177,6 +184,7 @@ pub struct VendorFlags { #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum DenoSubcommand { + Bench(BenchFlags), Bundle(BundleFlags), Cache(CacheFlags), Compile(CompileFlags), @@ -487,26 +495,27 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> { } match matches.subcommand() { - Some(("run", m)) => run_parse(&mut flags, m), - Some(("fmt", m)) => fmt_parse(&mut flags, m), - Some(("types", m)) => types_parse(&mut flags, m), + Some(("bench", m)) => bench_parse(&mut flags, m), + Some(("bundle", m)) => bundle_parse(&mut flags, m), Some(("cache", m)) => cache_parse(&mut flags, m), + Some(("compile", m)) => compile_parse(&mut flags, m), + Some(("completions", m)) => completions_parse(&mut flags, m, app), Some(("coverage", m)) => coverage_parse(&mut flags, m), - Some(("info", m)) => info_parse(&mut flags, m), + Some(("doc", m)) => doc_parse(&mut flags, m), Some(("eval", m)) => eval_parse(&mut flags, m), - Some(("repl", m)) => repl_parse(&mut flags, m), - Some(("bundle", m)) => bundle_parse(&mut flags, m), + Some(("fmt", m)) => fmt_parse(&mut flags, m), + Some(("info", m)) => info_parse(&mut flags, m), Some(("install", m)) => install_parse(&mut flags, m), - Some(("uninstall", m)) => uninstall_parse(&mut flags, m), - Some(("completions", m)) => completions_parse(&mut flags, m, app), - Some(("test", m)) => test_parse(&mut flags, m), - Some(("upgrade", m)) => upgrade_parse(&mut flags, m), - Some(("doc", m)) => doc_parse(&mut flags, m), Some(("lint", m)) => lint_parse(&mut flags, m), - Some(("compile", m)) => compile_parse(&mut flags, m), Some(("lsp", m)) => lsp_parse(&mut flags, m), - Some(("vendor", m)) => vendor_parse(&mut flags, m), + Some(("repl", m)) => repl_parse(&mut flags, m), + Some(("run", m)) => run_parse(&mut flags, m), Some(("task", m)) => task_parse(&mut flags, m), + Some(("test", m)) => test_parse(&mut flags, m), + Some(("types", m)) => types_parse(&mut flags, m), + Some(("uninstall", m)) => uninstall_parse(&mut flags, m), + Some(("upgrade", m)) => upgrade_parse(&mut flags, m), + Some(("vendor", m)) => vendor_parse(&mut flags, m), _ => handle_repl_flags(&mut flags, ReplFlags { eval: None }), } @@ -560,6 +569,7 @@ If the flag is set, restrict these messages to errors.", ) .global(true), ) + .subcommand(bench_subcommand()) .subcommand(bundle_subcommand()) .subcommand(cache_subcommand()) .subcommand(compile_subcommand()) @@ -584,6 +594,50 @@ If the flag is set, restrict these messages to errors.", .after_help(ENV_VARIABLES_HELP) } +fn bench_subcommand<'a>() -> App<'a> { + runtime_args(App::new("bench"), true, false) + .setting(AppSettings::TrailingVarArg) + .arg( + Arg::new("ignore") + .long("ignore") + .takes_value(true) + .use_delimiter(true) + .require_equals(true) + .help("Ignore files"), + ) + .arg( + Arg::new("filter") + .setting(ArgSettings::AllowHyphenValues) + .long("filter") + .takes_value(true) + .help("Run benchmarks with this string or pattern in the bench name"), + ) + .arg( + Arg::new("files") + .help("List of file names to run") + .takes_value(true) + .multiple_values(true) + .multiple_occurrences(true), + ) + .arg(watch_arg(false)) + .arg(no_clear_screen_arg()) + .arg(script_arg().last(true)) + .about("Run benchmarks") + .long_about( + "Run benchmarks using Deno's built-in bench tool. + +Evaluate the given modules, run all benches declared with 'Deno.bench()' and +report results to standard output: + + deno bench src/fetch_bench.ts src/signal_bench.ts + +Directory arguments are expanded to all contained files matching the glob +{*_,*.,}bench.{js,mjs,ts,jsx,tsx}: + + deno bench src/", + ) +} + fn bundle_subcommand<'a>() -> App<'a> { compile_args(App::new("bundle")) .arg(Arg::new("source_file").takes_value(true).required(true)) @@ -1880,6 +1934,51 @@ fn unsafely_ignore_certificate_errors_arg<'a>() -> Arg<'a> { .validator(crate::flags_allow_net::validator) } +fn bench_parse(flags: &mut Flags, matches: &clap::ArgMatches) { + runtime_args_parse(flags, matches, true, false); + + // NOTE: `deno bench` always uses `--no-prompt`, tests shouldn't ever do + // interactive prompts, unless done by user code + flags.no_prompt = true; + + let ignore = match matches.values_of("ignore") { + Some(f) => f.map(PathBuf::from).collect(), + None => vec![], + }; + + let filter = matches.value_of("filter").map(String::from); + + if matches.is_present("script_arg") { + let script_arg: Vec<String> = matches + .values_of("script_arg") + .unwrap() + .map(String::from) + .collect(); + + for v in script_arg { + flags.argv.push(v); + } + } + + let include = if matches.is_present("files") { + let files: Vec<String> = matches + .values_of("files") + .unwrap() + .map(String::from) + .collect(); + Some(files) + } else { + None + }; + + watch_arg_parse(flags, matches, false); + flags.subcommand = DenoSubcommand::Bench(BenchFlags { + include, + ignore, + filter, + }); +} + fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) { compile_args_parse(flags, matches); @@ -5166,4 +5265,39 @@ mod tests { } ); } + + #[test] + fn bench_with_flags() { + let r = flags_from_vec(svec![ + "deno", + "bench", + "--unstable", + "--filter", + "- foo", + "--location", + "https:foo", + "--allow-net", + "dir1/", + "dir2/", + "--", + "arg1", + "arg2" + ]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Bench(BenchFlags { + filter: Some("- foo".to_string()), + include: Some(svec!["dir1/", "dir2/"]), + ignore: vec![], + }), + unstable: true, + location: Some(Url::parse("https://foo/").unwrap()), + allow_net: Some(vec![]), + no_prompt: true, + argv: svec!["arg1", "arg2"], + ..Flags::default() + } + ); + } } |