diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-04-26 19:00:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-26 19:00:04 -0400 |
commit | 58eab0e2b37fd8c3c83445196d4bde419740373d (patch) | |
tree | 213d98203d18ce6f261f0e4b240450e1c4db73fc /runtime | |
parent | 2c33293f665c4d86a2196c3b2c0aa45b15b533c3 (diff) |
fix(test): capture worker stdout and stderr in test output (#14410)
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/examples/hello_runtime.rs | 1 | ||||
-rw-r--r-- | runtime/ops/io.rs | 64 | ||||
-rw-r--r-- | runtime/web_worker.rs | 4 | ||||
-rw-r--r-- | runtime/worker.rs | 5 |
4 files changed, 67 insertions, 7 deletions
diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index b4716076e..cdffa9713 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -57,6 +57,7 @@ async fn main() -> Result<(), AnyError> { broadcast_channel: InMemoryBroadcastChannel::default(), shared_array_buffer_store: None, compiled_wasm_module_store: None, + stdio: Default::default(), }; let js_path = diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index d54c66efd..f18624eb2 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -77,13 +77,67 @@ pub fn init() -> Extension { .build() } -pub fn init_stdio() -> Extension { +pub enum StdioPipe { + Inherit, + File(StdFile), +} + +impl Default for StdioPipe { + fn default() -> Self { + Self::Inherit + } +} + +impl Clone for StdioPipe { + fn clone(&self) -> Self { + match self { + StdioPipe::Inherit => StdioPipe::Inherit, + StdioPipe::File(pipe) => StdioPipe::File(pipe.try_clone().unwrap()), + } + } +} + +/// Specify how stdin, stdout, and stderr are piped. +/// By default, inherits from the process. +#[derive(Clone, Default)] +pub struct Stdio { + pub stdin: StdioPipe, + pub stdout: StdioPipe, + pub stderr: StdioPipe, +} + +pub fn init_stdio(stdio: Stdio) -> Extension { + // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics + let stdio = Rc::new(RefCell::new(Some(stdio))); + Extension::builder() - .state(|state| { + .state(move |state| { + let stdio = stdio + .borrow_mut() + .take() + .expect("Extension only supports being used once."); let t = &mut state.resource_table; - t.add(StdFileResource::stdio(&STDIN_HANDLE, "stdin")); - t.add(StdFileResource::stdio(&STDOUT_HANDLE, "stdout")); - t.add(StdFileResource::stdio(&STDERR_HANDLE, "stderr")); + t.add(StdFileResource::stdio( + match &stdio.stdin { + StdioPipe::Inherit => &STDIN_HANDLE, + StdioPipe::File(pipe) => pipe, + }, + "stdin", + )); + t.add(StdFileResource::stdio( + match &stdio.stdout { + StdioPipe::Inherit => &STDOUT_HANDLE, + StdioPipe::File(pipe) => pipe, + }, + "stdout", + )); + t.add(StdFileResource::stdio( + match &stdio.stderr { + StdioPipe::Inherit => &STDERR_HANDLE, + StdioPipe::File(pipe) => pipe, + }, + "stderr", + )); Ok(()) }) .build() diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index ac103adda..a1f5ea2ee 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -3,6 +3,7 @@ use crate::colors; use crate::inspector_server::InspectorServer; use crate::js; use crate::ops; +use crate::ops::io::Stdio; use crate::permissions::Permissions; use crate::tokio_util::run_basic; use crate::BootstrapOptions; @@ -335,6 +336,7 @@ pub struct WebWorkerOptions { pub shared_array_buffer_store: Option<SharedArrayBufferStore>, pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>, pub maybe_exit_code: Option<Arc<AtomicI32>>, + pub stdio: Stdio, } impl WebWorker { @@ -411,7 +413,7 @@ impl WebWorker { ops::fs_events::init().enabled(options.use_deno_namespace), ops::fs::init().enabled(options.use_deno_namespace), ops::io::init(), - ops::io::init_stdio().enabled(options.use_deno_namespace), + ops::io::init_stdio(options.stdio).enabled(options.use_deno_namespace), deno_tls::init().enabled(options.use_deno_namespace), deno_net::init::<Permissions>( options.root_cert_store.clone(), diff --git a/runtime/worker.rs b/runtime/worker.rs index 370475703..15f41fe56 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -3,6 +3,7 @@ use crate::inspector_server::InspectorServer; use crate::js; use crate::ops; +use crate::ops::io::Stdio; use crate::permissions::Permissions; use crate::BootstrapOptions; use deno_broadcast_channel::InMemoryBroadcastChannel; @@ -65,6 +66,7 @@ pub struct WorkerOptions { pub broadcast_channel: InMemoryBroadcastChannel, pub shared_array_buffer_store: Option<SharedArrayBufferStore>, pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>, + pub stdio: Stdio, } impl MainWorker { @@ -136,7 +138,7 @@ impl MainWorker { ops::fs_events::init(), ops::fs::init(), ops::io::init(), - ops::io::init_stdio(), + ops::io::init_stdio(options.stdio), deno_tls::init(), deno_net::init::<Permissions>( options.root_cert_store.clone(), @@ -390,6 +392,7 @@ mod tests { broadcast_channel: InMemoryBroadcastChannel::default(), shared_array_buffer_store: None, compiled_wasm_module_store: None, + stdio: Default::default(), }; MainWorker::bootstrap_from_options(main_module, permissions, options) |