From 14a44464a6e2c078fd329c26eff2ecfbc9b58603 Mon Sep 17 00:00:00 2001 From: Takahiko Inayama Date: Tue, 7 Jul 2020 20:05:28 +0900 Subject: [PATCH] feat: add lockfile support to bundle (#6624) --- cli/flags.rs | 26 +++++++++++++++++++++++ cli/tests/integration_tests.rs | 7 ++++++ cli/tests/lock_check_err_with_bundle.json | 5 +++++ cli/tests/lock_check_err_with_bundle.out | 3 +++ cli/tsc.rs | 22 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 cli/tests/lock_check_err_with_bundle.json create mode 100644 cli/tests/lock_check_err_with_bundle.out diff --git a/cli/flags.rs b/cli/flags.rs index bcc04d94de..b78dc646a2 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -381,6 +381,7 @@ fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) { config_arg_parse(flags, matches); importmap_arg_parse(flags, matches); unstable_arg_parse(flags, matches); + lock_args_parse(flags, matches); let source_file = matches.value_of("source_file").unwrap().to_string(); @@ -720,6 +721,8 @@ These must be added to the path manually if required.") fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("bundle") + .arg(lock_arg()) + .arg(lock_write_arg()) .arg( Arg::with_name("source_file") .takes_value(true) @@ -2109,6 +2112,29 @@ mod tests { ); } + #[test] + fn bundle_with_lock() { + let r = flags_from_vec_safe(svec![ + "deno", + "bundle", + "--lock-write", + "--lock=lock.json", + "source.ts" + ]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Bundle { + source_file: "source.ts".to_string(), + out_file: None, + }, + lock_write: true, + lock: Some("lock.json".to_string()), + ..Flags::default() + } + ); + } + #[test] fn run_importmap() { let r = flags_from_vec_safe(svec![ diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 651ffa110e..562c25de2b 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1629,6 +1629,13 @@ itest!(lock_check_err2 { http_server: true, }); +itest!(lock_check_err_with_bundle { + args: "bundle --lock=lock_check_err_with_bundle.json http://127.0.0.1:4545/cli/tests/subdir/mod1.ts", + output: "lock_check_err_with_bundle.out", + exit_code: 10, + http_server: true, +}); + itest!(async_error { exit_code: 1, args: "run --reload async_error.ts", diff --git a/cli/tests/lock_check_err_with_bundle.json b/cli/tests/lock_check_err_with_bundle.json new file mode 100644 index 0000000000..ae8f9dd9d9 --- /dev/null +++ b/cli/tests/lock_check_err_with_bundle.json @@ -0,0 +1,5 @@ +{ + "http://127.0.0.1:4545/cli/tests/subdir/mod1.ts": "f627f1649f9853adfa096241ae2defa75e4e327cbeb6af0e82a11304b3e5c8be", + "http://127.0.0.1:4545/cli/tests/subdir/print_hello.ts": "fe7bbccaedb6579200a8b582f905139296402d06b1b91109d6e12c41a23125da", + "http://127.0.0.1:4545/cli/tests/subdir/subdir2/mod2.ts": "bad" +} diff --git a/cli/tests/lock_check_err_with_bundle.out b/cli/tests/lock_check_err_with_bundle.out new file mode 100644 index 0000000000..ce0e029efe --- /dev/null +++ b/cli/tests/lock_check_err_with_bundle.out @@ -0,0 +1,3 @@ +[WILDCARD] +Subresource integrity check failed --lock=lock_check_err_with_bundle.json +http://127.0.0.1:4545/cli/tests/subdir/subdir2/mod2.ts diff --git a/cli/tsc.rs b/cli/tsc.rs index 0d61e2e723..9f8216e52b 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -625,6 +625,28 @@ impl TsCompiler { .add_to_graph(&module_specifier, None) .await?; let module_graph = module_graph_loader.get_graph(); + let module_graph_files = module_graph.values().collect::>(); + // Check integrity of every file in module graph + if let Some(ref lockfile) = global_state.lockfile { + let mut g = lockfile.lock().unwrap(); + + for graph_file in &module_graph_files { + let check_passed = + g.check_or_insert(&graph_file.url, &graph_file.source_code); + + if !check_passed { + eprintln!( + "Subresource integrity check failed --lock={}\n{}", + g.filename, graph_file.url + ); + std::process::exit(10); + } + } + } + if let Some(ref lockfile) = global_state.lockfile { + let g = lockfile.lock().unwrap(); + g.write()?; + } let module_graph_json = serde_json::to_value(module_graph).expect("Failed to serialize data");