Class: Spork::Runner

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

Overview

This is used by bin/spork. It’s wrapped in a class because it’s easier to test that way.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, output, error) ⇒ Runner

Returns a new instance of Runner.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spork/runner.rb', line 13

def initialize(args, output, error)
  raise ArgumentError, "expected array of args" unless args.is_a?(Array)
  @output = output
  @error = error
  @options = {}
  opt = OptionParser.new
  opt.banner = "Usage: spork [test framework name] [options]\n\n"
  
  opt.separator "Options:"
  opt.on("-b", "--bootstrap")  {|ignore| @options[:bootstrap] = true }
  opt.on("-d", "--diagnose")  {|ignore| @options[:diagnose] = true }
  opt.on("-h", "--help")  {|ignore| @options[:help] = true }
  opt.on("-p", "--port [PORT]") {|port| @options[:port] = port }
  non_option_args = args.select { |arg| ! args[0].match(/^-/) }
  @options[:server_matcher] = non_option_args[0]
  opt.parse!(args)
  
  if @options[:help]
    @output.puts opt
    @output.puts
    @output.puts supported_test_frameworks_text
    exit(0)
  end
end

Instance Attribute Details

#test_frameworkObject (readonly)

Returns the value of attribute test_framework.



7
8
9
# File 'lib/spork/runner.rb', line 7

def test_framework
  @test_framework
end

Class Method Details

.run(args, output, error) ⇒ Object



9
10
11
# File 'lib/spork/runner.rb', line 9

def self.run(args, output, error)
  self.new(args, output, error).run
end

Instance Method Details

#find_test_frameworkObject

Returns a server for the specified (or the detected default) testing framework. Returns nil if none detected, or if the specified is not supported or available.



48
49
50
51
52
53
54
# File 'lib/spork/runner.rb', line 48

def find_test_framework
  Spork::TestFramework.factory(@output, @error, options[:server_matcher])
rescue Spork::TestFramework::NoFrameworksAvailable => e
  @error.puts e.message
rescue Spork::TestFramework::FactoryException => e
  @error.puts "#{e.message}\n\n#{supported_test_frameworks_text}"
end

#runObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/spork/runner.rb', line 56

def run
  return false unless test_framework = find_test_framework
  ENV["DRB"] = 'true'
  @error.puts "Using #{test_framework.short_name}"
  @error.flush

  case
  when options[:bootstrap]
    test_framework.bootstrap
  when options[:diagnose]
    require 'spork/diagnoser'
    
    Spork::Diagnoser.install_hook!(test_framework.entry_point)
    test_framework.preload
    Spork::Diagnoser.output_results(@output)
    return true
  else
    run_strategy = Spork::RunStrategy.factory(test_framework)
    return(false) unless run_strategy.preload
    Spork::Server.run(:port => @options[:port] || test_framework.default_port, :run_strategy => run_strategy)
    return true
  end
end

#supported_test_frameworks_textObject



38
39
40
41
42
43
44
45
# File 'lib/spork/runner.rb', line 38

def supported_test_frameworks_text
  text = StringIO.new
  
  text.puts "Supported test frameworks:"
  text.puts Spork::TestFramework.supported_test_frameworks.sort { |a,b| a.short_name <=> b.short_name }.map { |s| (s.available? ? '(*) ' : '( ) ') + s.short_name }
  text.puts "\nLegend: ( ) - not detected in project   (*) - detected\n"
  text.string
end