Class: RSpec::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/daemon.rb,
lib/rspec/daemon/cli.rb,
lib/rspec/daemon/version.rb,
lib/rspec/daemon/client_cli.rb,
lib/rspec/daemon/configuration.rb

Defined Under Namespace

Classes: Cli, ClientCli, Configuration, Error

Constant Summary collapse

SCRIPT_NAME =
File.basename(__FILE__).freeze
VERSION =
"1.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(bind_address, port) ⇒ Daemon

Returns a new instance of Daemon.



16
17
18
19
# File 'lib/rspec/daemon.rb', line 16

def initialize(bind_address, port)
  @bind_address = bind_address
  @port = port
end

Instance Method Details

#cached_configObject



89
90
91
# File 'lib/rspec/daemon.rb', line 89

def cached_config
  @cached_config ||= RSpec::Daemon::Configuration.new
end

#handle_request(socket) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/daemon.rb', line 37

def handle_request(socket)
  status, out = run(socket.gets)

  socket.puts(status)
  socket.puts(out)
  puts out
  socket.puts(__FILE__)
rescue StandardError => e
  socket.puts e.full_message
ensure
  socket.close
end

#resetObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/daemon.rb', line 61

def reset
  RSpec::Core::Configuration.class_eval { define_method(:command) { "rspec" } }
  RSpec::Core::Runner.disable_autorun!
  RSpec.reset

  if cached_config.has_recorded_config?
    # Reload configuration from the first time
    cached_config.replay_configuration
    # Invoke auto reload (if Rails is in Zeitwerk mode and autoloading is enabled)
    if defined?(::Rails) && Rails.respond_to?(:autoloaders) && !::Rails.configuration.cache_classes?
      puts "Reloading..."
      ::Rails.autoloaders.main.reload
    end
  else
    # This is the first spec run
    cached_config.record_configuration(&rspec_configuration)
  end
end

#rspec_configurationObject



80
81
82
83
84
85
86
87
# File 'lib/rspec/daemon.rb', line 80

def rspec_configuration
  proc do
    ENV['RSPEC_DAEMON'] = "1"
    if File.exist? "spec/rails_helper.rb"
      require "rails_helper"
    end
  end
end

#run(msg, options = []) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/rspec/daemon.rb', line 50

def run(msg, options = [])
  options += ["--force-color", "--format", "documentation"]
  argv = msg.split(" ")

  reset
  out = StringIO.new
  status = RSpec::Core::Runner.run(options + argv, out, out)

  [status, out.string]
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec/daemon.rb', line 21

def start
  $LOAD_PATH << "./spec"

  RSpec::Core::Runner.disable_autorun!
  server = TCPServer.open(@bind_address, @port)
  puts "Listening on tcp://#{server.addr[2]}:#{server.addr[1]}"

  loop do
    handle_request(server.accept)
  rescue Interrupt
    puts "quit"
    server.close
    break
  end
end