Class: Spex::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/spex/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, scenario) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
# File 'lib/spex/runner.rb', line 12

def initialize(script, scenario)
  @script = script
  @scenario = scenario
end

Instance Attribute Details

#scenarioObject (readonly)

Returns the value of attribute scenario.



11
12
13
# File 'lib/spex/runner.rb', line 11

def scenario
  @scenario
end

#scriptObject (readonly)

Returns the value of attribute script.



11
12
13
# File 'lib/spex/runner.rb', line 11

def script
  @script
end

Instance Method Details

#execute(execution) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/spex/runner.rb', line 75

def execute(execution)
  log = {:stdout => '', :stderr => ''}
  Open3.popen3(execution.command) do |stdin, stdout, stderr|
    stdin.close
    log[:stdout] << stdout.read
    log[:stderr] << stderr.read
  end
  log
end

#find_source(e) ⇒ Object



62
63
64
# File 'lib/spex/runner.rb', line 62

def find_source(e)
  e.backtrace.detect { |line| !line.include?('test/unit') }
end

#output_log(execution, log) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/spex/runner.rb', line 66

def output_log(execution, log)
  log.each do |stream, content|
    next if content.empty?
    line = "\n#{stream.to_s.upcase} FOR `#{execution.command}`"
    puts line, ('=' * line.size)
    puts content
  end
end

#report(&block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/spex/runner.rb', line 51

def report(&block)
  yield
  puts 'PASSED'.green
  true
rescue Test::Unit::AssertionFailedError => e
  puts 'FAILED'.red
  puts e.message.yellow
  puts "At #{find_source(e)}".yellow
  false
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spex/runner.rb', line 17

def run
  puts %(Running scenario "#{scenario.name}").magenta.bold
  proceed = true
  scenario.each do |execution|
    puts %(Preparing to execute "#{execution.command}").bold
    execution.checks.each do |check|
      print "Pre-checks for #{check}: "
      check.prepare
      proceed = report { check.before }
      break unless proceed
    end
    if proceed
      print %(Executing "#{execution.command}": )
      start = Time.now
      log = execute(execution)
      elapsed = Time.now - start
      puts 'DONE (%.2fs)' % elapsed
      passed = true
      execution.checks.reverse.each do |check|
        print "Post-checks for #{check}: "
        passed = report { check.after }
        break unless passed
      end
      unless passed
        abort "SCENARIO CHECKS FAILED".red.bold
      end
      output_log(execution, log)
    else
      abort "SCENARIO FAILED (EXECUTION ABORTED)".red.bold
    end
  end
  puts "SCENARIO PASSED".green.bold
end

#test(execution, event) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spex/runner.rb', line 85

def test(execution, event)
  klass = Class.new(Test::Unit::TestCase) do
    class << self; attr_accessor :execution, :spex_runner, :name, :event; end
  end
  klass.name = "Spex::Test::Order#{event == :after ? 1 : 0}::#{event.to_s.capitalize}Execution"
  klass.spex_runner = self
  klass.execution = execution
  klass.event = event
  klass.context "#{event} executing `#{execution.command}`" do
    case parent.event
    when :after
      setup do
        @executed ||= self.class.spex_runner.execute(self.class.execution)
      end
      order = parent.execution.checks.reverse
    when :before
      order = parent.execution.checks
    end
    order.each do |check|
      should "pass check #{check.inspect}" do
        check.__send__(event, self)
      end
    end
  end
  klass
end