Class: Spec::Runner::BehaviourRunner

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

Constant Summary collapse

FILE_SORTERS =
{
  'mtime' => lambda {|file_a, file_b| File.mtime(file_b) <=> File.mtime(file_a)}
}

Instance Method Summary collapse

Constructor Details

#initialize(options, arg = nil) ⇒ BehaviourRunner

Returns a new instance of BehaviourRunner.



5
6
7
8
# File 'lib/spec/runner/behaviour_runner.rb', line 5

def initialize(options, arg=nil)
  @behaviours = []
  @options = options
end

Instance Method Details

#add_behaviour(behaviour) ⇒ Object



10
11
12
13
14
15
# File 'lib/spec/runner/behaviour_runner.rb', line 10

def add_behaviour(behaviour)
  if !specified_examples.nil? && !specified_examples.empty?
    behaviour.retain_examples_matching!(specified_examples)
  end
  @behaviours << behaviour if behaviour.number_of_examples != 0 && !behaviour.shared?
end

#number_of_examplesObject



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

def number_of_examples
  @behaviours.inject(0) {|sum, behaviour| sum + behaviour.number_of_examples}
end

#prepare!(paths) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/spec/runner/behaviour_runner.rb', line 45

def prepare!(paths)
  unless paths.nil? # It's nil when running single specs with ruby
    paths = find_paths(paths)
    sorted_paths = sort_paths(paths)
    load_specs(sorted_paths) # This will populate @behaviours via callbacks to add_behaviour
  end
  @options.reporter.start(number_of_examples)
  @behaviours.reverse! if @options.reverse
  set_sequence_numbers
end

#report_dumpObject



41
42
43
# File 'lib/spec/runner/behaviour_runner.rb', line 41

def report_dump
  @options.reporter.dump
end

#report_endObject



37
38
39
# File 'lib/spec/runner/behaviour_runner.rb', line 37

def report_end
  @options.reporter.end
end

#run(paths, exit_when_done) ⇒ Object

Runs all behaviours and returns the number of failures.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spec/runner/behaviour_runner.rb', line 18

def run(paths, exit_when_done)
  prepare!(paths)
  begin
    run_behaviours
  rescue Interrupt
  ensure
    report_end
  end
  failure_count = report_dump
  
  heckle if(failure_count == 0 && !@options.heckle_runner.nil?)
  
  if(exit_when_done)
    exit_code = (failure_count == 0) ? 0 : 1
    exit(exit_code)
  end
  failure_count
end

#run_behavioursObject



56
57
58
59
60
# File 'lib/spec/runner/behaviour_runner.rb', line 56

def run_behaviours
  @behaviours.each do |behaviour|
    behaviour.run(@options.reporter, @options.dry_run, @options.reverse, @options.timeout)
  end
end

#sort_paths(paths) ⇒ Object



74
75
76
77
78
# File 'lib/spec/runner/behaviour_runner.rb', line 74

def sort_paths(paths)
  sorter = sorter(paths)
  paths = paths.sort(&sorter) unless sorter.nil?
  paths
end

#sorter(paths) ⇒ Object



70
71
72
# File 'lib/spec/runner/behaviour_runner.rb', line 70

def sorter(paths)
  FILE_SORTERS[@options.loadby]
end