Skip to content

Commit

Permalink
testing: adjust useEnvironmentBuilder and add options to runCli
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 27, 2024
1 parent ad4c535 commit e8886fc
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions lib/testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JHipsterGeneratorOptions>;
Expand All @@ -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.
Expand Down Expand Up @@ -449,36 +453,51 @@ 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, ...otherOptions } = settings ?? {};
if (useEnvironmentBuilder) {
return this.run(jhipsterGenerator, undefined, { createEnv: createEnvBuilderEnvironment });
}
// If not using EnvironmentBuilder, use the default JHipster generators lookup.
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<string, CliCommand>; useEnvironmentBuilder?: boolean; entrypointGenerator?: string } = {},
): JHipsterRunContext {
const { useEnvironmentBuilder, ...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();
});
const context = this.run(
this.createDummyGenerator(),
{ namespace: 'non-used-dummy:generator' },
useEnvironmentBuilder ? { createEnv: createEnvBuilderEnvironment } : undefined,
);
if (!useEnvironmentBuilder) {
// If not using EnvironmentBuilder, use the default JHipster generators lookup.
context.withJHipsterGenerators();
}
return context.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();
});
}

/**
Expand Down

0 comments on commit e8886fc

Please sign in to comment.