test: add tests for readFilesIntoDict function
This commit is contained in:
@@ -2,6 +2,7 @@ const {
|
|||||||
validateAuthentication,
|
validateAuthentication,
|
||||||
replacePlaceholders,
|
replacePlaceholders,
|
||||||
readReplaceAndWriteFiles,
|
readReplaceAndWriteFiles,
|
||||||
|
readFilesIntoDict,
|
||||||
} = require("../index");
|
} = require("../index");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
@@ -106,6 +107,48 @@ describe("readReplaceAndWriteFiles", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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", () => {
|
describe("glob functionality", () => {
|
||||||
let tempDir;
|
let tempDir;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user