Skip to content

Commit

Permalink
testing: add blueprint support to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Nov 2, 2024
1 parent 238d950 commit a2f49d8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,9 @@ exports[`generator - generate-blueprint with all option should match snapshot 1`
"vitest.config.ts": {
"stateCleared": "modified",
},
"vitest.test-setup.ts": {
"stateCleared": "modified",
},
}
`;

Expand Down Expand Up @@ -907,5 +910,8 @@ exports[`generator - generate-blueprint with default config should write files a
"vitest.config.ts": {
"stateCleared": "modified",
},
"vitest.test-setup.ts": {
"stateCleared": "modified",
},
}
`;
1 change: 1 addition & 0 deletions generators/generate-blueprint/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const files = asWriteFilesSection<any>({
'README.md',
'tsconfig.json',
'vitest.config.ts',
'vitest.test-setup.ts',
'.blueprint/cli/commands.mjs',
'.blueprint/generate-sample/command.mjs',
'.blueprint/generate-sample/generator.mjs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ describe('SubGenerator <%= subGenerator %> of <%= application.baseName %> JHipst
describe('run', () => {
beforeAll(async function() {
await helpers
<%_ if (subGenerator === 'jdl') { _%>
.runJDL('application { }')
<%_ } else { _%>
.run(<%= customGenerator ? 'SUB_GENERATOR_NAMESPACE' : 'BLUEPRINT_NAMESPACE' %>)
<%_ } _%>
.withJHipsterConfig()
.withOptions({
ignoreNeedlesError: true,
<%_ if (!customGenerator) { _%>
blueprint: ['<%= application.baseName %>'],
<%_ } _%>
<%_ if (subGenerator === 'jdl') { _%>
inline: 'application { }',
<%_ } _%>
})
.withJHipsterGenerators()
.withParentBlueprintLookup();
.withConfiguredBlueprint()
.withBlueprintConfig();
});

it('should succeed', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fileURLToPath } from 'node:url';
import { defineDefaults } from 'generator-jhipster/testing';

defineDefaults({
blueprint: 'generator-jhipster-<%= baseName %>',
blueprintPackagePath: fileURLToPath(new URL('./', import.meta.url)),
});
52 changes: 49 additions & 3 deletions lib/testing/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { basename, dirname, isAbsolute, join } from 'path';
import assert from 'node:assert';
import { basename, dirname, isAbsolute, join } from 'node:path';
import { mock } from 'node:test';
import { merge, set, snakeCase } from 'lodash-es';
import type { RunContextSettings, RunResult } from 'yeoman-test';
Expand Down Expand Up @@ -65,6 +66,14 @@ type JHipsterRunResult<GeneratorType extends CoreGenerator = CoreGenerator> = Om
createJHipster: (ns: string, options?: WithJHipsterGenerators) => JHipsterRunContext;
};

type HelpersDefaults = {
/** Blueprint namespace */
blueprint?: string;
/** Path where blueprint's generators folder is located */
blueprintPackagePath?: string;
entrypointGenerator?: string;
};

const runResult = result as JHipsterRunResult;

export { runResult, runResult as result };
Expand All @@ -90,6 +99,11 @@ const defaultSharedApplication = Object.fromEntries(['CLIENT_WEBPACK_DIR'].map(k

let defaultMockFactory: (original?: any) => any;
let defaultAccumulateMockArgs: (mocks: Record<string, any>) => Record<string, any>;
let helpersDefaults: HelpersDefaults = {};

export const resetDefaults = () => {
helpersDefaults = {};
};

const createEnvBuilderEnvironment = (...args) => EnvironmentBuilder.createEnv(...args);

Expand All @@ -99,9 +113,11 @@ export const defineDefaults = async (
mockFactory?: any;
/** @deprecated mock from `node:test` is used internally */
accumulateMockArgs?: (mock: Record<string, any>) => Record<string, any>;
} = {},
} & HelpersDefaults = {},
) => {
const { mockFactory, accumulateMockArgs } = defaults;
const { mockFactory, accumulateMockArgs, ...rest } = defaults;
Object.assign(helpersDefaults, rest);

if (mockFactory) {
defaultMockFactory = mockFactory;
} else if (!defaultMockFactory) {
Expand Down Expand Up @@ -253,6 +269,36 @@ class JHipsterRunContext extends RunContext<GeneratorTestType> {
return this.withJHipsterGenerators();
}

withBlueprintConfig(config: Record<string, any>): this {
const { blueprint } = helpersDefaults;
assert(blueprint, 'Blueprint must be configured');
return this.withYoRcConfig(blueprint, config);
}

/**
* Use configured default blueprint.
*/
withConfiguredBlueprint(): this {
const { blueprint, blueprintPackagePath, entrypointGenerator } = helpersDefaults;
assert(blueprintPackagePath, 'Blueprint generators package path must be configured');
assert(blueprint, 'Blueprint must be configured');

if (entrypointGenerator) {
this.withOptions({ entrypointGenerator });
}

return this.withLookups([
{
packagePaths: [blueprintPackagePath],
// @ts-expect-error lookups is not exported
lookups: [`generators`, `generators/*/generators`],
customizeNamespace: ns => ns?.replaceAll(':generators:', ':'),
},
]).withOptions({
blueprint: [blueprint],
});
}

/**
* Lookup generators at generator-jhipster's parent at a npm repository
* @param lookups generators relative folder
Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/api.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ exports[`public api generator-jhipster/generators should match snapshot 2`] = `
"matchWrittenFiles",
"parseIssue",
"prepareSample",
"resetDefaults",
"result",
"runResult",
"setGithubTaskOutput",
Expand Down

0 comments on commit a2f49d8

Please sign in to comment.