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

Add configuration cli option #1794

Merged
merged 20 commits into from
Sep 3, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-cucumberjs) for more info.
[#1794](https://github.com/cucumber/cucumber-js/pull/1794)

### Changed

### Deprecated
Expand Down
9 changes: 9 additions & 0 deletions docs/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
22 changes: 22 additions & 0 deletions features/profiles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,25 @@ Feature: default command line arguments
1 step (1 passed)
<duration-stat>
"""

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 `<OPT> .cucumber-rc.js`
Then it outputs the text:
"""
-

1 scenario (1 skipped)
1 step (1 skipped)
<duration-stat>
"""

Examples:
| OPT |
| -c |
| --config |
6 changes: 6 additions & 0 deletions src/cli/argv_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IParsedArgvFormatOptions {

export interface IParsedArgvOptions {
backtrace: boolean
config: string
dryRun: boolean
exit: boolean
failFast: boolean
Expand Down Expand Up @@ -106,6 +107,11 @@ const ArgvParser = {
.usage('[options] [<GLOB|DIR|FILE[:LINE]>...]')
.version(version, '-v, --version')
.option('-b, --backtrace', 'show full backtrace for errors')
.option(
'-c, --config <TYPE[:PATH]>',
'specify configuration file',
'cucumber.js'
)
.option(
'-d, --dry-run',
'invoke formatters without executing steps',
Expand Down
5 changes: 4 additions & 1 deletion src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export async function getExpandedArgv({
}: IGetExpandedArgvRequest): Promise<string[]> {
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))
}
Expand Down
12 changes: 8 additions & 4 deletions src/cli/profile_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export default class ProfileLoader {
this.directory = directory
}

async getDefinitions(): Promise<Record<string, string>> {
const definitionsFilePath = path.join(this.directory, 'cucumber.js')
async getDefinitions(configFile?: string): Promise<Record<string, string>> {
const definitionsFilePath: string = path.join(
this.directory,
configFile || 'cucumber.js'
)

const exists = await fs.exists(definitionsFilePath)
if (!exists) {
return {}
Expand All @@ -23,8 +27,8 @@ export default class ProfileLoader {
return definitions
}

async getArgv(profiles: string[]): Promise<string[]> {
const definitions = await this.getDefinitions()
async getArgv(profiles: string[], configFile?: string): Promise<string[]> {
const definitions = await this.getDefinitions(configFile)
if (profiles.length === 0 && doesHaveValue(definitions.default)) {
profiles = ['default']
}
Expand Down
33 changes: 31 additions & 2 deletions src/cli/profile_loader_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { doesHaveValue, valueOrDefault } from '../value_checker'
interface TestProfileLoaderOptions {
definitionsFileContent?: string
profiles?: string[]
configFile?: string
}

async function testProfileLoader(
Expand All @@ -18,14 +19,24 @@ async function testProfileLoader(
const cwd = await promisify<DirOptions, string>(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', () => {
Expand Down Expand Up @@ -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'])
})
})
})
})