chore: Add a unit testing framework #49

Merged
Philipp merged 6 commits from add-testing into main 2026-01-04 07:02:24 +01:00
Showing only changes of commit 0c024fda94 - Show all commits

View File

@@ -1,4 +1,8 @@
const { validateAuthentication, replacePlaceholders } = require("../index"); const {
validateAuthentication,
replacePlaceholders,
readReplaceAndWriteFiles,
} = require("../index");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const os = require("os"); 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", () => { describe("glob functionality", () => {
let tempDir; let tempDir;