Merge pull request #324 from chef/sr/fix-octal-permissions

teach `cmp` matcher octal tricks
This commit is contained in:
Christoph Hartmann 2015-12-17 12:04:55 +01:00
commit 0368d1cba5
2 changed files with 12 additions and 1 deletions

View file

@ -240,14 +240,21 @@ RSpec::Matchers.define :cmp do |expected|
false false
end end
def octal?(value)
return true if value =~ /\A0+\d+\Z/
false
end
match do |actual| match do |actual|
# if actual and expected are strings # if actual and expected are strings
if actual.is_a?(String) && expected.is_a?(String) if expected.is_a?(String) && actual.is_a?(String)
actual.casecmp(expected) == 0 actual.casecmp(expected) == 0
elsif expected.is_a?(Integer) && integer?(actual) elsif expected.is_a?(Integer) && integer?(actual)
expected == actual.to_i expected == actual.to_i
elsif expected.is_a?(Float) && float?(actual) elsif expected.is_a?(Float) && float?(actual)
expected == actual.to_f expected == actual.to_f
elsif octal?(expected) && actual.is_a?(Integer)
expected.to_i(8) == actual
# fallback to equal # fallback to equal
else else
actual == expected actual == expected
@ -255,10 +262,12 @@ RSpec::Matchers.define :cmp do |expected|
end end
failure_message do |actual| failure_message do |actual|
actual = '0' + actual.to_s(8) if octal?(expected)
"\nexpected: #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n" "\nexpected: #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n"
end end
failure_message_when_negated do |actual| failure_message_when_negated do |actual|
actual = '0' + actual.to_s(8) if octal?(expected)
"\nexpected: value != #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n" "\nexpected: value != #{expected}\n got: #{actual}\n\n(compared using `cmp` matcher)\n"
end end
end end

View file

@ -40,6 +40,8 @@ if os.unix?
# it { should have_mode } # it { should have_mode }
its('mode') { should eq 00765 } its('mode') { should eq 00765 }
it { should be_mode 00765 } it { should be_mode 00765 }
its('mode') { should cmp '0765' }
its('mode') { should_not cmp '0777' }
it { should be_readable } it { should be_readable }
it { should be_readable.by('owner') } it { should be_readable.by('owner') }