summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Maslanka <85239869+mimaslanka@users.noreply.github.com>2022-06-07 09:28:56 -0500
committerGitHub <noreply@github.com>2022-06-07 09:28:56 -0500
commita466638b0be73480ce519024f39a22c1cf653098 (patch)
tree834c75aa03cfb1418419729e32407cba603c05fd
parent00caf8367203dca11a9d3ccfc4819f85b7fe2542 (diff)
parent62585a2cfebada08c829234efe585874305c7372 (diff)
downloadmixlib-config-a466638b0be73480ce519024f39a22c1cf653098.tar.gz
Merge pull request #115 from chef/nm/nested-hash-config
Fix in nested_hash parsing in config
-rw-r--r--lib/mixlib/config.rb5
-rw-r--r--spec/mixlib/config_spec.rb21
2 files changed, 25 insertions, 1 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index 7e4e139..fd50254 100644
--- a/lib/mixlib/config.rb
+++ b/lib/mixlib/config.rb
@@ -568,7 +568,10 @@ module Mixlib
# hash<Hash>:: The hash to apply to the config object
def apply_nested_hash(hash)
hash.each do |k, v|
- if v.is_a? Hash
+ if v.is_a?(Hash) && internal_get(k.to_sym).is_a?(Hash)
+ # If it is a plain config key (not a context) and the value is a Hash, plain merge the Hashes.
+ internal_set(k.to_sym, internal_get(k.to_sym).merge(v))
+ elsif v.is_a? Hash
# If loading from hash, and we reference a context that doesn't exist
# and warning/strict is off, we need to create the config context that we expected to be here.
context = internal_get(k.to_sym) || config_context(k.to_sym)
diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb
index 1155e75..974111e 100644
--- a/spec/mixlib/config_spec.rb
+++ b/spec/mixlib/config_spec.rb
@@ -1298,6 +1298,27 @@ describe Mixlib::Config do
expect(ConfigIt.windows_path).to eql('C:\Windows Has Awful\Paths')
end
end
+ context "when configurable and hash is defined" do
+ before :each do
+ @klass = Class.new
+ @klass.extend(::Mixlib::Config)
+ @klass.class_eval do
+ configurable(:environment) do |c|
+ c.defaults_to({})
+ end
+ end
+ end
+
+ let(:hash) do
+ {
+ environment: { "GEM_PATH" => "SOME_PATH" },
+ }
+ end
+ it "configures the config object from a hash" do
+ hash_config = @klass.from_hash(hash)
+ expect(hash_config[:environment]).to eql({ "GEM_PATH" => "SOME_PATH" })
+ end
+ end
context "when contexts in the hash are undefined and strict disabled" do
before do
ConfigIt.strict_mode = true