Repository navigation
fix: --reporter-option on CLI overrides values from config (#5532) #5986
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
Closed
devareddy05
wants to merge
1
commit into
mochajs:main
from
devareddy05:fix/reporter-options-cli-override
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,6 +283,16 @@ export const loadPkgRc = (args = {}) => { | |
| * @alias module:lib/cli.loadOptions | ||
| * @returns {external:yargsParser.Arguments} Parsed args from everything | ||
| */ | ||
| // Reads `reporter-option` from a config source under any of its known | ||
| // aliases. Keeps `loadOptions` agnostic of how each source was authored. | ||
| const readReporterOption = (source) => | ||
| source && | ||
| (source["reporter-option"] ?? | ||
| source["reporter-options"] ?? | ||
| source.reporterOption ?? | ||
| source.reporterOptions ?? | ||
| source.O); | ||
|
|
||
| export const loadOptions = (argv = []) => { | ||
| let args = parse(argv); | ||
| // short-circuit: look for a flag that would abort loading of options | ||
|
|
@@ -299,6 +309,15 @@ export const loadOptions = (argv = []) => { | |
| const rcConfig = loadRc(args); | ||
| const pkgConfig = loadPkgRc(args); | ||
|
|
||
| // Capture per-source `reporter-option` before yargs-parser's `combine-arrays` | ||
| // flattens them and loses provenance. See the re-merge step below for why. | ||
| const reporterOptionSources = { | ||
| pkg: readReporterOption(pkgConfig), | ||
| rc: readReporterOption(rcConfig), | ||
| env: readReporterOption(envConfig), | ||
| cli: readReporterOption(args), | ||
| }; | ||
|
|
||
| if (rcConfig) { | ||
| args.config = false; | ||
| args._ = args._.concat(rcConfig._ || []); | ||
|
|
@@ -317,6 +336,20 @@ export const loadOptions = (argv = []) => { | |
| pkgConfig || {}, | ||
| ); | ||
|
|
||
| // `combine-arrays` concatenates `reporter-option` from every source in | ||
| // high→low priority order. The downstream coerce in `lib/cli/run.js` reduces | ||
| // it left-to-right with last-writer-wins, which inverts the intended | ||
| // precedence. Re-emit lowest→highest priority so CLI overrides config. | ||
| const orderedReporterOption = [ | ||
| reporterOptionSources.pkg, | ||
| reporterOptionSources.rc, | ||
| reporterOptionSources.env, | ||
| reporterOptionSources.cli, | ||
| ].reduce((acc, src) => (src ? acc.concat(list(src)) : acc), []); | ||
| if (orderedReporterOption.length) { | ||
| args["reporter-option"] = orderedReporterOption; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Refactor] This is quite a lot of complexity for a pretty small area of bug. And, we actually refactored away |
||
|
|
||
| // recombine positional arguments and "spec" | ||
| if (args.spec) { | ||
| args._ = args._.concat(args.spec); | ||
|
|
||
3 changes: 3 additions & 0 deletions
3
test/integration/fixtures/options/reporter-option-mocharc.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| reporter-options: | ||
| - foo=fromConfig | ||
| - extra=keepMe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Comments] This
//comment conflicts with the existing JSDoc for the function. This should add to the JSDoc instead.