From 22bd1fee856a8af591da1f4650c146edb2e4c1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 24 Jun 2022 12:00:53 +0200 Subject: [PATCH] fix: don't error if Deno.bench() or Deno.test() are used in run subcommand (#14946) --- cli/lsp/testing/execution.rs | 5 +++++ cli/tests/integration/run_tests.rs | 5 +++++ cli/tests/testdata/test_and_bench_in_run.js | 5 +++++ cli/tools/bench.rs | 5 +++++ cli/tools/test.rs | 5 +++++ runtime/js/40_testing.js | 17 +++++++++++++++++ 6 files changed, 42 insertions(+) create mode 100644 cli/tests/testdata/test_and_bench_in_run.js diff --git a/cli/lsp/testing/execution.rs b/cli/lsp/testing/execution.rs index 4f4b9bf1f1..03fdba63c4 100644 --- a/cli/lsp/testing/execution.rs +++ b/cli/lsp/testing/execution.rs @@ -199,6 +199,11 @@ async fn test_specifier( }, ); + worker.js_runtime.execute_script( + &located_script_name!(), + r#"Deno[Deno.internal].enableTestAndBench()"#, + )?; + worker .execute_script( &located_script_name!(), diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index fb0d6313b9..09a1ba500e 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2728,3 +2728,8 @@ fn running_declaration_files() { assert!(output.status.success()); } } + +itest!(test_and_bench_are_noops_in_run { + args: "run test_and_bench_in_run.js", + output_str: Some(""), +}); diff --git a/cli/tests/testdata/test_and_bench_in_run.js b/cli/tests/testdata/test_and_bench_in_run.js new file mode 100644 index 0000000000..108ae937a9 --- /dev/null +++ b/cli/tests/testdata/test_and_bench_in_run.js @@ -0,0 +1,5 @@ +Deno.test(function foo() { +}); + +Deno.bench(function bar() { +}); diff --git a/cli/tools/bench.rs b/cli/tools/bench.rs index e881516489..b51938d133 100644 --- a/cli/tools/bench.rs +++ b/cli/tools/bench.rs @@ -369,6 +369,11 @@ async fn bench_specifier( Default::default(), ); + worker.js_runtime.execute_script( + &located_script_name!(), + r#"Deno[Deno.internal].enableTestAndBench()"#, + )?; + if options.compat_mode { worker.execute_side_module(&compat::GLOBAL_URL).await?; worker.execute_side_module(&compat::MODULE_URL).await?; diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 170c1a12d5..94412a3ae8 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -743,6 +743,11 @@ async fn test_specifier( }, ); + worker.js_runtime.execute_script( + &located_script_name!(), + r#"Deno[Deno.internal].enableTestAndBench()"#, + )?; + let mut maybe_coverage_collector = if let Some(ref coverage_dir) = ps.coverage_dir { diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js index 3850e411d5..13cdcd32ed 100644 --- a/runtime/js/40_testing.js +++ b/runtime/js/40_testing.js @@ -554,6 +554,7 @@ const tests = []; /** @type {BenchDescription[]} */ const benchDescs = []; + let isTestOrBenchSubcommand = false; // Main test function provided by Deno. function test( @@ -561,6 +562,10 @@ optionsOrFn, maybeFn, ) { + if (!isTestOrBenchSubcommand) { + return; + } + let testDef; const defaults = { ignore: false, @@ -669,6 +674,10 @@ optionsOrFn, maybeFn, ) { + if (!isTestOrBenchSubcommand) { + return; + } + core.opSync("op_bench_check_unstable"); let benchDesc; const defaults = { @@ -1043,6 +1052,13 @@ return core.opSync("op_bench_now"); } + // This function is called by Rust side if we're in `deno test` or + // `deno bench` subcommand. If this function is not called then `Deno.test()` + // and `Deno.bench()` become noops. + function enableTestAndBench() { + isTestOrBenchSubcommand = true; + } + async function runTests({ filter = null, shuffle = null, @@ -1507,6 +1523,7 @@ window.__bootstrap.internals = { ...window.__bootstrap.internals ?? {}, + enableTestAndBench, runTests, runBenchmarks, };