Skip to content

Commit 68b0387

Browse files
Add configuration cli option (#1794)
* Config file Option update * Add --config option in the argv parser * Add a scenario in profiles.feature * Add unit tests and refactorize profile_loader * Consider the new --config option when loading profiles * Add some documentation * Add an entry in the changelog Co-authored-by: deepziem <[email protected]>
1 parent 8f2ff57 commit 68b0387

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro
1919

2020
### Added
2121

22+
* `--config` option to the CLI. It allows you to specify a configuration file other than `cucumber.js`.
23+
See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumberjs) for more info.
24+
[#1794](https://github.com/cucumber/cucumber-js/pull/1794)
25+
2226
### Changed
2327

2428
### Deprecated

docs/profiles.md

+9
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ Some notes about how Profiles work:
3939

4040
- The `--profile` CLI option is repeatable, so you can apply multiple profiles at once.
4141
- You can still supply options directly on the command line when using profiles, they will be appended to whatever comes from profiles.
42+
43+
## Using another file than `cucumber.js`
44+
45+
Run `cucumber-js` with `--config` - or `-c` - to specify your configuration file
46+
if it is something else than the default `cucumber.js`.
47+
48+
```shell
49+
$ cucumber-js --config .cucumber-rc.js
50+
```

features/profiles.feature

+22
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,25 @@ Feature: default command line arguments
6969
1 step (1 passed)
7070
<duration-stat>
7171
"""
72+
73+
Scenario Outline: specifying a configuration file
74+
Given a file named ".cucumber-rc.js" with:
75+
"""
76+
module.exports = {
77+
'default': '--dry-run'
78+
};
79+
"""
80+
When I run cucumber-js with `<OPT> .cucumber-rc.js`
81+
Then it outputs the text:
82+
"""
83+
-
84+
85+
1 scenario (1 skipped)
86+
1 step (1 skipped)
87+
<duration-stat>
88+
"""
89+
90+
Examples:
91+
| OPT |
92+
| -c |
93+
| --config |

src/cli/argv_parser.ts

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface IParsedArgvFormatOptions {
2020

2121
export interface IParsedArgvOptions {
2222
backtrace: boolean
23+
config: string
2324
dryRun: boolean
2425
exit: boolean
2526
failFast: boolean
@@ -106,6 +107,11 @@ const ArgvParser = {
106107
.usage('[options] [<GLOB|DIR|FILE[:LINE]>...]')
107108
.version(version, '-v, --version')
108109
.option('-b, --backtrace', 'show full backtrace for errors')
110+
.option(
111+
'-c, --config <TYPE[:PATH]>',
112+
'specify configuration file',
113+
'cucumber.js'
114+
)
109115
.option(
110116
'-d, --dry-run',
111117
'invoke formatters without executing steps',

src/cli/helpers.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export async function getExpandedArgv({
2626
}: IGetExpandedArgvRequest): Promise<string[]> {
2727
const { options } = ArgvParser.parse(argv)
2828
let fullArgv = argv
29-
const profileArgv = await new ProfileLoader(cwd).getArgv(options.profile)
29+
const profileArgv = await new ProfileLoader(cwd).getArgv(
30+
options.profile,
31+
options.config
32+
)
3033
if (profileArgv.length > 0) {
3134
fullArgv = argv.slice(0, 2).concat(profileArgv).concat(argv.slice(2))
3235
}

src/cli/profile_loader.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export default class ProfileLoader {
1010
this.directory = directory
1111
}
1212

13-
async getDefinitions(): Promise<Record<string, string>> {
14-
const definitionsFilePath = path.join(this.directory, 'cucumber.js')
13+
async getDefinitions(configFile?: string): Promise<Record<string, string>> {
14+
const definitionsFilePath: string = path.join(
15+
this.directory,
16+
configFile || 'cucumber.js'
17+
)
18+
1519
const exists = await fs.exists(definitionsFilePath)
1620
if (!exists) {
1721
return {}
@@ -23,8 +27,8 @@ export default class ProfileLoader {
2327
return definitions
2428
}
2529

26-
async getArgv(profiles: string[]): Promise<string[]> {
27-
const definitions = await this.getDefinitions()
30+
async getArgv(profiles: string[], configFile?: string): Promise<string[]> {
31+
const definitions = await this.getDefinitions(configFile)
2832
if (profiles.length === 0 && doesHaveValue(definitions.default)) {
2933
profiles = ['default']
3034
}

src/cli/profile_loader_spec.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { doesHaveValue, valueOrDefault } from '../value_checker'
1010
interface TestProfileLoaderOptions {
1111
definitionsFileContent?: string
1212
profiles?: string[]
13+
configFile?: string
1314
}
1415

1516
async function testProfileLoader(
@@ -18,14 +19,24 @@ async function testProfileLoader(
1819
const cwd = await promisify<DirOptions, string>(tmp.dir)({
1920
unsafeCleanup: true,
2021
})
22+
let configurationFileName = 'cucumber.js'
23+
24+
if (doesHaveValue(opts.configFile)) {
25+
configurationFileName = opts.configFile
26+
}
27+
2128
if (doesHaveValue(opts.definitionsFileContent)) {
2229
await fs.writeFile(
23-
path.join(cwd, 'cucumber.js'),
30+
path.join(cwd, configurationFileName),
2431
opts.definitionsFileContent
2532
)
2633
}
34+
2735
const profileLoader = new ProfileLoader(cwd)
28-
return await profileLoader.getArgv(valueOrDefault(opts.profiles, []))
36+
return await profileLoader.getArgv(
37+
valueOrDefault(opts.profiles, []),
38+
opts.configFile
39+
)
2940
}
3041

3142
describe('ProfileLoader', () => {
@@ -150,5 +161,23 @@ describe('ProfileLoader', () => {
150161
})
151162
})
152163
})
164+
165+
describe('with non-default configuration file', () => {
166+
it('returns the argv for the given profile', async function () {
167+
// Arrange
168+
const definitionsFileContent =
169+
'module.exports = {profile3: "--opt3 --opt4"}'
170+
171+
// Act
172+
const result = await testProfileLoader({
173+
definitionsFileContent,
174+
profiles: ['profile3'],
175+
configFile: '.cucumber-rc.js',
176+
})
177+
178+
// Assert
179+
expect(result).to.eql(['--opt3', '--opt4'])
180+
})
181+
})
153182
})
154183
})

0 commit comments

Comments
 (0)