class SpecInfra::Backend::Exec

Attributes

chroot_dir[RW]

Public Instance Methods

run_command(cmd, opts={}) click to toggle source
ORIGINAL
def run_command(cmd, opts={})
  cmd = build_command(cmd)
  cmd = add_pre_command(cmd)
  stdout = run_with_no_ruby_environment do
    `#{build_command(cmd)} 2>&1`
  end
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
  # stdout, stderr, status = Open3.capture3(cmd)

  if @example
    @example.metadata[:command] = cmd
    @example.metadata[:stdout]  = stdout
  end

  CommandResult.new :stdout => stdout, :exit_status => $?.exitstatus
end

/ORIGINAL

# File lib/monkeypatch/serverspec/backend/exec.rb, line 32
def run_command(cmd, opts={})
  cmd = build_command(cmd)
  cmd = add_pre_command(cmd)
  # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
  # stdout, stderr, status = Open3.capture3(cmd)

  if use_chroot?
    chroot_stdout = `#{chroot_cmd(cmd)} 2>&1`

    stdout = get_stdout(chroot_stdout)
    exit_status = get_exit_status(chroot_stdout)
  else
    stdout = run_with_no_ruby_environment { `#{cmd} 2>&1` }
    exit_status = $?.exitstatus
  end

  if @example
    @example.metadata[:command] = cmd
    @example.metadata[:stdout]  = stdout
  end

  CommandResult.new :stdout => stdout, :exit_status => exit_status
end

Private Instance Methods

chroot_cmd(cmd) click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 81
    def chroot_cmd(cmd)
      #quoting command so $ will not be interpreted by shell
      quoted_cmd = cmd.gsub('$', '\$')
      %Q{
sudo chroot #{chroot_dir} /bin/bash <<CHROOT_CMD
  #{quoted_cmd} 2>&1; echo #{exit_code_token}\\$?
CHROOT_CMD
}
    end
exit_code_regexp() click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 77
def exit_code_regexp
  "#{exit_code_token}(\\d+)\s*\n"
end
exit_code_token() click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 73
def exit_code_token
  'EXIT_CODE='
end
get_exit_status(chroot_stdout) click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 64
def get_exit_status(chroot_stdout)
  chroot_command_exit_status = chroot_stdout.match(/#{exit_code_regexp}/)[1]
  chroot_command_exit_status.to_i
end
get_stdout(chroot_stdout) click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 60
def get_stdout(chroot_stdout)
  chroot_stdout.gsub(/#{exit_code_regexp}/, '')
end
use_chroot?() click to toggle source
# File lib/monkeypatch/serverspec/backend/exec.rb, line 69
def use_chroot?
  chroot_dir
end