From 0d61ed44dc652898a1f906a4bcf938c70210d1d2 Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Thu, 24 Apr 2008 14:01:55 +0100 Subject: [PATCH] spec run working --- script/spec | 4 ++ script/spec_server | 101 ++++++++++++++++++++++++++++++++++++++++++++ spec/rcov.opts | 2 + spec/spec_helper.rb | 47 +++++++++++++++++++++ stories/all.rb | 4 ++ stories/helper.rb | 3 ++ 6 files changed, 161 insertions(+) create mode 100755 script/spec create mode 100755 script/spec_server create mode 100644 spec/rcov.opts create mode 100644 spec/spec_helper.rb create mode 100644 stories/all.rb create mode 100644 stories/helper.rb diff --git a/script/spec b/script/spec new file mode 100755 index 00000000..d8155657 --- /dev/null +++ b/script/spec @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../vendor/plugins/rspec/lib")) +require 'spec' +exit ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(ARGV, STDERR, STDOUT)) diff --git a/script/spec_server b/script/spec_server new file mode 100755 index 00000000..b9734cd6 --- /dev/null +++ b/script/spec_server @@ -0,0 +1,101 @@ +#!/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(true) + elsif ::Dispatcher.respond_to?(:reset_application!) + ::Dispatcher.reset_application! + else + raise "Application reloading failed" + end + ::Dependencies.mechanism = :load + require_dependency('application.rb') unless Object.const_defined?(:ApplicationController) + load File.dirname(__FILE__) + '/../spec/spec_helper.rb' + + ::Spec::Runner::CommandLine.run( + ::Spec::Runner::OptionParser.parse( + argv, + $stderr, + $stdout + ) + ) + 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 diff --git a/spec/rcov.opts b/spec/rcov.opts new file mode 100644 index 00000000..baf694c9 --- /dev/null +++ b/spec/rcov.opts @@ -0,0 +1,2 @@ +--exclude "spec/*,gems/*" +--rails \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..938dd7b4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,47 @@ +# This file is copied to ~/spec when you run 'ruby script/generate rspec' +# from the project root directory. +ENV["RAILS_ENV"] = "test" +require File.expand_path(File.dirname(__FILE__) + "/../config/environment") +require 'spec' +require 'spec/rails' + +Spec::Runner.configure do |config| + # If you're not using ActiveRecord you should remove these + # lines, delete config/database.yml and disable :active_record + # in your config/boot.rb + config.use_transactional_fixtures = true + config.use_instantiated_fixtures = false + config.fixture_path = RAILS_ROOT + '/spec/fixtures/' + + # == Fixtures + # + # You can declare fixtures for each example_group like this: + # describe "...." do + # fixtures :table_a, :table_b + # + # Alternatively, if you prefer to declare them only once, you can + # do so right here. Just uncomment the next line and replace the fixture + # names with your fixtures. + # + # config.global_fixtures = :table_a, :table_b + # + # If you declare global fixtures, be aware that they will be declared + # for all of your examples, even those that don't use them. + # + # You can also declare which fixtures to use (for example fixtures for test/fixtures): + # + # config.fixture_path = RAILS_ROOT + '/spec/fixtures/' + # + # == Mock Framework + # + # RSpec uses it's own mocking framework by default. 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 + # + # == Notes + # + # For more information take a look at Spec::Example::Configuration and Spec::Runner +end diff --git a/stories/all.rb b/stories/all.rb new file mode 100644 index 00000000..2e8f46a4 --- /dev/null +++ b/stories/all.rb @@ -0,0 +1,4 @@ +dir = File.dirname(__FILE__) +Dir[File.expand_path("#{dir}/**/*.rb")].uniq.each do |file| + require file +end \ No newline at end of file diff --git a/stories/helper.rb b/stories/helper.rb new file mode 100644 index 00000000..da7a13a6 --- /dev/null +++ b/stories/helper.rb @@ -0,0 +1,3 @@ +ENV["RAILS_ENV"] = "test" +require File.expand_path(File.dirname(__FILE__) + "/../config/environment") +require 'spec/rails/story_adapter' \ No newline at end of file