Merge pull request #20378 from backstage/rugvip/20-fixes

More fixes for node 20 test breakage + overridePackagePathResolution
This commit is contained in:
Patrik Oldsberg
2023-10-05 21:01:48 +02:00
committed by GitHub
25 changed files with 434 additions and 436 deletions
+4
View File
@@ -10,6 +10,7 @@
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./testUtils": "./src/testUtils.ts",
"./package.json": "./package.json"
},
"typesVersions": {
@@ -17,6 +18,9 @@
"alpha": [
"src/alpha.ts"
],
"testUtils": [
"src/testUtils.ts"
],
"package.json": [
"package.json"
]
+14
View File
@@ -18,6 +18,12 @@ import { isChildPath } from '@backstage/cli-common';
import { NotAllowedError } from '@backstage/errors';
import { resolve as resolvePath } from 'path';
/** @internal */
export const packagePathMocks = new Map<
string,
(paths: string[]) => string | undefined
>();
/**
* Resolve a path relative to the root of a package directory.
* Additional path arguments are resolved relative to the package dir.
@@ -29,6 +35,14 @@ import { resolve as resolvePath } from 'path';
* @public
*/
export function resolvePackagePath(name: string, ...paths: string[]) {
const mockedResolve = packagePathMocks.get(name);
if (mockedResolve) {
const resolved = mockedResolve(paths);
if (resolved) {
return resolved;
}
}
const req =
typeof __non_webpack_require__ === 'undefined'
? require
+79
View File
@@ -0,0 +1,79 @@
/*
* 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.
*/
import { packagePathMocks } from './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);
},
};
}
@@ -0,0 +1,26 @@
## API Report File for "@backstage/backend-common"
> 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)
```
@@ -91,7 +91,7 @@ describe('createMockDirectory', () => {
mockDir.addContent({
'b.txt': 'b',
b: {
[mockDir.resolve('b')]: {
'c.txt': 'c',
},
});
@@ -279,19 +279,18 @@ class MockDirectoryImpl {
const entries: MockEntry[] = [];
function traverse(node: MockDirectoryContent[string], path: string) {
const trimmedPath = path.startsWith('/') ? path.slice(1) : path; // trim leading slash
if (typeof node === 'string') {
entries.push({
type: 'file',
path: trimmedPath,
path,
content: Buffer.from(node, 'utf8'),
});
} else if (node instanceof Buffer) {
entries.push({ type: 'file', path: trimmedPath, content: node });
entries.push({ type: 'file', path, content: node });
} else {
entries.push({ type: 'dir', path: trimmedPath });
entries.push({ type: 'dir', path });
for (const [name, child] of Object.entries(node)) {
traverse(child, `${trimmedPath}/${name}`);
traverse(child, path ? `${path}/${name}` : name);
}
}
}