79298a5 by Johan Sørensen at 2007-08-14 1
#!/usr/bin/env ruby
2
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
3
require 'rubygems'
4
require 'drb/drb'
5
require 'rbconfig'
6
require 'spec'
7
require 'optparse'
8
9
# This is based on Florian Weber's TDDMate
10
module Spec
11
  module Runner
12
    class RailsSpecServer
0a7b4ac by Johan Sørensen at 2007-11-18 13
      def run(argv, stderr, stdout)
14
        $stdout = stdout
15
        $stderr = stderr
16
17
        base = ActiveRecord::Base
18
        def base.clear_reloadable_connections!
19
          active_connections.each do |name, conn|
20
            if conn.requires_reloading?
21
              conn.disconnect!
22
              active_connections.delete(name)
23
            end
24
          end
8790579 by Johan Sørensen at 2008-06-01 25
        end
79298a5 by Johan Sørensen at 2007-08-14 26
695f845 by David A. Cuadrado at 2008-03-25 27
        if ActionController.const_defined?(:Dispatcher)
28
          dispatcher = ::ActionController::Dispatcher.new($stdout)
8790579 by Johan Sørensen at 2008-06-01 29
          dispatcher.cleanup_application
0a7b4ac by Johan Sørensen at 2007-11-18 30
        elsif ::Dispatcher.respond_to?(:reset_application!)
31
          ::Dispatcher.reset_application!
695f845 by David A. Cuadrado at 2008-03-25 32
        else
33
          raise "Application reloading failed"
0a7b4ac by Johan Sørensen at 2007-11-18 34
        end
8790579 by Johan Sørensen at 2008-06-01 35
        if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
36
          Fixtures.reset_cache
37
        end
38
79298a5 by Johan Sørensen at 2007-08-14 39
        ::Dependencies.mechanism = :load
40
        require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
41
        load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
0a7b4ac by Johan Sørensen at 2007-11-18 42
8790579 by Johan Sørensen at 2008-06-01 43
        if in_memory_database?
44
          load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
45
          ActiveRecord::Migrator.up('db/migrate') # use migrations
46
        end
47
0a7b4ac by Johan Sørensen at 2007-11-18 48
        ::Spec::Runner::CommandLine.run(
49
          ::Spec::Runner::OptionParser.parse(
50
            argv,
51
            $stderr,
52
            $stdout
53
          )
54
        )
79298a5 by Johan Sørensen at 2007-08-14 55
      end
8790579 by Johan Sørensen at 2008-06-01 56
57
      def in_memory_database?
58
        ENV["RAILS_ENV"] == "test" and
59
        ::ActiveRecord::Base.connection.class.to_s == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" and
60
        ::Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
61
      end
79298a5 by Johan Sørensen at 2007-08-14 62
    end
63
  end
64
end
65
puts "Loading Rails environment"
66
67
ENV["RAILS_ENV"] = "test"
68
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
69
require 'dispatcher'
70
71
def restart_test_server
72
  puts "restarting"
73
  config       = ::Config::CONFIG
74
  ruby         = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
75
  command_line = [ruby, $0, ARGV].flatten.join(' ')
76
  exec(command_line)
0a7b4ac by Johan Sørensen at 2007-11-18 77
end
79298a5 by Johan Sørensen at 2007-08-14 78
79
def daemonize(pid_file = nil)
80
  return yield if $DEBUG
81
  pid = Process.fork{
82
    Process.setsid
83
    Dir.chdir(RAILS_ROOT)
84
    trap("SIGINT"){ exit! 0 }
85
    trap("SIGTERM"){ exit! 0 }
86
    trap("SIGHUP"){ restart_test_server }
87
    File.open("/dev/null"){|f|
88
      STDERR.reopen f
89
      STDIN.reopen  f
90
      STDOUT.reopen f
91
    }
92
    yield
93
  }
94
  puts "spec_server launched. (PID: %d)" % pid
95
  File.open(pid_file,"w"){|f| f.puts pid } if pid_file
96
  exit! 0
97
end
98
99
options = Hash.new
100
opts = OptionParser.new
101
opts.on("-d", "--daemon"){|v| options[:daemon] = true }
102
opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
103
opts.parse!(ARGV)
104
105
puts "Ready"
106
exec_server = lambda {
107
  trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
a4b1037 by Fabio Akita at 2009-01-07 108
  DRb.start_service("druby://127.0.0.1:8989", Spec::Runner::RailsSpecServer.new)
79298a5 by Johan Sørensen at 2007-08-14 109
  DRb.thread.join
110
}
111
112
if options[:daemon]
113
  daemonize(options[:pid], &exec_server)
114
else
115
  exec_server.call
116
end