aboutsummaryrefslogtreecommitdiffstats
path: root/script/spec_server
blob: cac91e2b89c5b2a889ec2b7317fb4c02521f545a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../rspec/lib' # For svn
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
require 'rubygems'
require 'drb/drb'
require 'rbconfig'
require 'spec'
require 'optparse'
specmate = ENV['HOME'] + "/Library/Application\ Support/TextMate/Bundles/RSpec.tmbundle/Support/lib"
if File.directory?(specmate)
  $LOAD_PATH.unshift(specmate) 
  require 'text_mate_formatter'
end

# This is based on Florian Weber's TDDMate

module Spec
  module Runner
    class RailsSpecServer
      def run(args, stderr, stdout)
    	$stdout = stdout
    	$stderr = stderr

        ::Dispatcher.reset_application!
        ::Dependencies.mechanism = :load
        require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
        load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
    
        ::Spec::Runner::CommandLine.run(args, stderr, stdout, false, true)
      end
    end
  end
end
puts "Loading Rails environment"

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'dispatcher'

def restart_test_server
  puts "restarting"
  config       = ::Config::CONFIG
  ruby         = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
  command_line = [ruby, $0, ARGV].flatten.join(' ')
  exec(command_line)
end  

def daemonize(pid_file = nil)
  return yield if $DEBUG
  pid = Process.fork{
    Process.setsid
    Dir.chdir(RAILS_ROOT)
    trap("SIGINT"){ exit! 0 }
    trap("SIGTERM"){ exit! 0 }
    trap("SIGHUP"){ restart_test_server }
    File.open("/dev/null"){|f|
      STDERR.reopen f
      STDIN.reopen  f
      STDOUT.reopen f
    }
    yield
  }
  puts "spec_server launched. (PID: %d)" % pid
  File.open(pid_file,"w"){|f| f.puts pid } if pid_file
  exit! 0
end

options = Hash.new
opts = OptionParser.new
opts.on("-d", "--daemon"){|v| options[:daemon] = true }
opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
opts.parse!(ARGV)

puts "Ready"
exec_server = lambda {
  trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
  DRb.start_service("druby://localhost:8989", Spec::Runner::RailsSpecServer.new)
  DRb.thread.join
}

if options[:daemon]
  daemonize(options[:pid], &exec_server)
else
  exec_server.call
end