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

Throw error on storeOptionsAsProperties() after setting option values #1928

Merged
merged 3 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

storeOptionsAsProperties(storeAsProperties = true) {
this._storeOptionsAsProperties = !!storeAsProperties;
if (this.options.length) {
throw new Error('call .storeOptionsAsProperties() before adding options');
}
if (Object.keys(this._optionValues).length) {
throw new Error('call .storeOptionsAsProperties() before setting option values');
}
this._storeOptionsAsProperties = !!storeAsProperties;
return this;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/commander.configureCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ test('when storeOptionsAsProperties() after adding option then throw', () => {
program.storeOptionsAsProperties(false);
}).toThrow();
});

test('when storeOptionsAsProperties() after setting option value then throw', () => {
const program = new commander.Command();
program.setOptionValue('foo', 'bar');
expect(() => {
program.storeOptionsAsProperties(false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Passing false is same as the default state, so it isn't the interesting case and pretty unlikely to be encountered:

Suggested change
program.storeOptionsAsProperties(false);
program.storeOptionsAsProperties();

(I know the throw does not currently care, but I would still like to test the interesting case.)

Copy link
Contributor Author

@aweebit aweebit Aug 11, 2023

Choose a reason for hiding this comment

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

I actually just copied the call from above, and before doing that, I took a minute to think about why you'd decided to pass false in that previous test. The way I ended up explaining it to myself is that you'd probably wanted to test the unproblematic call that would not even change anything if it were allowed because that demonstrates how intransigent the check is. It is obvious an error will be thrown when passing true or nothing if even passing false results in an error (despite the fact that the call would simply be no-op otherwise). That last bit is not so obvious, so that is why I think tests with false might actually be the more interesting ones here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I actually just copied the call from above

Oh, digging... The previous test logic dates back to when the default was to store options as properties, and was not correctly updated when the default behaviour changed. Please change the previous test too! Either pass true or no param, either is fine.

}).toThrow();
});