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

Commit c103c78

Browse files
author
exoego
committed
Add more fs module helper
1 parent 6a5857a commit c103c78

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,27 @@ package object fs {
108108
promiseWithError0[FileIOError](instance.mkdir(path, options, _))
109109
}
110110

111+
@inline
112+
def mkdtempFuture(prefix: String, options: FileEncodingOptions = ???): Future[String] = {
113+
promiseWithError1[FileIOError, String](instance.mkdtemp(prefix, options, _))
114+
}
115+
111116
@inline
112117
def openFuture(path: Buffer | String, flags: Flags, mode: FileMode): Future[FileDescriptor] = {
113118
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, mode, _))
114119
}
120+
115121
@inline
116122
def openFuture(path: Buffer | String, flags: Flags): Future[FileDescriptor] = {
117123
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, _))
118124
}
119125

126+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
127+
@inline
128+
def openFuture(path: Buffer | String): Future[FileDescriptor] = {
129+
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, _))
130+
}
131+
120132
@inline
121133
def readFuture(fd: FileDescriptor,
122134
buffer: Buffer,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package object nodejs {
3232

3333
type FileIOError = SystemError
3434

35-
type FileMode = Int | Null
35+
type FileMode = Int
3636

3737
type FileType = Int
3838

app/nodejs-v8/src/test/scala/io/scalajs/nodejs/fs/FsAsyncTest.scala

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.scalajs.nodejs.buffer.Buffer
44
import org.scalatest.{AsyncFunSpec, BeforeAndAfterEach}
55

66
import scala.concurrent.{ExecutionContext, Future}
7+
import scala.util.{Failure, Success}
78

89
class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {
910

@@ -40,5 +41,49 @@ class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {
4041
}
4142
}
4243
}
44+
45+
describe("accessFuture") {
46+
it("should succeed") {
47+
for {
48+
_ <- Fs.accessFuture("package.json")
49+
_ <- Fs.accessFuture("package.json", Fs.constants.R_OK)
50+
} yield {
51+
succeed
52+
}
53+
}
54+
55+
it("should fail: no such file") {
56+
Fs.accessFuture("package.json111").transformWith {
57+
case Failure(_) => succeed
58+
case Success(_) => fail("expected failure")
59+
}
60+
}
61+
62+
it("should fail: invalid file mode") {
63+
Fs.accessFuture("package.json", Fs.constants.X_OK).transformWith {
64+
case Failure(_) => succeed
65+
case Success(_) => fail("expected failure")
66+
}
67+
}
68+
}
69+
70+
describe("appendFileFuture") {
71+
it("should support option") {
72+
for {
73+
_ <- Fs.appendFileFuture("x.AppendFile.txt", "yay")
74+
_ <- Fs.appendFileFuture("x.AppendFile.sh", "echo 0", new FileAppendOptions(mode = Fs.constants.X_OK))
75+
defaultStat <- Fs.statFuture("x.AppendFile.txt")
76+
executableStat <- Fs.statFuture("x.AppendFile.sh")
77+
_ <- Fs.unlinkFuture("x.AppendFile.txt")
78+
_ <- Fs.unlinkFuture("x.AppendFile.sh")
79+
} yield {
80+
assert((defaultStat.mode & Fs.constants.R_OK) > 0)
81+
assert((defaultStat.mode & Fs.constants.X_OK) === 0)
82+
assert((executableStat.mode & Fs.constants.R_OK) === 0)
83+
assert((executableStat.mode & Fs.constants.X_OK) > 0)
84+
succeed
85+
}
86+
}
87+
}
4388
}
4489
}

0 commit comments

Comments
 (0)