class RspecRunner::Server

Public Class Methods

execute(path) click to toggle source
# File lib/rspec_runner/server.rb, line 36
def execute(path)
  RSpec.configuration.start_time = Time.now
  RSpec::Core::Runner.run(filepaths(path))
ensure
  reset_rspec!
end
restart() click to toggle source
# File lib/rspec_runner/server.rb, line 43
def restart
  stop
  create_uri_file
  fork_process
end
start() click to toggle source
# File lib/rspec_runner/server.rb, line 8
def start
  create_uri_file

  puts 'Preloading gems...'
  require 'rubygems'
  require 'bundler'

  Bundler.load.dependencies.reject! do |d|
    spec = d.to_spec

    if spec.gem_dir == Dir.pwd
      @gem_name = spec.name
    else
      spec.name == 'rspec_runner'
    end
  end

  if gem?
    Bundler.require(:default, :development)
  else
    Bundler.require(:default, :test)
  end

  $LOAD_PATH.unshift File.expand_path("#{Dir.pwd}/spec")

  fork_process
end
stop() click to toggle source
# File lib/rspec_runner/server.rb, line 49
def stop
  if @pid && @pid != 0
    # TODO: try to kill without -9
    send_signal('KILL')
  end
  delete_uri_file
end

Private Class Methods

assign_uri() click to toggle source
# File lib/rspec_runner/server.rb, line 92
def assign_uri
  socket = Socket.new(:INET, :STREAM, 0)
  socket.bind(Addrinfo.tcp('127.0.0.1'.freeze, 0))
  free_port = socket.local_address.ip_port
  uri = "druby://localhost:#{free_port}"

  if File.exist?(RspecRunner.configuration.uri_filepath)
    File.write(RspecRunner.configuration.uri_filepath, uri)
  else
    create_uri_file(uri)
  end

  uri
end
create_uri_file(uri = nil) click to toggle source
# File lib/rspec_runner/server.rb, line 82
def create_uri_file(uri = nil)
  dirname = File.dirname(RspecRunner.configuration.uri_filepath)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  File.write(RspecRunner.configuration.uri_filepath, uri)
end
delete_uri_file() click to toggle source
# File lib/rspec_runner/server.rb, line 88
def delete_uri_file
  File.delete(RspecRunner.configuration.uri_filepath) if File.exist?(RspecRunner.configuration.uri_filepath)
end
filepaths(path) click to toggle source
# File lib/rspec_runner/server.rb, line 107
def filepaths(path)
  multiple_files?(path) ? Dir.glob(path) : [path]
end
fork_process() click to toggle source
# File lib/rspec_runner/server.rb, line 63
def fork_process
  @pid = fork do
    puts 'Preloading dependencies...'
    require @gem_name if gem?
    require 'spec_helper.rb'

    DRb.start_service(assign_uri, self)
    puts 'Server started!'

    DRb.thread.join
  end

  Process.detach(@pid) # so if the child exits, it dies
end
gem?() click to toggle source
# File lib/rspec_runner/server.rb, line 59
def gem?
  !!@gem_name
end
multiple_files?(path) click to toggle source
# File lib/rspec_runner/server.rb, line 111
def multiple_files?(path)
  path.include?('*'.freeze)
end
reset_rspec!() click to toggle source
# File lib/rspec_runner/server.rb, line 115
def reset_rspec!
  world = RSpec.world
  configuration = RSpec.configuration

  world.reset
  world.filtered_examples.clear
  world.instance_variable_set(:@example_group_counts_by_spec_file, Hash.new(0))
  configuration.reset
  configuration.reset_filters if configuration.respond_to?(:reset_filters)
  configuration.filter_manager = RSpec::Core::FilterManager.new if configuration.respond_to?(:filter_manager=)
  configuration.files_to_run = []
  configuration.files_or_directories_to_run = []
  configuration.seed = rand(0xFFFF) if configuration.seed_used?

  return unless configuration.respond_to?(:loaded_spec_files)
  set = configuration.loaded_spec_files
  set.each { |k| set.delete(k) }
end
send_signal(signal) click to toggle source
# File lib/rspec_runner/server.rb, line 78
def send_signal(signal)
  Process.kill(signal, @pid)
end