changelog shortlog tags changeset manifest revisions annotate raw

script/spec_server

changeset 17: 930559402bbe
author: moriq <moriq@moriq.com>
date: Mon Mar 10 10:14:24 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: generate rspec.
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
4require 'rubygems'
5require 'drb/drb'
6require 'rbconfig'
7require 'spec'
8require 'optparse'
9
10# This is based on Florian Weber's TDDMate
11module Spec
12 module Runner
13 class RailsSpecServer
14 def run(argv, stderr, stdout)
15 $stdout = stdout
16 $stderr = stderr
17
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
27
28 if ActionController.const_defined?(:Dispatcher)
29 dispatcher = ::ActionController::Dispatcher.new($stdout)
30 dispatcher.cleanup_application(true)
31 elsif ::Dispatcher.respond_to?(:reset_application!)
32 ::Dispatcher.reset_application!
33 else
34 raise "Application reloading failed"
35 end
36 ::Dependencies.mechanism = :load
37 require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
38 load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
39
40 ::Spec::Runner::CommandLine.run(
41 ::Spec::Runner::OptionParser.parse(
42 argv,
43 $stderr,
44 $stdout
45 )
46 )
47 end
48 end
49 end
50end
51puts "Loading Rails environment"
52
53ENV["RAILS_ENV"] = "test"
54require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
55require 'dispatcher'
56
57def restart_test_server
58 puts "restarting"
59 config = ::Config::CONFIG
60 ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
61 command_line = [ruby, $0, ARGV].flatten.join(' ')
62 exec(command_line)
63end
64
65def daemonize(pid_file = nil)
66 return yield if $DEBUG
67 pid = Process.fork{
68 Process.setsid
69 Dir.chdir(RAILS_ROOT)
70 trap("SIGINT"){ exit! 0 }
71 trap("SIGTERM"){ exit! 0 }
72 trap("SIGHUP"){ restart_test_server }
73 File.open("/dev/null"){|f|
74 STDERR.reopen f
75 STDIN.reopen f
76 STDOUT.reopen f
77 }
78 yield
79 }
80 puts "spec_server launched. (PID: %d)" % pid
81 File.open(pid_file,"w"){|f| f.puts pid } if pid_file
82 exit! 0
83end
84
85options = Hash.new
86opts = OptionParser.new
87opts.on("-d", "--daemon"){|v| options[:daemon] = true }
88opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
89opts.parse!(ARGV)
90
91puts "Ready"
92exec_server = lambda {
93 trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
94 DRb.start_service("druby://localhost:8989", Spec::Runner::RailsSpecServer.new)
95 DRb.thread.join
96}
97
98if options[:daemon]
99 daemonize(options[:pid], &exec_server)
100else
101 exec_server.call
102end