2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
require "autospec/rspec_runner"
|
|
|
|
|
2013-10-23 19:06:05 -04:00
|
|
|
module Autospec
|
2013-11-01 18:57:50 -04:00
|
|
|
|
|
|
|
class SimpleRunner < RspecRunner
|
2018-06-17 23:20:23 -04:00
|
|
|
def initialize
|
|
|
|
@mutex = Mutex.new
|
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
|
|
|
|
def run(specs)
|
2019-05-02 18:17:27 -04:00
|
|
|
puts "Running Rspec: #{specs}"
|
2013-11-01 18:57:50 -04:00
|
|
|
# kill previous rspec instance
|
2018-06-17 23:20:23 -04:00
|
|
|
@mutex.synchronize do
|
|
|
|
self.abort
|
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
# we use our custom rspec formatter
|
2019-06-20 20:59:01 -04:00
|
|
|
args = [
|
|
|
|
"-r", "#{File.dirname(__FILE__)}/formatter.rb",
|
|
|
|
"-f", "Autospec::Formatter"
|
|
|
|
]
|
2019-04-01 08:03:51 -04:00
|
|
|
|
|
|
|
command = begin
|
2019-06-20 21:00:28 -04:00
|
|
|
if ENV["PARALLEL_SPEC"] == '1' &&
|
2019-04-01 08:03:51 -04:00
|
|
|
!specs.split.any? { |s| puts s; s =~ /\:/ } # Parallel spec can't run specific groups
|
2019-04-02 10:33:26 -04:00
|
|
|
|
2019-06-20 20:59:01 -04:00
|
|
|
"bin/turbo_rspec #{args.join(" ")} #{specs.split.join(" ")}"
|
2019-04-01 08:03:51 -04:00
|
|
|
else
|
|
|
|
"bin/rspec #{args.join(" ")} #{specs.split.join(" ")}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-01 18:57:50 -04:00
|
|
|
# launch rspec
|
2020-03-26 11:32:41 -04:00
|
|
|
Dir.chdir(Rails.root) do # rubocop:disable Discourse/NoChdir because this is not part of the app
|
2017-07-27 21:20:09 -04:00
|
|
|
env = { "RAILS_ENV" => "test" }
|
|
|
|
if specs.split(' ').any? { |s| s =~ /^(.\/)?plugins/ }
|
2017-06-08 18:02:30 -04:00
|
|
|
env["LOAD_PLUGINS"] = "1"
|
|
|
|
puts "Loading plugins while running specs"
|
|
|
|
end
|
2018-06-17 23:20:23 -04:00
|
|
|
pid =
|
|
|
|
@mutex.synchronize do
|
2019-04-01 08:03:51 -04:00
|
|
|
@pid = Process.spawn(env, command)
|
2018-06-17 23:20:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
_, status = Process.wait2(pid)
|
|
|
|
|
2017-04-13 17:24:58 -04:00
|
|
|
status.exitstatus
|
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
end
|
2013-10-23 19:06:05 -04:00
|
|
|
|
|
|
|
def abort
|
2018-06-17 23:20:23 -04:00
|
|
|
if pid = @pid
|
|
|
|
Process.kill("TERM", pid) rescue nil
|
|
|
|
wait_for_done(pid)
|
|
|
|
pid = nil
|
2013-10-23 19:06:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
2018-06-17 23:20:23 -04:00
|
|
|
# assume sigint on child will take care of this?
|
|
|
|
if pid = @pid
|
|
|
|
wait_for_done(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_done(pid)
|
|
|
|
i = 3000
|
|
|
|
while (i > 0 && Process.getpgid(pid) rescue nil)
|
|
|
|
sleep 0.001
|
|
|
|
i -= 1
|
|
|
|
end
|
|
|
|
if (Process.getpgid(pid) rescue nil)
|
|
|
|
STDERR.puts "Terminating rspec #{pid} by force cause it refused graceful termination"
|
|
|
|
Process.kill("KILL", pid)
|
|
|
|
end
|
2013-10-23 19:06:05 -04:00
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
|
2013-10-23 19:06:05 -04:00
|
|
|
end
|
2013-11-01 18:57:50 -04:00
|
|
|
|
2013-10-23 19:06:05 -04:00
|
|
|
end
|