From bc33f69b08ee1f2605f2fa902cd445f305746d68 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Fri, 27 Sep 2024 14:38:13 -0300 Subject: [PATCH] testing: adjust useEnvironmentBuilder and add options to runCli --- lib/testing/helpers.ts | 47 +++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/testing/helpers.ts b/lib/testing/helpers.ts index f3d21153cb3..c24adaba71f 100644 --- a/lib/testing/helpers.ts +++ b/lib/testing/helpers.ts @@ -20,6 +20,7 @@ import type { ApplicationConfiguration } from '../types/application/yo-rc.js'; import { getDefaultJDLApplicationConfig } from '../command/jdl.js'; import type { Entity } from '../types/base/entity.js'; import { buildJHipster, createProgram } from '../../cli/program.mjs'; +import type { CliCommand } from '../../cli/types.js'; import getGenerator, { getGeneratorRelativeFolder } from './get-generator.js'; type GeneratorTestType = YeomanGenerator; @@ -37,6 +38,9 @@ type WithJHipsterGenerators = { * Filter to mock a generator. */ useMock?: (ns: string) => boolean; +}; + +type RunJHipster = WithJHipsterGenerators & { /** * Use the EnviromentBuilder default preparation to create the environment. * Includes local and dev blueprints. @@ -449,36 +453,45 @@ class JHipsterTest extends YeomanTest { settings?: RunContextSettings | undefined, envOptions?: BaseEnvironmentOptions | undefined, ): JHipsterRunContext; - runJHipster(jhipsterGenerator: string, options?: WithJHipsterGenerators): JHipsterRunContext; + runJHipster(jhipsterGenerator: string, options?: RunJHipster): JHipsterRunContext; runJHipster( jhipsterGenerator: string, - settings?: RunContextSettings | WithJHipsterGenerators | undefined, + settings: RunContextSettings | RunJHipster | undefined, envOptions?: BaseEnvironmentOptions | undefined, ): JHipsterRunContext { if (!isAbsolute(jhipsterGenerator)) { jhipsterGenerator = toJHipsterNamespace(jhipsterGenerator); } - const isWithJHipsterGenerators = (opt: any): opt is WithJHipsterGenerators | undefined => + const isRunJHipster = (opt: any): opt is RunJHipster | undefined => opt === undefined || 'actualGeneratorsList' in opt || 'useMock' in opt || 'useDefaultMocks' in opt || 'useEnvironmentBuilder' in opt; - if (isWithJHipsterGenerators(settings)) { - const createEnv = settings?.useEnvironmentBuilder ? createEnvBuilderEnvironment : undefined; - return this.run(jhipsterGenerator, undefined, { createEnv }).withJHipsterGenerators(settings); + if (isRunJHipster(settings)) { + const { useEnvironmentBuilder = true, ...otherOptions } = settings ?? {}; + if (useEnvironmentBuilder) { + return this.run(jhipsterGenerator, undefined, { createEnv: createEnvBuilderEnvironment }); + } + return this.run(jhipsterGenerator).withJHipsterGenerators(otherOptions); } return this.run(getGenerator(jhipsterGenerator), settings, envOptions).withJHipsterGenerators(); } - runCli(command: string | string[]): JHipsterRunContext { + runCli( + command: string | string[], + options: { commands?: Record; useEnvironmentBuilder?: boolean; entrypointGenerator?: string } = {}, + ): JHipsterRunContext { + const { useEnvironmentBuilder = true, ...buildJHipsterOptions } = options; // Use a dummy generator which will not be used to match yeoman-test requirement. - return this.run(this.createDummyGenerator(), { namespace: 'non-used-dummy:generator' }) - .withJHipsterGenerators({ useEnvironmentBuilder: true }) - .withEnvironmentRun(async function (this, env) { - // Customize program to throw an error instead of exiting the process on cli parse error. - const program = createProgram().exitOverride(); - await buildJHipster({ program, env: env as any, silent: true }); - await program.parseAsync(['jhipster', 'jhipster', ...(Array.isArray(command) ? command : command.split(' '))]); - // Put the rootGenerator in context to be used in result assertions. - this.generator = env.rootGenerator(); - }); + return this.run( + this.createDummyGenerator(), + { namespace: 'non-used-dummy:generator' }, + useEnvironmentBuilder ? { createEnv: createEnvBuilderEnvironment } : undefined, + ).withEnvironmentRun(async function (this, env) { + // Customize program to throw an error instead of exiting the process on cli parse error. + const program = createProgram().exitOverride(); + await buildJHipster({ program, env: env as any, silent: true, ...buildJHipsterOptions }); + await program.parseAsync(['jhipster', 'jhipster', ...(Array.isArray(command) ? command : command.split(' '))]); + // Put the rootGenerator in context to be used in result assertions. + this.generator = env.rootGenerator(); + }); } /**