From 0c024fda940639b44d2b0ff8b6572a05343d597a Mon Sep 17 00:00:00 2001 From: Philipp Horstenkamp Date: Sun, 4 Jan 2026 05:44:14 +0000 Subject: [PATCH] test: add tests for readReplaceAndWriteFiles function --- __tests__/index.test.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 0fe2967..87686be 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -1,4 +1,8 @@ -const { validateAuthentication, replacePlaceholders } = require("../index"); +const { + validateAuthentication, + replacePlaceholders, + readReplaceAndWriteFiles, +} = require("../index"); const fs = require("fs"); const path = require("path"); const os = require("os"); @@ -67,6 +71,41 @@ describe("replacePlaceholders", () => { }); }); +describe("readReplaceAndWriteFiles", () => { + let tempDir; + + beforeEach(async () => { + tempDir = await fs.promises.mkdtemp( + path.join(os.tmpdir(), "replace-test-"), + ); + process.env.GITHUB_SHA = "test-sha"; + process.env.GITHUB_REF = "test-ref"; + }); + + afterEach(async () => { + if (tempDir) { + await fs.promises.rm(tempDir, { recursive: true, force: true }); + } + }); + + it("should find files and replace placeholders", async () => { + const fileName = "test.js"; + const filePath = path.join(tempDir, fileName); + const content = "hash: {{gitHash}}, ref: {{gitRef}}, host: {{hostname}}"; + await fs.promises.writeFile(filePath, content); + + const pattern = "*.js"; + // We pass tempDir as the prefix so glob searches inside it + await readReplaceAndWriteFiles(pattern, tempDir, "test-host"); + + const updatedContent = await fs.promises.readFile(filePath, "utf8"); + + expect(updatedContent).toContain("hash: test-sha"); + expect(updatedContent).toContain("ref: test-ref"); + expect(updatedContent).toContain("host: test-host"); + }); +}); + describe("glob functionality", () => { let tempDir;