Class: Spork::Runner

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

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)


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

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("-h", "--help")  {|ignore| @options[:help] = true }
  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_servers_text
    exit(0)
  end
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

Class Method Details

.run(args, output, error) ⇒ Object



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

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

Instance Method Details

#find_serverObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/spork/runner.rb', line 44

def find_server
  if options[:server_matcher]
    @server = Spork::Server.supported_servers(options[:server_matcher]).first
    unless @server
      @output.puts <<-ERROR
#{options[:server_matcher].inspect} didn't match a supported test framework.

#{supported_servers_text}
      ERROR
      return
    end
    
    unless @server.available?
      @output.puts  <<-USEFUL_ERROR
I can't find the helper file #{@server.helper_file} for the #{@server.server_name} testing framework.
Are you running me from the project directory?
      USEFUL_ERROR
      return
    end
  else
    @server = Spork::Server.available_servers.first
    if @server.nil?
      @output.puts  <<-USEFUL_ERROR
I can't find any testing frameworks to use.
Are you running me from a project directory?
      USEFUL_ERROR
      return
    end
  end
  @server
end

#runObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/spork/runner.rb', line 76

def run
  return false unless find_server
  ENV["DRB"] = 'true'
  ENV["RAILS_ENV"] ||= 'test' if server.using_rails?
  @output.puts "Using #{server.server_name}"
  return server.bootstrap if options[:bootstrap]
  return(false) unless server.preload
  server.run
  return true
end

#supported_servers_textObject



35
36
37
38
39
40
41
42
# File 'lib/spork/runner.rb', line 35

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