chore: fix tests

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-04-16 11:12:37 +02:00
parent ed9260f650
commit a544108f93
5 changed files with 56 additions and 44 deletions
+1
View File
@@ -386,6 +386,7 @@ export function registerCommands(program: Command) {
'main',
)
.option('--skip-install', 'Skips yarn install step')
.option('--skip-migrate', 'Skips migration of any moved packages')
.description('Bump Backstage packages to the latest versions')
.action(lazy(() => import('./versions/bump').then(m => m.default)));
+12 -12
View File
@@ -58,16 +58,16 @@ jest.mock('ora', () => ({
}));
let mockDir: MockDirectory;
jest.mock('../../lib/paths', () => ({
paths: {
jest.mock('@backstage/cli-common', () => ({
...jest.requireActual('@backstage/cli-common'),
findPaths: () => ({
resolveTargetRoot(filename: string) {
return mockDir.resolve(filename);
},
get targetDir() {
return mockDir.path;
},
},
}),
}));
jest.mock('../../lib/run', () => {
@@ -200,7 +200,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({ pattern: null, release: 'main' } as unknown as Command);
});
expectLogsToMatch(logs, [
@@ -303,7 +303,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({
pattern: null,
release: 'main',
@@ -419,7 +419,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({ pattern: null, release: 'main' } as unknown as Command);
});
expectLogsToMatch(logs, [
@@ -517,7 +517,7 @@ describe('bump', () => {
(_, res, ctx) => res(ctx.status(404), ctx.json({})),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await expect(
bump({ pattern: null, release: '999.0.1' } as unknown as Command),
).rejects.toThrow('No release found for 999.0.1 version');
@@ -622,7 +622,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({ pattern: null, release: 'next' } as unknown as Command);
});
expectLogsToMatch(logs, [
@@ -718,7 +718,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({
pattern: '@{backstage,backstage-extra}/*',
release: 'main',
@@ -837,7 +837,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({ pattern: null, release: 'main' } as unknown as Command);
});
expectLogsToMatch(logs, [
@@ -952,7 +952,7 @@ describe('bump', () => {
),
),
);
const { log: logs } = await withLogCollector(['log'], async () => {
const { log: logs } = await withLogCollector(['log', 'warn'], async () => {
await bump({ pattern: null, release: 'main' } as unknown as Command);
});
expectLogsToMatch(logs, [
@@ -40,6 +40,7 @@ import {
} from '@backstage/release-manifests';
import 'global-agent/bootstrap';
import { PackageGraph } from '@backstage/cli-node';
import { migrateMovedPackages } from './migrate';
const DEP_TYPES = [
'dependencies',
@@ -264,6 +265,15 @@ export default async (opts: OptionValues) => {
console.log(chalk.yellow(`Skipping yarn install`));
}
if (!opts.skipMigrate) {
const changed = await migrateMovedPackages({
pattern: opts.pattern,
});
if (changed && !opts.skipInstall) {
await runYarnInstall();
}
}
if (breakingUpdates.size > 0) {
console.log();
console.log(
@@ -20,8 +20,18 @@ import {
import * as run from '../../lib/run';
import migrate from './migrate';
import { withLogCollector } from '@backstage/test-utils';
import { paths } from '../../lib/paths';
import fs from 'fs-extra';
import { expectLogsToMatch } from '../../lib/new/factories/common/testUtils';
// Remove log coloring to simplify log matching
jest.mock('chalk', () => ({
red: (str: string) => str,
blue: (str: string) => str,
cyan: (str: string) => str,
green: (str: string) => str,
magenta: (str: string) => str,
yellow: (str: string) => str,
}));
let mockDir: MockDirectory;
jest.mock('@backstage/cli-common', () => ({
@@ -31,7 +41,6 @@ jest.mock('@backstage/cli-common', () => ({
return mockDir.resolve(filename);
},
get targetDir() {
console.log('calling!');
return mockDir.path;
},
}),
@@ -105,35 +114,23 @@ describe('versions:migrate', () => {
});
jest.spyOn(run, 'run').mockResolvedValue(undefined);
await migrate({});
const { log: logs } = await withLogCollector(['log'], async () => {});
// expectLogsToMatch(logs, [
// 'Checking for updates of @backstage/core',
// 'Checking for updates of @backstage/custom',
// 'Checking for updates of @backstage/custom-two',
// 'Checking for updates of @backstage/theme',
// 'Checking for updates of @backstage/core-api',
// 'Some packages are outdated, updating',
// 'Using default pattern glob @backstage?(-community)/*',
// 'unlocking @backstage/core@^1.0.3 ~> 1.0.6',
// 'unlocking @backstage-community/custom@^1.0.1 ~> 1.1.0',
// 'unlocking @backstage/core-api@^1.0.6 ~> 1.0.7',
// 'unlocking @backstage/core-api@^1.0.3 ~> 1.0.7',
// 'bumping @backstage/core in a to ^1.0.6',
// 'bumping @backstage-community/custom in a to ^1.1.0',
// 'bumping @backstage-community/custom-two in a to ^2.0.0',
// 'bumping @backstage/core in b to ^1.0.6',
// 'bumping @backstage-community/custom in b to ^1.1.0',
// 'bumping @backstage-community/custom-two in b to ^2.0.0',
// 'bumping @backstage/theme in b to ^2.0.0',
// 'Running yarn install to install new versions',
// '⚠️ The following packages may have breaking changes:',
// ' @backstage-community/custom-two : 1.0.0 ~> 2.0.0',
// ' @backstage/theme : 1.0.0 ~> 2.0.0',
// ' https://github.com/backstage/backstage/blob/master/packages/theme/CHANGELOG.md',
// 'Version bump complete!',
// ]);
const { warn, log: logs } = await withLogCollector(async () => {
await migrate({});
});
expectLogsToMatch(logs, [
'Found a moved package @backstage/custom@^1.0.1 -> @backstage-community/custom in a (dependencies)',
'Found a moved package @backstage/custom-two@^1.0.0 -> @backstage-community/custom-two in a (dependencies)',
'Found a moved package @backstage/custom@^1.1.0 -> @backstage-community/custom in b (dependencies)',
'Found a moved package @backstage/custom-two@^1.0.0 -> @backstage-community/custom-two in b (dependencies)',
]);
expectLogsToMatch(warn, [
'Could not find package.json for @backstage/core@^1.0.5 in a (dependencies)',
'Could not find package.json for @backstage/core@^1.0.3 in b (dependencies)',
'Could not find package.json for @backstage/theme@^1.0.0 in b (dependencies)',
]);
expect(run.run).toHaveBeenCalledTimes(1);
expect(run.run).toHaveBeenCalledWith(
@@ -77,7 +77,9 @@ export async function migrateMovedPackages(options?: { pattern?: string }) {
);
} catch (ex) {
console.warn(
chalk.yellow`Could not find package.json for ${depName}@${depVersion} in ${pkgName} (${depType})`,
chalk.yellow(
`Could not find package.json for ${depName}@${depVersion} in ${pkgName} (${depType})`,
),
);
continue;
}
@@ -86,7 +88,9 @@ export async function migrateMovedPackages(options?: { pattern?: string }) {
if (movedPackageName) {
console.log(
chalk.yellow`Found a moved package ${depName}@${depVersion} -> ${movedPackageName} in ${pkgName} (${depType})`,
chalk.yellow(
`Found a moved package ${depName}@${depVersion} -> ${movedPackageName} in ${pkgName} (${depType})`,
),
);
didPackageChange = true;