diff options
| author | Luke Kanies <luke@madstop.com> | 2007-08-24 19:19:44 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-08-24 19:19:44 -0500 |
| commit | ab5418390e3e49ce3a12b60902f405db157ed45b (patch) | |
| tree | 897566c79345a1d9f6ce42b905cba42026fc5187 | |
| parent | c6e201cdd4ca5ee11a04cac77bf32faf40640b6d (diff) | |
The config class now has support for add an environment to its search path. Now I just need to go through the whole system and use the search path in addition to the parameter name itself.
| -rw-r--r-- | lib/puppet/util/config.rb | 15 | ||||
| -rwxr-xr-x | spec/unit/util/config.rb | 102 |
2 files changed, 86 insertions, 31 deletions
diff --git a/lib/puppet/util/config.rb b/lib/puppet/util/config.rb index 27004cf41..796529f32 100644 --- a/lib/puppet/util/config.rb +++ b/lib/puppet/util/config.rb @@ -447,13 +447,11 @@ class Puppet::Util::Config # The order in which to search for values. def searchpath(environment = nil) - # Start with a stupid list. - [:cache, :cli, :memory, :name, :main] -# unless @searchpath -# @searchpath = [:cache, :memory, :cli, :name, :main].collect do |source| -# end -# end -# @searchpath + if environment + [:cache, :cli, :memory, environment, :name, :main] + else + [:cache, :cli, :memory, :name, :main] + end end # Get a list of objects per section @@ -720,8 +718,11 @@ Generated on #{Time.now}. @config.has_key?(param) end + # Find the correct value using our search path. Optionally accept an environment + # in which to search before the other configuration sections. def value(param, environment = nil) param = symbolize(param) + environment = symbolize(environment) if environment # Short circuit to nil for undefined parameters. return nil unless @config.include?(param) diff --git a/spec/unit/util/config.rb b/spec/unit/util/config.rb index 69a2268ab..7f9b64c94 100755 --- a/spec/unit/util/config.rb +++ b/spec/unit/util/config.rb @@ -80,12 +80,6 @@ describe Puppet::Util::Config, " when setting values" do @config[:bool].should == true end - it "should support a mechanism for setting values in a specific search section" do - pending "This code requires the search path functionality" - #@config.set(:myval, "new value", :cli) - #@config[:myval].should == "new value" - end - it "should call passed blocks when values are set" do values = [] @config.setdefaults(:section, :hooker => {:default => "yay", :desc => "boo", :hook => lambda { |v| values << v }}) @@ -118,18 +112,6 @@ describe Puppet::Util::Config, " when returning values" do @config[:one].should == "other" end - it "should return default values if no values have been set" do - @config[:one].should == "ONE" - end - - it "should support a search path for finding values" do - pending "I have no idea how this will work yet" - end - - it "should return set values in the order defined in the search path" do - pending "Still no clear idea how this will work" - end - it "should interpolate default values for other parameters into returned parameter values" do @config[:one].should == "ONE" @config[:two].should == "ONE TWO" @@ -165,23 +147,95 @@ describe Puppet::Util::Config, " when returning values" do end end +describe Puppet::Util::Config, " when choosing which value to return" do + before do + @config = Puppet::Util::Config.new + @config.setdefaults :section, + :one => ["ONE", "a"], + :name => ["myname", "w"] + end + + it "should return default values if no values have been set" do + @config[:one].should == "ONE" + end + + it "should return values set on the cli before values set in the configuration file" do + text = "[main]\none = fileval\n" + file = mock 'file' + file.stubs(:changed?).returns(true) + file.stubs(:file).returns("/whatever") + @config.stubs(:parse_file).returns(text) + @config.handlearg("--one", "clival") + @config.parse(file) + + @config[:one].should == "clival" + end + + it "should return values set on the cli before values set in Ruby" do + @config[:one] = "rubyval" + @config.handlearg("--one", "clival") + @config[:one].should == "clival" + end + + it "should return values set in the executable-specific section before values set in the main section" do + text = "[main]\none = mainval\n[myname]\none = nameval\n" + file = mock 'file' + file.stubs(:changed?).returns(true) + file.stubs(:file).returns("/whatever") + @config.stubs(:read_file).with(file).returns(text) + @config.parse(file) + + @config[:one].should == "nameval" + end + + it "should not return values outside of its search path" do + text = "[other]\none = oval\n" + file = "/some/file" + file = mock 'file' + file.stubs(:changed?).returns(true) + file.stubs(:file).returns("/whatever") + @config.stubs(:read_file).with(file).returns(text) + @config.parse(file) + @config[:one].should == "ONE" + end + + it "should return values in a specified environment" do + text = "[env]\none = envval\n" + file = "/some/file" + file = mock 'file' + file.stubs(:changed?).returns(true) + file.stubs(:file).returns("/whatever") + @config.stubs(:read_file).with(file).returns(text) + @config.parse(file) + @config.value(:one, "env").should == "envval" + end + + it "should return values in a specified environment before values in the main or name sections" do + text = "[env]\none = envval\n[main]\none = mainval\n[myname]\none = nameval\n" + file = "/some/file" + file = mock 'file' + file.stubs(:changed?).returns(true) + file.stubs(:file).returns("/whatever") + @config.stubs(:read_file).with(file).returns(text) + @config.parse(file) + @config.value(:one, "env").should == "envval" + end +end + describe Puppet::Util::Config, " when parsing its configuration" do before do @config = Puppet::Util::Config.new @config.setdefaults :section, :one => ["ONE", "a"], :two => ["$one TWO", "b"], :three => ["$one $two THREE", "c"] end - it "should not return values outside of its search path" do + it "should return values set in the configuration file" do text = "[main] - one = mval - [other] - two = oval + one = fileval " file = "/some/file" @config.expects(:read_file).with(file).returns(text) @config.parse(file) - @config[:one].should == "mval" - @config[:two].should == "mval TWO" + @config[:one].should == "fileval" end #484 - this should probably be in the regression area |
