Compare commits
5 Commits
1730ad07fc
...
build/mini
| Author | SHA1 | Date | |
|---|---|---|---|
| c05341c0a7 | |||
| 6a098d425e | |||
| 4a77ba188a | |||
| 6c7a0961a7 | |||
| 3ff19001a7 |
@@ -10,9 +10,9 @@ jobs:
|
||||
runs-on: pi
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/setup-node@v4
|
||||
- uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: '22'
|
||||
node-version: '24'
|
||||
- run: npm install
|
||||
shell: bash
|
||||
- run: npm test
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1077,3 +1077,4 @@ FodyWeavers.xsd
|
||||
/node_modules/
|
||||
/node_modules/.cache/
|
||||
/coverage/
|
||||
!/dist/
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
const { validateAuthentication, replacePlaceholders } = require("../index");
|
||||
const {
|
||||
validateAuthentication,
|
||||
replacePlaceholders,
|
||||
readReplaceAndWriteFiles,
|
||||
readFilesIntoDict,
|
||||
} = require("../index");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
@@ -67,6 +72,83 @@ 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("readFilesIntoDict", () => {
|
||||
let tempDir;
|
||||
|
||||
beforeEach(async () => {
|
||||
tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "read-test-"));
|
||||
await fs.promises.mkdir(path.join(tempDir, "subdir"), { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (tempDir) {
|
||||
await fs.promises.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("should read files into a dictionary with correct keys", async () => {
|
||||
const file1 = "file1.js";
|
||||
const content1 = "content1";
|
||||
await fs.promises.writeFile(path.join(tempDir, file1), content1);
|
||||
|
||||
const file2 = "subdir/file2.js";
|
||||
const content2 = "content2";
|
||||
await fs.promises.writeFile(path.join(tempDir, file2), content2);
|
||||
|
||||
const pattern = "**/*.js";
|
||||
const result = await readFilesIntoDict(pattern, tempDir);
|
||||
|
||||
// Keys should be relative paths without extension
|
||||
// On Windows, the path separator might differ, so we should be careful or just check contents
|
||||
|
||||
// Based on implementation:
|
||||
// key = key.slice(prefix.length);
|
||||
// key = path.basename(key, path.extname(key)); // Drop the file suffix -> THIS IS BUGGY for subdirs?
|
||||
|
||||
// Let's check the implementation of readFilesIntoDict again in index.js
|
||||
// It does: key = path.basename(key, path.extname(key));
|
||||
// This removes the directory part! So subdir/file2.js becomes file2
|
||||
|
||||
expect(result["file1"]).toBe(content1);
|
||||
expect(result["file2"]).toBe(content2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("glob functionality", () => {
|
||||
let tempDir;
|
||||
|
||||
|
||||
51721
dist/index.js
vendored
51721
dist/index.js
vendored
File diff suppressed because one or more lines are too long
8
index.js
8
index.js
@@ -174,9 +174,15 @@ async function postCode() {
|
||||
});
|
||||
}
|
||||
}
|
||||
postCode();
|
||||
|
||||
if (require.main === module) {
|
||||
postCode();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validateAuthentication,
|
||||
replacePlaceholders,
|
||||
postCode,
|
||||
readReplaceAndWriteFiles,
|
||||
readFilesIntoDict,
|
||||
};
|
||||
|
||||
433
package-lock.json
generated
433
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,7 @@
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "vitest run --globals --coverage",
|
||||
"build": "ncc build index.js -o dist --external utf-8-validate --external bufferutil"
|
||||
"build": "ncc build index.js -o dist -m --external utf-8-validate --external bufferutil"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/core": "^2.0.0",
|
||||
|
||||
Reference in New Issue
Block a user