From c003d29c72ea22f0f59f99f9ed6198d1bbe5e42a Mon Sep 17 00:00:00 2001 From: TAGOMORI Satoshi Date: Mon, 14 Apr 2014 20:27:03 +0900 Subject: [PATCH] add check_file_contain_lines: matcher for fixed-line sequence --- lib/specinfra/command/base.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/specinfra/command/base.rb b/lib/specinfra/command/base.rb index 117d73e17..f0fa957d8 100644 --- a/lib/specinfra/command/base.rb +++ b/lib/specinfra/command/base.rb @@ -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)}"