Open History to see the last commit
1import * as NodeFileSystem from "@effect/platform-node-shared/NodeFileSystem"
2import { assert, describe, expect, it } from "@effect/vitest"
3import { Array } from "effect"
4import * as Effect from "effect/Effect"
5import * as Fs from "effect/FileSystem"
6import * as Stream from "effect/Stream"
7
8const runPromise = <E, A>(self: Effect.Effect<A, E, Fs.FileSystem>) =>
9 Effect.runPromise(
10 Effect.provide(self, NodeFileSystem.layer)
11 )
12
13describe("FileSystem", () => {
14 it("readFile", () =>
15 runPromise(Effect.gen(function*() {
16 const fs = yield* Fs.FileSystem
17 const data = yield* fs.readFile(`${__dirname}/fixtures/text.txt`)
18 const text = new TextDecoder().decode(data)
19 expect(text.trim()).toEqual("lorem ipsum dolar sit amet")
20 })))
21
22 it("makeTempDirectory", () =>
23 runPromise(Effect.gen(function*() {
24 const fs = yield* Fs.FileSystem
25 let dir = ""
26 yield* Effect.scoped(Effect.gen(function*() {
27 dir = yield* fs.makeTempDirectory()
28 const stat = yield* fs.stat(dir)
29 expect(stat.type).toEqual("Directory")
30 }))
31 const stat = yield* fs.stat(dir)
32 expect(stat.type).toEqual("Directory")
33 })))
34
35 it("makeTempDirectoryScoped", () =>
36 runPromise(Effect.gen(function*() {
37 const fs = yield* Fs.FileSystem
38 let dir = ""
39 yield* Effect.scoped(
40 Effect.gen(function*() {
41 dir = yield* fs.makeTempDirectoryScoped()
42 const stat = yield* fs.stat(dir)
43 expect(stat.type).toEqual("Directory")
44 })
45 )
46 const error = yield* Effect.flip(fs.stat(dir))
47 assert(error.reason._tag === "NotFound")
48 })))
49
50 it("truncate", () =>
51 runPromise(Effect.gen(function*() {
52 const fs = yield* Fs.FileSystem
53 const file = yield* fs.makeTempFile()
54
55 const text = "hello world"
56 yield* fs.writeFile(file, new TextEncoder().encode(text))
57
58 const before = yield* Effect.map(fs.readFile(file), (_) => new TextDecoder().decode(_))
59 expect(before).toEqual(text)
60
61 yield* fs.truncate(file)
62
63 const after = yield* Effect.map(fs.readFile(file), (_) => new TextDecoder().decode(_))
64 expect(after).toEqual("")
65 })))
66
67 it("should track the cursor position when reading", () =>
68 runPromise(Effect.gen(function*() {
69 const fs = yield* Fs.FileSystem
70
71 yield* Effect.gen(function*() {
72 let text: string
73 const file = yield* fs.open(`${__dirname}/fixtures/text.txt`)
74
75 text = yield* file.readAlloc(Fs.Size(5)).pipe(
76 Effect.flatMap(Effect.fromOption),
77 Effect.map((_) => new TextDecoder().decode(_))
78 )
79 expect(text).toBe("lorem")
80
81 yield* file.seek(Fs.Size(7), "current")
82 text = yield* file.readAlloc(Fs.Size(5)).pipe(
83 Effect.flatMap(Effect.fromOption),
84 Effect.map((_) => new TextDecoder().decode(_))
85 )
86 expect(text).toBe("dolar")
87
88 yield* file.seek(Fs.Size(1), "current")
89 text = yield* file.readAlloc(Fs.Size(8)).pipe(
90 Effect.flatMap(Effect.fromOption),
91 Effect.map((_) => new TextDecoder().decode(_))
92 )
93 expect(text).toBe("sit amet")
94
95 yield* file.seek(Fs.Size(0), "start")
96 text = yield* file.readAlloc(Fs.Size(11)).pipe(
97 Effect.flatMap(Effect.fromOption),
98 Effect.map((_) => new TextDecoder().decode(_))
99 )
100 expect(text).toBe("lorem ipsum")
101
102 text = yield* fs.stream(`${__dirname}/fixtures/text.txt`, { offset: Fs.Size(6), bytesToRead: Fs.Size(5) }).pipe(
103 Stream.map((_) => new TextDecoder().decode(_)),
104 Stream.runCollect,
105 Effect.map(Array.join(""))
106 )
107 expect(text).toBe("ipsum")
108 }).pipe(
109 Effect.scoped
110 )
111 })))
112
113 it("should track the cursor position when writing", () =>
114 runPromise(Effect.gen(function*() {
115 const fs = yield* Fs.FileSystem
116
117 yield* Effect.gen(function*() {
118 let text: string
119 const path = yield* fs.makeTempFileScoped()
120 const file = yield* fs.open(path, { flag: "w+" })
121
122 yield* file.write(new TextEncoder().encode("lorem ipsum"))
123 yield* file.write(new TextEncoder().encode(" "))
124 yield* file.write(new TextEncoder().encode("dolor sit amet"))
125 text = yield* fs.readFileString(path)
126 expect(text).toBe("lorem ipsum dolor sit amet")
127
128 yield* file.seek(Fs.Size(-4), "current")
129 yield* file.write(new TextEncoder().encode("hello world"))
130 text = yield* fs.readFileString(path)
131 expect(text).toBe("lorem ipsum dolor sit hello world")
132
133 yield* file.seek(Fs.Size(6), "start")
134 yield* file.write(new TextEncoder().encode("blabl"))
135 text = yield* fs.readFileString(path)
136 expect(text).toBe("lorem blabl dolor sit hello world")
137 }).pipe(
138 Effect.scoped
139 )
140 })))
141
142 it("should maintain a read cursor in append mode", () =>
143 runPromise(Effect.gen(function*() {
144 const fs = yield* Fs.FileSystem
145
146 yield* Effect.gen(function*() {
147 let text: string
148 const path = yield* fs.makeTempFileScoped()
149 const file = yield* fs.open(path, { flag: "a+" })
150
151 yield* file.write(new TextEncoder().encode("foo"))
152 yield* file.seek(Fs.Size(0), "start")
153
154 yield* file.write(new TextEncoder().encode("bar"))
155 text = yield* fs.readFileString(path)
156 expect(text).toBe("foobar")
157
158 text = yield* file.readAlloc(Fs.Size(3)).pipe(
159 Effect.flatMap(Effect.fromOption),
160 Effect.map((_) => new TextDecoder().decode(_))
161 )
162 expect(text).toBe("foo")
163
164 yield* file.write(new TextEncoder().encode("baz"))
165 text = yield* fs.readFileString(path)
166 expect(text).toBe("foobarbaz")
167
168 text = yield* file.readAlloc(Fs.Size(6)).pipe(
169 Effect.flatMap(Effect.fromOption),
170 Effect.map((_) => new TextDecoder().decode(_))
171 )
172 expect(text).toBe("barbaz")
173 }).pipe(
174 Effect.scoped
175 )
176 })))
177
178 it("should keep the current cursor if truncating doesn't affect it", () =>
179 runPromise(Effect.gen(function*() {
180 const fs = yield* Fs.FileSystem
181
182 yield* Effect.gen(function*() {
183 const path = yield* fs.makeTempFileScoped()
184 const file = yield* fs.open(path, { flag: "w+" })
185
186 yield* file.write(new TextEncoder().encode("lorem ipsum dolor sit amet"))
187 yield* file.seek(Fs.Size(6), "start")
188 yield* file.truncate(Fs.Size(11))
189
190 const cursor = yield* file.seek(Fs.Size(0), "current")
191 expect(cursor).toBe(Fs.Size(6))
192 }).pipe(
193 Effect.scoped
194 )
195 })))
196
197 it("should update the current cursor if truncating affects it", () =>
198 runPromise(Effect.gen(function*() {
199 const fs = yield* Fs.FileSystem
200
201 yield* Effect.gen(function*() {
202 const path = yield* fs.makeTempFileScoped()
203 const file = yield* fs.open(path, { flag: "w+" })
204
205 yield* file.write(new TextEncoder().encode("lorem ipsum dolor sit amet"))
206 yield* file.truncate(Fs.Size(11))
207
208 const cursor = yield* file.seek(Fs.Size(0), "current")
209 expect(cursor).toBe(Fs.Size(11))
210 }).pipe(
211 Effect.scoped
212 )
213 })))
214})