From 264746dd3c59a1e113a4d084e317433dc6d152f5 Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 16:49:22 -0400 Subject: [PATCH 01/17] Config file Option update --- src/cli/profile_loader.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index 0dd9b6344..3bbf7f061 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -12,7 +12,18 @@ export default class ProfileLoader { } async getDefinitions(): Promise> { - const definitionsFilePath = path.join(this.directory, 'cucumber.js') + const isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') + let configFilePath = null + if (isConfigPresent > 0) { + configFilePath = path.join( + this.directory, + process.argv[isConfigPresent + 1].toString() + ) + } else { + configFilePath = path.join(this.directory, 'cucumber.js') + } + const definitionsFilePath = configFilePath + const exists = await fs.exists(definitionsFilePath) if (!exists) { return {} From d712e0dc6e417d6b3c130fb13311c2dfa58b24ac Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 16:55:37 -0400 Subject: [PATCH 02/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index 3bbf7f061..790588141 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -13,16 +13,15 @@ export default class ProfileLoader { async getDefinitions(): Promise> { const isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') - let configFilePath = null + let definitionsFilePath = null if (isConfigPresent > 0) { - configFilePath = path.join( + definitionsFilePath = path.join( this.directory, process.argv[isConfigPresent + 1].toString() ) } else { - configFilePath = path.join(this.directory, 'cucumber.js') + definitionsFilePath = path.join(this.directory, 'cucumber.js') } - const definitionsFilePath = configFilePath const exists = await fs.exists(definitionsFilePath) if (!exists) { From bbfe2670b0aed0ca36ff6254bc6e827af5404990 Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 17:14:37 -0400 Subject: [PATCH 03/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index 790588141..bf16ac3ad 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -14,14 +14,19 @@ export default class ProfileLoader { async getDefinitions(): Promise> { const isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') let definitionsFilePath = null - if (isConfigPresent > 0) { + if ( isConfigPresent > 0 || + !Number.isNaN(isConfigPresent) || + isConfigPresent != null || + // eslint-disable-next-line eqeqeq + isConfigPresent != undefined + ){ definitionsFilePath = path.join( this.directory, process.argv[isConfigPresent + 1].toString() ) - } else { + } else { definitionsFilePath = path.join(this.directory, 'cucumber.js') - } + } const exists = await fs.exists(definitionsFilePath) if (!exists) { From 1d3a322af6b7fa57140c98fc63585e312421d740 Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 17:18:57 -0400 Subject: [PATCH 04/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index bf16ac3ad..3b390c2a0 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -12,14 +12,10 @@ export default class ProfileLoader { } async getDefinitions(): Promise> { - const isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') - let definitionsFilePath = null - if ( isConfigPresent > 0 || - !Number.isNaN(isConfigPresent) || - isConfigPresent != null || - // eslint-disable-next-line eqeqeq - isConfigPresent != undefined - ){ + let isConfigPresent: number = 0; + isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') + let definitionsFilePath: string = '' + if ( isConfigPresent > 0){ definitionsFilePath = path.join( this.directory, process.argv[isConfigPresent + 1].toString() From 85981e1057afd48bd4007b4467d94f3dc74ae26e Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 17:26:27 -0400 Subject: [PATCH 05/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index 3b390c2a0..df892e5c8 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -12,8 +12,8 @@ export default class ProfileLoader { } async getDefinitions(): Promise> { - let isConfigPresent: number = 0; - isConfigPresent = process.argv.indexOf('--config') || process.argv.indexOf('-c') + let isConfigPresent: number = process.argv.indexOf('--config') + let definitionsFilePath: string = '' if ( isConfigPresent > 0){ definitionsFilePath = path.join( From 30aa000a1c7d747a9d9d26960535cab8568bbdc2 Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 17:31:58 -0400 Subject: [PATCH 06/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index df892e5c8..e690d7bf4 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -12,18 +12,17 @@ export default class ProfileLoader { } async getDefinitions(): Promise> { - let isConfigPresent: number = process.argv.indexOf('--config') - + const isConfigPresent: number = process.argv.indexOf('--config') let definitionsFilePath: string = '' - if ( isConfigPresent > 0){ + if ( isConfigPresent > 0) { definitionsFilePath = path.join( this.directory, process.argv[isConfigPresent + 1].toString() ) - } else { + } else { definitionsFilePath = path.join(this.directory, 'cucumber.js') } - + const exists = await fs.exists(definitionsFilePath) if (!exists) { return {} From f58a74c78a5afd0d8bb6bcdbaf8e041a94e0e47b Mon Sep 17 00:00:00 2001 From: deepziem <54252717+deepziem@users.noreply.github.com> Date: Sun, 13 Sep 2020 17:34:47 -0400 Subject: [PATCH 07/17] Update profile_loader.ts --- src/cli/profile_loader.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index e690d7bf4..d6abb4e19 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -14,14 +14,14 @@ export default class ProfileLoader { async getDefinitions(): Promise> { const isConfigPresent: number = process.argv.indexOf('--config') let definitionsFilePath: string = '' - if ( isConfigPresent > 0) { + if (isConfigPresent > 0) { definitionsFilePath = path.join( this.directory, process.argv[isConfigPresent + 1].toString() ) - } else { + } else { definitionsFilePath = path.join(this.directory, 'cucumber.js') - } + } const exists = await fs.exists(definitionsFilePath) if (!exists) { From c25d7f0ab283f0b19fd1e186c3a3f8a4e9a39166 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:12:09 +0200 Subject: [PATCH 08/17] Use Record instead of Dictionary in profile_loader --- src/cli/profile_loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index e30d8d7e4..1a36e0d5f 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -10,7 +10,7 @@ export default class ProfileLoader { this.directory = directory } - async getDefinitions(): Promise> { + async getDefinitions(): Promise> { const isConfigPresent: number = process.argv.indexOf('--config') let definitionsFilePath: string = '' if (isConfigPresent > 0) { From fc65e4dab69c5b02b78c51bddb36bf3aee903b03 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:20:16 +0200 Subject: [PATCH 09/17] Add --config option in the argv parser --- src/cli/argv_parser.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cli/argv_parser.ts b/src/cli/argv_parser.ts index 87dbd2f1c..a7a75fbf0 100644 --- a/src/cli/argv_parser.ts +++ b/src/cli/argv_parser.ts @@ -217,6 +217,12 @@ const ArgvParser = { ArgvParser.mergeJson('--world-parameters'), {} ) + .option( + '--config, -c ', + 'specify configuration file. Default is ./cucumber.js', + ArgvParser.collect, + [] + ) program.on('--help', () => { /* eslint-disable no-console */ From 445b45cff72bf9ce0f291323524988d475e9027d Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:55:26 +0200 Subject: [PATCH 10/17] Add a scenario in profiles.feature --- features/profiles.feature | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/features/profiles.feature b/features/profiles.feature index 97b17a2fe..c229ca4e8 100644 --- a/features/profiles.feature +++ b/features/profiles.feature @@ -69,3 +69,25 @@ Feature: default command line arguments 1 step (1 passed) """ + + Scenario Outline: specifying a configuration file + Given a file named ".cucumber-rc.js" with: + """ + module.exports = { + 'default': '--dry-run' + }; + """ + When I run cucumber-js with ` .cucumber-rc.js` + Then it outputs the text: + """ + - + + 1 scenario (1 skipped) + 1 step (1 skipped) + + """ + + Examples: + | OPT | + | -c | + | --config | From f816fb294e402a7182e898ee875e34f2d4bcdda4 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:55:57 +0200 Subject: [PATCH 11/17] Order options alhpabetically in argv_parser --- src/cli/argv_parser.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cli/argv_parser.ts b/src/cli/argv_parser.ts index a7a75fbf0..cdc787b9e 100644 --- a/src/cli/argv_parser.ts +++ b/src/cli/argv_parser.ts @@ -20,6 +20,7 @@ export interface IParsedArgvFormatOptions { export interface IParsedArgvOptions { backtrace: boolean + config: string dryRun: boolean exit: boolean failFast: boolean @@ -106,6 +107,11 @@ const ArgvParser = { .usage('[options] [...]') .version(version, '-v, --version') .option('-b, --backtrace', 'show full backtrace for errors') + .option( + '-c, --config ', + 'specify configuration file', + 'cucumber.js' + ) .option( '-d, --dry-run', 'invoke formatters without executing steps', @@ -217,12 +223,6 @@ const ArgvParser = { ArgvParser.mergeJson('--world-parameters'), {} ) - .option( - '--config, -c ', - 'specify configuration file. Default is ./cucumber.js', - ArgvParser.collect, - [] - ) program.on('--help', () => { /* eslint-disable no-console */ From 8bf6dce743d5afcc6a546bce39442fc5b87c5401 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:56:26 +0200 Subject: [PATCH 12/17] Add unit tests and refactorize profile_loader --- src/cli/profile_loader.ts | 20 +++++++------------- src/cli/profile_loader_spec.ts | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/cli/profile_loader.ts b/src/cli/profile_loader.ts index 1a36e0d5f..4543e71f0 100644 --- a/src/cli/profile_loader.ts +++ b/src/cli/profile_loader.ts @@ -10,17 +10,11 @@ export default class ProfileLoader { this.directory = directory } - async getDefinitions(): Promise> { - const isConfigPresent: number = process.argv.indexOf('--config') - let definitionsFilePath: string = '' - if (isConfigPresent > 0) { - definitionsFilePath = path.join( - this.directory, - process.argv[isConfigPresent + 1].toString() - ) - } else { - definitionsFilePath = path.join(this.directory, 'cucumber.js') - } + async getDefinitions(configFile?: string): Promise> { + const definitionsFilePath: string = path.join( + this.directory, + configFile || 'cucumber.js' + ) const exists = await fs.exists(definitionsFilePath) if (!exists) { @@ -33,8 +27,8 @@ export default class ProfileLoader { return definitions } - async getArgv(profiles: string[]): Promise { - const definitions = await this.getDefinitions() + async getArgv(profiles: string[], configFile?: string): Promise { + const definitions = await this.getDefinitions(configFile) if (profiles.length === 0 && doesHaveValue(definitions.default)) { profiles = ['default'] } diff --git a/src/cli/profile_loader_spec.ts b/src/cli/profile_loader_spec.ts index a57d52ccd..909dbb281 100644 --- a/src/cli/profile_loader_spec.ts +++ b/src/cli/profile_loader_spec.ts @@ -10,6 +10,7 @@ import { doesHaveValue, valueOrDefault } from '../value_checker' interface TestProfileLoaderOptions { definitionsFileContent?: string profiles?: string[] + configFile?: string } async function testProfileLoader( @@ -18,14 +19,24 @@ async function testProfileLoader( const cwd = await promisify(tmp.dir)({ unsafeCleanup: true, }) + let configurationFileName = 'cucumber.js' + + if (doesHaveValue(opts.configFile)) { + configurationFileName = opts.configFile + } + if (doesHaveValue(opts.definitionsFileContent)) { await fs.writeFile( - path.join(cwd, 'cucumber.js'), + path.join(cwd, configurationFileName), opts.definitionsFileContent ) } + const profileLoader = new ProfileLoader(cwd) - return await profileLoader.getArgv(valueOrDefault(opts.profiles, [])) + return await profileLoader.getArgv( + valueOrDefault(opts.profiles, []), + opts.configFile + ) } describe('ProfileLoader', () => { @@ -150,5 +161,23 @@ describe('ProfileLoader', () => { }) }) }) + + describe('with non-default configuration file', () => { + it('returns the argv for the given profile', async function () { + // Arrange + const definitionsFileContent = + 'module.exports = {profile3: "--opt3 --opt4"}' + + // Act + const result = await testProfileLoader({ + definitionsFileContent, + profiles: ['profile3'], + configFile: '.cucumber-rc.js', + }) + + // Assert + expect(result).to.eql(['--opt3', '--opt4']) + }) + }) }) }) From a4efb2d135fea79e8153342b1434ea44b94d8055 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 14:56:55 +0200 Subject: [PATCH 13/17] Consider the new --config option when loading profiles --- src/cli/helpers.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cli/helpers.ts b/src/cli/helpers.ts index bd371886a..92aa407c9 100644 --- a/src/cli/helpers.ts +++ b/src/cli/helpers.ts @@ -26,7 +26,10 @@ export async function getExpandedArgv({ }: IGetExpandedArgvRequest): Promise { const { options } = ArgvParser.parse(argv) let fullArgv = argv - const profileArgv = await new ProfileLoader(cwd).getArgv(options.profile) + const profileArgv = await new ProfileLoader(cwd).getArgv( + options.profile, + options.config + ) if (profileArgv.length > 0) { fullArgv = argv.slice(0, 2).concat(profileArgv).concat(argv.slice(2)) } From b6452afe2a030a206c2ab27e630bf5f13ef6b571 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 15:01:24 +0200 Subject: [PATCH 14/17] Add some documentation --- docs/profiles.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/profiles.md b/docs/profiles.md index b7eb10e80..993c31ba4 100644 --- a/docs/profiles.md +++ b/docs/profiles.md @@ -39,3 +39,12 @@ Some notes about how Profiles work: - The `--profile` CLI option is repeatable, so you can apply multiple profiles at once. - You can still supply options directly on the command line when using profiles, they will be appended to whatever comes from profiles. + +## Using another file than `cucumber.js` + +Run `cucumber-js` with `--config` - or `-c` - to specify your configuration file +if it is something else than the default `cucumber.js`. + +```shell +$ cucumber-js --config .cucumber-rc.js +``` From 4d90c6c0c49d2c5be0b1e55035cf0cf4f004b86c Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 15:03:58 +0200 Subject: [PATCH 15/17] Add an entry in the changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d9c3602..0910973c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro ### Added +* `--config` option to the CLI. It allows you to specify a configuration file other than `cucumber.js`. + See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumber-js) for more info. + [#1434](https://github.com/cucumber/cucumber-js/pull/1434) + ### Changed ### Deprecated From ea7c471d217f70b91f3937efeaa0a75686fd21aa Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 15:10:27 +0200 Subject: [PATCH 16/17] [skip ci] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0910973c0..333ea7fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro * `--config` option to the CLI. It allows you to specify a configuration file other than `cucumber.js`. See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumber-js) for more info. - [#1434](https://github.com/cucumber/cucumber-js/pull/1434) + [#1794](https://github.com/cucumber/cucumber-js/pull/1794) ### Changed From d35e9923d7d43dc75a4c115781ac8bd7c56908c6 Mon Sep 17 00:00:00 2001 From: aurelien-reeves Date: Thu, 2 Sep 2021 15:13:37 +0200 Subject: [PATCH 17/17] Fix link in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 333ea7fba..633ea7fc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro ### Added * `--config` option to the CLI. It allows you to specify a configuration file other than `cucumber.js`. - See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumber-js) for more info. + See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumberjs) for more info. [#1794](https://github.com/cucumber/cucumber-js/pull/1794) ### Changed