From 68ac4999abe9e1f77d8aa1d90c45235bb3938cab Mon Sep 17 00:00:00 2001 From: Elthariel Date: Mon, 7 Nov 2016 00:11:02 +0100 Subject: [PATCH] MergedConfig: Use Chef::Mash for storage, add tests Signed-off-by: Julien 'Lta' BALLET --- lib/cheffish/merged_config.rb | 6 ++-- spec/functional/merged_config_spec.rb | 42 ++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/cheffish/merged_config.rb b/lib/cheffish/merged_config.rb index f45f7d7..9c238f5 100644 --- a/lib/cheffish/merged_config.rb +++ b/lib/cheffish/merged_config.rb @@ -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 diff --git a/spec/functional/merged_config_spec.rb b/spec/functional/merged_config_spec.rb index 1a6d747..00fe8dd 100644 --- a/spec/functional/merged_config_spec.rb +++ b/spec/functional/merged_config_spec.rb @@ -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 @@ -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 ["test", "test2"] end end