Class: Spec::Rails::SpecServer

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/rails/spec_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.daemonize(pid_file = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spec/rails/spec_server.rb', line 17

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

.restart_test_serverObject



9
10
11
12
13
14
15
# File 'lib/spec/rails/spec_server.rb', line 9

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

.runObject



37
38
39
40
41
# File 'lib/spec/rails/spec_server.rb', line 37

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

Instance Method Details

#in_memory_database?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/spec/rails/spec_server.rb', line 90

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

#run(argv, stderr, stdout) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spec/rails/spec_server.rb', line 44

def run(argv, stderr, stdout)
  $stdout = stdout
  $stderr = stderr
  
  ::Rails::Configuration.extend Module.new {def cache_classes; false; end}

  ::ActiveSupport.const_defined?(:Dependencies) ?
    ::ActiveSupport::Dependencies.mechanism = :load :
    ::Dependencies.mechanism = :load
  
  require 'action_controller/dispatcher'
  dispatcher = ::ActionController::Dispatcher.new($stdout)

  if ::ActionController::Dispatcher.respond_to?(:reload_application)
    ::ActionController::Dispatcher.reload_application
  else
    dispatcher.reload_application
  end
  
  if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
    Fixtures.reset_cache
  end

  unless Object.const_defined?(:ApplicationController)
    %w(application_controller.rb application.rb).each do |name|
      require_dependency(name) if File.exists?("#{RAILS_ROOT}/app/controllers/#{name}")
    end
  end
  load "#{RAILS_ROOT}/spec/spec_helper.rb"

  if in_memory_database?
    load "#{RAILS_ROOT}/db/schema.rb"
    ActiveRecord::Migrator.up('db/migrate')
  end
  
  ::Spec::Runner::CommandLine.run(
    ::Spec::Runner::OptionParser.parse(
      argv,
      $stderr,
      $stdout
    )
  )

  dispatcher.cleanup_application if dispatcher.respond_to?(:cleanup_application)
end