From a35c8e6588fec21586bcb19146cad19fa01f4f23 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 13 Mar 2023 17:04:00 -0400 Subject: [PATCH] fix(info/doc): add missing `--no-lock` and `--lock` flags (#18166) Closes #18159 --- cli/args/flags.rs | 91 ++++++++++++------- cli/tests/integration/bench_tests.rs | 15 +++ cli/tests/integration/doc_tests.rs | 15 +++ cli/tests/integration/info_tests.rs | 15 +++ cli/tests/integration/test_tests.rs | 15 +++ .../testdata/lockfile/basic/bench.nolock.out | 7 ++ cli/tests/testdata/lockfile/basic/deno.json | 5 + cli/tests/testdata/lockfile/basic/deno.lock | 6 ++ .../testdata/lockfile/basic/doc.nolock.out | 1 + cli/tests/testdata/lockfile/basic/fail.out | 4 + .../testdata/lockfile/basic/info.nolock.out | 8 ++ .../testdata/lockfile/basic/main.bench.ts | 8 ++ .../testdata/lockfile/basic/main.test.ts | 8 ++ cli/tests/testdata/lockfile/basic/main.ts | 1 + cli/tests/testdata/lockfile/basic/mod.ts | 1 + .../testdata/lockfile/basic/test.nolock.out | 5 + cli/tools/doc.rs | 6 ++ cli/tools/info.rs | 5 + 18 files changed, 182 insertions(+), 34 deletions(-) create mode 100644 cli/tests/testdata/lockfile/basic/bench.nolock.out create mode 100644 cli/tests/testdata/lockfile/basic/deno.json create mode 100644 cli/tests/testdata/lockfile/basic/deno.lock create mode 100644 cli/tests/testdata/lockfile/basic/doc.nolock.out create mode 100644 cli/tests/testdata/lockfile/basic/fail.out create mode 100644 cli/tests/testdata/lockfile/basic/info.nolock.out create mode 100644 cli/tests/testdata/lockfile/basic/main.bench.ts create mode 100644 cli/tests/testdata/lockfile/basic/main.test.ts create mode 100644 cli/tests/testdata/lockfile/basic/main.ts create mode 100644 cli/tests/testdata/lockfile/basic/mod.ts create mode 100644 cli/tests/testdata/lockfile/basic/test.nolock.out diff --git a/cli/args/flags.rs b/cli/args/flags.rs index f789388697..26cf497f6a 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -1092,6 +1092,10 @@ Show documentation for runtime built-ins: ) .arg(import_map_arg()) .arg(reload_arg()) + .arg(lock_arg()) + .arg(no_lock_arg()) + .arg(no_npm_arg()) + .arg(no_remote_arg()) .arg( Arg::new("json") .long("json") @@ -1339,9 +1343,12 @@ TypeScript compiler cache: Subdirectory containing TS compiler output.", .conflicts_with("file") .help("Show files used for origin bound APIs like the Web Storage API when running a script with '--location='") ) - // TODO(lucacasonato): remove for 2.0 - .arg(no_check_arg().hide(true)) + .arg(no_check_arg().hide(true)) // TODO(lucacasonato): remove for 2.0 .arg(no_config_arg()) + .arg(no_remote_arg()) + .arg(no_npm_arg()) + .arg(no_lock_arg()) + .arg(lock_arg()) .arg(config_arg()) .arg(import_map_arg()) .arg(local_npm_arg()) @@ -1890,20 +1897,7 @@ Remote modules and multiple modules may also be specified: } fn compile_args(app: Command) -> Command { - app - .arg(import_map_arg()) - .arg(no_remote_arg()) - .arg(no_npm_arg()) - .arg(local_npm_arg()) - .arg(no_config_arg()) - .arg(config_arg()) - .arg(no_check_arg()) - .arg(check_arg()) - .arg(reload_arg()) - .arg(lock_arg()) - .arg(lock_write_arg()) - .arg(no_lock_arg()) - .arg(ca_file_arg()) + compile_args_without_check_args(app.arg(no_check_arg()).arg(check_arg())) } fn compile_args_without_check_args(app: Command) -> Command { @@ -2304,6 +2298,7 @@ fn lock_write_arg<'a>() -> Arg<'a> { Arg::new("lock-write") .long("lock-write") .help("Force overwriting the lock file.") + .conflicts_with("no-lock") } fn no_lock_arg<'a>() -> Arg<'a> { @@ -2311,7 +2306,6 @@ fn no_lock_arg<'a>() -> Arg<'a> { .long("no-lock") .help("Disable auto discovery of the lock file.") .conflicts_with("lock") - .conflicts_with("lock-write") } static CONFIG_HELP: Lazy = Lazy::new(|| { @@ -2461,7 +2455,7 @@ fn cache_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn check_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.type_check_mode = TypeCheckMode::Local; - compile_args_without_no_check_parse(flags, matches); + compile_args_without_check_parse(flags, matches); let files = matches .values_of("file") .unwrap() @@ -2559,6 +2553,10 @@ fn coverage_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); reload_arg_parse(flags, matches); + lock_arg_parse(flags, matches); + no_lock_arg_parse(flags, matches); + no_npm_arg_parse(flags, matches); + no_remote_arg_parse(flags, matches); let source_file = matches .value_of("source_file") @@ -2683,6 +2681,10 @@ fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { location_arg_parse(flags, matches); ca_file_arg_parse(flags, matches); local_npm_args_parse(flags, matches); + lock_arg_parse(flags, matches); + no_lock_arg_parse(flags, matches); + no_remote_arg_parse(flags, matches); + no_npm_arg_parse(flags, matches); let json = matches.is_present("json"); flags.subcommand = DenoSubcommand::Info(InfoFlags { file: matches.value_of("file").map(|f| f.to_string()), @@ -3011,19 +3013,12 @@ fn vendor_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } fn compile_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { - import_map_arg_parse(flags, matches); - no_remote_arg_parse(flags, matches); - no_npm_arg_parse(flags, matches); - local_npm_args_parse(flags, matches); - config_args_parse(flags, matches); + compile_args_without_check_parse(flags, matches); no_check_arg_parse(flags, matches); check_arg_parse(flags, matches); - reload_arg_parse(flags, matches); - lock_args_parse(flags, matches); - ca_file_arg_parse(flags, matches); } -fn compile_args_without_no_check_parse( +fn compile_args_without_check_parse( flags: &mut Flags, matches: &clap::ArgMatches, ) { @@ -3116,6 +3111,7 @@ fn unsafely_ignore_certificate_errors_parse( flags.unsafely_ignore_certificate_errors = Some(ic_allowlist); } } + fn runtime_args_parse( flags: &mut Flags, matches: &clap::ArgMatches, @@ -3259,12 +3255,10 @@ fn check_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn lock_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { lock_arg_parse(flags, matches); + no_lock_arg_parse(flags, matches); if matches.is_present("lock-write") { flags.lock_write = true; } - if matches.is_present("no-lock") { - flags.no_lock = true; - } } fn lock_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -3278,6 +3272,12 @@ fn lock_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } } +fn no_lock_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { + if matches.is_present("no-lock") { + flags.no_lock = true; + } +} + fn config_args_parse(flags: &mut Flags, matches: &ArgMatches) { flags.config_flag = if matches.is_present("no-config") { ConfigFlag::Disabled @@ -4289,7 +4289,14 @@ mod tests { } ); - let r = flags_from_vec(svec!["deno", "info", "--config", "tsconfig.json"]); + let r = flags_from_vec(svec![ + "deno", + "info", + "--no-npm", + "--no-remote", + "--config", + "tsconfig.json" + ]); assert_eq!( r.unwrap(), Flags { @@ -4298,6 +4305,8 @@ mod tests { file: None }), config_flag: ConfigFlag::Path("tsconfig.json".to_owned()), + no_npm: true, + no_remote: true, ..Flags::default() } ); @@ -5715,7 +5724,7 @@ mod tests { #[test] fn test_with_flags() { #[rustfmt::skip] - let r = flags_from_vec(svec!["deno", "test", "--unstable", "--trace-ops", "--no-run", "--filter", "- foo", "--coverage=cov", "--location", "https:foo", "--allow-net", "--allow-none", "dir1/", "dir2/", "--", "arg1", "arg2"]); + let r = flags_from_vec(svec!["deno", "test", "--unstable", "--no-npm", "--no-remote", "--trace-ops", "--no-run", "--filter", "- foo", "--coverage=cov", "--location", "https:foo", "--allow-net", "--allow-none", "dir1/", "dir2/", "--", "arg1", "arg2"]); assert_eq!( r.unwrap(), Flags { @@ -5735,6 +5744,8 @@ mod tests { }), unstable: true, no_prompt: true, + no_npm: true, + no_remote: true, coverage_dir: Some("cov".to_string()), location: Some(Url::parse("https://foo/").unwrap()), type_check_mode: TypeCheckMode::Local, @@ -6140,8 +6151,14 @@ mod tests { } ); - let r = - flags_from_vec(svec!["deno", "doc", "--private", "path/to/module.js"]); + let r = flags_from_vec(svec![ + "deno", + "doc", + "--no-npm", + "--no-remote", + "--private", + "path/to/module.js" + ]); assert_eq!( r.unwrap(), Flags { @@ -6151,6 +6168,8 @@ mod tests { source_file: DocSourceFileFlag::Path("path/to/module.js".to_string()), filter: None, }), + no_npm: true, + no_remote: true, ..Flags::default() } ); @@ -6617,6 +6636,8 @@ mod tests { "bench", "--json", "--unstable", + "--no-npm", + "--no-remote", "--filter", "- foo", "--location", @@ -6640,6 +6661,8 @@ mod tests { }, }), unstable: true, + no_npm: true, + no_remote: true, type_check_mode: TypeCheckMode::Local, location: Some(Url::parse("https://foo/").unwrap()), allow_net: Some(vec![]), diff --git a/cli/tests/integration/bench_tests.rs b/cli/tests/integration/bench_tests.rs index b2cd38475d..e5174025e0 100644 --- a/cli/tests/integration/bench_tests.rs +++ b/cli/tests/integration/bench_tests.rs @@ -224,3 +224,18 @@ itest!(package_json_basic { copy_temp_dir: Some("package_json/basic"), exit_code: 0, }); + +itest!(bench_lock { + args: "bench", + http_server: true, + cwd: Some("lockfile/basic"), + exit_code: 10, + output: "lockfile/basic/fail.out", +}); + +itest!(bench_no_lock { + args: "bench --no-lock", + http_server: true, + cwd: Some("lockfile/basic"), + output: "lockfile/basic/bench.nolock.out", +}); diff --git a/cli/tests/integration/doc_tests.rs b/cli/tests/integration/doc_tests.rs index de611d4056..72f5f43140 100644 --- a/cli/tests/integration/doc_tests.rs +++ b/cli/tests/integration/doc_tests.rs @@ -64,3 +64,18 @@ itest!(deno_doc_invalid_url { output: "doc/invalid_url.out", exit_code: 1, }); + +itest!(doc_lock { + args: "doc main.ts", + http_server: true, + cwd: Some("lockfile/basic"), + exit_code: 10, + output: "lockfile/basic/fail.out", +}); + +itest!(doc_no_lock { + args: "doc --no-lock main.ts", + http_server: true, + cwd: Some("lockfile/basic"), + output: "lockfile/basic/doc.nolock.out", +}); diff --git a/cli/tests/integration/info_tests.rs b/cli/tests/integration/info_tests.rs index 704aaa7afc..5b2f2d740b 100644 --- a/cli/tests/integration/info_tests.rs +++ b/cli/tests/integration/info_tests.rs @@ -94,6 +94,21 @@ itest!(info_missing_module { output: "info/info_missing_module.out", }); +itest!(info_lock { + args: "info main.ts", + http_server: true, + cwd: Some("lockfile/basic"), + exit_code: 10, + output: "lockfile/basic/fail.out", +}); + +itest!(info_no_lock { + args: "info --no-lock main.ts", + http_server: true, + cwd: Some("lockfile/basic"), + output: "lockfile/basic/info.nolock.out", +}); + itest!(info_recursive_modules { args: "info --quiet info/info_recursive_imports_test.ts", output: "info/info_recursive_imports_test.out", diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index de7bc5fede..047dcbc7ef 100644 --- a/cli/tests/integration/test_tests.rs +++ b/cli/tests/integration/test_tests.rs @@ -453,3 +453,18 @@ itest!(package_json_basic { copy_temp_dir: Some("package_json/basic"), exit_code: 0, }); + +itest!(test_lock { + args: "test", + http_server: true, + cwd: Some("lockfile/basic"), + exit_code: 10, + output: "lockfile/basic/fail.out", +}); + +itest!(test_no_lock { + args: "test --no-lock", + http_server: true, + cwd: Some("lockfile/basic"), + output: "lockfile/basic/test.nolock.out", +}); diff --git a/cli/tests/testdata/lockfile/basic/bench.nolock.out b/cli/tests/testdata/lockfile/basic/bench.nolock.out new file mode 100644 index 0000000000..e81474f2ac --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/bench.nolock.out @@ -0,0 +1,7 @@ +Download http://localhost:4545/lockfile/basic/mod.ts +Check file:///[WILDCARD]/main.bench.ts +5 +cpu: [WILDCARD] +runtime: [WILDCARD] + +[WILDCARD] diff --git a/cli/tests/testdata/lockfile/basic/deno.json b/cli/tests/testdata/lockfile/basic/deno.json new file mode 100644 index 0000000000..d6541b78eb --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/deno.json @@ -0,0 +1,5 @@ +{ + "imports": { + "mod": "http://localhost:4545/lockfile/basic/mod.ts" + } +} diff --git a/cli/tests/testdata/lockfile/basic/deno.lock b/cli/tests/testdata/lockfile/basic/deno.lock new file mode 100644 index 0000000000..42ab94f9bc --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/deno.lock @@ -0,0 +1,6 @@ +{ + "version": "2", + "remote": { + "http://localhost:4545/lockfile/basic/mod.ts": "invalid" + } +} diff --git a/cli/tests/testdata/lockfile/basic/doc.nolock.out b/cli/tests/testdata/lockfile/basic/doc.nolock.out new file mode 100644 index 0000000000..e2d66c027b --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/doc.nolock.out @@ -0,0 +1 @@ +Download http://localhost:4545/lockfile/basic/mod.ts diff --git a/cli/tests/testdata/lockfile/basic/fail.out b/cli/tests/testdata/lockfile/basic/fail.out new file mode 100644 index 0000000000..6a808c0a54 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/fail.out @@ -0,0 +1,4 @@ +Download http://localhost:4545/lockfile/basic/mod.ts +error: The source code is invalid, as it does not match the expected hash in the lock file. + Specifier: [WILDCARD]mod.ts + Lock file: [WILDCARD]deno.lock diff --git a/cli/tests/testdata/lockfile/basic/info.nolock.out b/cli/tests/testdata/lockfile/basic/info.nolock.out new file mode 100644 index 0000000000..d1ef82e49f --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/info.nolock.out @@ -0,0 +1,8 @@ +Download http://localhost:4545/lockfile/basic/mod.ts +local: [WILDCARD]main.ts +type: TypeScript +dependencies: 1 unique +size: [WILDCARD] + +file:///[WILDCARD]/main.ts ([WILDCARD]) +└── http://localhost:4545/lockfile/basic/mod.ts ([WILDCARD]) diff --git a/cli/tests/testdata/lockfile/basic/main.bench.ts b/cli/tests/testdata/lockfile/basic/main.bench.ts new file mode 100644 index 0000000000..2a74576801 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/main.bench.ts @@ -0,0 +1,8 @@ +import "./main.ts"; + +Deno.bench("bench", () => { + const testing = 1 + 2; + if (testing !== 3) { + throw "FAIL"; + } +}); diff --git a/cli/tests/testdata/lockfile/basic/main.test.ts b/cli/tests/testdata/lockfile/basic/main.test.ts new file mode 100644 index 0000000000..bb757de239 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/main.test.ts @@ -0,0 +1,8 @@ +import "./main.ts"; + +Deno.test("test", () => { + const testing = 1 + 2; + if (testing !== 3) { + throw "FAIL"; + } +}); diff --git a/cli/tests/testdata/lockfile/basic/main.ts b/cli/tests/testdata/lockfile/basic/main.ts new file mode 100644 index 0000000000..5eed100f54 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/main.ts @@ -0,0 +1 @@ +import "mod"; diff --git a/cli/tests/testdata/lockfile/basic/mod.ts b/cli/tests/testdata/lockfile/basic/mod.ts new file mode 100644 index 0000000000..0f3785f910 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/mod.ts @@ -0,0 +1 @@ +console.log(5); diff --git a/cli/tests/testdata/lockfile/basic/test.nolock.out b/cli/tests/testdata/lockfile/basic/test.nolock.out new file mode 100644 index 0000000000..3b59995132 --- /dev/null +++ b/cli/tests/testdata/lockfile/basic/test.nolock.out @@ -0,0 +1,5 @@ +Download http://localhost:4545/lockfile/basic/mod.ts +Check file:///[WILDCARD]/main.test.ts +5 +running 1 test from ./main.test.ts +[WILDCARD] diff --git a/cli/tools/doc.rs b/cli/tools/doc.rs index e0413ab798..1f8bfad7ab 100644 --- a/cli/tools/doc.rs +++ b/cli/tools/doc.rs @@ -7,6 +7,7 @@ use crate::colors; use crate::display::write_json_to_stdout; use crate::display::write_to_stdout_ignore_sigpipe; use crate::file_fetcher::File; +use crate::graph_util::graph_lock_or_exit; use crate::proc_state::ProcState; use crate::tsc::get_types_declaration_file_text; use deno_ast::MediaType; @@ -77,6 +78,11 @@ pub async fn print_docs( ps.file_fetcher.insert_cached(root); let graph = ps.create_graph(vec![root_specifier.clone()]).await?; + + if let Some(lockfile) = &ps.lockfile { + graph_lock_or_exit(&graph, &mut lockfile.lock()); + } + let doc_parser = doc::DocParser::new( graph, doc_flags.private, diff --git a/cli/tools/info.rs b/cli/tools/info.rs index 8a7f4b6b98..f7284154a8 100644 --- a/cli/tools/info.rs +++ b/cli/tools/info.rs @@ -23,6 +23,7 @@ use deno_runtime::colors; use crate::args::Flags; use crate::args::InfoFlags; use crate::display; +use crate::graph_util::graph_lock_or_exit; use crate::npm::NpmPackageId; use crate::npm::NpmPackageResolver; use crate::npm::NpmResolutionPackage; @@ -40,6 +41,10 @@ pub async fn info(flags: Flags, info_flags: InfoFlags) -> Result<(), AnyError> { .create_graph_with_loader(vec![specifier], &mut loader) .await?; + if let Some(lockfile) = &ps.lockfile { + graph_lock_or_exit(&graph, &mut lockfile.lock()); + } + if info_flags.json { let mut json_graph = json!(graph); add_npm_packages_to_json(&mut json_graph, &ps.npm_resolver);