Skip to content

Commit

Permalink
isTTY uses isatty; add some unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Feb 23, 2020
1 parent dcc832d commit f1fbe6f
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 56 deletions.
4 changes: 2 additions & 2 deletions cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export {
ShutdownMode,
shutdown
} from "./net.ts";
export { dir, env, exit, isTTY, execPath, hostname, loadavg } from "./os.ts";
export { dir, env, exit, execPath, hostname, loadavg } from "./os.ts";
export {
permissions,
PermissionName,
Expand Down Expand Up @@ -114,7 +114,7 @@ export { statSync, lstatSync, stat, lstat } from "./stat.ts";
export { symlinkSync, symlink } from "./symlink.ts";
export { connectTLS, listenTLS } from "./tls.ts";
export { truncateSync, truncate } from "./truncate.ts";
export { isatty, setRaw } from "./tty.ts";
export { isatty, isTTY, setRaw } from "./tty.ts";
export { utimeSync, utime } from "./utime.ts";
export { version } from "./version.ts";
export { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
Expand Down
1 change: 0 additions & 1 deletion cli/js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AsyncHandler } from "./plugins.ts";
export let OP_READ: number;
export let OP_WRITE: number;
export let OP_EXIT: number;
export let OP_IS_TTY: number;
export let OP_ENV: number;
export let OP_EXEC_PATH: number;
export let OP_UTIME: number;
Expand Down
20 changes: 10 additions & 10 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ declare namespace Deno {

export function runTests(opts?: RunTestsOptions): Promise<void>;

/** Check if running in terminal.
*
* console.log(Deno.isTTY().stdout);
*/
export function isTTY(): {
stdin: boolean;
stdout: boolean;
stderr: boolean;
};

/** Get the loadavg. Requires the `--allow-env` flag.
*
* console.log(Deno.loadavg());
Expand Down Expand Up @@ -564,6 +554,16 @@ declare namespace Deno {
*/
export function setRaw(rid: number, mode: boolean): void;

/** Check if running in terminal.
*
* console.log(Deno.isTTY().stdout);
*/
export function isTTY(): {
stdin: boolean;
stdout: boolean;
stderr: boolean;
};

// @url js/buffer.d.ts

/** A Buffer is a variable-sized buffer of bytes with read() and write()
Expand Down
7 changes: 0 additions & 7 deletions cli/js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { sendSync } from "./dispatch_json.ts";
import { Err } from "./errors.ts";
import * as util from "./util.ts";

/** Check if running in terminal.
*
* console.log(Deno.isTTY().stdout);
*/
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
return sendSync(dispatch.OP_IS_TTY);
}
/** Get the loadavg.
* Requires the `--allow-env` flag.
*
Expand Down
4 changes: 0 additions & 4 deletions cli/js/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ test(function osPid(): void {
assert(Deno.pid > 0);
});

test(function osIsTTYSmoke(): void {
console.log(Deno.isTTY());
});

testPerm({ env: true }, function getDir(): void {
type supportOS = "mac" | "win" | "linux";

Expand Down
12 changes: 12 additions & 0 deletions cli/js/tty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function setRaw(rid: number, mode: boolean): void {
mode
});
}

/** Check if running in terminal.
*
* console.log(Deno.isTTY().stdout);
*/
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
return {
stdin: isatty(0),
stdout: isatty(1),
stderr: isatty(2)
};
}
27 changes: 27 additions & 0 deletions cli/js/tty_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert } from "./test_util.ts";

// setRaw test is in integration tests.

// Smoke test to ensure no error.
test(function isTTYSmoke(): void {
console.log(Deno.isTTY());
});

testPerm({ read: true }, function isatty(): void {
// CI not under TTY, so cannot test stdin/stdout/stderr.
const f = Deno.openSync("cli/tests/hello.txt");
assert(!Deno.isatty(f.rid));
});

test(function isattyError(): void {
let caught = false;
try {
// Absurdly large rid.
Deno.isatty(0x7fffffff);
} catch (e) {
caught = true;
assert(e instanceof Deno.Err.BadResource);
}
assert(caught);
});
1 change: 1 addition & 0 deletions cli/js/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import "./text_encoding_test.ts";
import "./timers_test.ts";
import "./tls_test.ts";
import "./truncate_test.ts";
import "./tty_test.ts";
import "./url_test.ts";
import "./url_search_params_test.ts";
import "./utime_test.ts";
Expand Down
2 changes: 0 additions & 2 deletions cli/ops/files.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::io::{FileMetadata, StreamResource};
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::fs as deno_fs;
use crate::op_error::OpError;
use crate::ops::json_op;
Expand Down
14 changes: 0 additions & 14 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::op_error::OpError;
use crate::ops::json_op;
use crate::state::State;
use atty;
use deno_core::*;
use std::collections::HashMap;
use std::env;
Expand All @@ -13,7 +12,6 @@ use url::Url;

