|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
2 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 4 | import { it, describe, expect } from "@effect/vitest"; |
4 | 5 | import * as Effect from "effect/Effect"; |
5 | 6 | import * as FileSystem from "effect/FileSystem"; |
6 | 7 | import * as Layer from "effect/Layer"; |
7 | 8 | import * as Path from "effect/Path"; |
| 9 | +import { projectFileRevision } from "@t3tools/contracts"; |
| 10 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
8 | 11 | import * as ServerConfig from "../config.ts"; |
9 | 12 | import * as ServerSettings from "../serverSettings.ts"; |
10 | 13 | import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
11 | 14 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
12 | 15 | import * as KnownWorkspaces from "./KnownWorkspaces.ts"; |
13 | 16 | import * as WorkspaceEntries from "./WorkspaceEntries.ts"; |
14 | 17 | import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts"; |
| 18 | +import * as WorkspaceFileSystemFork from "./WorkspaceFileSystem.fork.ts"; |
15 | 19 | import * as WorkspacePaths from "./WorkspacePaths.ts"; |
16 | 20 | const ServerSettingsLayer = ServerSettings.layerTest(); |
17 | 21 | const KnownWorkspacesLayer = Layer.mock(KnownWorkspaces.KnownWorkspaces)({ |
@@ -22,6 +26,7 @@ const ProjectLayer = WorkspaceFileSystem.layer.pipe( |
22 | 26 | Layer.provide(WorkspacePaths.layer), |
23 | 27 | Layer.provide(WorkspaceEntries.layer.pipe(Layer.provide(WorkspacePaths.layer))), |
24 | 28 | Layer.provide(ServerSettingsLayer), |
| 29 | + WorkspaceFileSystemFork.oneWritePerFile, |
25 | 30 | ); |
26 | 31 | const TestLayer = Layer.empty.pipe( |
27 | 32 | Layer.provideMerge(ProjectLayer), |
@@ -81,4 +86,203 @@ it.layer(TestLayer, { excludeTestServices: true })("WorkspaceFileSystemLive", (i |
81 | 86 | }), |
82 | 87 | ); |
83 | 88 | }); |
| 89 | + |
| 90 | + describe("writeFile", () => { |
| 91 | + it.effect("refuses a save whose draft predates a disk change", () => |
| 92 | + Effect.gen(function* () { |
| 93 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 94 | + const fileSystem = yield* FileSystem.FileSystem; |
| 95 | + const path = yield* Path.Path; |
| 96 | + const cwd = yield* makeTempDir; |
| 97 | + yield* writeTextFile(cwd, "README.md", "# Readme\n"); |
| 98 | + const loadedRevision = projectFileRevision("# Readme\n"); |
| 99 | + yield* writeTextFile(cwd, "README.md", "# Readme\nline from disk\n"); |
| 100 | + |
| 101 | + const error = yield* workspaceFileSystem |
| 102 | + .writeFile({ |
| 103 | + cwd, |
| 104 | + relativePath: "README.md", |
| 105 | + contents: "# Readme edited\n", |
| 106 | + expectedRevision: loadedRevision, |
| 107 | + }) |
| 108 | + .pipe(Effect.flip); |
| 109 | + |
| 110 | + expect(error._tag).toBe("WorkspaceFileChangedError"); |
| 111 | + expect(yield* fileSystem.readFileString(path.join(cwd, "README.md"))).toBe( |
| 112 | + "# Readme\nline from disk\n", |
| 113 | + ); |
| 114 | + }), |
| 115 | + ); |
| 116 | + |
| 117 | + it.effect("refuses a save whose loaded file was deleted since", () => |
| 118 | + Effect.gen(function* () { |
| 119 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 120 | + const fileSystem = yield* FileSystem.FileSystem; |
| 121 | + const path = yield* Path.Path; |
| 122 | + const cwd = yield* makeTempDir; |
| 123 | + |
| 124 | + const error = yield* workspaceFileSystem |
| 125 | + .writeFile({ |
| 126 | + cwd, |
| 127 | + relativePath: "empty.md", |
| 128 | + contents: "draft\n", |
| 129 | + expectedRevision: projectFileRevision(""), |
| 130 | + }) |
| 131 | + .pipe(Effect.flip); |
| 132 | + |
| 133 | + expect(error._tag).toBe("WorkspaceFileChangedError"); |
| 134 | + expect(yield* fileSystem.exists(path.join(cwd, "empty.md"))).toBe(false); |
| 135 | + }), |
| 136 | + ); |
| 137 | + |
| 138 | + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 139 | + "refuses a save whose loaded file became a FIFO as not a file", |
| 140 | + () => |
| 141 | + Effect.gen(function* () { |
| 142 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 143 | + const fileSystem = yield* FileSystem.FileSystem; |
| 144 | + const path = yield* Path.Path; |
| 145 | + const cwd = yield* makeTempDir; |
| 146 | + const filePath = path.join(cwd, "README.md"); |
| 147 | + yield* writeTextFile(cwd, "README.md", "# Readme\n"); |
| 148 | + yield* fileSystem.remove(filePath); |
| 149 | + yield* Effect.promise( |
| 150 | + () => |
| 151 | + new Promise<void>((resolve, reject) => |
| 152 | + NodeChildProcess.execFile("mkfifo", [filePath], (error) => |
| 153 | + error ? reject(error) : resolve(), |
| 154 | + ), |
| 155 | + ), |
| 156 | + ); |
| 157 | + |
| 158 | + const error = yield* workspaceFileSystem |
| 159 | + .writeFile({ |
| 160 | + cwd, |
| 161 | + relativePath: "README.md", |
| 162 | + contents: "# Readme edited\n", |
| 163 | + expectedRevision: projectFileRevision("# Readme\n"), |
| 164 | + }) |
| 165 | + .pipe(Effect.flip); |
| 166 | + |
| 167 | + // Not a disk change: Overwrite would block opening the FIFO. |
| 168 | + expect(error).toBeInstanceOf(WorkspaceFileSystem.WorkspacePathNotFileError); |
| 169 | + }), |
| 170 | + ); |
| 171 | + |
| 172 | + it.effect("reports an unreadable file as a failed read, not a disk change", () => |
| 173 | + Effect.gen(function* () { |
| 174 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 175 | + const fileSystem = yield* FileSystem.FileSystem; |
| 176 | + const path = yield* Path.Path; |
| 177 | + const cwd = yield* makeTempDir; |
| 178 | + yield* writeTextFile(cwd, "README.md", "# Readme\n"); |
| 179 | + yield* fileSystem.chmod(path.join(cwd, "README.md"), 0o000); |
| 180 | + |
| 181 | + const error = yield* workspaceFileSystem |
| 182 | + .writeFile({ |
| 183 | + cwd, |
| 184 | + relativePath: "README.md", |
| 185 | + contents: "# Readme edited\n", |
| 186 | + expectedRevision: projectFileRevision("# Readme\n"), |
| 187 | + }) |
| 188 | + .pipe(Effect.flip); |
| 189 | + |
| 190 | + expect(error).toMatchObject({ |
| 191 | + _tag: "WorkspaceFileSystemOperationError", |
| 192 | + operation: "read", |
| 193 | + }); |
| 194 | + }), |
| 195 | + ); |
| 196 | + |
| 197 | + it.effect("lets only one of two saves from the same disk version through, by any name", () => |
| 198 | + Effect.gen(function* () { |
| 199 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 200 | + const fileSystem = yield* FileSystem.FileSystem; |
| 201 | + const path = yield* Path.Path; |
| 202 | + const cwd = yield* makeTempDir; |
| 203 | + yield* writeTextFile(cwd, "README.md", "# Readme\n"); |
| 204 | + yield* fileSystem.symlink(path.join(cwd, "README.md"), path.join(cwd, "Alias.md")); |
| 205 | + yield* fileSystem.link(path.join(cwd, "README.md"), path.join(cwd, "Hard.md")); |
| 206 | + const save = (relativePath: string, contents: string) => |
| 207 | + workspaceFileSystem |
| 208 | + .writeFile({ |
| 209 | + cwd, |
| 210 | + relativePath, |
| 211 | + contents, |
| 212 | + expectedRevision: projectFileRevision("# Readme\n"), |
| 213 | + }) |
| 214 | + .pipe(Effect.result); |
| 215 | + |
| 216 | + const saves = [ |
| 217 | + save("README.md", "# From one\n"), |
| 218 | + save("Alias.md", "# From two\n"), |
| 219 | + save("Hard.md", "# From three\n"), |
| 220 | + ]; |
| 221 | + const results = yield* Effect.all(saves, { |
| 222 | + concurrency: "unbounded", |
| 223 | + }); |
| 224 | + |
| 225 | + expect(results.map((result) => result._tag).toSorted()).toEqual([ |
| 226 | + "Failure", |
| 227 | + "Failure", |
| 228 | + "Success", |
| 229 | + ]); |
| 230 | + }), |
| 231 | + ); |
| 232 | + |
| 233 | + it.effect("refuses a path outside the workspace before following it", () => |
| 234 | + Effect.gen(function* () { |
| 235 | + const fileSystem = yield* FileSystem.FileSystem; |
| 236 | + const cwd = yield* makeTempDir; |
| 237 | + const followed: Array<string> = []; |
| 238 | + const watchedFileSystem = Layer.succeed(FileSystem.FileSystem, { |
| 239 | + ...fileSystem, |
| 240 | + stat: (target) => { |
| 241 | + followed.push(target); |
| 242 | + return fileSystem.stat(target); |
| 243 | + }, |
| 244 | + realPath: (target) => { |
| 245 | + followed.push(target); |
| 246 | + return fileSystem.realPath(target); |
| 247 | + }, |
| 248 | + }); |
| 249 | + |
| 250 | + const failures = yield* Effect.gen(function* () { |
| 251 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 252 | + return yield* Effect.forEach( |
| 253 | + ["../outside.md", "//server/share/notes.md"], |
| 254 | + (relativePath) => |
| 255 | + workspaceFileSystem.writeFile({ cwd, relativePath, contents: "x" }).pipe(Effect.flip), |
| 256 | + ); |
| 257 | + }).pipe(Effect.provide(Layer.fresh(ProjectLayer).pipe(Layer.provide(watchedFileSystem)))); |
| 258 | + |
| 259 | + expect(failures.map((failure) => failure._tag)).toEqual([ |
| 260 | + "WorkspacePathOutsideRootError", |
| 261 | + "WorkspacePathOutsideRootError", |
| 262 | + ]); |
| 263 | + expect(followed).toEqual([]); |
| 264 | + }), |
| 265 | + ); |
| 266 | + |
| 267 | + it.effect("saves over the disk version the draft started from", () => |
| 268 | + Effect.gen(function* () { |
| 269 | + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; |
| 270 | + const fileSystem = yield* FileSystem.FileSystem; |
| 271 | + const path = yield* Path.Path; |
| 272 | + const cwd = yield* makeTempDir; |
| 273 | + yield* writeTextFile(cwd, "README.md", "# Readme\n"); |
| 274 | + |
| 275 | + yield* workspaceFileSystem.writeFile({ |
| 276 | + cwd, |
| 277 | + relativePath: "README.md", |
| 278 | + contents: "# Readme edited\n", |
| 279 | + expectedRevision: projectFileRevision("# Readme\n"), |
| 280 | + }); |
| 281 | + |
| 282 | + expect(yield* fileSystem.readFileString(path.join(cwd, "README.md"))).toBe( |
| 283 | + "# Readme edited\n", |
| 284 | + ); |
| 285 | + }), |
| 286 | + ); |
| 287 | + }); |
84 | 288 | }); |