Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix upgrade executable and add tests. #25235

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
);
});
});
});
Loading