From 4606abef326ec47ceed07cd2e284e3f8712288f7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 21 Sep 2023 12:17:37 +0200 Subject: [PATCH] backend-test-utils: MockDirectory API report updates Signed-off-by: Patrik Oldsberg --- packages/backend-test-utils/api-report.md | 32 +++++++++++++++++++ .../src/filesystem/MockDirectory.ts | 16 +++++----- .../src/filesystem/index.ts | 7 +++- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index 4a27ac21a9..03df9de264 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -4,6 +4,7 @@ ```ts /// +/// import { Backend } from '@backstage/backend-app-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; @@ -33,6 +34,37 @@ import { UrlReaderService } from '@backstage/backend-plugin-api'; // @public (undocumented) export function isDockerDisabledForTests(): boolean; +// @public +export class MockDirectory { + addContent(root: MockDirectoryContent): Promise; + cleanup: () => Promise; + clear: () => Promise; + content( + options?: MockDirectoryContentOptions, + ): Promise; + static create(options?: MockDirectoryCreateOptions): MockDirectory; + static mockOsTmpDir(): MockDirectory; + get path(): string; + resolve(...paths: string[]): string; + setContent(root: MockDirectoryContent): Promise; +} + +// @public +export type MockDirectoryContent = { + [name in string]: MockDirectoryContent | string | Buffer; +}; + +// @public +export interface MockDirectoryContentOptions { + path?: string; + shouldReadAsText?: boolean | ((path: string, buffer: Buffer) => boolean); +} + +// @public +export interface MockDirectoryCreateOptions { + root?: string; +} + // @public (undocumented) export namespace mockServices { // (undocumented) diff --git a/packages/backend-test-utils/src/filesystem/MockDirectory.ts b/packages/backend-test-utils/src/filesystem/MockDirectory.ts index 2ef61b3d0c..81a65783fb 100644 --- a/packages/backend-test-utils/src/filesystem/MockDirectory.ts +++ b/packages/backend-test-utils/src/filesystem/MockDirectory.ts @@ -129,7 +129,7 @@ export class MockDirectory { * }) * ``` */ - static create(options?: MockDirectoryCreateOptions) { + static create(options?: MockDirectoryCreateOptions): MockDirectory { const root = options?.root ?? fs.mkdtempSync(joinPath(getTmpDir(), 'backstage-tmp-test-dir-')); @@ -156,7 +156,7 @@ export class MockDirectory { * * @returns */ - static mockOsTmpDir() { + static mockOsTmpDir(): MockDirectory { const mocker = MockDirectory.create(); const origTmpdir = os.tmpdir; os.tmpdir = () => mocker.path; @@ -180,14 +180,14 @@ export class MockDirectory { /** * The path to the root of the mock directory */ - get path() { + get path(): string { return this.#root; } /** * Resolves a path relative to the root of the mock directory. */ - resolve(...paths: string[]) { + resolve(...paths: string[]): string { return resolvePath(this.#root, ...paths); } @@ -207,7 +207,7 @@ export class MockDirectory { * }); * ``` */ - async setContent(root: MockDirectoryContent) { + async setContent(root: MockDirectoryContent): Promise { await this.cleanup(); return this.addContent(root); @@ -229,7 +229,7 @@ export class MockDirectory { * }); * ``` */ - async addContent(root: MockDirectoryContent) { + async addContent(root: MockDirectoryContent): Promise { const entries = this.#transformInput(root); for (const entry of entries) { @@ -324,14 +324,14 @@ export class MockDirectory { /** * Clears the content of the mock directory, ensuring that the directory itself exists. */ - clear = async () => { + clear = async (): Promise => { await this.setContent({}); }; /** * Removes the mock directory and all its contents. */ - cleanup = async () => { + cleanup = async (): Promise => { await fs.rm(this.#root, { recursive: true, force: true }); }; diff --git a/packages/backend-test-utils/src/filesystem/index.ts b/packages/backend-test-utils/src/filesystem/index.ts index f0b05641f5..d1077db980 100644 --- a/packages/backend-test-utils/src/filesystem/index.ts +++ b/packages/backend-test-utils/src/filesystem/index.ts @@ -14,4 +14,9 @@ * limitations under the License. */ -export { MockDirectory } from './MockDirectory'; +export { + MockDirectory, + type MockDirectoryContent, + type MockDirectoryContentOptions, + type MockDirectoryCreateOptions, +} from './MockDirectory';