Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created May 19, 2009 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcharper/114361 to your computer and use it in GitHub Desktop.
Save timcharper/114361 to your computer and use it in GitHub Desktop.
a fork of script/spec_server, which forks a copy of itself each time specs are run via DRB, giving you a fresh already-initialized copy each time
#!/usr/bin/env ruby
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
puts "Loading Rails environment"
ENV["RAILS_ENV"] ||= 'test'
ENV["DRB"] = 'true'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT)
require 'optparse'
require 'drb/drb'
require 'rbconfig'
# This is based on Florian Weber's TDDMate
module Spec
module Rails
class SpecServer
class << self
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
}
run
}
puts "spec_server launched (PID: %d)" % pid
File.open(pid_file,"w"){|f| f.puts pid } if pid_file
exit! 0
end
def run
trap("USR2") { ::Spec::Rails::SpecServer.restart_test_server } if Signal.list.has_key?("USR2")
DRb.start_service("druby://127.0.0.1:8989", ::Spec::Rails::SpecServer.new)
DRb.thread.join
end
end
def run(argv, stderr, stdout)
$stdout = stdout
$stderr = stderr
child_pid = Kernel.fork do
load "#{RAILS_ROOT}/spec/spec_helper.rb"
::Spec::Runner::CommandLine.run(
::Spec::Runner::OptionParser.parse(
argv,
$stderr,
$stdout
)
)
end
Process.wait(child_pid)
end
def in_memory_database?
ENV["RAILS_ENV"] == "test" and
::ActiveRecord::Base.connection.class.to_s == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" and
::Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
end
end
end
end
options = Hash.new
parser = OptionParser.new
parser.on("-d", "--daemon") {|ignore| options[:daemon] = true }
parser.on("-p", "--pid PIDFILE"){|pid| options[:pid] = pid }
parser.parse!(ARGV)
if options[:daemon]
::Spec::Rails::SpecServer.daemonize(options[:pid])
else
::Spec::Rails::SpecServer.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment