Skip to content

Commit

Permalink
Merge pull request #25235 from mshima/upgrade-blueprints
Browse files Browse the repository at this point in the history
fix upgrade executable and add tests.
  • Loading branch information
DanielFran committed Feb 17, 2024
2 parents 93bfdca + 65f2f49 commit 65f5168
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 9 deletions.
6 changes: 3 additions & 3 deletions generators/upgrade/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand Down Expand Up @@ -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())) {
Expand Down
111 changes: 105 additions & 6 deletions generators/upgrade/generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand All @@ -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),
);
});
});
});

0 comments on commit 65f5168

Please sign in to comment.