-
Notifications
You must be signed in to change notification settings - Fork 87
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
Fixes #19574 - Validator uses options from option collector #242
Conversation
Hold on with merging this. I just found out it breaks two tests in hammer-cli-katello. Override of |
35dfdbb
to
191488b
Compare
191488b
to
1470e95
Compare
1470e95
to
2028e56
Compare
The original commit that fixes #19574 wasn't compatible with most of option validations and tests in Katello that expect ID resolution to happen after the validations. To get it working I added changes allowing to place validation block between option sources #22253. It's several commits and we'll have to squash that prior to merge. Changes in the core I made:
validate_options(:after, 'UserParams') do
# ...inserts the validation block after UserParams option source
end
# or
validate_options(:after, 'UserParams', validator: Custom::Validator.new)
validate_options(:append) do
# ...adds validation to the end of the queue (default behavior)
end
# or
validate_options(:append, validator: Custom::Validator.new)
# Similar to inserting output or help blocks, following modes can be used:
# before, after, append, prepend, replace The intended structure of option sources in hammer-cli-foreman is:
The default behavior always adds the block at the very end of the queue so that it validates already resolved options. If a different behavior is required, a validation can be placed before the id resolution with: validate_options :before, 'IdResolution' do
end Changes in pluginsThese are changes that have to be made in plugins after this PR is merged: Other plugins might need changes too. Todo
Please review the design and test the behavior. I'll work on the tests in the meantime. |
@mbacovsky ^^^ |
@tstrachota, thanks for this PR! It works pretty well! I like the nested structure of option sources, it is quite flexible and easy to extend. As per our discussion I'd like to suggest a few changes:
Also some docs needs to be updated: |
255708b
to
3a5b740
Compare
@mbacovsky updated according to your comments. I also added tests and some docs. |
#### Default option sources | ||
|
||
Abstract Hammer command uses two default option sources - | ||
`HammerCLI::Options::Sources::CommandLine` responsible for intial population of the options and | ||
`HammerCLI::Options::Sources::SavedDefaults` adding defaults managed by the `defaults` command. |
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.
Could we add something like: The default option sources are wrapped in DefaultInputs
source list so that the default hierarchy of sources is
DefaultInputs
CommandLine
SavedDefaults
assert_equal 'DefaultInputs', target | ||
assert validator, custom_validator | ||
end | ||
end | ||
end |
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.
Would it make sense to add a test for add_validator
method as it is documented extension point?
@mbacovsky ok, I can add that. I've opened PRs with changes required for the Foreman and Katello: |
@mbacovsky updated |
test/unit/abstract_test.rb
Outdated
it 'allows for creating inheritable validators' do | ||
cmd = Class.new(HammerCLI::AbstractCommand) | ||
cmd.send(:define_method, :add_validators) do |sources| | ||
[ stub('Validator1', :name => 'Validator1') ] |
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.
@tstrachota this stub
needs to be executed outside of the block otherwise the test fails.
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.
validator1 = stub('Validator1', :name => 'Validator1')
cmd = Class.new(HammerCLI::AbstractCommand)
cmd.send(:define_method, :add_validators) { |sources| [ validator1 ] }
@tstrachota, it looks almost perfect now. Could you fix the failing test? |
69f8395
to
f8616e0
Compare
@mbacovsky sorry for the test I missed. I fixed that and squashed all the work into 2 commits. |
|
Well done, good to go. Thanks @tstrachota! |
In 5bf5073 Hammer introduced option sources that enable reading option values from additional providers. Default options have been turned into an option source too. Validator currently reads defaults directly. It should use option sources instead, otherwise it will prevent a command to start when required options come from some additional option source (potentially added in future).