diff --git a/cli/ops/io.rs b/cli/ops/io.rs index 0e007ad1a5..785bb42b7a 100644 --- a/cli/ops/io.rs +++ b/cli/ops/io.rs @@ -37,6 +37,17 @@ lazy_static! { /// resource table is dropped storing reference to that handle, the handle /// itself won't be closed (so Deno.core.print) will still work. // TODO(ry) It should be possible to close stdout. + static ref STDIN_HANDLE: std::fs::File = { + #[cfg(not(windows))] + let stdin = unsafe { std::fs::File::from_raw_fd(0) }; + #[cfg(windows)] + let stdin = unsafe { + std::fs::File::from_raw_handle(winapi::um::processenv::GetStdHandle( + winapi::um::winbase::STD_INPUT_HANDLE, + )) + }; + stdin + }; static ref STDOUT_HANDLE: std::fs::File = { #[cfg(not(windows))] let stdout = unsafe { std::fs::File::from_raw_fd(1) }; @@ -71,10 +82,10 @@ pub fn get_stdio() -> ( StreamResourceHolder, StreamResourceHolder, ) { - let stdin = StreamResourceHolder::new(StreamResource::Stdin( - tokio::io::stdin(), - TTYMetadata::default(), - )); + let stdin = StreamResourceHolder::new(StreamResource::FsFile(Some({ + let stdin = STDIN_HANDLE.try_clone().unwrap(); + (tokio::fs::File::from_std(stdin), FileMetadata::default()) + }))); let stdout = StreamResourceHolder::new(StreamResource::FsFile(Some({ let stdout = STDOUT_HANDLE.try_clone().unwrap(); (tokio::fs::File::from_std(stdout), FileMetadata::default()) diff --git a/cli/tests/unit/stdio_test.ts b/cli/tests/unit/stdio_test.ts new file mode 100644 index 0000000000..244ebcd476 --- /dev/null +++ b/cli/tests/unit/stdio_test.ts @@ -0,0 +1,32 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { unitTest, assertEquals } from "./test_util.ts"; + +unitTest(async function stdioStdinRead() { + const nread = await Deno.stdin.read(new Uint8Array(0)); + assertEquals(nread, 0); +}); + +unitTest(function stdioStdinReadSync() { + const nread = Deno.stdin.readSync(new Uint8Array(0)); + assertEquals(nread, 0); +}); + +unitTest(async function stdioStdoutWrite() { + const nwritten = await Deno.stdout.write(new Uint8Array(0)); + assertEquals(nwritten, 0); +}); + +unitTest(function stdioStdoutWriteSync() { + const nwritten = Deno.stdout.writeSync(new Uint8Array(0)); + assertEquals(nwritten, 0); +}); + +unitTest(async function stdioStderrWrite() { + const nwritten = await Deno.stderr.write(new Uint8Array(0)); + assertEquals(nwritten, 0); +}); + +unitTest(function stdioStderrWriteSync() { + const nwritten = Deno.stderr.writeSync(new Uint8Array(0)); + assertEquals(nwritten, 0); +}); diff --git a/cli/tests/unit/unit_tests.ts b/cli/tests/unit/unit_tests.ts index 515da9f4a0..7891ea418f 100644 --- a/cli/tests/unit/unit_tests.ts +++ b/cli/tests/unit/unit_tests.ts @@ -52,6 +52,7 @@ import "./request_test.ts"; import "./resources_test.ts"; import "./signal_test.ts"; import "./stat_test.ts"; +import "./stdio_test.ts"; import "./streams_internal_test.ts"; import "./streams_piping_test.ts"; import "./streams_transform_test.ts";