pub fn init(i: &mut Isolate, s: &State) {
i.register_op("exit", s.core_op(json_op(s.stateful_op(op_exit))));
i.register_op("is_tty", s.core_op(json_op(s.stateful_op(op_is_tty))));
i.register_op("env", s.core_op(json_op(s.stateful_op(op_env))));
i.register_op("exec_path", s.core_op(json_op(s.stateful_op(op_exec_path))));
i.register_op("set_env", s.core_op(json_op(s.stateful_op(op_set_env))));
Expand Down Expand Up @@ -151,18 +149,6 @@ fn op_exit(
std::process::exit(args.code)
}

fn op_is_tty(
_s: &State,
_args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
Ok(JsonOp::Sync(json!({
"stdin": atty::is(atty::Stream::Stdin),
"stdout": atty::is(atty::Stream::Stdout),
"stderr": atty::is(atty::Stream::Stderr),
})))
}

fn op_loadavg(
state: &State,
_args: Value,
Expand Down
31 changes: 15 additions & 16 deletions cli/ops/tty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::dispatch_json::JsonOp;
use super::io::StreamResource;
use crate::deno_error::bad_resource;
use crate::deno_error::other_error;
use crate::op_error::OpError;
use crate::ops::json_op;
use crate::state::State;
use atty;
Expand All @@ -22,15 +21,15 @@ const RAW_MODE_MASK: DWORD = wincon::ENABLE_LINE_INPUT
#[cfg(windows)]
fn get_windows_handle(
f: &std::fs::File,
) -> Result<std::os::windows::io::RawHandle, ErrBox> {
) -> Result<std::os::windows::io::RawHandle, OpError> {
use std::os::windows::io::AsRawHandle;
use winapi::um::handleapi;

let handle = f.as_raw_handle();
if handle == handleapi::INVALID_HANDLE_VALUE {
return Err(ErrBox::from(std::io::Error::last_os_error()));
return Err(OpError::from(std::io::Error::last_os_error()));
} else if handle.is_null() {
return Err(ErrBox::from(other_error("null handle".to_owned())));
return Err(OpError::other("null handle".to_owned()));
}
Ok(handle)
}
Expand All @@ -45,7 +44,7 @@ macro_rules! wincheck {
($funcall:expr) => {{
let rc = unsafe { $funcall };
if rc == 0 {
Err(ErrBox::from(std::io::Error::last_os_error()))?;
Err(OpError::from(std::io::Error::last_os_error()))?;
}
rc
}};
Expand All @@ -61,7 +60,7 @@ pub fn op_set_raw(
state_: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
) -> Result<JsonOp, OpError> {
let args: SetRawArgs = serde_json::from_value(args)?;
let rid = args.rid;
let is_raw = args.mode;
Expand All @@ -79,7 +78,7 @@ pub fn op_set_raw(
let state = state_.borrow_mut();
let resource = state.resource_table.get::<StreamResource>(rid);
if resource.is_none() {
return Err(bad_resource());
return Err(OpError::bad_resource());
}

// For now, only stdin.
Expand All @@ -91,14 +90,14 @@ pub fn op_set_raw(
std_file.as_raw_handle()
}
_ => {
return Err(other_error("Not supported".to_owned()));
return Err(OpError::OpError::other("Not supported".to_owned()));
}
};

if handle == handleapi::INVALID_HANDLE_VALUE {
return Err(ErrBox::from(std::io::Error::last_os_error()));
return Err(OpError::from(std::io::Error::last_os_error()));
} else if handle.is_null() {
return Err(ErrBox::from(other_error("null handle".to_owned())));
return Err(OpError::other("null handle".to_owned()));
}
let mut original_mode: DWORD = 0;
wincheck!(consoleapi::GetConsoleMode(handle, &mut original_mode));
Expand All @@ -118,7 +117,7 @@ pub fn op_set_raw(
let mut state = state_.borrow_mut();
let resource = state.resource_table.get_mut::<StreamResource>(rid);
if resource.is_none() {
return Err(bad_resource());
return Err(OpError::bad_resource());
}

if is_raw {
Expand All @@ -132,7 +131,7 @@ pub fn op_set_raw(
(std_file.as_raw_fd(), &mut metadata.tty.mode)
}
_ => {
return Err(other_error("Not supported".to_owned()));
return Err(OpError::other("Not supported".to_owned()));
}
};

Expand Down Expand Up @@ -174,7 +173,7 @@ pub fn op_set_raw(
(std_file.as_raw_fd(), &mut metadata.tty.mode)
}
_ => {
return Err(other_error("Not supported".to_owned()));
return Err(OpError::other("Not supported".to_owned()));
}
};

Expand All @@ -196,13 +195,13 @@ pub fn op_isatty(
state_: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
) -> Result<JsonOp, OpError> {
let args: IsattyArgs = serde_json::from_value(args)?;
let rid = args.rid;

let state = state_.borrow_mut();
if !state.resource_table.has(rid) {
return Err(bad_resource());
return Err(OpError::bad_resource());
}

let resource = state.resource_table.get::<StreamResource>(rid);
Expand Down

0 comments on commit f1fbe6f

Please sign in to comment.