Files
discourse/lib/autospec/simple_runner.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.2 KiB
Ruby
Raw Normal View History

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