Skip to content

Commit

Permalink
testing: adjusts to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 27, 2024
1 parent 39fb346 commit c80f5bd
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 15 deletions.
10 changes: 5 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export const isDistFolder = () => {
return basename(join(__dirname, '..')) === 'dist';
return basename(dirname(__dirname)) === 'dist';
};

export const getPackageRoot = (relativePath?: string) => {
const sourceRoot = join(__dirname, '..');
const packageDirectory = isDistFolder() ? join(sourceRoot, '..') : sourceRoot;
const sourceRoot = dirname(__dirname);
const packageDirectory = isDistFolder() ? dirname(sourceRoot) : sourceRoot;
return relativePath ? join(packageDirectory, relativePath) : packageDirectory;
};

export const getGeneratorsParentPath = (relativePath?: string) => {
const sourceRoot = join(__dirname, '..');
export const getSourceRoot = (relativePath?: string) => {
const sourceRoot = dirname(__dirname);
return relativePath ? join(sourceRoot, relativePath) : sourceRoot;
};

Expand Down
164 changes: 163 additions & 1 deletion lib/testing/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,168 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`helpers defaults should register jhipster generators namespaces 1`] = `
exports[`helpers run using withJHipsterGenerators should register jhipster generators namespaces 1`] = `
[
"jhipster:angular",
"jhipster:app",
"jhipster:base",
"jhipster:base-application",
"jhipster:base-core",
"jhipster:base-entity-changes",
"jhipster:base-workspaces",
"jhipster:bootstrap",
"jhipster:bootstrap-application",
"jhipster:bootstrap-application-base",
"jhipster:bootstrap-application-client",
"jhipster:bootstrap-application-server",
"jhipster:bootstrap-workspaces",
"jhipster:ci-cd",
"jhipster:client",
"jhipster:client:common",
"jhipster:common",
"jhipster:cucumber",
"jhipster:cypress",
"jhipster:docker",
"jhipster:docker-compose",
"jhipster:entities",
"jhipster:entity",
"jhipster:export-jdl",
"jhipster:feign-client",
"jhipster:gatling",
"jhipster:generate-blueprint",
"jhipster:git",
"jhipster:gradle",
"jhipster:gradle:code-quality",
"jhipster:gradle:jib",
"jhipster:gradle:node-gradle",
"jhipster:heroku",
"jhipster:info",
"jhipster:init",
"jhipster:java",
"jhipster:java:bootstrap",
"jhipster:java:build-tool",
"jhipster:java:code-quality",
"jhipster:java:domain",
"jhipster:java:jib",
"jhipster:java:node",
"jhipster:java:openapi-generator",
"jhipster:javascript:bootstrap",
"jhipster:javascript:eslint",
"jhipster:javascript:husky",
"jhipster:javascript:prettier",
"jhipster:jdl",
"jhipster:kubernetes",
"jhipster:kubernetes-helm",
"jhipster:kubernetes-knative",
"jhipster:languages",
"jhipster:liquibase",
"jhipster:maven",
"jhipster:maven:code-quality",
"jhipster:maven:frontend-plugin",
"jhipster:maven:jib",
"jhipster:project-name",
"jhipster:react",
"jhipster:server",
"jhipster:spring-boot",
"jhipster:spring-cache",
"jhipster:spring-cloud-stream",
"jhipster:spring-cloud-stream:kafka",
"jhipster:spring-cloud-stream:pulsar",
"jhipster:spring-cloud:gateway",
"jhipster:spring-data-cassandra",
"jhipster:spring-data-couchbase",
"jhipster:spring-data-elasticsearch",
"jhipster:spring-data-mongodb",
"jhipster:spring-data-neo4j",
"jhipster:spring-data-relational",
"jhipster:spring-websocket",
"jhipster:upgrade",
"jhipster:vue",
"jhipster:workspaces",
]
`;

exports[`helpers runJHipster defaults should register jhipster generators namespaces 1`] = `
[
"jhipster:angular",
"jhipster:app",
"jhipster:base",
"jhipster:base-application",
"jhipster:base-core",
"jhipster:base-entity-changes",
"jhipster:base-workspaces",
"jhipster:bootstrap",
"jhipster:bootstrap-application",
"jhipster:bootstrap-application-base",
"jhipster:bootstrap-application-client",
"jhipster:bootstrap-application-server",
"jhipster:bootstrap-workspaces",
"jhipster:ci-cd",
"jhipster:client",
"jhipster:client:common",
"jhipster:common",
"jhipster:cucumber",
"jhipster:cypress",
"jhipster:docker",
"jhipster:docker-compose",
"jhipster:entities",
"jhipster:entity",
"jhipster:export-jdl",
"jhipster:feign-client",
"jhipster:gatling",
"jhipster:generate-blueprint",
"jhipster:git",
"jhipster:gradle",
"jhipster:gradle:code-quality",
"jhipster:gradle:jib",
"jhipster:gradle:node-gradle",
"jhipster:heroku",
"jhipster:info",
"jhipster:init",
"jhipster:java",
"jhipster:java:bootstrap",
"jhipster:java:build-tool",
"jhipster:java:code-quality",
"jhipster:java:domain",
"jhipster:java:jib",
"jhipster:java:node",
"jhipster:java:openapi-generator",
"jhipster:javascript:bootstrap",
"jhipster:javascript:eslint",
"jhipster:javascript:husky",
"jhipster:javascript:prettier",
"jhipster:jdl",
"jhipster:kubernetes",
"jhipster:kubernetes-helm",
"jhipster:kubernetes-knative",
"jhipster:languages",
"jhipster:liquibase",
"jhipster:maven",
"jhipster:maven:code-quality",
"jhipster:maven:frontend-plugin",
"jhipster:maven:jib",
"jhipster:project-name",
"jhipster:react",
"jhipster:server",
"jhipster:spring-boot",
"jhipster:spring-cache",
"jhipster:spring-cloud-stream",
"jhipster:spring-cloud-stream:kafka",
"jhipster:spring-cloud-stream:pulsar",
"jhipster:spring-cloud:gateway",
"jhipster:spring-data-cassandra",
"jhipster:spring-data-couchbase",
"jhipster:spring-data-elasticsearch",
"jhipster:spring-data-mongodb",
"jhipster:spring-data-neo4j",
"jhipster:spring-data-relational",
"jhipster:spring-websocket",
"jhipster:upgrade",
"jhipster:vue",
"jhipster:workspaces",
]
`;

exports[`helpers runJHipster with useDefaultMocks should register jhipster generators namespaces 1`] = `
[
"jhipster:angular",
"jhipster:app",
Expand Down
44 changes: 41 additions & 3 deletions lib/testing/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
import { before, describe, expect, it } from 'esmocha';
import { defaultHelpers as helpers, runResult } from './helpers.js';

const DUMMY_NAMESPACE = 'dummy';
const DUMMY_NAMESPACE = 'jhipster:dummy';

describe('helpers', () => {
describe('defaults', () => {
describe('run defaults', () => {
before(async () => {
await helpers.runJHipster('info');
await helpers.run(helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE });
});
it('should register not jhipster generators namespaces', () => {
expect(
Object.keys(runResult.env.store._meta)
.filter(ns => ns !== DUMMY_NAMESPACE)
.sort(),
).toHaveLength(0);
});
});
describe('runJHipster defaults', () => {
before(async () => {
await helpers.runJHipster('dummy').withGenerators([[helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE }]]);
});
it('should register jhipster generators namespaces', () => {
expect(
Object.keys(runResult.env.store._meta)
.filter(ns => ns !== DUMMY_NAMESPACE)
.sort(),
).toMatchSnapshot();
});
});
describe('run using withJHipsterGenerators', () => {
before(async () => {
await helpers.run(helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE }).withJHipsterGenerators();
});
it('should register jhipster generators namespaces', () => {
expect(
Object.keys(runResult.env.store._meta)
.filter(ns => ns !== DUMMY_NAMESPACE)
.sort(),
).toMatchSnapshot();
});
});
describe('runJHipster with useDefaultMocks', () => {
before(async () => {
await helpers
.runJHipster('dummy', { useDefaultMocks: true })
.withGenerators([[helpers.createDummyGenerator(), { namespace: DUMMY_NAMESPACE }]]);
});
it('should register jhipster generators namespaces', () => {
expect(
Expand Down
14 changes: 8 additions & 6 deletions lib/testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GENERATOR_WORKSPACES } from '../../generators/generator-list.js';
import { createJHipsterLogger, normalizePathEnd, parseCreationTimestamp } from '../../generators/base/support/index.js';
import BaseGenerator from '../../generators/base/index.js';
import type { JHipsterGeneratorOptions } from '../../generators/base/api.js';
import { getPackageRoot, isDistFolder } from '../index.js';
import { getPackageRoot, getSourceRoot, isDistFolder } from '../index.js';
import type CoreGenerator from '../../generators/base-core/generator.js';
import type { ApplicationConfiguration } from '../types/application/yo-rc.js';
import { getDefaultJDLApplicationConfig } from '../command/jdl.js';
Expand Down Expand Up @@ -66,7 +66,8 @@ const DEFAULT_TEST_SETTINGS = { forwardCwd: true };
const DEFAULT_TEST_OPTIONS = { skipInstall: true };
const DEFAULT_TEST_ENV_OPTIONS = { skipInstall: true, dryRun: false };

const generatorsDir = join(getPackageRoot(), 'generators');
const toJHipsterNamespace = (ns: string) => (/^jhipster[:-]/.test(ns) ? ns : `jhipster:${ns}`);
const generatorsDir = getSourceRoot('generators');
const allGenerators = [
...globSync('*/index.{j,t}s', { cwd: generatorsDir, posix: true }).map(file => dirname(file)),
...globSync('*/generators/*/index.{j,t}s', { cwd: generatorsDir, posix: true }).map(file => dirname(file).replace('/generators/', ':')),
Expand Down Expand Up @@ -325,15 +326,15 @@ class JHipsterRunContext extends RunContext<GeneratorTestType> {
withMockedJHipsterGenerators(options: string[] | { except?: string[]; filter?: (string) => boolean } = {}): this {
const optionsObj = Array.isArray(options) ? { except: options } : options;
const { except = [], filter = filterBootstrapGenerators } = optionsObj;
const jhipsterExceptList = except.map(gen => (gen.startsWith('jhipster:') ? gen : `jhipster:${gen}`));
const jhipsterExceptList = except.map(toJHipsterNamespace);
return this.withMockedGenerators(
allGenerators.filter(filter).filter(gen => !jhipsterExceptList.includes(gen) && (this as any).Generator !== gen),
);
}

withJHipsterGenerators(options: WithJHipsterGenerators = {}): this {
const { useDefaultMocks, actualGeneratorsList = [], useMock = useDefaultMocks ? filterBootstrapGenerators : () => false } = options;
const jhipsterExceptList = actualGeneratorsList.map(gen => (gen.startsWith('jhipster:') ? gen : `jhipster:${gen}`));
const jhipsterExceptList = actualGeneratorsList.map(toJHipsterNamespace);
const mockedGenerators = allGenerators
.filter(useMock)
.filter(gen => !jhipsterExceptList.includes(gen) && (this as any).Generator !== gen);
Expand All @@ -345,6 +346,7 @@ class JHipsterRunContext extends RunContext<GeneratorTestType> {
// @ts-expect-error lookups is not exported
lookups: [`${prefix}generators`, `${prefix}generators/*/generators`],
filePatterns,
customizeNamespace: ns => ns?.replaceAll(':generators:', ':'),
});
}

Expand Down Expand Up @@ -409,7 +411,7 @@ plugins {
runResult.composedMockedGenerators = composedGeneratorsToCheck.filter(gen => runResult.mockedGenerators[gen]?.mock.callCount() > 0);

runResult.createJHipster = (ns: string, options?: WithJHipsterGenerators) => {
ns = ns.startsWith('jhipster:') ? ns : `jhipster:${ns}`;
ns = toJHipsterNamespace(ns);
const context = runResult.create(ns) as JHipsterRunContext;
return context.withJHipsterGenerators(options);
};
Expand Down Expand Up @@ -451,7 +453,7 @@ class JHipsterTest extends YeomanTest {
envOptions?: BaseEnvironmentOptions | undefined,
): JHipsterRunContext {
if (!isAbsolute(jhipsterGenerator)) {
jhipsterGenerator = jhipsterGenerator.startsWith('jhipster:') ? jhipsterGenerator : `jhipster:${jhipsterGenerator}`;
jhipsterGenerator = toJHipsterNamespace(jhipsterGenerator);
}
const isWithJHipsterGenerators = (opt: any): opt is WithJHipsterGenerators | undefined =>
opt === undefined || 'actualGeneratorsList' in opt || 'useMock' in opt || 'useDefaultMocks' in opt || 'useEnvironmentBuilder' in opt;
Expand Down

0 comments on commit c80f5bd

Please sign in to comment.