Skip to content

Commit

Permalink
allow provided configuration to be a string (#2373)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Jan 15, 2024
1 parent 53ccc44 commit 3f59e62
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber.

## [Unreleased]
### Added
- Allow `provided` configuration to be a string ([#2373](https://github.com/cucumber/cucumber-js/pull/2373))

## [10.2.1] - 2024-01-07
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion exports/api/report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IConfiguration {
export interface ILoadConfigurationOptions {
file?: string | false;
profiles?: string[];
provided?: Partial<IConfiguration>;
provided?: Partial<IConfiguration> | string;
}

// @public
Expand Down
5 changes: 3 additions & 2 deletions src/api/load_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
DEFAULT_CONFIGURATION,
fromFile,
mergeConfigurations,
parseConfiguration,
validateConfiguration,
} from '../configuration'
import { validateConfiguration } from '../configuration/validate_configuration'
import { convertConfiguration } from './convert_configuration'
import { mergeEnvironment } from './environment'
import {
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function loadConfiguration(
const original = mergeConfigurations(
DEFAULT_CONFIGURATION,
profileConfiguration,
options.provided
parseConfiguration(logger, 'Provided', options.provided)
)
logger.debug('Resolved configuration:', original)
validateConfiguration(original, logger)
Expand Down
9 changes: 9 additions & 0 deletions src/api/load_configuration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe('loadConfiguration', function () {
})
afterEach(async () => teardownEnvironment(environment))

it('should handle configuration directly provided as a string', async () => {
const { useConfiguration } = await loadConfiguration(
{ provided: `--world-parameters '{"foo":"bar"}'` },
environment
)

expect(useConfiguration.worldParameters).to.deep.eq({ foo: 'bar' })
})

it('should skip trying to resolve from a file if `file=false`', async () => {
const { useConfiguration } = await loadConfiguration(
{ file: false },
Expand Down
10 changes: 9 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ export interface ILoadConfigurationOptions {
/**
* Ad-hoc configuration options to be merged over the top of whatever is
* loaded from the configuration file/profiles
* @example
* \{
* failFast: true,
* parallel: 2
* \}
* @example "--fail-fast --parallel 2"
* @remarks
* This can also be provided as a string of argv-style arguments.
*/
provided?: Partial<IConfiguration>
provided?: Partial<IConfiguration> | string
}

/**
Expand Down
35 changes: 6 additions & 29 deletions src/configuration/from_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import fs from 'node:fs'
import path from 'node:path'
import { promisify } from 'node:util'
import { pathToFileURL } from 'node:url'
import stringArgv from 'string-argv'
import YAML from 'yaml'
import readPkgUp from 'read-pkg-up'
import { ILogger } from '../logger'
import { IConfiguration } from './types'
import { mergeConfigurations } from './merge_configurations'
import ArgvParser from './argv_parser'
import { checkSchema } from './check_schema'
import { parseConfiguration } from './parse_configuration'

export async function fromFile(
logger: ILogger,
Expand All @@ -35,7 +33,11 @@ export async function fromFile(
return mergeConfigurations(
{},
...profiles.map((profileKey) =>
extractConfiguration(logger, profileKey, definitions[profileKey])
parseConfiguration(
logger,
`Profile "${profileKey}"`,
definitions[profileKey]
)
)
)
}
Expand Down Expand Up @@ -109,28 +111,3 @@ async function readPackageJson(filePath: string) {
const parentPackage = await readPkgUp({ cwd: path.dirname(filePath) })
return parentPackage?.packageJson
}

function extractConfiguration(
logger: ILogger,
name: string,
definition: any
): Partial<IConfiguration> {
if (typeof definition === 'string') {
logger.debug(`Profile "${name}" value is a string; parsing as argv`)
const { configuration } = ArgvParser.parse([
'node',
'cucumber-js',
...stringArgv(definition),
])
return configuration
}
try {
return checkSchema(definition)
} catch (error) {
throw new Error(
`Requested profile "${name}" failed schema validation: ${error.errors.join(
' '
)}`
)
}
}
2 changes: 1 addition & 1 deletion src/configuration/from_file_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('fromFile', () => {
expect.fail('should have thrown')
} catch (error) {
expect(error.message).to.eq(
'Requested profile "p1" failed schema validation: paths must be a `array` type, but the final value was: `4`. requireModule must be a `array` type, but the final value was: `"aardvark"`.'
'Profile "p1" configuration value failed schema validation: paths must be a `array` type, but the final value was: `4`. requireModule must be a `array` type, but the final value was: `"aardvark"`.'
)
}
})
Expand Down
2 changes: 2 additions & 0 deletions src/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export * from './default_configuration'
export * from './from_file'
export * from './helpers'
export * from './merge_configurations'
export * from './parse_configuration'
export * from './split_format_descriptor'
export * from './types'
export * from './validate_configuration'
33 changes: 33 additions & 0 deletions src/configuration/parse_configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import stringArgv from 'string-argv'
import { ILogger } from '../logger'
import { IConfiguration } from './types'
import ArgvParser from './argv_parser'
import { checkSchema } from './check_schema'

export function parseConfiguration(
logger: ILogger,
source: string,
definition: Partial<IConfiguration> | string | undefined
): Partial<IConfiguration> {
if (!definition) {
return {}
}
if (typeof definition === 'string') {
logger.debug(`${source} configuration value is a string; parsing as argv`)
const { configuration } = ArgvParser.parse([
'node',
'cucumber-js',
...stringArgv(definition),
])
return configuration
}
try {
return checkSchema(definition)
} catch (error) {
throw new Error(
`${source} configuration value failed schema validation: ${error.errors.join(
' '
)}`
)
}
}

0 comments on commit 3f59e62

Please sign in to comment.