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

Add more fs module test #106

Merged
merged 1 commit into from
Oct 7, 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
12 changes: 12 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,27 @@ package object fs {
promiseWithError0[FileIOError](instance.mkdir(path, options, _))
}

@inline
def mkdtempFuture(prefix: String, options: FileEncodingOptions = ???): Future[String] = {
promiseWithError1[FileIOError, String](instance.mkdtemp(prefix, options, _))
}

@inline
def openFuture(path: Buffer | String, flags: Flags, mode: FileMode): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, mode, _))
}

@inline
def openFuture(path: Buffer | String, flags: Flags): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, _))
}

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
@inline
def openFuture(path: Buffer | String): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, _))
}

@inline
def readFuture(fd: FileDescriptor,
buffer: Buffer,
Expand Down
2 changes: 1 addition & 1 deletion app/current/src/main/scala/io/scalajs/nodejs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ package object nodejs {

type FileIOError = SystemError

type FileMode = Int | Null
type FileMode = Int

type FileType = Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.scalajs.nodejs.buffer.Buffer
import org.scalatest.{AsyncFunSpec, BeforeAndAfterEach}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}

class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {

Expand Down Expand Up @@ -40,5 +41,49 @@ class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {
}
}
}

describe("accessFuture") {
it("should succeed") {
for {
_ <- Fs.accessFuture("package.json")
_ <- Fs.accessFuture("package.json", Fs.constants.R_OK)
} yield {
succeed
}
}

it("should fail: no such file") {
Fs.accessFuture("package.json111").transformWith {
case Failure(_) => succeed
case Success(_) => fail("expected failure")
}
}

it("should fail: invalid file mode") {
Fs.accessFuture("package.json", Fs.constants.X_OK).transformWith {
case Failure(_) => succeed
case Success(_) => fail("expected failure")
}
}
}

describe("appendFileFuture") {
it("should support option") {
for {
_ <- Fs.appendFileFuture("x.AppendFile.txt", "yay")
_ <- Fs.appendFileFuture("x.AppendFile.sh", "echo 0", new FileAppendOptions(mode = Fs.constants.X_OK))
defaultStat <- Fs.statFuture("x.AppendFile.txt")
executableStat <- Fs.statFuture("x.AppendFile.sh")
_ <- Fs.unlinkFuture("x.AppendFile.txt")
_ <- Fs.unlinkFuture("x.AppendFile.sh")
} yield {
assert((defaultStat.mode & Fs.constants.R_OK) > 0)
assert((defaultStat.mode & Fs.constants.X_OK) === 0)
assert((executableStat.mode & Fs.constants.R_OK) === 0)
assert((executableStat.mode & Fs.constants.X_OK) > 0)
succeed
}
}
}
}
}