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;