Skip to content

Commit

Permalink
Merge pull request #233 from serverspec/fix-for-issue-232
Browse files Browse the repository at this point in the history
[WIP]-F option works fine with the pattern in #232
  • Loading branch information
mizzy committed Aug 30, 2013
2 parents 4b4b20e + 6010e57 commit 205e933
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 34 deletions.
14 changes: 12 additions & 2 deletions lib/serverspec/commands/base.rb
Expand Up @@ -114,9 +114,17 @@ def check_process(process)
end

def check_file_contain(file, expected_pattern)
"#{check_file_contain_with_regexp(file, expected_pattern)} || #{check_file_contain_with_fixed_strings(file, expected_pattern)}"
end

def check_file_contain_with_regexp(file, expected_pattern)
"grep -q -- #{escape(expected_pattern)} #{escape(file)}"
end

def check_file_contain_with_fixed_strings(file, expected_pattern)
"grep -qF -- #{escape(expected_pattern)} #{escape(file)}"
end

def check_file_md5checksum(file, expected)
regexp = "^#{expected}"
"md5sum #{escape(file)} | grep -iw -- #{escape(regexp)}"
Expand All @@ -130,8 +138,10 @@ def check_file_sha256checksum(file, expected)
def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
from ||= '1'
to ||= '$'
checker = check_file_contain("-", expected_pattern)
"sed -n #{escape(from)},#{escape(to)}p #{escape(file)} | #{checker}"
sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
checker_with_regexp = check_file_contain_with_regexp("-", expected_pattern)
checker_with_fixed = check_file_contain_with_fixed_strings("-", expected_pattern)
"#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
end

def check_mode(file, mode)
Expand Down
6 changes: 4 additions & 2 deletions lib/serverspec/commands/solaris.rb
Expand Up @@ -75,8 +75,10 @@ def check_svcprops(svc, property)
def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
from ||= '1'
to ||= '$'
checker = check_file_contain("/dev/stdin", expected_pattern)
"sed -n #{escape(from)},#{escape(to)}p #{escape(file)} | #{checker}"
sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
checker_with_regexp = check_file_contain_with_regexp("/dev/stdin", expected_pattern)
checker_with_fixed = check_file_contain_with_fixed_strings("/dev/stdin", expected_pattern)
"#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
end

def check_belonging_group(user, group)
Expand Down
10 changes: 5 additions & 5 deletions spec/darwin/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config"}
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec - || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down
10 changes: 5 additions & 5 deletions spec/debian/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec - || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down
10 changes: 5 additions & 5 deletions spec/gentoo/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec - || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down
10 changes: 5 additions & 5 deletions spec/redhat/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec - || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec -" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec - || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec -" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down
10 changes: 5 additions & 5 deletions spec/solaris/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec /dev/stdin || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down
10 changes: 5 additions & 5 deletions spec/solaris11/file_spec.rb
Expand Up @@ -31,12 +31,12 @@

describe file('/etc/ssh/sshd_config') do
it { should contain 'This is the sshd server system-wide configuration file' }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
it { should contain /^This is the sshd server system-wide configuration file/ }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
its(:command) { should eq "grep -q -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config || grep -qF -- \\^This\\ is\\ the\\ sshd\\ server\\ system-wide\\ configuration\\ file /etc/ssh/sshd_config" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -45,7 +45,7 @@

describe file('Gemfile') do
it { should contain('rspec').from(/^group :test do/).to(/^end/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin || sed -n /\\^group\\ :test\\ do/,/\\^end/p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -54,7 +54,7 @@

describe file('Gemfile') do
it { should contain('rspec').after(/^group :test do/) }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -q -- rspec /dev/stdin || sed -n /\\^group\\ :test\\ do/,\\$p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand All @@ -63,7 +63,7 @@

describe file('Gemfile') do
it { should contain('rspec').before(/^end/) }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin" }
its(:command) { should eq "sed -n 1,/\\^end/p Gemfile | grep -q -- rspec /dev/stdin || sed -n 1,/\\^end/p Gemfile | grep -qF -- rspec /dev/stdin" }
end

describe file('/etc/ssh/sshd_config') do
Expand Down

0 comments on commit 205e933

Please sign in to comment.