Skip to content

Commit

Permalink
Merge pull request #85 from tagomoris/matcher_fixed_lines
Browse files Browse the repository at this point in the history
add check_file_contain_lines: matcher for fixed-line sequence
  • Loading branch information
mizzy committed Apr 15, 2014
2 parents 377d4c6 + c003d29 commit 0dd6302
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/specinfra/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ def check_file_contain_within(file, expected_pattern, from=nil, to=nil)
"#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
end

def check_file_contain_lines(file, expected_lines, from=nil, to=nil)
require 'digest/md5'
from ||= '1'
to ||= '$'
sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
head_line = expected_lines.first.chomp
lines_checksum = Digest::MD5.hexdigest(expected_lines.map(&:chomp).join("\n") + "\n")
afterwards_length = expected_lines.length - 1
"#{sed} | grep -A #{escape(afterwards_length)} -F -- #{escape(head_line)} | md5sum | grep -qiw -- #{escape(lines_checksum)}"
end

def check_mode(file, mode)
regexp = "^#{mode}$"
"stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
Expand Down

0 comments on commit 0dd6302

Please sign in to comment.