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

Commit 589672c

Browse files
authored
Merge pull request #61 from exoego/os
Overhaul os module
2 parents fc2ed8a + c22a035 commit 589672c

File tree

6 files changed

+42
-74
lines changed

6 files changed

+42
-74
lines changed

app/current/src/main/scala/io/scalajs/nodejs/os/CPUInfo.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package io.scalajs.nodejs.os
22

33
import scala.scalajs.js
44

5-
/**
6-
* CPU Information
7-
*/
85
@js.native
96
trait CPUInfo extends js.Object {
10-
var model: String = js.native
11-
var speed: Double = js.native
12-
var times: js.Dictionary[Double] = js.native
7+
val model: String = js.native
8+
val speed: Double = js.native
9+
val times: CPUTime = js.native
10+
}
11+
12+
@js.native
13+
trait CPUTime extends js.Object {
14+
val user: Double = js.native
15+
val nice: Double = js.native
16+
val sys: Double = js.native
17+
val idle: Double = js.native
18+
val irq: Double = js.native
1319
}

app/current/src/main/scala/io/scalajs/nodejs/os/CPUTime.scala

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/current/src/main/scala/io/scalajs/nodejs/os/OS.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.scalajs.nodejs.os
22

3+
import com.thoughtworks.enableIf
4+
35
import scala.scalajs.js
46
import scala.scalajs.js.annotation.JSImport
57

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

2528
/**
@@ -63,6 +66,9 @@ trait OS extends js.Object {
6366
*/
6467
def freemem(): Double = js.native
6568

69+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
70+
def getPriority(pid: Int = js.native): Int = js.native
71+
6672
/**
6773
* Returns the home directory of the current user.
6874
* @example os.homedir()
@@ -92,7 +98,7 @@ trait OS extends js.Object {
9298
* Get a list of network interfaces
9399
* @example os.networkInterfaces()
94100
*/
95-
def networkInterfaces(): js.Dictionary[NetworkInterface] = js.native
101+
def networkInterfaces(): js.Dictionary[js.Array[NetworkInterface]] = js.native
96102

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

116+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
117+
def setPriority(pid: Int, priority: Int): Unit = js.native
118+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
119+
def setPriority(priority: Int): Unit = js.native
120+
110121
/**
111122
* Returns the operating system's default directory for temporary files.
112123
* @example os.tmpdir()
@@ -129,6 +140,7 @@ trait OS extends js.Object {
129140
* Returns the system uptime in seconds.
130141
* @example os.uptime()
131142
*/
143+
// TODO: Return type should be Int after dropping Node.js 8 (Windows returns decimal until Node.js 10))
132144
def uptime(): Double = js.native
133145

134146
/**
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.scalajs.nodejs.os
22

3+
import io.scalajs.nodejs.{GID, UID}
4+
35
import scala.scalajs.js
46

57
/**
68
* User Information Object
79
* @example {{{ {"uid":501,"gid":20,"username":"ldaniels","homedir":"/Users/ldaniels","shell":"/bin/bash"} }}}
810
*/
9-
class UserInfoObject(val uid: Int, val gid: Int, val username: String, val homedir: String, val shell: String)
11+
class UserInfoObject(val uid: UID, val gid: GID, val username: String, val homedir: String, val shell: String)
1012
extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/os/package.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.scalajs.nodejs.os
22

3-
import io.scalajs.JSON
43
import io.scalajs.util.ScalaJsHelper._
54
import org.scalatest.FunSpec
65

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

1413
it("supports arch()") {
15-
info(s"arch: ${OS.arch()}")
16-
assert(isDefined(OS.arch()))
14+
assert(OS.arch().nonEmpty)
1715
}
1816

1917
it("supports constants") {
20-
info(s"constants: ${JSON.stringify(OS.constants)}")
2118
assert(isDefined(OS.constants))
2219
}
2320

2421
it("supports cpus()") {
2522
val cpus = OS.cpus()
26-
cpus.zipWithIndex foreach {
27-
case (cpu, n) =>
28-
info(s"cpu$n: ${JSON.stringify(cpu)}")
29-
}
3023
assert(isDefined(cpus))
24+
assert(cpus(0).model.nonEmpty)
25+
assert(cpus(0).speed > 0)
26+
assert(cpus(0).times.user > 0)
3127
}
3228

3329
it("supports endianness()") {
34-
info(s"endianness: ${OS.endianness()}")
35-
assert(isDefined(OS.endianness()))
30+
assert(OS.endianness() === "BE" || OS.endianness() === "LE")
3631
}
3732

3833
it("supports EOL") {
39-
info(s"EOL: ${OS.EOL}")
40-
assert(isDefined(OS.EOL))
34+
assert(OS.EOL === "\n" || OS.EOL === "\r\n")
4135
}
4236

4337
it("supports freemem()") {
44-
info(s"freemem: ${OS.freemem()}")
45-
assert(isDefined(OS.freemem()))
38+
assert(OS.freemem() > 0)
4639
}
4740

4841
it("supports homedir()") {
49-
info(s"homedir: ${OS.homedir()}")
5042
assert(isDefined(OS.homedir()))
5143
}
5244

5345
it("supports hostname()") {
54-
info(s"hostname: ${OS.hostname()}")
5546
assert(isDefined(OS.hostname()))
5647
}
5748

5849
it("supports loadavg()") {
59-
info(s"loadavg: ${OS.loadavg()}")
6050
assert(isDefined(OS.loadavg()))
51+
assert(OS.loadavg().nonEmpty)
6152
}
6253

6354
it("supports networkInterfaces()") {
64-
OS.networkInterfaces() foreach {
55+
val networkInterfaces = OS.networkInterfaces()
56+
assert(isDefined(networkInterfaces))
57+
networkInterfaces foreach {
6558
case (name, iface) =>
66-
info(s"$name: ${JSON.stringify(iface)}")
59+
assert(name.nonEmpty)
60+
assert(iface.nonEmpty)
6761
}
68-
assert(isDefined(OS.networkInterfaces()))
6962
}
7063

7164
it("supports platform()") {
72-
info(s"platform: ${OS.platform()}")
73-
assert(isDefined(OS.platform()))
65+
assert(OS.platform().nonEmpty)
7466
}
7567

7668
it("supports release()") {
77-
info(s"release: ${OS.release()}")
7869
assert(isDefined(OS.release()))
7970
}
8071

8172
it("supports tmpdir()") {
82-
info(s"tmpdir: ${OS.tmpdir()}")
8373
assert(isDefined(OS.tmpdir()))
8474
}
8575

8676
it("supports totalmem()") {
87-
info(s"totalmem: ${OS.totalmem()}")
8877
assert(isDefined(OS.totalmem()))
8978
}
9079

9180
it("supports type()") {
92-
info(s"type: ${OS.`type`()}")
9381
assert(isDefined(OS.`type`()))
9482
}
9583

9684
it("supports uptime()") {
97-
info(s"uptime: ${OS.uptime()}")
9885
assert(isDefined(OS.uptime()))
9986
}
10087

10188
it("supports userInfo()") {
102-
info(s"userInfo: ${JSON.stringify(OS.userInfo())}")
10389
assert(isDefined(OS.userInfo()))
10490
}
105-
10691
}
107-
10892
}

0 commit comments

Comments
 (0)