1import * as DenoFileSystem from "@effect/platform-deno/DenoFileSystem"
2import { assert, describe, it } from "@effect/vitest"
3import * as Effect from "effect/Effect"
4import * as FileSystem from "effect/FileSystem"
5import { testLayer } from "../../../effect/test/FileSystem.test-utils.ts"
6
7describe("FileSystem", () =>
8 testLayer(DenoFileSystem.layer, {
9 accessOnDirectory: false,
10 tempFileScopedRemovesDirectory: false
11 }))
12
13describe("truncate", () => {
14 it.effect.each([1.5, NaN])(
15 "rejects length %s at the call site without changing the file",
16 (length) =>
17 Effect.gen(function*() {
18 const fs = yield* FileSystem.FileSystem
19 const path = yield* fs.makeTempFileScoped()
20 yield* fs.writeFileString(path, "contents")
21 const file = yield* fs.open(path, { flag: "r+" })
22 yield* file.seek(BigInt(4), "start")
23 let threwAtCall = false
24
25 yield* Effect.exit(Effect.suspend(() => {
26 try {
27 return file.truncate(length)
28 } catch (error) {
29 threwAtCall = error instanceof RangeError
30 return Effect.die(error)
31 }
32 }))
33
34 assert.deepStrictEqual({
35 threwAtCall,
36 content: yield* fs.readFileString(path),
37 position: yield* file.seek(BigInt(0), "current")
38 }, {
39 threwAtCall: true,
40 content: "contents",
41 position: BigInt(4)
42 })
43 }).pipe(Effect.provide(DenoFileSystem.layer))
44 )
45})
46
47describe.skipIf(Deno.build.os === "windows")("writeFile", () => {
48 it.effect("applies the mode when creating a file", () =>
49 Effect.gen(function*() {
50 const fs = yield* FileSystem.FileSystem
51 const directory = yield* fs.makeTempDirectoryScoped()
52 const path = `${directory}/created`
53
54 yield* fs.writeFileString(path, "content", { mode: 0o600 })
55
56 assert.strictEqual((yield* fs.stat(path)).mode & 0o777, 0o600)
57 }).pipe(Effect.provide(DenoFileSystem.layer)))
58
59 it.effect("preserves the mode of an existing file", () =>
60 Effect.gen(function*() {
61 const fs = yield* FileSystem.FileSystem
62 const path = yield* fs.makeTempFileScoped()
63 yield* fs.chmod(path, 0o640)
64
65 yield* fs.writeFileString(path, "content", { mode: 0o600 })
66
67 assert.strictEqual((yield* fs.stat(path)).mode & 0o777, 0o640)
68 }).pipe(Effect.provide(DenoFileSystem.layer)))
69})