backend-test-utils: make it possible to pass absolute paths to setContent

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-04 20:57:35 +02:00
parent b160cb5551
commit 699203d94f
2 changed files with 5 additions and 6 deletions
@@ -91,7 +91,7 @@ describe('createMockDirectory', () => {
mockDir.addContent({
'b.txt': 'b',
b: {
[mockDir.resolve('b')]: {
'c.txt': 'c',
},
});
@@ -279,19 +279,18 @@ class MockDirectoryImpl {
const entries: MockEntry[] = [];
function traverse(node: MockDirectoryContent[string], path: string) {
const trimmedPath = path.startsWith('/') ? path.slice(1) : path; // trim leading slash
if (typeof node === 'string') {
entries.push({
type: 'file',
path: trimmedPath,
path,
content: Buffer.from(node, 'utf8'),
});
} else if (node instanceof Buffer) {
entries.push({ type: 'file', path: trimmedPath, content: node });
entries.push({ type: 'file', path, content: node });
} else {
entries.push({ type: 'dir', path: trimmedPath });
entries.push({ type: 'dir', path });
for (const [name, child] of Object.entries(node)) {
traverse(child, `${trimmedPath}/${name}`);
traverse(child, path ? `${path}/${name}` : name);
}
}
}