From 65f2f498de11abb869f311cb978cfa80c9aa87be Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Sat, 17 Feb 2024 11:48:10 -0300 Subject: [PATCH] fix upgrade and add tests. --- generators/upgrade/generator.js | 6 +- generators/upgrade/generator.spec.js | 111 +++++++++++++++++++++++++-- 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/generators/upgrade/generator.js b/generators/upgrade/generator.js index e3fc39a1ad4b..3ed3af105304 100644 --- a/generators/upgrade/generator.js +++ b/generators/upgrade/generator.js @@ -189,8 +189,8 @@ export default class UpgradeGenerator extends BaseGenerator { customCliOptions.push('--with-entities'); } // Regenerate sources - this.log.info(`Regenerating sources using ${this.programName} executable`); - await this.spawn('npx', ['--no', this.programName, ...customCliOptions, ...DEFAULT_CLI_OPTIONS.split(' ')], { + this.log.info(`Regenerating sources using ${this.executable} executable`); + await this.spawn('npx', ['--no', this.executable, ...customCliOptions, ...DEFAULT_CLI_OPTIONS.split(' ')], { stdio: this.spawnStdio, }); } @@ -303,7 +303,7 @@ export default class UpgradeGenerator extends BaseGenerator { * Remove every generated file not related to the generation. */ async cleanUp() { - const gitignoreContent = this.readDestination('.gitignore'); + const gitignoreContent = this.readDestination('.gitignore', { defaults: '' }); const ignoredFiles = gitignoreContent ? gitignore(gitignoreContent).patterns ?? [] : []; const filesToKeep = ['.yo-rc.json', '.jhipster', 'package.json', 'package-lock.json', 'node_modules', '.git', ...ignoredFiles]; for (const file of await readdir(this.destinationPath())) { diff --git a/generators/upgrade/generator.spec.js b/generators/upgrade/generator.spec.js index e6ad1f604729..687903884e09 100644 --- a/generators/upgrade/generator.spec.js +++ b/generators/upgrade/generator.spec.js @@ -18,17 +18,15 @@ */ import { basename, dirname } from 'path'; import { fileURLToPath } from 'url'; -import { it, describe, expect } from 'esmocha'; -import lodash from 'lodash'; +import { it, describe, expect, before, fn } from 'esmocha'; +import { snakeCase } from 'lodash-es'; import git from 'simple-git'; import Generator from './index.js'; import { shouldSupportFeatures } from '../../test/support/tests.js'; -import { basicHelpers as helpers } from '../../test/support/index.js'; +import { basicHelpers as helpers, result } from '../../test/support/index.js'; import { UPGRADE_BRANCH } from './support/index.js'; -const { snakeCase } = lodash; - const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -74,7 +72,7 @@ describe(`generator - ${generator}`, () => { }), ).rejects.toThrow('local changes found.'); }); - it('should at upgrade branch', async () => { + it('should throw at upgrade branch', async () => { await expect( helpers .runJHipster(generator) @@ -88,4 +86,105 @@ describe(`generator - ${generator}`, () => { }), ).rejects.toThrow('You are on the upgrade branch, please switch to another branch before upgrading.'); }); + describe('with createEnvBuilder option', async () => { + let createEnvBuilder; + + before(async () => { + createEnvBuilder = fn().mockReturnValue({ getEnvironment: () => ({ run: () => {} }) }); + await helpers + .runJHipster(generator) + .withJHipsterConfig() + .withSpawnMock() + .withFiles({ + 'package.json': { devDependencies: { 'generator-jhipster': '7.9.4' } }, + }) + .commitFiles() + .withOptions({ + programName: 'customProgramName', + createEnvBuilder, + }); + }); + it('should call createEnvBuilder', async () => { + expect(createEnvBuilder).toHaveBeenCalledTimes(1); + }); + }); + describe('with createEnvBuilder and applyConfig options', async () => { + let createEnvBuilder; + + before(async () => { + createEnvBuilder = fn().mockReturnValue({ getEnvironment: () => ({ run: () => {} }) }); + await helpers + .runJHipster(generator) + .withJHipsterConfig() + .withSpawnMock() + .withFiles({ + 'package.json': { devDependencies: { 'generator-jhipster': '7.9.4' } }, + }) + .commitFiles() + .withOptions({ + programName: 'customProgramName', + applyConfig: true, + createEnvBuilder, + }); + }); + it('should call createEnvBuilder twice', async () => { + expect(createEnvBuilder).toHaveBeenCalledTimes(2); + }); + }); + describe('with programName option', async () => { + before(async () => { + await helpers + .runJHipster(generator) + .withJHipsterConfig() + .withSpawnMock({ + registerSinonDefaults: false, + stub: fn(), + }) + .withFiles({ + 'package.json': { devDependencies: { 'generator-jhipster': '7.9.4' } }, + }) + .commitFiles() + .withOptions({ + programName: 'customProgramName', + createEnvBuilder: () => ({ getEnvironment: () => ({ run: () => {} }) }), + }); + }); + it('should execute programName', async () => { + expect(result.spawnStub).toHaveBeenLastCalledWith( + 'spawn', + 'npx', + expect.arrayContaining(['--no', 'customProgramName']), + expect.any(Object), + ); + }); + }); + + describe('with programName and executable options', async () => { + before(async () => { + await helpers + .runJHipster(generator) + .withJHipsterConfig() + .withSpawnMock({ + registerSinonDefaults: false, + stub: fn(), + }) + .withFiles({ + 'package.json': { devDependencies: { 'generator-jhipster': '7.9.4' } }, + }) + .commitFiles() + .withOptions({ + programName: 'customProgramName', + executable: 'customExecutable', + createEnvBuilder: () => ({ getEnvironment: () => ({ run: () => {} }) }), + }); + }); + it('should execute executable', async () => { + expect(result.spawnStub).toHaveBeenLastCalledWith( + 'spawn', + 'npx', + expect.arrayContaining(['--no', 'customExecutable']), + expect.any(Object), + ); + }); + }); });