From c80f5bd051e942885b7ef900cab1381f59e63745 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 26 Sep 2024 22:48:55 -0300 Subject: [PATCH] testing: adjusts to helpers --- lib/index.ts | 10 +- .../__snapshots__/helpers.spec.ts.snap | 164 +++++++++++++++++- lib/testing/helpers.spec.ts | 44 ++++- lib/testing/helpers.ts | 14 +- 4 files changed, 217 insertions(+), 15 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 418168e1581a..3aa6f9620427 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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; }; diff --git a/lib/testing/__snapshots__/helpers.spec.ts.snap b/lib/testing/__snapshots__/helpers.spec.ts.snap index 5634e66167a6..eb662d922656 100644 --- a/lib/testing/__snapshots__/helpers.spec.ts.snap +++ b/lib/testing/__snapshots__/helpers.spec.ts.snap @@ -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", diff --git a/lib/testing/helpers.spec.ts b/lib/testing/helpers.spec.ts index 90d4244529c7..d51c8a086a6f 100644 --- a/lib/testing/helpers.spec.ts +++ b/lib/testing/helpers.spec.ts @@ -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( diff --git a/lib/testing/helpers.ts b/lib/testing/helpers.ts index 4faf9b2ffc16..9185eff065e8 100644 --- a/lib/testing/helpers.ts +++ b/lib/testing/helpers.ts @@ -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'; @@ -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/', ':')), @@ -325,7 +326,7 @@ class JHipsterRunContext extends RunContext { 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), ); @@ -333,7 +334,7 @@ class JHipsterRunContext extends RunContext { 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); @@ -345,6 +346,7 @@ class JHipsterRunContext extends RunContext { // @ts-expect-error lookups is not exported lookups: [`${prefix}generators`, `${prefix}generators/*/generators`], filePatterns, + customizeNamespace: ns => ns?.replaceAll(':generators:', ':'), }); } @@ -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); }; @@ -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;