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

fix: Do not treat an array of config values as a sub-command config section #1221

Merged
merged 6 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ export function applyConfigToArgv({
}: ApplyConfigToArgvParams): Object {
let newArgv = {...argv};

for (const option in configObject) {
for (const option of Object.keys(configObject)) {
if (camelCase(option) !== option) {
throw new UsageError(
`The config option "${option}" must be ` +
`specified in camel case: "${camelCase(option)}"`);
}

if (typeof options[option] === 'object' &&
// A config option cannot be a sub-command config
// object if it is an array.
if (!Array.isArray(configObject[option]) &&
typeof options[option] === 'object' &&
typeof configObject[option] === 'object') {
// Descend into the nested configuration for a sub-command.
newArgv = applyConfigToArgv({
Expand Down
6 changes: 3 additions & 3 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Example: $0 --help run.
'run against multiple targets.',
default: 'firefox-desktop',
demand: false,
type: 'string',
type: 'array',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update the Flow type:

https://github.com/rpl/web-ext/blob/3848c777b290b4c5ed6ad1af0436afe5f952b005/src/cmd/run.js#L44

It should now just be target?: Array<string>.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied these changes to the flow types in
025db7c

},
'firefox': {
alias: ['f', 'firefox-binary'],
Expand Down Expand Up @@ -447,15 +447,15 @@ Example: $0 --help run.
'preference.',
demand: false,
requiresArg: true,
type: 'string',
type: 'array',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also update this Flow type:

https://github.com/rpl/web-ext/blob/3848c777b290b4c5ed6ad1af0436afe5f952b005/src/firefox/preferences.js#L120

It should now just be cliPrefs: Array<string>. I think you may need to update some tests after that.

coerce: coerceCLICustomPreference,
},
'start-url': {
alias: ['u', 'url'],
describe: 'Launch firefox at specified page',
demand: false,
requiresArg: true,
type: 'string',
type: 'array',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
'browser-console': {
alias: ['bc'],
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,30 @@ describe('config', () => {
assert.strictEqual(argv.ignoreFiles, configObject.ignoreFiles);
});

it('does not mistake an array config values for a sub-command',
() => {
const params = makeArgv({
userCmd: ['fakecommand'],
globalOpt: {
pref: {
demand: false,
type: 'string',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be type: 'array' so that it tests the bug you were fixing more directly? I'm a bit confused because the exception message in your assertion doesn't seem to match the spec of this test.

},
},
});

const configObject = {
pref: ['pref1=true', 'pref2=false'],
};

// TODO: expect a raised exception array is not a string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this todo comment mean? Was there another test you wanted to add?

assert.throws(
() => applyConf({...params, configObject}),
UsageError,
'type of "pref" incorrectly as "array" (expected type "string")'
);
});

it('uses CLI option over undefined configured option and default', () => {
const cmdLineSrcDir = '/user/specified/source/dir/';
const params = makeArgv({
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test.program.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ describe('program.main', () => {
.then(() => {
sinon.assert.calledWithMatch(
fakeCommands.run,
{startUrl: 'www.example.com'}
{startUrl: ['www.example.com']}
);
});
});
Expand Down