module Rootage::ScenarioTest

This module helps tests of command execution.

Public Class Methods

fail(scenario, args=nil, &block) click to toggle source

Run the action, expecting scenario execution fails.

# File lib/rootage/test-helper.rb, line 57
def fail(scenario, args=nil, &block)
  res = execute(scenario, args, &block)
  res.report if res.success?
  res.should.not.success
  return res
end
succeed(scenario, args=nil, &block) click to toggle source

Run the action, expecting scenario execution succeeds.

# File lib/rootage/test-helper.rb, line 49
def succeed(scenario, args=nil, &block)
  res = execute(scenario, args, &block)
  res.report if not(res.success?)
  res.success?.should.be.true
  return res
end

Private Class Methods

execute(scenario, args=nil, &b) click to toggle source

Run the scenario.

# File lib/rootage/test-helper.rb, line 67
def execute(scenario, args=nil, &b)
  # make result
  _args = args ? args : scenario.args.clone
  stdout = StringIO.new("", "w")
  stderr = StringIO.new("", "w")
  res = ScenarioResult.new(
    scenario: scenario, args: _args, stdout: stdout, stderr: stderr
  )

  # setup stdout and stderr
  $stdout = stdout
  $stderr = stderr

  orig_logger_block = Log.get_logger_block
  null_logger = NullLogger.new
  Log.set_logger_block {null_logger}

  # run the action
  begin
    if block_given?
      b.call(scenario, args)
    else
      if scenario.is_a?(Scenario)
        scenario.run
      else
        scenario.run(args)
      end
    end
  rescue Object => e
    res.exception = e
  end

  # revert
  $stdout = STDOUT
  $stderr = STDERR
  Log.set_logger_block(&orig_logger_block)

  # kill childs
  Sys::ProcTable.ps.select {|ps|
    if ps.ppid == Process.pid
      Process.kill(:TERM, ps.pid)
    end
  }

  return res
end