Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Overhaul os module #61

Merged
merged 1 commit into from
Sep 24, 2019
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
18 changes: 12 additions & 6 deletions app/current/src/main/scala/io/scalajs/nodejs/os/CPUInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package io.scalajs.nodejs.os

import scala.scalajs.js

/**
* CPU Information
*/
@js.native
trait CPUInfo extends js.Object {
var model: String = js.native
var speed: Double = js.native
var times: js.Dictionary[Double] = js.native
val model: String = js.native
val speed: Double = js.native
val times: CPUTime = js.native
}

@js.native
trait CPUTime extends js.Object {
val user: Double = js.native
val nice: Double = js.native
val sys: Double = js.native
val idle: Double = js.native
val irq: Double = js.native
}
15 changes: 0 additions & 15 deletions app/current/src/main/scala/io/scalajs/nodejs/os/CPUTime.scala

This file was deleted.

14 changes: 13 additions & 1 deletion app/current/src/main/scala/io/scalajs/nodejs/os/OS.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.scalajs.nodejs.os

import com.thoughtworks.enableIf

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

Expand All @@ -20,6 +22,7 @@ trait OS extends js.Object {
* are described in OS Constants.
* @see https://nodejs.org/api/os.html#os_os_constants_1
*/
// TODO: Implement as object
def constants: js.Dictionary[js.Any] = js.native

/**
Expand Down Expand Up @@ -63,6 +66,9 @@ trait OS extends js.Object {
*/
def freemem(): Double = js.native

@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def getPriority(pid: Int = js.native): Int = js.native

/**
* Returns the home directory of the current user.
* @example os.homedir()
Expand Down Expand Up @@ -92,7 +98,7 @@ trait OS extends js.Object {
* Get a list of network interfaces
* @example os.networkInterfaces()
*/
def networkInterfaces(): js.Dictionary[NetworkInterface] = js.native
def networkInterfaces(): js.Dictionary[js.Array[NetworkInterface]] = js.native

/**
* Returns the operating system platform. Possible values are 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'.
Expand All @@ -107,6 +113,11 @@ trait OS extends js.Object {
*/
def release(): String = js.native

@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def setPriority(pid: Int, priority: Int): Unit = js.native
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def setPriority(priority: Int): Unit = js.native

/**
* Returns the operating system's default directory for temporary files.
* @example os.tmpdir()
Expand All @@ -129,6 +140,7 @@ trait OS extends js.Object {
* Returns the system uptime in seconds.
* @example os.uptime()
*/
// TODO: Return type should be Int after dropping Node.js 8 (Windows returns decimal until Node.js 10))
def uptime(): Double = js.native

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.scalajs.nodejs.os

import io.scalajs.nodejs.{GID, UID}

import scala.scalajs.js

/**
* User Information Object
* @example {{{ {"uid":501,"gid":20,"username":"ldaniels","homedir":"/Users/ldaniels","shell":"/bin/bash"} }}}
*/
class UserInfoObject(val uid: Int, val gid: Int, val username: String, val homedir: String, val shell: String)
class UserInfoObject(val uid: UID, val gid: GID, val username: String, val homedir: String, val shell: String)
extends js.Object
21 changes: 0 additions & 21 deletions app/current/src/main/scala/io/scalajs/nodejs/os/package.scala

This file was deleted.

44 changes: 14 additions & 30 deletions app/nodejs-v8/src/test/scala/nodejs/os/OSTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.scalajs.nodejs.os

import io.scalajs.JSON
import io.scalajs.util.ScalaJsHelper._
import org.scalatest.FunSpec

Expand All @@ -12,97 +11,82 @@ class OSTest extends FunSpec {
describe("OS") {

it("supports arch()") {
info(s"arch: ${OS.arch()}")
assert(isDefined(OS.arch()))
assert(OS.arch().nonEmpty)
}

it("supports constants") {
info(s"constants: ${JSON.stringify(OS.constants)}")
assert(isDefined(OS.constants))
}

it("supports cpus()") {
val cpus = OS.cpus()
cpus.zipWithIndex foreach {
case (cpu, n) =>
info(s"cpu$n: ${JSON.stringify(cpu)}")
}
assert(isDefined(cpus))
assert(cpus(0).model.nonEmpty)
assert(cpus(0).speed > 0)
assert(cpus(0).times.user > 0)
}

it("supports endianness()") {
info(s"endianness: ${OS.endianness()}")
assert(isDefined(OS.endianness()))
assert(OS.endianness() === "BE" || OS.endianness() === "LE")
}

it("supports EOL") {
info(s"EOL: ${OS.EOL}")
assert(isDefined(OS.EOL))
assert(OS.EOL === "\n" || OS.EOL === "\r\n")
}

it("supports freemem()") {
info(s"freemem: ${OS.freemem()}")
assert(isDefined(OS.freemem()))
assert(OS.freemem() > 0)
}

it("supports homedir()") {
info(s"homedir: ${OS.homedir()}")
assert(isDefined(OS.homedir()))
}

it("supports hostname()") {
info(s"hostname: ${OS.hostname()}")
assert(isDefined(OS.hostname()))
}

it("supports loadavg()") {
info(s"loadavg: ${OS.loadavg()}")
assert(isDefined(OS.loadavg()))
assert(OS.loadavg().nonEmpty)
}

it("supports networkInterfaces()") {
OS.networkInterfaces() foreach {
val networkInterfaces = OS.networkInterfaces()
assert(isDefined(networkInterfaces))
networkInterfaces foreach {
case (name, iface) =>
info(s"$name: ${JSON.stringify(iface)}")
assert(name.nonEmpty)
assert(iface.nonEmpty)
}
assert(isDefined(OS.networkInterfaces()))
}

it("supports platform()") {
info(s"platform: ${OS.platform()}")
assert(isDefined(OS.platform()))
assert(OS.platform().nonEmpty)
}

it("supports release()") {
info(s"release: ${OS.release()}")
assert(isDefined(OS.release()))
}

it("supports tmpdir()") {
info(s"tmpdir: ${OS.tmpdir()}")
assert(isDefined(OS.tmpdir()))
}

it("supports totalmem()") {
info(s"totalmem: ${OS.totalmem()}")
assert(isDefined(OS.totalmem()))
}

it("supports type()") {
info(s"type: ${OS.`type`()}")
assert(isDefined(OS.`type`()))
}

it("supports uptime()") {
info(s"uptime: ${OS.uptime()}")
assert(isDefined(OS.uptime()))
}

it("supports userInfo()") {
info(s"userInfo: ${JSON.stringify(OS.userInfo())}")
assert(isDefined(OS.userInfo()))
}

}

}