mirror of
https://github.com/inspec/inspec
synced 2024-11-27 23:20:33 +00:00
Merge pull request #737 from chef/chris-rock/deprecate-array-matcher
deprecate array matcher
This commit is contained in:
commit
fbf737a999
3 changed files with 8 additions and 131 deletions
|
@ -10,7 +10,6 @@ Inspec uses matchers to help compare resource values to expectations. The follow
|
||||||
* `eq`
|
* `eq`
|
||||||
* `include`
|
* `include`
|
||||||
* `match`
|
* `match`
|
||||||
* Array matchers: `contain_match`, `all_match`, `none_match`, `contain_duplicates`
|
|
||||||
|
|
||||||
|
|
||||||
be
|
be
|
||||||
|
@ -136,66 +135,3 @@ Check if a string matches a regular expression.
|
||||||
describe sshd_config do
|
describe sshd_config do
|
||||||
its('Ciphers') { should_not match /cbc/ }
|
its('Ciphers') { should_not match /cbc/ }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
Array Matchers
|
|
||||||
=====================================================
|
|
||||||
|
|
||||||
The following matchers are designed to work with arrays:
|
|
||||||
|
|
||||||
|
|
||||||
contain_match
|
|
||||||
-----------------------------------------------------
|
|
||||||
|
|
||||||
Check if an array contains at least one item that matches the regex:
|
|
||||||
|
|
||||||
.. code-block:: ruby
|
|
||||||
|
|
||||||
describe ['lemon', 'ginger', 'grapes'] do
|
|
||||||
it { should contain_match /^gin/}
|
|
||||||
end
|
|
||||||
describe port(25) do
|
|
||||||
its('addresses') it { should_not contain_match /0\.0\.0\.0/}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
all_match
|
|
||||||
-----------------------------------------------------
|
|
||||||
|
|
||||||
Check if all items of an array match the regex:
|
|
||||||
|
|
||||||
.. code-block:: ruby
|
|
||||||
|
|
||||||
describe ['grapefruit', 'grapes'] do
|
|
||||||
it { should all_match /^grape.+/}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
none_match
|
|
||||||
-----------------------------------------------------
|
|
||||||
|
|
||||||
Check if all items of an array match the regex:
|
|
||||||
|
|
||||||
.. code-block:: ruby
|
|
||||||
|
|
||||||
describe ['ginger', 'grapefruit'] do
|
|
||||||
it { should none_match /^sugar$/}
|
|
||||||
end
|
|
||||||
describe port(25) do
|
|
||||||
its('addresses') it { should none_match /^0\.0\.0\.0$/ }
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
contain_duplicates
|
|
||||||
-----------------------------------------------------
|
|
||||||
|
|
||||||
Check if an array contains duplicate items:
|
|
||||||
|
|
||||||
.. code-block:: ruby
|
|
||||||
|
|
||||||
describe [80, 443, 80] do
|
|
||||||
it { should contain_duplicates }
|
|
||||||
end
|
|
||||||
describe ['ginger', 'grapefruit'] do
|
|
||||||
it { should_not contain_duplicates }
|
|
||||||
end
|
|
||||||
|
|
|
@ -70,34 +70,26 @@ end
|
||||||
# matcher to check /etc/passwd, /etc/shadow and /etc/group
|
# matcher to check /etc/passwd, /etc/shadow and /etc/group
|
||||||
RSpec::Matchers.define :contain_legacy_plus do
|
RSpec::Matchers.define :contain_legacy_plus do
|
||||||
match do |file|
|
match do |file|
|
||||||
|
warn '[DEPRECATION] `contain_legacy_plus` is deprecated and will be removed for InSpec 1.0. Please use `describe file(\'/etc/passwd\') do its(\'content\') { should_not match /^\+:/ } end`'
|
||||||
file.content =~ /^\+:/
|
file.content =~ /^\+:/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# verifies that all entries match the regex
|
|
||||||
RSpec::Matchers.define :all_match do |regex|
|
|
||||||
match do |arr|
|
|
||||||
Array(arr).all? { |element| element.to_s.match(regex) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# verifies that all entries don't match a regex
|
|
||||||
RSpec::Matchers.define :none_match do |regex|
|
|
||||||
match do |arr|
|
|
||||||
Array(arr).all? { |element| !element.to_s.match(regex) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# verifies that no entry in an array contains a value
|
# verifies that no entry in an array contains a value
|
||||||
RSpec::Matchers.define :contain_match do |regex|
|
RSpec::Matchers.define :contain_match do |regex|
|
||||||
match do |arr|
|
match do |arr|
|
||||||
Array(arr).one? { |element| element.to_s.match(regex) }
|
warn '[DEPRECATION] `contain_match` is deprecated and will be removed for InSpec 1.0. See https://github.com/chef/inspec/issues/738 for more details'
|
||||||
|
arr.inject { |result, i|
|
||||||
|
result = i.match(regex)
|
||||||
|
result || i.match(/$/)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
RSpec::Matchers.define :contain_duplicates do
|
RSpec::Matchers.define :contain_duplicates do
|
||||||
match do |arr|
|
match do |arr|
|
||||||
dup = Array(arr).select { |element| arr.count(element) > 1 }
|
warn '[DEPRECATION] `contain_duplicates` is deprecated and will be removed for InSpec 1.0. See https://github.com/chef/inspec/issues/738 for more details'
|
||||||
|
dup = arr.select { |element| arr.count(element) > 1 }
|
||||||
!dup.uniq.empty?
|
!dup.uniq.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
# encoding: utf-8
|
|
||||||
|
|
||||||
# 'all_match' matcher tests
|
|
||||||
describe ['ananas', 'apples', 'oranges', 'air', 'alot'] do
|
|
||||||
it { should all_match /[a]./}
|
|
||||||
end
|
|
||||||
describe ['ananas', 'apples', 'oranges', 'melons', 'air'] do
|
|
||||||
it { should_not all_match /[a]./}
|
|
||||||
end
|
|
||||||
describe [true, true, true] do
|
|
||||||
it { should all_match /^true$/}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# 'none_match' matcher tests
|
|
||||||
describe ['kiwi', 'avocado', 'grapefruit'] do
|
|
||||||
it { should none_match /xyz/}
|
|
||||||
end
|
|
||||||
describe ['kiwi', 'avocado', 'grapefruit'] do
|
|
||||||
it { should_not none_match /^avo/}
|
|
||||||
end
|
|
||||||
describe 999 do
|
|
||||||
it { should none_match /^666/}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# 'contain_match' matcher tests
|
|
||||||
describe ['lemon', 'ginger', 'grapes'] do
|
|
||||||
it { should contain_match /^gin/}
|
|
||||||
end
|
|
||||||
describe ['lemon', 'ginger', 'gra(pes'] do
|
|
||||||
it { should contain_match 'gra\(pe' }
|
|
||||||
end
|
|
||||||
describe ['lemon', 'ginger', 'grapes'] do
|
|
||||||
it { should_not contain_match /^chocolate/}
|
|
||||||
end
|
|
||||||
describe 'kiwi' do
|
|
||||||
it { should contain_match /^kiw/}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# 'contain_duplicates' matcher tests
|
|
||||||
describe ['onion', 'carrot', 'onion'] do
|
|
||||||
it { should contain_duplicates }
|
|
||||||
end
|
|
||||||
describe ['onion', 'carrot', 'brocoli'] do
|
|
||||||
it { should_not contain_duplicates }
|
|
||||||
end
|
|
||||||
describe [80, 443] do
|
|
||||||
it { should_not contain_duplicates }
|
|
||||||
end
|
|
Loading…
Reference in a new issue