refator(backend-common): extract test utilities to plugin api
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
## API Report File for "@backstage/backend-plugin-api"
|
||||
|
||||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
||||
|
||||
```ts
|
||||
// @public
|
||||
export function overridePackagePathResolution(
|
||||
options: OverridePackagePathResolutionOptions,
|
||||
): PackagePathResolutionOverride;
|
||||
|
||||
// @public (undocumented)
|
||||
export interface OverridePackagePathResolutionOptions {
|
||||
packageName: string;
|
||||
path?: string;
|
||||
paths?: {
|
||||
[path in string]: string | (() => string);
|
||||
};
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface PackagePathResolutionOverride {
|
||||
restore(): void;
|
||||
}
|
||||
|
||||
// (No @packageDocumentation comment for this package)
|
||||
```
|
||||
@@ -21,6 +21,7 @@
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./alpha": "./src/alpha.ts",
|
||||
"./testUtils": "./src/testUtils.ts",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"main": "src/index.ts",
|
||||
@@ -30,6 +31,9 @@
|
||||
"alpha": [
|
||||
"src/alpha.ts"
|
||||
],
|
||||
"testUtils": [
|
||||
"src/testUtils.ts"
|
||||
],
|
||||
"package.json": [
|
||||
"package.json"
|
||||
]
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// TODO: Remove this relative import when extrating the path utilities to this package
|
||||
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
|
||||
import { packagePathMocks } from '../../backend-common/src/paths';
|
||||
import { posix as posixPath, resolve as resolvePath } from 'path';
|
||||
|
||||
/** @public */
|
||||
export interface PackagePathResolutionOverride {
|
||||
/** Restores the normal behavior of resolvePackagePath */
|
||||
restore(): void;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface OverridePackagePathResolutionOptions {
|
||||
/** The name of the package to mock the resolved path of */
|
||||
packageName: string;
|
||||
|
||||
/** A replacement for the root package path */
|
||||
path?: string;
|
||||
|
||||
/**
|
||||
* Replacements for package sub-paths, each key must be an exact match of the posix-style path
|
||||
* that is being resolved within the package.
|
||||
*
|
||||
* For example, code calling `resolvePackagePath('x', 'foo', 'bar')` would match only the following
|
||||
* configuration: `overridePackagePathResolution({ packageName: 'x', paths: { 'foo/bar': baz } })`
|
||||
*/
|
||||
paths?: { [path in string]: string | (() => string) };
|
||||
}
|
||||
|
||||
/**
|
||||
* This utility helps you override the paths returned by `resolvePackagePath` for a given package.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function overridePackagePathResolution(
|
||||
options: OverridePackagePathResolutionOptions,
|
||||
): PackagePathResolutionOverride {
|
||||
const name = options.packageName;
|
||||
|
||||
if (packagePathMocks.has(name)) {
|
||||
throw new Error(
|
||||
`Tried to override resolution for '${name}' more than once for package '${name}'`,
|
||||
);
|
||||
}
|
||||
|
||||
packagePathMocks.set(name, paths => {
|
||||
const joinedPath = posixPath.join(...paths);
|
||||
const localResolver = options.paths?.[joinedPath];
|
||||
if (localResolver) {
|
||||
return typeof localResolver === 'function'
|
||||
? localResolver()
|
||||
: localResolver;
|
||||
}
|
||||
if (options.path) {
|
||||
return resolvePath(options.path, ...paths);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
return {
|
||||
restore() {
|
||||
packagePathMocks.delete(name);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user