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

Add chain from() and to() to contain mathcer #32

Merged
merged 1 commit into from
Apr 3, 2013
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
7 changes: 7 additions & 0 deletions lib/serverspec/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def check_file_contain file, expected_pattern
"grep -q '#{expected_pattern}' #{file}"
end

def check_file_contain_within file, expected_pattern, from=nil, to=nil
from ||= '1'
to ||= '$'
checker = check_file_contain("-", expected_pattern)
"sed -n '#{from},#{to}p' #{file} | #{checker}"
end

def check_mode file, mode
"stat -c %a #{file} | grep #{mode}"
end
Expand Down
13 changes: 12 additions & 1 deletion lib/serverspec/matchers/contain.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
RSpec::Matchers.define :contain do |expected|
match do |actual|
ret = ssh_exec(commands.check_file_contain(actual, expected))
if (@from || @to).nil?
cmd = commands.check_file_contain(actual, expected)
else
cmd = commands.check_file_contain_within(actual, expected, @from, @to)
end
ret = ssh_exec(cmd)
ret[:exit_code] == 0
end
chain :from do |from|
@from = Regexp.new(from).inspect
end
chain :to do |to|
@to = Regexp.new(to).inspect
end
end
16 changes: 16 additions & 0 deletions spec/debian/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
it { should eq "grep -q 'root' /etc/passwd" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec') do
it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_mode('/etc/sudoers', 440) do
it { should eq 'stat -c %a /etc/sudoers | grep 440' }
end
Expand Down
1 change: 1 addition & 0 deletions spec/debian/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
it_behaves_like 'support be_listening matcher', 22
it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'See the sshd_config(5) manpage'
it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
it_behaves_like 'support be_user matcher', 'root'
it_behaves_like 'support be_group matcher', 'wheel'

Expand Down
16 changes: 16 additions & 0 deletions spec/gentoo/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
it { should eq "grep -q 'root' /etc/passwd" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec') do
it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_mode('/etc/sudoers', 440) do
it { should eq 'stat -c %a /etc/sudoers | grep 440' }
end
Expand Down
1 change: 1 addition & 0 deletions spec/gentoo/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support be_directory matcher', '/etc/ssh'
it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
it_behaves_like 'support be_user matcher', 'root'
it_behaves_like 'support be_group matcher', 'wheel'

Expand Down
16 changes: 16 additions & 0 deletions spec/redhat/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
it { should eq "grep -q 'root' /etc/passwd" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec') do
it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_mode('/etc/sudoers', 440) do
it { should eq 'stat -c %a /etc/sudoers | grep 440' }
end
Expand Down
1 change: 1 addition & 0 deletions spec/redhat/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support be_directory matcher', '/etc/ssh'
it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
it_behaves_like 'support be_user matcher', 'root'
it_behaves_like 'support be_group matcher', 'wheel'

Expand Down
16 changes: 16 additions & 0 deletions spec/solaris/commads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
it { should eq "grep -q 'root' /etc/passwd" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec') do
it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_mode('/etc/sudoers', 440) do
it { should eq 'stat -c %a /etc/sudoers | grep 440' }
end
Expand Down
1 change: 1 addition & 0 deletions spec/solaris/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
it_behaves_like 'support be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support be_directory matcher', '/etc/ssh'
it_behaves_like 'support contain matcher', '/etc/ssh/sshd_config', 'Configuration file for sshd(1m) (see also sshd_config(4))'
it_behaves_like 'support contain.from.to matcher', 'Gemfile', 'rspec', /^group :test do/, /^end/
it_behaves_like 'support be_user matcher', 'root'
it_behaves_like 'support be_group matcher', 'root'

Expand Down
12 changes: 12 additions & 0 deletions spec/support/shared_matcher_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@
end
end

shared_examples_for 'support contain.from.to matcher' do |valid_file, pattern, from, to|
describe 'contain' do
describe valid_file do
it { should contain(pattern).from(from).to(to) }
end

describe '/etc/ssh/sshd_config' do
it { should_not contain('This is invalid text!!').from(from).to(to) }
end
end
end

shared_examples_for 'support be_user matcher' do |valid_user|
describe 'be_user' do
describe valid_user do
Expand Down