Skip to content

Commit

Permalink
fix!: writeFileSafely関数を非同期に変更し、テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jan 23, 2025
1 parent 3c69896 commit 6b2b949
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/backend/electron/fileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from "fs";
import { moveFileSync } from "move-file";
import { uuid4 } from "@/helpers/random";
import { createLogger } from "@/helpers/log";

Expand All @@ -14,15 +13,13 @@ export function writeFileSafely(
data: string | NodeJS.ArrayBufferView,
) {
const tmpPath = `${path}-${uuid4()}.tmp`;
fs.writeFileSync(tmpPath, data, { flag: "wx" });

try {
moveFileSync(tmpPath, path, {
overwrite: true,
});
fs.writeFileSync(tmpPath, data, { flag: "wx" });

Check failure on line 18 in src/backend/electron/fileHelper.ts

View workflow job for this annotation

GitHub Actions / unit-test

../tests/unit/backend/electron/fileHelper.node.spec.ts > writeFileSafely > 存在しないディレクトリに書き込もうとするとエラー

Error: ENOENT: no such file or directory, open 'C:\Users\RUNNER~1\AppData\Local\Temp\b1ccffec-9c62-46a9-a281-6529fc8237259fHtWi\b2ed068f-cd28-4d4f-9a63-abecafd870a5\subdir\test.txt-1ea29e82-51e8-452d-9150-f63fdffcedcb.tmp' ❯ Module.writeFileSafely backend/electron/fileHelper.ts:18:8 ❯ ../tests/unit/backend/electron/fileHelper.node.spec.ts:37:18 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'C:\Users\RUNNER~1\AppData\Local\Temp\b1ccffec-9c62-46a9-a281-6529fc8237259fHtWi\b2ed068f-cd28-4d4f-9a63-abecafd870a5\subdir\test.txt-1ea29e82-51e8-452d-9150-f63fdffcedcb.tmp' }
fs.renameSync(tmpPath, path);
} catch (error) {
if (fs.existsSync(tmpPath)) {
fs.promises.unlink(tmpPath).catch((reason) => {
void fs.promises.unlink(tmpPath).catch((reason) => {
log.warn("Failed to remove %s\n %o", tmpPath, reason);
});
}
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/backend/electron/fileHelper.node.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from "fs";
import path from "path";
import os from "os";
import { test, expect, beforeAll, afterAll } from "vitest";
import { writeFileSafely } from "@/backend/electron/fileHelper";
import { uuid4 } from "@/helpers/random";

let tmpDir: string;

beforeAll(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), uuid4()));
});

afterAll(() => {
fs.rmdirSync(tmpDir, { recursive: true });
});

describe("writeFileSafely", () => {
test("ファイルを書き込める", async () => {
const filePath = path.join(tmpDir, uuid4());
const content = "Hello World";
writeFileSafely(filePath, content);
expect(fs.readFileSync(filePath, "utf-8")).toBe(content);
});

test("ファイルを上書きできる", async () => {
const filePath = path.join(tmpDir, uuid4());
fs.writeFileSync(filePath, "old content");
const newContent = "new content";
writeFileSafely(filePath, newContent);
expect(fs.readFileSync(filePath, "utf-8")).toBe(newContent);
});

test("存在しないディレクトリに書き込もうとするとエラー", async () => {
const nonExistentDir = path.join(tmpDir, uuid4(), "subdir");
const filePath = path.join(nonExistentDir, "test.txt");
await expect(writeFileSafely(filePath, "data")).rejects.toThrow();
});
});

0 comments on commit 6b2b949

Please sign in to comment.