backend-test-utils: MockDirectory fixes for windows

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-21 17:33:21 +02:00
parent b516ebc8e7
commit 71b63f6ad1
4 changed files with 25 additions and 6 deletions
+1
View File
@@ -46,6 +46,7 @@
"@backstage/backend-common": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
"@backstage/plugin-auth-node": "workspace:^",
"@backstage/types": "workspace:^",
"better-sqlite3": "^8.0.0",
@@ -16,7 +16,11 @@
import fs from 'fs-extra';
import os from 'os';
import { relative as relativePath } from 'path';
import {
join as joinPath,
resolve as resolvePath,
relative as relativePath,
} from 'path';
import { MockDirectory } from './MockDirectory';
describe('MockDirectory', () => {
@@ -27,7 +31,9 @@ describe('MockDirectory', () => {
it('should resolve paths', () => {
expect(mockDir.path).toEqual(expect.any(String));
expect(relativePath(mockDir.path, mockDir.resolve('a'))).toBe('a');
expect(relativePath(mockDir.path, mockDir.resolve('a/b/c'))).toBe('a/b/c');
expect(relativePath(mockDir.path, mockDir.resolve('a/b/c'))).toBe(
joinPath('a', 'b', 'c'),
);
});
it('should remove itself', async () => {
@@ -267,14 +273,15 @@ describe('MockDirectory', () => {
});
it('should reject non-child paths', async () => {
const path = resolvePath('/root/a.txt');
await expect(mockDir.setContent({ '/root/a.txt': 'a' })).rejects.toThrow(
"Provided path must resolve to a child path of the mock directory, got '/root/a.txt'",
`Provided path must resolve to a child path of the mock directory, got '${path}'`,
);
await expect(mockDir.addContent({ '/root/a.txt': 'a' })).rejects.toThrow(
"Provided path must resolve to a child path of the mock directory, got '/root/a.txt'",
`Provided path must resolve to a child path of the mock directory, got '${path}'`,
);
await expect(mockDir.content({ path: '/root/a.txt' })).rejects.toThrow(
"Provided path must resolve to a child path of the mock directory, got '/root/a.txt'",
`Provided path must resolve to a child path of the mock directory, got '${path}'`,
);
});
@@ -28,6 +28,7 @@ import {
win32,
posix,
} from 'path';
import { isError } from '@backstage/errors';
/**
* The content of a mock directory represented by a nested object structure.
@@ -334,7 +335,16 @@ export class MockDirectory {
* Removes the mock directory and all its contents.
*/
remove = async (): Promise<void> => {
await fs.rm(this.#root, { recursive: true, force: true });
try {
await fs.rm(this.#root, { recursive: true, force: true });
} catch (error: unknown) {
if (isError(error) && error.code === 'ENOTEMPTY') {
// Windows can be a bit flaky, give it another go
await fs.rm(this.#root, { recursive: true, force: true });
} else {
throw error;
}
}
};
#transformInput(input: MockDirectoryContent[string]): MockEntry[] {
+1
View File
@@ -3642,6 +3642,7 @@ __metadata:
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/errors": "workspace:^"
"@backstage/plugin-auth-node": "workspace:^"
"@backstage/types": "workspace:^"
"@types/supertest": ^2.0.8