Upgraded Rails
[monkeycharger.git] / script / spec_server
blobd48df921df66cb3f44be1c409d751b9412616aad
1 #!/usr/bin/env ruby
2 $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../rspec/lib' # For svn
3 $LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
4 require 'rubygems'
5 require 'drb/drb'
6 require 'rbconfig'
7 require 'spec'
8 require 'optparse'
10 # This is based on Florian Weber's TDDMate
11 module Spec
12 module Runner
13 class RailsSpecServer
14 def run(argv, stderr, stdout)
15 $stdout = stdout
16 $stderr = stderr
18 base = ActiveRecord::Base
19 def base.clear_reloadable_connections!
20 active_connections.each do |name, conn|
21 if conn.requires_reloading?
22 conn.disconnect!
23 active_connections.delete(name)
24 end
25 end
26 end
28 if ::Dispatcher.respond_to?(:cleanup_application)
29 ::Dispatcher.cleanup_application
30 elsif ::Dispatcher.respond_to?(:reset_application!)
31 ::Dispatcher.reset_application!
32 end
33 ::Dependencies.mechanism = :load
34 require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
35 load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
37 ::Spec::Runner::CommandLine.run(
38 ::Spec::Runner::OptionParser.parse(
39 argv,
40 $stderr,
41 $stdout
44 end
45 end
46 end
47 end
48 puts "Loading Rails environment"
50 ENV["RAILS_ENV"] = "test"
51 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
52 require 'dispatcher'
54 def restart_test_server
55 puts "restarting"
56 config = ::Config::CONFIG
57 ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
58 command_line = [ruby, $0, ARGV].flatten.join(' ')
59 exec(command_line)
60 end
62 def daemonize(pid_file = nil)
63 return yield if $DEBUG
64 pid = Process.fork{
65 Process.setsid
66 Dir.chdir(RAILS_ROOT)
67 trap("SIGINT"){ exit! 0 }
68 trap("SIGTERM"){ exit! 0 }
69 trap("SIGHUP"){ restart_test_server }
70 File.open("/dev/null"){|f|
71 STDERR.reopen f
72 STDIN.reopen f
73 STDOUT.reopen f
75 yield
77 puts "spec_server launched. (PID: %d)" % pid
78 File.open(pid_file,"w"){|f| f.puts pid } if pid_file
79 exit! 0
80 end
82 options = Hash.new
83 opts = OptionParser.new
84 opts.on("-d", "--daemon"){|v| options[:daemon] = true }
85 opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
86 opts.parse!(ARGV)
88 puts "Ready"
89 exec_server = lambda {
90 trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
91 DRb.start_service("druby://localhost:8989", Spec::Runner::RailsSpecServer.new)
92 DRb.thread.join
95 if options[:daemon]
96 daemonize(options[:pid], &exec_server)
97 else
98 exec_server.call
99 end