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

Add missing node os.release() implementation #4065

Merged
merged 5 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ export {
ShutdownMode,
shutdown
} from "./net.ts";
export { dir, env, exit, isTTY, execPath, hostname, loadavg } from "./os.ts";
export {
dir,
env,
exit,
isTTY,
execPath,
hostname,
loadavg,
osRelease
} from "./os.ts";
export {
permissions,
PermissionName,
Expand Down
1 change: 1 addition & 0 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export let OP_SIGNAL_BIND: number;
export let OP_SIGNAL_UNBIND: number;
export let OP_SIGNAL_POLL: number;
export let OP_LOADAVG: number;
export let OP_OS_RELEASE: number;

const PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();

Expand Down
6 changes: 6 additions & 0 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ declare namespace Deno {
*/
export function hostname(): string;

/** Get the OS release. Requires the `--allow-env` flag.
*
* console.log(Deno.osRelease());
*/
export function osRelease(): string;

/** Exit the Deno process with optional exit code. */
export function exit(code?: number): never;

Expand Down
9 changes: 9 additions & 0 deletions cli/js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export function hostname(): string {
return sendSync(dispatch.OP_HOSTNAME);
}

/** Get OS release.
* Requires the `--allow-env` flag.
*
* console.log(Deno.osRelease());
*/
export function osRelease(): string {
return sendSync(dispatch.OP_OS_RELEASE);
}

/** Exit the Deno process with optional exit code. */
export function exit(code = 0): never {
sendSync(dispatch.OP_EXIT, { code });
Expand Down
16 changes: 16 additions & 0 deletions cli/js/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,19 @@ testPerm({ env: false }, function hostnamePerm(): void {
}
assert(caughtError);
});

testPerm({ env: true }, function releaseDir(): void {
assertNotEquals(Deno.osRelease(), "");
});

testPerm({ env: false }, function releasePerm(): void {
let caughtError = false;
try {
Deno.osRelease();
} catch (err) {
caughtError = true;
assert(err instanceof Deno.Err.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
});
16 changes: 15 additions & 1 deletion cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub fn init(i: &mut Isolate, s: &State) {
i.register_op("get_dir", s.core_op(json_op(s.stateful_op(op_get_dir))));
i.register_op("hostname", s.core_op(json_op(s.stateful_op(op_hostname))));
i.register_op("loadavg", s.core_op(json_op(s.stateful_op(op_loadavg))));
i.register_op(
"os_release",
s.core_op(json_op(s.stateful_op(op_os_release))),
);
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -185,6 +189,16 @@ fn op_hostname(
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
state.check_env()?;
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned());
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
Ok(JsonOp::Sync(json!(hostname)))
}

fn op_os_release(
state: &State,
_args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
state.check_env()?;
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
Ok(JsonOp::Sync(json!(release)))
}
4 changes: 2 additions & 2 deletions std/node/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export function platform(): string {
return process.platform;
}

/** Not yet implemented */
/** Returns the operating system as a string */
export function release(): string {
notImplemented(SEE_GITHUB_ISSUE);
return Deno.osRelease();
}

/** Not yet implemented */
Expand Down
14 changes: 7 additions & 7 deletions std/node/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ test({
}
});

test({
name: "release is a string",
fn() {
assertEquals(typeof os.release(), "string");
}
});

test({
name: "getPriority(): PID must be a 32 bit integer",
fn() {
Expand Down Expand Up @@ -216,13 +223,6 @@ test({
Error,
"Not implemented"
);
assertThrows(
() => {
os.release();
},
Error,
"Not implemented"
);
assertThrows(
() => {
os.setPriority(0);
Expand Down