From 67f4e0fcdd937359174093a8a1376cad72a5b3b5 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Sun, 2 Mar 2025 09:30:12 -0600 Subject: [PATCH] module(cli): migrate test Signed-off-by: aramissennyeydd --- packages/cli/cli-report.md | 4 +- packages/cli/src/alpha.ts | 1 + packages/cli/src/commands/index.ts | 35 ++-------- packages/cli/src/modules/test/alpha.ts | 64 +++++++++++++++++++ .../test/commands/package}/test.ts | 4 +- .../test}/commands/repo/test.test.ts | 0 .../{ => modules/test}/commands/repo/test.ts | 6 +- packages/cli/src/modules/test/index.ts | 51 +++++++++++++++ 8 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 packages/cli/src/modules/test/alpha.ts rename packages/cli/src/{commands => modules/test/commands/package}/test.ts (96%) rename packages/cli/src/{ => modules/test}/commands/repo/test.test.ts (100%) rename packages/cli/src/{ => modules/test}/commands/repo/test.ts (98%) create mode 100644 packages/cli/src/modules/test/index.ts diff --git a/packages/cli/cli-report.md b/packages/cli/cli-report.md index 02051b69f7..0609db33a2 100644 --- a/packages/cli/cli-report.md +++ b/packages/cli/cli-report.md @@ -199,8 +199,8 @@ Options: Commands: start [options] build [options] - lint [options] [directories...] test + lint [options] [directories...] clean prepack postpack @@ -401,11 +401,11 @@ Options: Commands: build [options] + test [options] lint [options] fix [options] clean list-deprecations [options] - test [options] help [command] ``` diff --git a/packages/cli/src/alpha.ts b/packages/cli/src/alpha.ts index 85a8763974..eff1480391 100644 --- a/packages/cli/src/alpha.ts +++ b/packages/cli/src/alpha.ts @@ -27,5 +27,6 @@ import chalk from 'chalk'; initializer.add(import('./modules/config/alpha')); initializer.add(import('./modules/build/alpha')); initializer.add(import('./modules/migrate/alpha')); + initializer.add(import('./modules/test/alpha')); await initializer.run(); })(); diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index ee2af9541c..93c1a60b40 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -26,6 +26,10 @@ import { registerCommands as registerBuildCommands, } from '../modules/build'; import { registerCommands as registerMigrateCommand } from '../modules/migrate'; +import { + registerRepoCommands as registerRepoTestCommands, + registerPackageCommands as registerPackageTestCommands, +} from '../modules/test'; export function registerRepoCommand(program: Command) { const command = program @@ -33,6 +37,7 @@ export function registerRepoCommand(program: Command) { .description('Command that run across an entire Backstage project'); registerRepoBuildCommands(command); + registerRepoTestCommands(command); command .command('lint') @@ -84,28 +89,6 @@ export function registerRepoCommand(program: Command) { .description('List deprecations') .option('--json', 'Output as JSON') .action(lazy(() => import('./repo/list-deprecations'), 'command')); - - command - .command('test') - .allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args - .option( - '--since ', - 'Only test packages that changed since the specified ref', - ) - .option( - '--successCache', - 'Enable success caching, which skips running tests for unchanged packages that were successful in the previous run', - ) - .option( - '--successCacheDir ', - 'Set the success cache location, (default: node_modules/.cache/backstage-cli)', - ) - .option( - '--jest-help', - 'Show help for Jest CLI options, which are passed through', - ) - .description('Run tests, forwarding args to Jest, defaulting to watch mode') - .action(lazy(() => import('./repo/test'), 'command')); } export function registerScriptCommand(program: Command) { @@ -129,6 +112,7 @@ export function registerScriptCommand(program: Command) { .action(lazy(() => import('./start'), 'command')); registerPackageBuildCommands(command); + registerPackageTestCommands(command); command .command('lint [directories...]') @@ -149,13 +133,6 @@ export function registerScriptCommand(program: Command) { .description('Lint a package') .action(lazy(() => import('./lint'), 'default')); - command - .command('test') - .allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args - .helpOption(', --backstage-cli-help') // Let Jest handle help - .description('Run tests, forwarding args to Jest, defaulting to watch mode') - .action(lazy(() => import('./test'), 'default')); - command .command('clean') .description('Delete cache directories') diff --git a/packages/cli/src/modules/test/alpha.ts b/packages/cli/src/modules/test/alpha.ts new file mode 100644 index 0000000000..1583c74997 --- /dev/null +++ b/packages/cli/src/modules/test/alpha.ts @@ -0,0 +1,64 @@ +/* + * Copyright 2024 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 { createCliPlugin } from '../../wiring/factory'; +import { Command } from 'commander'; +import { lazy } from '../../lib/lazy'; + +export default createCliPlugin({ + pluginId: 'test', + init: async reg => { + reg.addCommand({ + path: ['repo', 'test'], + description: + 'Run tests, forwarding args to Jest, defaulting to watch mode', + execute: async ({ args }) => { + const command = new Command(); + command.allowUnknownOption(true); + command.option( + '--since ', + 'Only test packages that changed since the specified ref', + ); + command.option('--successCache', 'Enable success caching'); + command.option( + '--successCacheDir ', + 'Set the success cache location, (default: node_modules/.cache/backstage-cli)', + ); + command.option( + '--jest-help', + 'Show help for Jest CLI options, which are passed through', + ); + command.action(lazy(() => import('./commands/repo/test'), 'command')); + await command.parseAsync(args, { from: 'user' }); + }, + }); + + reg.addCommand({ + path: ['package', 'test'], + description: + 'Run tests, forwarding args to Jest, defaulting to watch mode', + execute: async ({ args }) => { + const command = new Command(); + + command.allowUnknownOption(true); + command.helpOption(', --backstage-cli-help'); + command.action( + lazy(() => import('./commands/package/test'), 'default'), + ); + await command.parseAsync(args, { from: 'user' }); + }, + }); + }, +}); diff --git a/packages/cli/src/commands/test.ts b/packages/cli/src/modules/test/commands/package/test.ts similarity index 96% rename from packages/cli/src/commands/test.ts rename to packages/cli/src/modules/test/commands/package/test.ts index b55919b2ce..e7ef0af302 100644 --- a/packages/cli/src/commands/test.ts +++ b/packages/cli/src/modules/test/commands/package/test.ts @@ -15,8 +15,8 @@ */ import { Command, OptionValues } from 'commander'; -import { paths } from '../lib/paths'; -import { runCheck } from '../lib/run'; +import { paths } from '../../../../lib/paths'; +import { runCheck } from '../../../../lib/run'; function includesAnyOf(hayStack: string[], ...needles: string[]) { for (const needle of needles) { diff --git a/packages/cli/src/commands/repo/test.test.ts b/packages/cli/src/modules/test/commands/repo/test.test.ts similarity index 100% rename from packages/cli/src/commands/repo/test.test.ts rename to packages/cli/src/modules/test/commands/repo/test.test.ts diff --git a/packages/cli/src/commands/repo/test.ts b/packages/cli/src/modules/test/commands/repo/test.ts similarity index 98% rename from packages/cli/src/commands/repo/test.ts rename to packages/cli/src/modules/test/commands/repo/test.ts index 6b3478b5d9..f0520d29e2 100644 --- a/packages/cli/src/commands/repo/test.ts +++ b/packages/cli/src/modules/test/commands/repo/test.ts @@ -21,10 +21,10 @@ import { run as runJest, yargsOptions as jestYargsOptions } from 'jest-cli'; import { relative as relativePath } from 'path'; import { Command, OptionValues } from 'commander'; import { Lockfile, PackageGraph } from '@backstage/cli-node'; -import { paths } from '../../lib/paths'; -import { runCheck, runPlain } from '../../lib/run'; +import { paths } from '../../../../lib/paths'; +import { runCheck, runPlain } from '../../../../lib/run'; import { isChildPath } from '@backstage/cli-common'; -import { SuccessCache } from '../../lib/cache/SuccessCache'; +import { SuccessCache } from '../../../../lib/cache/SuccessCache'; type JestProject = { displayName: string; diff --git a/packages/cli/src/modules/test/index.ts b/packages/cli/src/modules/test/index.ts new file mode 100644 index 0000000000..8e4720109f --- /dev/null +++ b/packages/cli/src/modules/test/index.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2025 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 { Command } from 'commander'; +import { lazy } from '../../lib/lazy'; + +export function registerRepoCommands(command: Command) { + command + .command('test') + .allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args + .option( + '--since ', + 'Only test packages that changed since the specified ref', + ) + .option( + '--successCache', + 'Enable success caching, which skips running tests for unchanged packages that were successful in the previous run', + ) + .option( + '--successCacheDir ', + 'Set the success cache location, (default: node_modules/.cache/backstage-cli)', + ) + .option( + '--jest-help', + 'Show help for Jest CLI options, which are passed through', + ) + .description('Run tests, forwarding args to Jest, defaulting to watch mode') + .action(lazy(() => import('./commands/repo/test'), 'command')); +} + +export function registerPackageCommands(command: Command) { + command + .command('test') + .allowUnknownOption(true) // Allows the command to run, but we still need to parse raw args + .helpOption(', --backstage-cli-help') // Let Jest handle help + .description('Run tests, forwarding args to Jest, defaulting to watch mode') + .action(lazy(() => import('./commands/package/test'), 'default')); +}