backend-test-utils: add directoryMocker.getContent()

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-20 11:55:49 +02:00
parent c09cd27904
commit 8f6194ff18
@@ -81,6 +81,32 @@ export class DirectoryMocker {
}
}
async getContent(): Promise<MockDirectory | undefined> {
async function read(path: string): Promise<MockDirectory | undefined> {
if (!(await fs.pathExists(path))) {
return undefined;
}
const entries = await fs.readdir(path);
return Object.fromEntries(
await Promise.all(
entries.map(async entry => {
const fullPath = resolvePath(path, entry);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
return [entry, await read(fullPath)];
}
const content = await fs.readFile(fullPath);
return [entry, content.toString('utf8')];
}),
),
);
}
return read(this.#root);
}
#transformInput(input: MockDirectory[string]): MockEntry[] {
const entries: MockEntry[] = [];