diff options
| author | Pieter van de Bruggen <pieter@puppetlabs.com> | 2011-04-25 12:20:34 -0700 |
|---|---|---|
| committer | Pieter van de Bruggen <pieter@puppetlabs.com> | 2011-04-25 12:20:34 -0700 |
| commit | ced9f553772d6fb786e620fada1c23427bd269bf (patch) | |
| tree | db36cd3d7c31aed2fa5824abed32c84cda5461f4 | |
| parent | 89620ab51997e032ccde218de13acab9f55da4b8 (diff) | |
| parent | 9329a1f33b4d7df81ad8661de74f8a3656428570 (diff) | |
| download | puppet-ced9f553772d6fb786e620fada1c23427bd269bf.tar.gz puppet-ced9f553772d6fb786e620fada1c23427bd269bf.tar.xz puppet-ced9f553772d6fb786e620fada1c23427bd269bf.zip | |
Merge branch 'tickets/2.7.next/7220' into 2.7.next
| -rw-r--r-- | lib/puppet/interface/action.rb | 5 | ||||
| -rw-r--r-- | lib/puppet/interface/action_builder.rb | 10 | ||||
| -rwxr-xr-x | spec/unit/interface/action_builder_spec.rb | 55 | ||||
| -rwxr-xr-x | spec/unit/interface/action_spec.rb | 55 |
4 files changed, 125 insertions, 0 deletions
diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index 08bc0a345..f8eef69b1 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -217,6 +217,11 @@ WRAPPER option end + def inherit_options_from(action) + options = action.options.map { |opt| action.get_option(opt, false) } + options.reject!(&:nil?).uniq.each { |option| add_option(option) } + end + def option?(name) @options.include? name.to_sym end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb index 2ffa38709..fd8b0856f 100644 --- a/lib/puppet/interface/action_builder.rb +++ b/lib/puppet/interface/action_builder.rb @@ -38,6 +38,16 @@ class Puppet::Interface::ActionBuilder @action.add_option(option) end + def inherit_options_from(action) + if action.is_a? Symbol + name = action + action = @face.get_action(name) or + raise ArgumentError, "This Face has no action named #{name}!" + end + + @action.inherit_options_from(action) + end + def default(value = true) @action.default = !!value end diff --git a/spec/unit/interface/action_builder_spec.rb b/spec/unit/interface/action_builder_spec.rb index bf7afa74e..6f36d2b44 100755 --- a/spec/unit/interface/action_builder_spec.rb +++ b/spec/unit/interface/action_builder_spec.rb @@ -52,6 +52,61 @@ describe Puppet::Interface::ActionBuilder do end end + describe "#inherit_options_from" do + let :face do + Puppet::Interface.new(:face_with_some_options, '0.0.1') do + option '-w' + + action(:foo) do + option '-x', '--ex' + option '-y', '--why' + end + + action(:bar) do + option '-z', '--zee' + end + + action(:baz) do + option '-z', '--zed' + end + end + end + + it 'should add the options from the specified action' do + foo = face.get_action(:foo) + action = Puppet::Interface::ActionBuilder.build(face, :inherit_options) do + inherit_options_from foo + end + action.options.should == foo.options + end + + it 'should add the options from multiple actions' do + foo = face.get_action(:foo) + bar = face.get_action(:bar) + action = Puppet::Interface::ActionBuilder.build(face, :inherit_options) do + inherit_options_from foo + inherit_options_from bar + end + action.options.should == (foo.options + bar.options).uniq.sort + end + + it 'should permit symbolic names for actions in the same face' do + foo = face.get_action(:foo) + action = Puppet::Interface::ActionBuilder.build(face, :inherit_options) do + inherit_options_from :foo + end + action.options.should == foo.options + end + + it 'should raise a useful error if you supply a bad action name' do + expect do + Puppet::Interface::ActionBuilder.build(face, :inherit_options) do + inherit_options_from :nowhere + end + end.to raise_error /nowhere/ + end + end + context "inline documentation" do it "should set the summary" do action = Puppet::Interface::ActionBuilder.build(face, :foo) do diff --git a/spec/unit/interface/action_spec.rb b/spec/unit/interface/action_spec.rb index 24826a6ef..735fbcb72 100755 --- a/spec/unit/interface/action_spec.rb +++ b/spec/unit/interface/action_spec.rb @@ -68,6 +68,61 @@ describe Puppet::Interface::Action do end end + describe "#inherit_options_from" do + let :face do + Puppet::Interface.new(:face_with_some_options, '0.0.1') do + option '-w' + + action(:foo) do + option '-x', '--ex' + option '-y', '--why' + end + + action(:bar) do + option '-z', '--zee' + end + + action(:baz) do + option '-z', '--zed' + end + + action(:noopts) do + # no options declared + end + end + end + + subject { action = face.action(:new_action) { } } + + it 'should add the options from the specified action' do + subject.inherit_options_from(foo = face.get_action(:foo)) + subject.options.should == foo.options + end + + it 'should not die when the specified action has no options' do + original_options = subject.options + subject.inherit_options_from(face.get_action(:noopts)) + subject.options.should == original_options + end + + it 'should add the options from multiple actions' do + subject.inherit_options_from(foo = face.get_action(:foo)) + subject.inherit_options_from(bar = face.get_action(:bar)) + subject.options.should == (foo.options + bar.options).uniq.sort + end + + it 'should not inherit face options' do + subject.expects(:add_option) + subject.expects(:add_option).with(face.get_option(:w)).never + subject.inherit_options_from(face.get_action(:bar)) + end + + it 'should raise an error if inheritance would duplicate options' do + subject.inherit_options_from(face.get_action(:bar)) + expect { subject.inherit_options_from(face.get_action(:baz)) }.to raise_error + end + end + describe "when invoking" do it "should be able to call other actions on the same object" do face = Puppet::Interface.new(:my_face, '0.0.1') do |
