From 45f9748e436ef19bbc5121edfb5bfcd795eb3fef Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 5 Oct 2023 21:29:15 +0200 Subject: [PATCH] backend-test-utils: add MockDirectory content callback + default file modes Signed-off-by: Patrik Oldsberg --- packages/backend-test-utils/api-report.md | 17 ++++++- .../src/filesystem/MockDirectory.test.ts | 14 ++++++ .../src/filesystem/MockDirectory.ts | 49 +++++++++++++++++-- .../src/filesystem/index.ts | 2 + 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index a477f70298..ac012e2f69 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -54,9 +54,24 @@ export interface MockDirectory { // @public export type MockDirectoryContent = { - [name in string]: MockDirectoryContent | string | Buffer; + [name in string]: + | MockDirectoryContent + | string + | Buffer + | MockDirectoryContentCallback; }; +// @public +export type MockDirectoryContentCallback = ( + ctx: MockDirectoryContentCallbackContext, +) => void; + +// @public +export interface MockDirectoryContentCallbackContext { + path: string; + symlink(target: string): void; +} + // @public export interface MockDirectoryContentOptions { path?: string; diff --git a/packages/backend-test-utils/src/filesystem/MockDirectory.test.ts b/packages/backend-test-utils/src/filesystem/MockDirectory.test.ts index 8040d03f44..fd22f09432 100644 --- a/packages/backend-test-utils/src/filesystem/MockDirectory.test.ts +++ b/packages/backend-test-utils/src/filesystem/MockDirectory.test.ts @@ -119,6 +119,20 @@ describe('createMockDirectory', () => { }); }); + it('should be able to use callback for more detailed file system operations', () => { + mockDir.setContent({ + 'a.txt': 'a', + 'b.txt': ctx => ctx.symlink('./a.txt'), + 'c.txt': ctx => fs.copyFileSync(mockDir.resolve('a.txt'), ctx.path), + }); + + expect(mockDir.content()).toEqual({ + 'a.txt': 'a', + 'b.txt': 'a', + 'c.txt': 'a', + }); + }); + it('should read content from sub dirs', () => { mockDir.setContent({ 'a.txt': 'a', diff --git a/packages/backend-test-utils/src/filesystem/MockDirectory.ts b/packages/backend-test-utils/src/filesystem/MockDirectory.ts index 61eb4fce24..b066b9acd5 100644 --- a/packages/backend-test-utils/src/filesystem/MockDirectory.ts +++ b/packages/backend-test-utils/src/filesystem/MockDirectory.ts @@ -30,6 +30,28 @@ import { const tmpdirMarker = Symbol('os-tmpdir-mock'); +/** + * A context that allows for more advanced file system operations when writing mock directory content. + * + * @public + */ +export interface MockDirectoryContentCallbackContext { + /** Absolute path to the location of this piece of content on the filesystem */ + path: string; + + /** Creates a symbolic link at the current location */ + symlink(target: string): void; +} + +/** + * A callback that allows for more advanced file system operations when writing mock directory content. + * + * @public + */ +export type MockDirectoryContentCallback = ( + ctx: MockDirectoryContentCallbackContext, +) => void; + /** * The content of a mock directory represented by a nested object structure. * @@ -54,7 +76,11 @@ const tmpdirMarker = Symbol('os-tmpdir-mock'); * @public */ export type MockDirectoryContent = { - [name in string]: MockDirectoryContent | string | Buffer; + [name in string]: + | MockDirectoryContent + | string + | Buffer + | MockDirectoryContentCallback; }; /** @@ -178,6 +204,11 @@ type MockEntry = | { type: 'dir'; path: string; + } + | { + type: 'callback'; + path: string; + callback: MockDirectoryContentCallback; }; /** @internal */ @@ -214,10 +245,18 @@ class MockDirectoryImpl { } if (entry.type === 'dir') { - fs.ensureDirSync(fullPath, { mode: 0o777 }); + fs.ensureDirSync(fullPath); } else if (entry.type === 'file') { - fs.ensureDirSync(dirname(fullPath), { mode: 0o777 }); - fs.writeFileSync(fullPath, entry.content, { mode: 0o666 }); + fs.ensureDirSync(dirname(fullPath)); + fs.writeFileSync(fullPath, entry.content); + } else if (entry.type === 'callback') { + fs.ensureDirSync(dirname(fullPath)); + entry.callback({ + path: fullPath, + symlink(target: string) { + fs.symlinkSync(target, fullPath); + }, + }); } } } @@ -287,6 +326,8 @@ class MockDirectoryImpl { }); } else if (node instanceof Buffer) { entries.push({ type: 'file', path, content: node }); + } else if (typeof node === 'function') { + entries.push({ type: 'callback', path, callback: node }); } else { entries.push({ type: 'dir', path }); for (const [name, child] of Object.entries(node)) { diff --git a/packages/backend-test-utils/src/filesystem/index.ts b/packages/backend-test-utils/src/filesystem/index.ts index 0a4d8c7d00..e18b0b55b8 100644 --- a/packages/backend-test-utils/src/filesystem/index.ts +++ b/packages/backend-test-utils/src/filesystem/index.ts @@ -20,4 +20,6 @@ export { type MockDirectoryOptions, type MockDirectoryContent, type MockDirectoryContentOptions, + type MockDirectoryContentCallback, + type MockDirectoryContentCallbackContext, } from './MockDirectory';