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

MergedConfig: Use Chef::Mash as storage #115

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/cheffish/merged_config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "chef/mash"

module Cheffish
class MergedConfig
def initialize(*configs)
@configs = configs
@merge_arrays = {}
@configs = configs.map { |config| Chef::Mash.from_hash config }
@merge_arrays = Chef::Mash.new
end

include Enumerable
Expand Down
42 changes: 41 additions & 1 deletion spec/functional/merged_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
Cheffish::MergedConfig.new({ :test => "val" })
end

let(:collision) do
c1 = { :test1 => "c1.1", "test2" => "c1.2" }
c2 = { "test1" => "c2.1", "test3" => "c2.3" }
Cheffish::MergedConfig.new(c1, c2)
end

let(:config_mismatch) do
c1 = { :test => { :test => "val" } }
c2 = { :test => [2, 3, 4] }
Cheffish::MergedConfig.new(c1, c2)
end

let(:config_hashes) do
c1 = { :test => { :test => "val" } }
c2 = { :test => { :test2 => "val2" } }
Cheffish::MergedConfig.new(c1, c2)
end

it "returns value in config" do
expect(config.test).to eq("val")
end
Expand All @@ -15,6 +33,28 @@
end

it "has an informative string representation" do
expect("#{config}").to eq("{:test=>\"val\"}")
expect("#{config}").to eq("{\"test\"=>\"val\"}")
end

it "has indifferent str/sym access" do
expect(config["test"]).to eq("val")
end

it "respects precedence between the different configs" do
expect(collision["test1"]).to eq("c1.1")
expect(collision[:test1]).to eq("c1.1")
end

it "merges the configs" do
expect(collision[:test2]).to eq("c1.2")
expect(collision[:test3]).to eq("c2.3")
end

it "handle merged value type mismatch" do
expect(config_mismatch[:test]).to eq("test" => "val")
end

it "merges values when they're hashes" do
expect(config_hashes[:test].keys).to eq(%w{test test2})
end
end