Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix(testing): don't allow test files to call Deno.exit() #12938

Closed
wants to merge 10 commits into from
11 changes: 11 additions & 0 deletions cli/ops/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::tools::test::TestEvent;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::op_sync;
use deno_core::v8;
use deno_core::Extension;
use deno_core::ModuleSpecifier;
use deno_core::OpState;
Expand All @@ -25,6 +26,10 @@ pub fn init(sender: Sender<TestEvent>) -> Extension {
("op_get_test_origin", op_sync(op_get_test_origin)),
("op_dispatch_test_event", op_sync(op_dispatch_test_event)),
])
.middleware(|name, opfn| match name {
"op_exit" => op_sync(op_exit),
_ => opfn,
})
.state(move |state| {
state.put(sender.clone());
Ok(())
Expand Down Expand Up @@ -89,3 +94,9 @@ fn op_dispatch_test_event(

Ok(())
}

fn op_exit(state: &mut OpState, _: (), _: ()) -> Result<(), AnyError> {
let handle = state.borrow::<v8::IsolateHandle>();
handle.terminate_execution();
Ok(())
}
6 changes: 6 additions & 0 deletions cli/tests/integration/test_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ itest!(exit_sanitizer {
exit_code: 1,
});

itest!(exit_handler {
args: "test test/exit_handler.ts",
output: "test/exit_handler.out",
exit_code: 1,
});

itest!(clear_timeout {
args: "test test/clear_timeout.ts",
exit_code: 0,
Expand Down
24 changes: 24 additions & 0 deletions cli/tests/testdata/test/exit_handler.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Check [WILDCARD]exit_handler.ts
running 2 tests from [WILDCARD]exit_handler.ts
test ok ... ok ([WILDCARD])
test error ... FAILED ([WILDCARD])

failures:

error
Error: boom!
at [WILDCARD]exit_handler.ts:6:9
[WILDCARD]

failures:

error

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])

error: Uncaught Error: Test file attempted to exit with exit code: 0
Deno.exit(0);
^
[WILDCARD]
at self.onunload ([WILDCARD]exit_handler.ts:10:8)
[WILDCARD]
11 changes: 11 additions & 0 deletions cli/tests/testdata/test/exit_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Deno.test("ok", function () {
// pass
});

Deno.test("error", function () {
throw new Error("boom!");
});

self.onunload = () => {
Deno.exit(0);
};
5 changes: 5 additions & 0 deletions cli/tests/testdata/test/exit_set_code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deno.test("ok", function () {
// pass
});

Deno.core.opSync("op_set_exit_code", 42);
11 changes: 11 additions & 0 deletions cli/tests/testdata/test/exit_set_code2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Deno.test("ok", function () {
// pass
});

Deno.test("error", function () {
throw new Error("boom!");
});

self.onunload = () => {
Deno.exit(0);
};
10 changes: 10 additions & 0 deletions cli/tools/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use deno_core::futures::stream;
use deno_core::futures::FutureExt;
use deno_core::futures::StreamExt;
use deno_core::serde_json::json;
use deno_core::v8;
use deno_core::ModuleSpecifier;
use deno_graph::Module;
use deno_runtime::permissions::Permissions;
Expand Down Expand Up @@ -468,6 +469,15 @@ async fn test_specifier(
vec![ops::testing::init(channel.clone())],
);

{
let js_runtime = &mut worker.js_runtime;
let thread_safe_handle = js_runtime.v8_isolate().thread_safe_handle();
js_runtime
.op_state()
.borrow_mut()
.put::<v8::IsolateHandle>(thread_safe_handle);
}

let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
ps.coverage_dir
{
Expand Down
11 changes: 10 additions & 1 deletion core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,16 @@ impl OpTable {
F: Fn(Rc<RefCell<OpState>>, OpPayload) -> Op + 'static,
{
let (op_id, prev) = self.0.insert_full(name.to_owned(), Rc::new(op_fn));
assert!(prev.is_none());
assert!(prev.is_none(), "Op '{}' was already registered", name);
op_id
}

pub fn overwrite_op<F>(&mut self, name: &str, op_fn: F) -> OpId
where
F: Fn(Rc<RefCell<OpState>>, OpPayload) -> Op + 'static,
{
let (op_id, prev) = self.0.insert_full(name.to_owned(), Rc::new(op_fn));
assert!(prev.is_some(), "Op '{}' wasn't already registered", name);
op_id
}

Expand Down
12 changes: 12 additions & 0 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,18 @@ impl JsRuntime {
.register_op(name, op_fn)
}

pub fn overwrite_op<F>(&mut self, name: &str, op_fn: F) -> OpId
where
F: Fn(Rc<RefCell<OpState>>, OpPayload) -> Op + 'static,
{
Self::state(self.v8_isolate())
.borrow_mut()
.op_state
.borrow_mut()
.op_table
.overwrite_op(name, op_fn)
}

/// Registers a callback on the isolate when the memory limits are approached.
/// Use this to prevent V8 from crashing the process when reaching the limit.
///
Expand Down