1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(test): grab runTests() and runBenchmarks() from __bootstrap (#15420)

This commit is contained in:
Nayeem Rahman 2022-09-02 18:44:45 +01:00 committed by GitHub
parent 5262937285
commit 4f8dea100e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 128 additions and 64 deletions

View file

@ -38,12 +38,12 @@ failing step in failing test ... FAILED ([WILDCARD])
nested failure => ./test/steps/failing_steps.ts:[WILDCARD]
error: Error: 1 test step failed.
at runTest (deno:runtime/js/40_testing.js:[WILDCARD])
at async Object.runTests (deno:runtime/js/40_testing.js:[WILDCARD])
at async runTests (deno:runtime/js/40_testing.js:[WILDCARD])
multiple test step failures => ./test/steps/failing_steps.ts:[WILDCARD]
error: Error: 2 test steps failed.
at runTest (deno:runtime/js/40_testing.js:[WILDCARD])
at async Object.runTests (deno:runtime/js/40_testing.js:[WILDCARD])
at async runTests (deno:runtime/js/40_testing.js:[WILDCARD])
failing step in failing test => ./test/steps/failing_steps.ts:[WILDCARD]
error: Error: Fail test.

View file

@ -7,7 +7,6 @@ use deno_core::error::AnyError;
use deno_core::futures::task::LocalFutureObj;
use deno_core::futures::FutureExt;
use deno_core::located_script_name;
use deno_core::serde_json::json;
use deno_core::Extension;
use deno_core::ModuleId;
use deno_graph::source::ResolveResponse;
@ -220,10 +219,7 @@ impl CliMainWorker {
&mut self,
mode: TestMode,
) -> Result<(), AnyError> {
self.worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;
self.worker.enable_test();
// Enable op call tracing in core to enable better debugging of op sanitizer
// failures.
@ -273,17 +269,10 @@ impl CliMainWorker {
}
self.worker.dispatch_load_event(&located_script_name!())?;
let test_result = self.worker.js_runtime.execute_script(
&located_script_name!(),
&format!(
r#"Deno[Deno.internal].runTests({})"#,
json!({ "shuffle": self.ps.options.shuffle_tests() }),
),
)?;
self.worker.js_runtime.resolve_value(test_result).await?;
self
.worker
.run_tests(&self.ps.options.shuffle_tests())
.await?;
loop {
if !self
.worker
@ -309,10 +298,7 @@ impl CliMainWorker {
&mut self,
mode: TestMode,
) -> Result<(), AnyError> {
self.worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;
self.worker.enable_test();
self
.worker
@ -328,14 +314,7 @@ impl CliMainWorker {
}
self.worker.dispatch_load_event(&located_script_name!())?;
let test_result = self.worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].runTests()"#,
)?;
self.worker.js_runtime.resolve_value(test_result).await?;
self.worker.run_tests(&None).await?;
loop {
if !self
.worker
@ -350,10 +329,7 @@ impl CliMainWorker {
}
pub async fn run_bench_specifier(&mut self) -> Result<(), AnyError> {
self.worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].enableTestAndBench()"#,
)?;
self.worker.enable_bench();
if self.ps.options.compat() {
self.worker.execute_side_module(&compat::GLOBAL_URL).await?;
@ -383,14 +359,7 @@ impl CliMainWorker {
}
self.worker.dispatch_load_event(&located_script_name!())?;
let bench_result = self.worker.js_runtime.execute_script(
&located_script_name!(),
r#"Deno[Deno.internal].runBenchmarks()"#,
)?;
self.worker.js_runtime.resolve_value(bench_result).await?;
self.worker.run_benchmarks().await?;
loop {
if !self
.worker

View file

@ -629,7 +629,7 @@ impl JsRuntime {
.ok()
}
pub(crate) fn grab_global<'s, T>(
pub fn grab_global<'s, T>(
scope: &mut v8::HandleScope<'s>,
path: &str,
) -> Option<v8::Local<'s, T>>

View file

@ -16,6 +16,7 @@
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSort,
BigInt,
DateNow,
Error,
FunctionPrototype,
@ -600,7 +601,8 @@
const testStates = new Map();
/** @type {BenchDescription[]} */
const benchDescs = [];
let isTestOrBenchSubcommand = false;
let isTestSubcommand = false;
let isBenchSubcommand = false;
// Main test function provided by Deno.
function test(
@ -608,7 +610,7 @@
optionsOrFn,
maybeFn,
) {
if (!isTestOrBenchSubcommand) {
if (!isTestSubcommand) {
return;
}
@ -727,7 +729,7 @@
optionsOrFn,
maybeFn,
) {
if (!isTestOrBenchSubcommand) {
if (!isBenchSubcommand) {
return;
}
@ -1032,11 +1034,12 @@
return ops.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;
function enableTest() {
isTestSubcommand = true;
}
function enableBench() {
isBenchSubcommand = true;
}
async function runTests({
@ -1062,15 +1065,16 @@
if (shuffle !== null) {
// http://en.wikipedia.org/wiki/Linear_congruential_generator
// Use BigInt for everything because the random seed is u64.
const nextInt = (function (state) {
const m = 0x80000000;
const a = 1103515245;
const c = 12345;
const m = 0x80000000n;
const a = 1103515245n;
const c = 12345n;
return function (max) {
return state = ((a * state + c) % m) % max;
return state = ((a * state + c) % m) % BigInt(max);
};
}(shuffle));
}(BigInt(shuffle)));
for (let i = filtered.length - 1; i > 0; i--) {
const j = nextInt(i);
@ -1390,15 +1394,12 @@
return testFn;
}
window.__bootstrap.internals = {
...window.__bootstrap.internals ?? {},
enableTestAndBench,
runTests,
runBenchmarks,
};
window.__bootstrap.testing = {
test,
bench,
enableBench,
enableTest,
runBenchmarks,
runTests,
test,
};
})(this);

View file

@ -11,6 +11,9 @@ use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::Future;
use deno_core::located_script_name;
use deno_core::serde_json::json;
use deno_core::serde_v8;
use deno_core::v8;
use deno_core::CompiledWasmModuleStore;
use deno_core::Extension;
use deno_core::GetErrorClassFn;
@ -59,6 +62,10 @@ pub struct MainWorker {
pub js_runtime: JsRuntime,
should_break_on_first_statement: bool,
exit_code: ExitCode,
js_run_tests_callback: v8::Global<v8::Function>,
js_run_benchmarks_callback: v8::Global<v8::Function>,
js_enable_test_callback: v8::Global<v8::Function>,
js_enable_bench_callback: v8::Global<v8::Function>,
}
pub struct WorkerOptions {
@ -86,6 +93,15 @@ pub struct WorkerOptions {
pub stdio: Stdio,
}
fn grab_cb(
scope: &mut v8::HandleScope,
path: &str,
) -> v8::Global<v8::Function> {
let cb = JsRuntime::grab_global::<v8::Function>(scope, path)
.unwrap_or_else(|| panic!("{} must be defined", path));
v8::Global::new(scope, cb)
}
impl MainWorker {
pub fn bootstrap_from_options(
main_module: ModuleSpecifier,
@ -198,10 +214,29 @@ impl MainWorker {
);
}
let (
js_run_tests_callback,
js_run_benchmarks_callback,
js_enable_test_callback,
js_enable_bench_callback,
) = {
let scope = &mut js_runtime.handle_scope();
(
grab_cb(scope, "__bootstrap.testing.runTests"),
grab_cb(scope, "__bootstrap.testing.runBenchmarks"),
grab_cb(scope, "__bootstrap.testing.enableTest"),
grab_cb(scope, "__bootstrap.testing.enableBench"),
)
};
Self {
js_runtime,
should_break_on_first_statement: options.should_break_on_first_statement,
exit_code,
js_run_tests_callback,
js_run_benchmarks_callback,
js_enable_test_callback,
js_enable_bench_callback,
}
}
@ -289,6 +324,65 @@ impl MainWorker {
self.evaluate_module(id).await
}
/// Run tests declared with `Deno.test()`. Test events will be dispatched
/// by calling ops which are currently only implemented in the CLI crate.
// TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
#[doc(hidden)]
pub async fn run_tests(
&mut self,
shuffle: &Option<u64>,
) -> Result<(), AnyError> {
let promise = {
let scope = &mut self.js_runtime.handle_scope();
let cb = self.js_run_tests_callback.open(scope);
let this = v8::undefined(scope).into();
let options =
serde_v8::to_v8(scope, json!({ "shuffle": shuffle })).unwrap();
let promise = cb.call(scope, this, &[options]).unwrap();
v8::Global::new(scope, promise)
};
self.js_runtime.resolve_value(promise).await?;
Ok(())
}
/// Run benches declared with `Deno.bench()`. Bench events will be dispatched
/// by calling ops which are currently only implemented in the CLI crate.
// TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
#[doc(hidden)]
pub async fn run_benchmarks(&mut self) -> Result<(), AnyError> {
let promise = {
let scope = &mut self.js_runtime.handle_scope();
let cb = self.js_run_benchmarks_callback.open(scope);
let this = v8::undefined(scope).into();
let promise = cb.call(scope, this, &[]).unwrap();
v8::Global::new(scope, promise)
};
self.js_runtime.resolve_value(promise).await?;
Ok(())
}
/// Enable `Deno.test()`. If this isn't called before executing user code,
/// `Deno.test()` calls will noop.
// TODO(nayeemrmn): Move testing ops to deno_runtime and redesign/unhide.
#[doc(hidden)]
pub fn enable_test(&mut self) {
let scope = &mut self.js_runtime.handle_scope();
let cb = self.js_enable_test_callback.open(scope);
let this = v8::undefined(scope).into();
cb.call(scope, this, &[]).unwrap();
}
/// Enable `Deno.bench()`. If this isn't called before executing user code,
/// `Deno.bench()` calls will noop.
// TODO(nayeemrmn): Move benchmark ops to deno_runtime and redesign/unhide.
#[doc(hidden)]
pub fn enable_bench(&mut self) {
let scope = &mut self.js_runtime.handle_scope();
let cb = self.js_enable_bench_callback.open(scope);
let this = v8::undefined(scope).into();
cb.call(scope, this, &[]).unwrap();
}
fn wait_for_inspector_session(&mut self) {
if self.should_break_on_first_statement {
self