backend-test-utils: add DirectoryMocker shouldReadAsText option
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
"mysql2": "^2.2.5",
|
||||
"pg": "^8.3.0",
|
||||
"testcontainers": "^8.1.2",
|
||||
"textextensions": "^5.16.0",
|
||||
"uuid": "^8.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -16,8 +16,14 @@
|
||||
|
||||
import { resolveSafeChildPath } from '@backstage/backend-common';
|
||||
import fs from 'fs-extra';
|
||||
import textextensions from 'textextensions';
|
||||
import { tmpdir as getTmpDir } from 'os';
|
||||
import { dirname, join as joinPath, resolve as resolvePath } from 'path';
|
||||
import {
|
||||
dirname,
|
||||
extname,
|
||||
join as joinPath,
|
||||
resolve as resolvePath,
|
||||
} from 'path';
|
||||
|
||||
type MockEntry = {
|
||||
path: string;
|
||||
@@ -37,6 +43,15 @@ interface DirectoryMockerOptions {
|
||||
root?: string;
|
||||
}
|
||||
|
||||
interface DirectoryMockerGetContentOptions {
|
||||
/**
|
||||
* Whether or not to return files as text rather than buffers.
|
||||
*
|
||||
* Defaults to checking the file extension against a list of known text extensions.
|
||||
*/
|
||||
shouldReadAsText?: boolean | ((path: string, buffer: Buffer) => boolean);
|
||||
}
|
||||
|
||||
export class DirectoryMocker {
|
||||
static create(options?: DirectoryMockerOptions) {
|
||||
const root =
|
||||
@@ -81,7 +96,14 @@ export class DirectoryMocker {
|
||||
}
|
||||
}
|
||||
|
||||
async getContent(): Promise<MockDirectory | undefined> {
|
||||
async getContent(
|
||||
options?: DirectoryMockerGetContentOptions,
|
||||
): Promise<MockDirectory | undefined> {
|
||||
const shouldReadAsText =
|
||||
(typeof options?.shouldReadAsText === 'boolean'
|
||||
? () => options?.shouldReadAsText
|
||||
: options?.shouldReadAsText) ??
|
||||
((path: string) => textextensions.includes(extname(path).slice(1)));
|
||||
async function read(path: string): Promise<MockDirectory | undefined> {
|
||||
if (!(await fs.pathExists(path))) {
|
||||
return undefined;
|
||||
@@ -98,7 +120,11 @@ export class DirectoryMocker {
|
||||
return [entry, await read(fullPath)];
|
||||
}
|
||||
const content = await fs.readFile(fullPath);
|
||||
return [entry, content.toString('utf8')];
|
||||
|
||||
if (shouldReadAsText(fullPath, content)) {
|
||||
return [entry, content.toString('utf8')];
|
||||
}
|
||||
return [entry, content];
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -3656,6 +3656,7 @@ __metadata:
|
||||
pg: ^8.3.0
|
||||
supertest: ^6.1.3
|
||||
testcontainers: ^8.1.2
|
||||
textextensions: ^5.16.0
|
||||
uuid: ^8.0.0
|
||||
peerDependencies:
|
||||
"@types/jest": "*"
|
||||
@@ -40458,10 +40459,10 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"textextensions@npm:^5.12.0, textextensions@npm:^5.13.0":
|
||||
version: 5.14.0
|
||||
resolution: "textextensions@npm:5.14.0"
|
||||
checksum: 1f610ccf2a2c1445fb7156c23b5c8defd608c74e8047df18abd4b9ee44c74d29b453ba104d14c53c91f9026670f7c923114cd12200ee5ec458cc518fd0798c74
|
||||
"textextensions@npm:^5.12.0, textextensions@npm:^5.13.0, textextensions@npm:^5.16.0":
|
||||
version: 5.16.0
|
||||
resolution: "textextensions@npm:5.16.0"
|
||||
checksum: d2abd5c962760046aa85d9ca542bd8bdb451370fc0a5e5f807aa80dd2f50175ec10d5ce9d28ae96968aaf6a1b1bea254cf4715f24852d0dcf29c6a60af7f793c
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user