Browse Source

Use Spork instead of writing our own

release/0.11
Henare Degan 9 years ago
parent
commit
1820c346e0
  1. 2
      .rspec
  2. 1
      Gemfile
  3. 5
      Gemfile.lock
  4. 116
      script/spec_server
  5. 228
      spec/spec_helper.rb

2
.rspec

@ -1 +1 @@
--color
--color --drb

1
Gemfile

@ -64,4 +64,5 @@ end
group :test, :development do
gem 'rspec-rails'
gem 'spork-rails'
end

5
Gemfile.lock

@ -184,6 +184,10 @@ GEM
skinny (0.2.3)
eventmachine (~> 1.0.0)
thin (~> 1.5.0)
spork (1.0.0rc3)
spork-rails (3.2.1)
rails (>= 3.0.0, < 3.3.0)
spork (>= 1.0rc0)
sqlite3 (1.3.6)
sqlite3-ruby (1.3.3)
sqlite3 (>= 1.3.3)
@ -248,6 +252,7 @@ DEPENDENCIES
ruby-debug
ruby-debug19
ruby-msg (~> 1.5.0)
spork-rails
syslog_protocol
test-unit (~> 1.2.3)
tmail

116
script/spec_server

@ -1,116 +0,0 @@
#!/usr/bin/env ruby
$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'
# This is based on Florian Weber's TDDMate
module Spec
module Runner
class RailsSpecServer
def run(argv, stderr, stdout)
$stdout = stdout
$stderr = stderr
base = ActiveRecord::Base
def base.clear_reloadable_connections!
active_connections.each do |name, conn|
if conn.requires_reloading?
conn.disconnect!
active_connections.delete(name)
end
end
end
if ActionController.const_defined?(:Dispatcher)
dispatcher = ::ActionController::Dispatcher.new($stdout)
dispatcher.cleanup_application
elsif ::Dispatcher.respond_to?(:reset_application!)
::Dispatcher.reset_application!
else
raise "Application reloading failed"
end
if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
Fixtures.reset_cache
end
::Dependencies.mechanism = :load
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
load File.dirname(__FILE__) + '/../spec/spec_helper.rb'
if in_memory_database?
load "#{Rails.root}/db/schema.rb" # use db agnostic schema by default
ActiveRecord::Migrator.up('db/migrate') # use migrations
end
::Spec::Runner::CommandLine.run(
::Spec::Runner::OptionParser.parse(
argv,
$stderr,
$stdout
)
)
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
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

228
spec/spec_helper.rb

@ -1,117 +1,131 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Use test-specific translations
FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po
FastGettext.default_text_domain = 'app'
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# The order (!) of this is important thanks to foreign keys
config.global_fixtures = :users,
:public_bodies,
:public_body_translations,
:public_body_versions,
:info_requests,
:raw_emails,
:incoming_messages,
:outgoing_messages,
:comments,
:info_request_events,
:track_things,
:foi_attachments,
:has_tag_string_tags,
:holidays,
:track_things_sent_emails
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
#config.order = "random"
# This is a workaround for a strange thing where ActionMailer::Base.deliveries isn't being
# cleared out correctly in controller specs. So, do it here for everything.
config.before(:each) do
ActionMailer::Base.deliveries = []
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Use test-specific translations
FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po
FastGettext.default_text_domain = 'app'
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# The order (!) of this is important thanks to foreign keys
config.global_fixtures = :users,
:public_bodies,
:public_body_translations,
:public_body_versions,
:info_requests,
:raw_emails,
:incoming_messages,
:outgoing_messages,
:comments,
:info_request_events,
:track_things,
:foi_attachments,
:has_tag_string_tags,
:holidays,
:track_things_sent_emails
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
#config.order = "random"
# This is a workaround for a strange thing where ActionMailer::Base.deliveries isn't being
# cleared out correctly in controller specs. So, do it here for everything.
config.before(:each) do
ActionMailer::Base.deliveries = []
end
end
end
# XXX No idea what namespace/class/module to put this in
# Create a clean xapian index based on the fixture files and the raw_email data.
def create_fixtures_xapian_index
load_raw_emails_data
rebuild_xapian_index
end
# XXX No idea what namespace/class/module to put this in
# Create a clean xapian index based on the fixture files and the raw_email data.
def create_fixtures_xapian_index
load_raw_emails_data
rebuild_xapian_index
end
# Copy the xapian index created in create_fixtures_xapian_index to a temporary
# copy at the same level and point xapian at the copy
def get_fixtures_xapian_index()
# Create a base index for the fixtures if not already created
$existing_xapian_db ||= create_fixtures_xapian_index
# Store whatever the xapian db path is originally
$original_xapian_path ||= ActsAsXapian.db_path
path_array = $original_xapian_path.split(File::Separator)
path_array.pop
temp_path = File.join(path_array, 'test.temp')
FileUtils.remove_entry_secure(temp_path, force=true)
FileUtils.cp_r($original_xapian_path, temp_path)
ActsAsXapian.db_path = temp_path
end
# Copy the xapian index created in create_fixtures_xapian_index to a temporary
# copy at the same level and point xapian at the copy
def get_fixtures_xapian_index()
# Create a base index for the fixtures if not already created
$existing_xapian_db ||= create_fixtures_xapian_index
# Store whatever the xapian db path is originally
$original_xapian_path ||= ActsAsXapian.db_path
path_array = $original_xapian_path.split(File::Separator)
path_array.pop
temp_path = File.join(path_array, 'test.temp')
FileUtils.remove_entry_secure(temp_path, force=true)
FileUtils.cp_r($original_xapian_path, temp_path)
ActsAsXapian.db_path = temp_path
end
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
yield
ensure
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
def with_active_record_default_timezone(zone)
old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
yield
ensure
ActiveRecord::Base.default_timezone = old_zone
end
def with_active_record_default_timezone(zone)
old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone
yield
ensure
ActiveRecord::Base.default_timezone = old_zone
end
def load_test_categories
PublicBodyCategories.add(:en, [
"Local and regional",
[ "local_council", "Local councils", "a local council" ],
"Miscellaneous",
[ "other", "Miscellaneous", "miscellaneous" ],])
end
def load_test_categories
PublicBodyCategories.add(:en, [
"Local and regional",
[ "local_council", "Local councils", "a local council" ],
"Miscellaneous",
[ "other", "Miscellaneous", "miscellaneous" ],])
def basic_auth_login(request, username = nil, password = nil)
username = Configuration::admin_username if username.nil?
password = Configuration::admin_password if password.nil?
request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{username}:#{password}")
end
end
def basic_auth_login(request, username = nil, password = nil)
username = Configuration::admin_username if username.nil?
password = Configuration::admin_password if password.nil?
request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{username}:#{password}")
Spork.each_run do
# This code will be run each time you run your specs.
end

Loading…
Cancel
Save