Skip to content

Commit

Permalink
feat(os): add release impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ecyrbe committed Feb 21, 2020
1 parent 754b8c6 commit efce9fd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export let OP_TRANSPILE: number;
export let OP_SIGNAL_BIND: number;
export let OP_SIGNAL_UNBIND: number;
export let OP_SIGNAL_POLL: 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 @@ -46,6 +46,12 @@ declare namespace Deno {
*/
export function hostname(): string;

/** Get the OS release. Requires the `--allow-env` flag.
*
* console.log(Deno.op_release());
*/
export function os_release(): 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 @@ -21,6 +21,15 @@ export function hostname(): string {
return sendSync(dispatch.OP_HOSTNAME);
}

/** Get OS release.
* Requires the `--allow-env` flag.
*
* console.log(Deno.os_release());
*/
export function os_release(): 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 @@ -298,3 +298,19 @@ testPerm({ env: false }, function hostnamePerm(): void {
}
assert(caughtError);
});

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

testPerm({ env: false }, function releasePerm(): void {
let caughtError = false;
try {
Deno.os_release();
} catch (err) {
caughtError = true;
assertEquals(err.kind, Deno.Err.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
});
14 changes: 14 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub fn init(i: &mut Isolate, s: &State) {
i.register_op("get_env", s.core_op(json_op(s.stateful_op(op_get_env))));
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(
"os_release",
s.core_op(json_op(s.stateful_op(op_os_release))),
);
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -167,3 +171,13 @@ fn op_hostname(
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned());
Ok(JsonOp::Sync(json!(hostname)))
}

fn op_os_release(
state: &State,
_args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
state.check_env()?;
let release = sys_info::os_release().unwrap_or_else(|_| "".to_owned());
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 @@ -150,9 +150,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.os_release();
}

/** Not yet implemented */
Expand Down
7 changes: 7 additions & 0 deletions std/node/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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

0 comments on commit efce9fd

Please sign in to comment.