Skip to content

Commit

Permalink
Modified subsumes? so that it can test all leaves and is still also t…
Browse files Browse the repository at this point in the history
…estable itself
  • Loading branch information
Theodore Nordsieck committed Aug 22, 2013
1 parent 0d9fc0e commit 8171a33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
18 changes: 12 additions & 6 deletions spec/unit/path/ohai_plugin_common.rb
Expand Up @@ -152,13 +152,12 @@ def create_exe(cmd, path, platform, arch, env)

#checks to see if the elements in test are also in source. Recursively decends into Hashes.
#nil values in test match against both nil and non-existance in source.
def subsumes?(source, test, path = [])
def subsumes?(source, test, path = [], &block)
if source.is_a?( Hash ) && test.is_a?( Hash )
test.all? { |k,v| subsumes?( source[k], v, path.clone << k )}
test.all? { |k,v| subsumes?( source[k], v, path.clone << k, &block )}
else
it "should set " + path.map { |s| "[#{s}]" }.join + " to #{source || 'nil'}" do
source.should eq( test )
end
block.call( path, source, test ) if block
source == test
end
end

Expand Down Expand Up @@ -204,7 +203,14 @@ def subsumes?(source, test, path = [])
cmd_list.each { |c| [ "", ".bat" ].each { |ext| Mixlib::ShellOut.new("rm #{path}/#{c}#{ext}").run_command if !cmd_not_found.include?( c )}}
end

subsumes?( @ohai.data, e[:ohai] )
enc = Yajl::Encoder
subsumes?( @ohai.data, e[:ohai] ) do | path, source, test |
path_txt = path.map { |e| "[#{enc.encode( e )}]" }.join
source_txt = if source.nil? then "nil" else enc.encode( source ) end
it "should set #{path_txt} to #{source_txt}" do
source.should eq( test )
end
end
end
end
end
Expand Down
57 changes: 30 additions & 27 deletions spec/unit/path/ohai_plugin_common_spec.rb
Expand Up @@ -19,31 +19,34 @@
require File.expand_path(File.dirname(__FILE__) + "/ohai_plugin_common.rb")

describe OhaiPluginCommon, "subsumes?" do
# before(:each) do
# @hash = { "languages" => { "python" => { "version" => "1.6.2", "type" => "interpreted" }}}
# end

@hash = { "languages" => { "python" => { "version" => "1.6.2", "type" => "interpreted" }}}

# returns true if given an exact duplicate
subsumes?( @hash, @hash )

# returns false if given an exact duplicate with extra info
# subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => "darwin", "type" => "interpreted" }}} )
# subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => {}, "type" => "interpreted" }}} ).should be_false
# subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => { "name" => "darwin" }, "type" => "interpreted" }}} ).should be_false

# returns true if all elements in the second hash are in the first hash
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2" }}} )
subsumes?( @hash, { "languages" => { "python" => {}}} )
subsumes?( @hash, { "languages" => {}} )

# returns true if the second hash contains a key pointing to a nil where the first hash has nothing
subsumes?( @hash, { "languages" => { "lua" => nil }} )
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2" }, "lua" => nil }} )

# returns false if the second hash has nil in the place of a real value
# subsumes?( @hash, { "languages" => { "python" => { "version" => nil }}} ).should be_false
# subsumes?( @hash, { "languages" => { "python" => nil }} ).should be_false
# subsumes?( { "languages" => {}}, { "languages" => nil } ).should be_false
before(:each) do
@hash = { "languages" => { "python" => { "version" => "1.6.2", "type" => "interpreted" }}}
end

it "returns true if given an exact duplicate" do
subsumes?( @hash, @hash ).should be_true
end

it "returns false if given an exact duplicate with extra info" do
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => "darwin", "type" => "interpreted" }}} ).should be_false
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => {}, "type" => "interpreted" }}} ).should be_false
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2", "os" => { "name" => "darwin" }, "type" => "interpreted" }}} ).should be_false
end

it "returns true if all elements in the second hash are in the first hash" do
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2" }}} ).should be_true
subsumes?( @hash, { "languages" => { "python" => {}}} ).should be_true
subsumes?( @hash, { "languages" => {}} ).should be_true
end

it "returns true if the second hash contains a key pointing to a nil where the first hash has nothing" do
subsumes?( @hash, { "languages" => { "lua" => nil }} ).should be_true
subsumes?( @hash, { "languages" => { "python" => { "version" => "1.6.2" }, "lua" => nil }} ).should be_true
end

it "returns false if the second hash has nil in the place of a real value" do
subsumes?( @hash, { "languages" => { "python" => { "version" => nil }}} ).should be_false
subsumes?( @hash, { "languages" => { "python" => nil }} ).should be_false
subsumes?( { "languages" => {}}, { "languages" => nil } ).should be_false
end
end

0 comments on commit 8171a33

Please sign in to comment.