From cee06b595291411383658612ee24f0cd059b5841 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 19 Sep 2023 19:31:00 +0200 Subject: [PATCH] backend-test-utils: added DirectoryMocker root option Signed-off-by: Patrik Oldsberg --- .../src/files/DirectoryMocker.ts | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/backend-test-utils/src/files/DirectoryMocker.ts b/packages/backend-test-utils/src/files/DirectoryMocker.ts index 6b67f8f9c0..7fe7a1d1f5 100644 --- a/packages/backend-test-utils/src/files/DirectoryMocker.ts +++ b/packages/backend-test-utils/src/files/DirectoryMocker.ts @@ -27,19 +27,33 @@ type DirectoryNode = { type MockDirectoryNode = DirectoryNode | FileNode; +interface DirectoryMockerOptions { + /** + * The root path to create the directory in. Defaults to a temporary directory. + * + * If an existing directory is provided, it will not be cleaned up after the test. + */ + root?: string; +} + export class DirectoryMocker { - static create() { - const root = fs.mkdtempSync( - joinPath(getTmpDir(), 'backstage-tmp-test-dir-'), - ); + static create(options?: DirectoryMockerOptions) { + const root = + options?.root ?? + fs.mkdtempSync(joinPath(getTmpDir(), 'backstage-tmp-test-dir-')); const mocker = new DirectoryMocker(root); - process.on('beforeExit', mocker.#cleanupSync); + const shouldCleanup = !options?.root || !fs.pathExistsSync(options.root); + if (shouldCleanup) { + process.on('beforeExit', mocker.#cleanupSync); - afterAll(async () => { - await mocker.cleanup(); - }); + try { + afterAll(mocker.cleanup); + } catch { + /* ignore */ + } + } return mocker; } @@ -73,11 +87,11 @@ export class DirectoryMocker { await createFiles(root, this.#root); } - async cleanup() { + cleanup = async () => { await fs.rm(this.#root, { recursive: true, force: true }); - } + }; - #cleanupSync() { + #cleanupSync = () => { fs.rmSync(this.#root, { recursive: true, force: true }); - } + }; }