From c63a1bbb7d50045dad933fcf28e4e758fc6f17bc Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Thu, 21 May 2026 19:47:00 +0700 Subject: [PATCH] improve test structure and mocking approach Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Alfi Maulana --- dist/main.bundle.mjs | 4 ++-- src/main.test.ts | 35 +++++++++++++++++++---------------- src/main.ts | 4 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/dist/main.bundle.mjs b/dist/main.bundle.mjs index 027c4fe2..3f5ac733 100644 --- a/dist/main.bundle.mjs +++ b/dist/main.bundle.mjs @@ -1,5 +1,5 @@ import 'node:fs'; -import fsPromises from 'node:fs/promises'; +import { mkdir } from 'node:fs/promises'; import os from 'node:os'; import 'node:path'; @@ -26,7 +26,7 @@ function logError(err) { try { const path = getInput("path"); - await fsPromises.mkdir(path, { recursive: true }); + await mkdir(path, { recursive: true }); } catch (err) { logError(err); diff --git a/src/main.test.ts b/src/main.test.ts index 53a60821..e491a9f1 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -1,34 +1,37 @@ -import fsPromises from "node:fs/promises"; -import os from "node:os"; -import { afterAll, beforeAll, beforeEach, expect, it, vi } from "vitest"; +import { access, mkdir, rm, writeFile } from "node:fs/promises"; +import path from "node:path"; +import { afterAll, beforeEach, expect, it, vi } from "vitest"; -beforeAll(() => { - process.chdir(os.tmpdir()); -}); +vi.mock("gha-utils"); + +const tmpDir = path.resolve(import.meta.dirname, ".main.test.tmp"); beforeEach(async () => { - await fsPromises.rm("path", { recursive: true, force: true }); + await rm(tmpDir, { recursive: true, force: true }); + await mkdir(tmpDir); + process.exitCode = undefined; vi.resetModules(); }); +afterAll(() => rm(tmpDir, { recursive: true, force: true })); + it("should create a directory recursively", async () => { - process.env.INPUT_PATH = "path/to/new/directory"; + const { getInput } = await import("gha-utils"); + vi.mocked(getInput).mockReturnValue(path.join(tmpDir, "new/directory")); + await import("./main.js"); - await fsPromises.access("path/to/new/directory"); + await access(path.join(tmpDir, "new/directory")); expect(process.exitCode).toBeUndefined(); }); it("should fail to create a directory because a file already exists", async () => { - await fsPromises.writeFile("path", "a data"); + await writeFile(path.join(tmpDir, "file"), ""); + + const { getInput } = await import("gha-utils"); + vi.mocked(getInput).mockReturnValue(path.join(tmpDir, "file/child")); - process.env.INPUT_PATH = "path/to/new/directory"; await import("./main.js"); expect(process.exitCode).toBe(1); - process.exitCode = undefined; -}); - -afterAll(async () => { - await fsPromises.rm("path", { recursive: true, force: true }); }); diff --git a/src/main.ts b/src/main.ts index 4e6064c7..84ffed8d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,9 @@ import { getInput, logError } from "gha-utils"; -import fsPromises from "node:fs/promises"; +import { mkdir } from "node:fs/promises"; try { const path = getInput("path"); - await fsPromises.mkdir(path, { recursive: true }); + await mkdir(path, { recursive: true }); } catch (err) { logError(err); process.exitCode = 1;