Skip to content

Commit

Permalink
internal: adjust command parsing with default option value (#27549)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Oct 11, 2024
1 parent cbc660b commit d33fea6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 18 deletions.
8 changes: 4 additions & 4 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Options:
--skip-jhipster-dependencies Don't write jhipster dependencies to package.json. (env: JHI_SKIP_JHIPSTER_DEPENDENCIES)
--creation-timestamp <value> Project creation timestamp (used for reproducible builds)
--jdl-store <value> JDL store
--prettier-tab-width <value> Default tab width for prettier
--prettier-tab-width <value> Default tab width for prettier (default: 2)
--monorepository Use monorepository
--skip-commit-hook Skip adding husky commit hooks
--db <value> Provide DB name for the application when skipping server side generation
Expand All @@ -47,14 +47,14 @@ Options:
--reactive Generate a reactive backend
--service-discovery-type <value> Service discovery type (choices: "consul", "eureka", "no")
--auth <value> Provide authentication type for the application when skipping server side generation (choices: "jwt", "oauth2", "session")
--feign-client Generate a feign client
--feign-client Generate a feign client (default: false)
--sync-user-with-idp Allow relationships with User for oauth2 applications
--message-broker <value> message broker (choices: "kafka", "pulsar", "no")
--database-migration <value> Database migration (choices: "liquibase")
--with-generated-flag Add a GeneratedByJHipster annotation to all generated java classes and interfaces
--package-name <value> The package name for the generated application
--build <value> Provide build tool for the application when skipping server side generation (choices: "maven", "gradle")
--enable-gradle-enterprise Enable Gradle Enterprise integration
--build <value> Provide build tool for the application when skipping server side generation (default: maven) (choices: "maven", "gradle")
--enable-gradle-enterprise Enable Gradle Enterprise integration (default: false)
--gradle-enterprise-host <value> Gradle Enterprise Host
--incremental-changelog Creates incremental database changelogs
--dev-database-type <value> Development database
Expand Down
2 changes: 1 addition & 1 deletion generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
} else if (optionDesc.scope !== 'none') {
throw new Error(`Scope ${optionDesc.scope} not supported`);
}
} else if (optionDesc.default && optionDesc.scope === 'generator' && this[optionName] === undefined) {
} else if (optionDesc.default !== undefined && optionDesc.scope === 'generator' && this[optionName] === undefined) {
this[optionName] = optionDesc.default;
}
});
Expand Down
2 changes: 1 addition & 1 deletion generators/init/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Options:
--commit-msg <value> Commit changes (implies forceGit)
--monorepository Use monorepository
--base-name <value> Application base name
--prettier-tab-width <value> Default tab width for prettier
--prettier-tab-width <value> Default tab width for prettier (default: 2)
--skip-commit-hook Skip adding husky commit hooks
-h, --help display help for command
"
Expand Down
8 changes: 4 additions & 4 deletions generators/jdl/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Options:
--skip-jhipster-dependencies Don't write jhipster dependencies to package.json. (env: JHI_SKIP_JHIPSTER_DEPENDENCIES)
--creation-timestamp <value> Project creation timestamp (used for reproducible builds)
--jdl-store <value> JDL store
--prettier-tab-width <value> Default tab width for prettier
--prettier-tab-width <value> Default tab width for prettier (default: 2)
--skip-commit-hook Skip adding husky commit hooks
--db <value> Provide DB name for the application when skipping server side generation
--recreate-initial-changelog Recreate the initial database changelog based on the current config
Expand All @@ -210,14 +210,14 @@ Options:
--reactive Generate a reactive backend
--service-discovery-type <value> Service discovery type (choices: "consul", "eureka", "no")
--auth <value> Provide authentication type for the application when skipping server side generation (choices: "jwt", "oauth2", "session")
--feign-client Generate a feign client
--feign-client Generate a feign client (default: false)
--sync-user-with-idp Allow relationships with User for oauth2 applications
--message-broker <value> message broker (choices: "kafka", "pulsar", "no")
--database-migration <value> Database migration (choices: "liquibase")
--with-generated-flag Add a GeneratedByJHipster annotation to all generated java classes and interfaces
--package-name <value> The package name for the generated application
--build <value> Provide build tool for the application when skipping server side generation (choices: "maven", "gradle")
--enable-gradle-enterprise Enable Gradle Enterprise integration
--build <value> Provide build tool for the application when skipping server side generation (default: maven) (choices: "maven", "gradle")
--enable-gradle-enterprise Enable Gradle Enterprise integration (default: false)
--gradle-enterprise-host <value> Gradle Enterprise Host
--incremental-changelog Creates incremental database changelogs
--dev-database-type <value> Development database
Expand Down
1 change: 1 addition & 0 deletions lib/command/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const convertConfigToOption = (name: string, config?: ConfigSpec<any>): J
const choices = config.choices?.map(choice => (typeof choice === 'string' ? choice : choice.value)) as any;
return {
name,
default: config.default,
description: config.description,
choices,
scope: config.scope ?? 'storage',
Expand Down
110 changes: 102 additions & 8 deletions lib/command/generator-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ const expectApplicationTestOption = () => expect(runResult.generator.sharedData.
describe('generator commands', () => {
for (const scope of ['generator', 'context', 'storage', 'blueprint', 'none'] as const) {
describe(`${scope} scoped`, () => {
const checkOptions = (value: any, argument = false) => {
if (argument) {
const checkOptions = (
value: any,
{ argument = false, expectUndefinedOption = false }: { argument?: boolean; expectUndefinedOption?: boolean } = {},
) => {
if (expectUndefinedOption) {
expectGeneratorOptionsTestOption().toBe(undefined);
} else if (argument) {
// Argument is passed through positionalArguments option.
expectGeneratorOptionsTestOption().toBeUndefined();
} else if (typeof value === 'number') {
Expand Down Expand Up @@ -107,7 +112,7 @@ describe('generator commands', () => {
expectBlueprintConfigTestOption().toBe(value);
}

if (!['application', 'storage', 'blueprint'].includes(scope)) {
if (!['application', 'context', 'storage', 'blueprint'].includes(scope)) {
expectApplicationTestOption().toBeUndefined();
} else if (Array.isArray(value)) {
expectApplicationTestOption().toEqual(value);
Expand Down Expand Up @@ -206,6 +211,95 @@ describe('generator commands', () => {
});
});
});

if (scope === 'generator') {
describe('cli option with default value', () => {
describe('boolean', () => {
const config: JHipsterConfig = {
cli: {
type: Boolean,
},
default: true,
scope,
};

it('without options', async () => {
await runDummyCli('', config);
checkOptions(true, { expectUndefinedOption: true });
});
it('with true option', async () => {
await runDummyCli('--test-option', config);
checkOptions(true);
});
it('with false option', async () => {
await runDummyCli('--no-test-option', config);
checkOptions(false);
});
});

describe('string', () => {
const config: JHipsterConfig = {
cli: {
type: String,
},
default: 'foo',
scope,
};

it('without options', async () => {
await runDummyCli('', config);
checkOptions('foo', { expectUndefinedOption: true });
});
it('with option value', async () => {
await runDummyCli('--test-option 1', config);
checkOptions('1');
});
});

describe('number', () => {
const config: JHipsterConfig = {
cli: {
type: Number,
},
default: 1,
scope,
};

it('without options', async () => {
await runDummyCli('', config);
checkOptions(1, { expectUndefinedOption: true });
});
it('with option value', async () => {
await runDummyCli('--test-option 1', config);
checkOptions(1);
});
});

describe('array', () => {
const config: JHipsterConfig = {
cli: {
type: Array,
},
default: ['1'],
scope,
};

it('without options', async () => {
await runDummyCli('', config);
checkOptions(['1'], { expectUndefinedOption: true });
});
it('with option value', async () => {
await runDummyCli('--test-option 1', config);
checkOptions(['1']);
});
it('with option values', async () => {
await runDummyCli('--test-option 1 2', config);
checkOptions(['1', '2']);
});
});
});
}

describe('cli argument', () => {
describe('string', () => {
const config: JHipsterConfig = {
Expand All @@ -217,11 +311,11 @@ describe('generator commands', () => {

it('without argument', async () => {
await runDummyCli('', config);
checkOptions(undefined, true);
checkOptions(undefined, { argument: true });
});
it('with argument value', async () => {
await runDummyCli('1', config);
checkOptions('1', true);
checkOptions('1', { argument: true });
});
});

Expand All @@ -235,15 +329,15 @@ describe('generator commands', () => {

it('without arguments', async () => {
await runDummyCli('', config);
checkOptions(undefined, true);
checkOptions(undefined, { argument: true });
});
it('with argument value', async () => {
await runDummyCli('1', config);
checkOptions(['1'], true);
checkOptions(['1'], { argument: true });
});
it('with arguments values', async () => {
await runDummyCli('1 2', config);
checkOptions(['1', '2'], true);
checkOptions(['1', '2'], { argument: true });
});
});
});
Expand Down

0 comments on commit d33fea6

Please sign in to comment.