lib/serverspec/backend/exec.rb in serverspec-0.2.22 vs lib/serverspec/backend/exec.rb in serverspec-0.2.23

- old
+ new

@@ -1,7 +1,5 @@ -require 'open3' - module Serverspec module Backend class Exec def initialize(commands) @commands ||= commands @@ -10,58 +8,62 @@ def commands @commands end def run_command(cmd, opts={}) + stdout = `#{cmd} 2>&1` # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8 #stdout, stderr, status = Open3.capture3(cmd) - # So get exit status with `command` - `#{cmd} 2>&1` - ret = { :exit_status => $?, :exit_signal => nil } - # Get stdout and stderr - stdin, stdout, stderr = Open3.popen3(cmd) - ret[:stdout] = stdout.read - ret[:stderr] = stderr.read - ret + if ! @example.nil? + @example.metadata[:command] = cmd + @example.metadata[:stdout] = stdout + end + + { :stdout => stdout, :stderr => nil, + :exit_status => $?, :exit_signal => nil } end def check_zero(cmd, *args) ret = run_command(commands.send(cmd, *args)) ret[:exit_status] == 0 end # Default action is to call check_zero with args def method_missing(meth, *args, &block) # Remove example object from *args - args.shift + @example = args.shift check_zero(meth, *args) end def check_installed_by_gem(example, package, version) + @example = example ret = run_command(commands.check_installed_by_gem(package)) res = ret[:exit_status] == 0 if res && version res = false if not ret[:stdout].match(/\(#{version}\)/) end res end def check_running(example, process) + @example = example ret = run_command(commands.check_running(process)) if ret[:exit_status] == 1 || ret[:stdout] =~ /stopped/ ret = run_command(commands.check_process(process)) end ret[:exit_status] == 0 end def check_running_under_supervisor(example, process) + @example = example ret = run_command(commands.check_running_under_supervisor(process)) ret[:exit_status] == 0 && ret[:stdout] =~ /RUNNING/ end def check_readable(example, file, by_whom) + @example = example mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil @@ -74,10 +76,11 @@ mode_octal & 0004 != 0 end end def check_writable(example, file, by_whom) + @example = example mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil @@ -90,10 +93,11 @@ mode_octal & 0002 != 0 end end def check_executable(example, file, by_whom) + @example = example mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil @@ -106,9 +110,10 @@ mode_octal & 0001 != 0 end end def check_mounted(example, path, expected_attr, only_with) + @example = example ret = run_command(commands.check_mounted(path)) if expected_attr.nil? || ret[:exit_status] != 0 return ret[:exit_status] == 0 end