|
| 1 | +import { chmod, mkdtemp, readFile, stat, symlink, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { writePrivateEnvFile } from "./privateEnvFile.js"; |
| 6 | + |
| 7 | +describe("writePrivateEnvFile", () => { |
| 8 | + it("creates private files", async () => { |
| 9 | + const directory = await mkdtemp(join(tmpdir(), "trigger-env-")); |
| 10 | + const outputPath = join(directory, ".env.local"); |
| 11 | + |
| 12 | + await writePrivateEnvFile(outputPath, "SECRET=value\n", "wx"); |
| 13 | + |
| 14 | + expect(await readFile(outputPath, "utf8")).toBe("SECRET=value\n"); |
| 15 | + expect((await stat(outputPath)).mode & 0o777).toBe(0o600); |
| 16 | + }); |
| 17 | + |
| 18 | + it("creates private files in overwrite mode when the path is absent", async () => { |
| 19 | + const directory = await mkdtemp(join(tmpdir(), "trigger-env-")); |
| 20 | + const outputPath = join(directory, ".env.local"); |
| 21 | + |
| 22 | + await writePrivateEnvFile(outputPath, "SECRET=value\n", "w"); |
| 23 | + |
| 24 | + expect(await readFile(outputPath, "utf8")).toBe("SECRET=value\n"); |
| 25 | + expect((await stat(outputPath)).mode & 0o777).toBe(0o600); |
| 26 | + }); |
| 27 | + |
| 28 | + it("does not replace an existing file in exclusive mode", async () => { |
| 29 | + const directory = await mkdtemp(join(tmpdir(), "trigger-env-")); |
| 30 | + const outputPath = join(directory, ".env.local"); |
| 31 | + await writeFile(outputPath, "old=value\n"); |
| 32 | + |
| 33 | + await expect(writePrivateEnvFile(outputPath, "SECRET=value\n", "wx")).rejects.toMatchObject({ |
| 34 | + code: "EEXIST", |
| 35 | + }); |
| 36 | + |
| 37 | + expect(await readFile(outputPath, "utf8")).toBe("old=value\n"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("tightens an existing file before replacing its contents", async () => { |
| 41 | + const directory = await mkdtemp(join(tmpdir(), "trigger-env-")); |
| 42 | + const outputPath = join(directory, ".env.local"); |
| 43 | + await writeFile(outputPath, "old=value\n", { mode: 0o644 }); |
| 44 | + await chmod(outputPath, 0o644); |
| 45 | + |
| 46 | + await writePrivateEnvFile(outputPath, "SECRET=value\n", "w"); |
| 47 | + |
| 48 | + expect(await readFile(outputPath, "utf8")).toBe("SECRET=value\n"); |
| 49 | + expect((await stat(outputPath)).mode & 0o777).toBe(0o600); |
| 50 | + }); |
| 51 | + |
| 52 | + it("preserves symlinks while tightening and replacing their target", async () => { |
| 53 | + const directory = await mkdtemp(join(tmpdir(), "trigger-env-")); |
| 54 | + const targetPath = join(directory, "target.env"); |
| 55 | + const outputPath = join(directory, ".env.local"); |
| 56 | + await writeFile(targetPath, "old=value\n", { mode: 0o644 }); |
| 57 | + await chmod(targetPath, 0o644); |
| 58 | + await symlink(targetPath, outputPath); |
| 59 | + |
| 60 | + await writePrivateEnvFile(outputPath, "SECRET=value\n", "w"); |
| 61 | + |
| 62 | + expect(await readFile(targetPath, "utf8")).toBe("SECRET=value\n"); |
| 63 | + expect((await stat(targetPath)).mode & 0o777).toBe(0o600); |
| 64 | + }); |
| 65 | +}); |
0 commit comments