2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
require "listen"
|
|
|
|
require "fileutils"
|
|
|
|
require "autospec/reload_css"
|
|
|
|
require "autospec/base_runner"
|
2017-04-26 00:12:39 +08:00
|
|
|
require "socket_server"
|
2013-11-02 06:57:50 +08:00
|
|
|
|
|
|
|
module Autospec
|
|
|
|
end
|
|
|
|
|
|
|
|
class Autospec::Manager
|
|
|
|
def self.run(opts = {})
|
2013-11-05 18:01:17 +08:00
|
|
|
self.new(opts).run
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
2013-11-05 18:01:17 +08:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
@debug = opts[:debug]
|
2019-11-08 22:22:57 +08:00
|
|
|
@auto_run_all = ENV["AUTO_RUN_ALL"] != "0"
|
2013-11-02 06:57:50 +08:00
|
|
|
@queue = []
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@signal = ConditionVariable.new
|
2013-11-29 00:04:53 +08:00
|
|
|
@runners = [ruby_runner]
|
2013-11-05 18:01:17 +08:00
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
|
2013-11-05 18:01:17 +08:00
|
|
|
def run
|
2013-11-02 06:57:50 +08:00
|
|
|
Signal.trap("HUP") do
|
|
|
|
stop_runners
|
|
|
|
exit
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2018-06-18 11:20:23 +08:00
|
|
|
|
|
|
|
Signal.trap("INT") do
|
|
|
|
begin
|
|
|
|
stop_runners
|
|
|
|
rescue => e
|
|
|
|
puts "FAILED TO STOP RUNNERS #{e}"
|
|
|
|
end
|
|
|
|
exit
|
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
|
2019-11-08 22:22:57 +08:00
|
|
|
ensure_all_specs_will_run if @auto_run_all
|
2013-11-02 06:57:50 +08:00
|
|
|
start_runners
|
|
|
|
start_service_queue
|
|
|
|
listen_for_changes
|
|
|
|
|
|
|
|
puts "Press [ENTER] to stop the current run"
|
2019-11-08 22:22:57 +08:00
|
|
|
puts "Press [ENTER] while stopped to run all specs" unless @auto_run_all
|
2013-11-02 06:57:50 +08:00
|
|
|
while @runners.any?(&:running?)
|
|
|
|
STDIN.gets
|
|
|
|
process_queue
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
fail(e, "failed in run")
|
|
|
|
ensure
|
|
|
|
stop_runners
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ruby_runner
|
2017-08-10 05:50:59 +08:00
|
|
|
require "autospec/simple_runner"
|
|
|
|
Autospec::SimpleRunner.new
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_runner
|
|
|
|
require "autospec/qunit_runner"
|
|
|
|
Autospec::QunitRunner.new
|
|
|
|
end
|
|
|
|
|
2017-05-03 04:02:00 +08:00
|
|
|
def ensure_all_specs_will_run(current_runner = nil)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ ensure_all_specs_will_run" if @debug
|
2017-05-03 04:02:00 +08:00
|
|
|
|
|
|
|
@queue.reject! { |_, s, _| s == "spec" }
|
|
|
|
|
|
|
|
@queue.concat [["spec", "spec", current_runner]] if current_runner
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
@runners.each do |runner|
|
2017-05-03 04:02:00 +08:00
|
|
|
unless @queue.any? { |_, s, r| s == "spec" && r == runner }
|
|
|
|
@queue.concat [["spec", "spec", runner]]
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
%i[start stop abort].each do |verb|
|
|
|
|
define_method("#{verb}_runners") do
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ #{verb}_runners" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
@runners.each(&verb)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_service_queue
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ start_service_queue" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
Thread.new { thread_loop while true }
|
|
|
|
end
|
|
|
|
|
|
|
|
# the main loop, will run the specs in the queue till one fails or the queue is empty
|
|
|
|
def thread_loop
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ thread_loop" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
@mutex.synchronize do
|
|
|
|
current = @queue.first
|
|
|
|
last_failed = false
|
|
|
|
last_failed = process_spec(current) if current
|
|
|
|
# stop & wait for the queue to have at least one item or when there's been a failure
|
2013-11-05 18:01:17 +08:00
|
|
|
if @debug
|
|
|
|
puts "@@@@@@@@@@@@ waiting because..."
|
|
|
|
puts "@@@@@@@@@@@@ ...current spec has failed" if last_failed
|
|
|
|
puts "@@@@@@@@@@@@ ...queue is empty" if @queue.length == 0
|
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
@signal.wait(@mutex) if @queue.length == 0 || last_failed
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
fail(e, "failed in main loop")
|
|
|
|
end
|
|
|
|
|
|
|
|
# will actually run the spec and check whether the spec has failed or not
|
|
|
|
def process_spec(current)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ process_spec --> #{current}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
has_failed = false
|
|
|
|
# retrieve the instance of the runner
|
|
|
|
runner = current[2]
|
|
|
|
# actually run the spec (blocking call)
|
|
|
|
result = runner.run(current[1]).to_i
|
|
|
|
|
|
|
|
if result == 0
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ success" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
# remove the spec from the queue
|
|
|
|
@queue.shift
|
|
|
|
else
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ failure" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
has_failed = true
|
|
|
|
if result > 0
|
|
|
|
focus_on_failed_tests(current)
|
2019-11-08 22:22:57 +08:00
|
|
|
ensure_all_specs_will_run(runner) if @auto_run_all
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
has_failed
|
|
|
|
end
|
|
|
|
|
|
|
|
def focus_on_failed_tests(current)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ focus_on_failed_tests --> #{current}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
runner = current[2]
|
|
|
|
# we only want 1 focus in the queue
|
|
|
|
@queue.shift if current[0] == "focus"
|
|
|
|
# focus on the first 10 failed specs
|
|
|
|
failed_specs = runner.failed_specs[0..10]
|
2016-07-08 10:54:38 +08:00
|
|
|
puts "@@@@@@@@@@@@ failed_specs --> #{failed_specs}" if @debug
|
|
|
|
|
|
|
|
# try focus tag
|
|
|
|
if failed_specs.length > 0
|
|
|
|
filename, _ = failed_specs[0].split(":")
|
2017-05-02 02:37:16 +08:00
|
|
|
if filename && File.exist?(filename) && !File.directory?(filename)
|
2016-07-08 10:54:38 +08:00
|
|
|
spec = File.read(filename)
|
2023-01-21 02:52:49 +08:00
|
|
|
start, _ = spec.split(/\S*#focus\S*\z/)
|
2016-07-08 10:54:38 +08:00
|
|
|
if start.length < spec.length
|
|
|
|
line = start.scan(/\n/).length + 1
|
|
|
|
puts "Found #focus tag on line #{line}!"
|
|
|
|
failed_specs = ["#{filename}:#{line + 1}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
# focus on the failed specs
|
|
|
|
@queue.unshift ["focus", failed_specs.join(" "), runner] if failed_specs.length > 0
|
|
|
|
end
|
|
|
|
|
2019-05-03 11:50:47 +08:00
|
|
|
def root_path
|
|
|
|
root_path ||= File.expand_path(File.dirname(__FILE__) + "../../..")
|
|
|
|
end
|
|
|
|
|
|
|
|
def reverse_symlink_map
|
|
|
|
map = {}
|
|
|
|
Dir[root_path + "/plugins/*"].each do |f|
|
|
|
|
next if !File.directory? f
|
|
|
|
resolved = File.realpath(f)
|
|
|
|
map[resolved] = f if resolved != f
|
|
|
|
end
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
|
|
|
# plugins can be symlinked, try to figure out which plugin this is
|
|
|
|
def reverse_symlink(file)
|
|
|
|
resolved = file
|
|
|
|
@reverse_map ||= reverse_symlink_map
|
|
|
|
@reverse_map.each do |location, discourse_location|
|
|
|
|
resolved = discourse_location + file[location.length..-1] if file.start_with?(location)
|
|
|
|
end
|
|
|
|
|
|
|
|
resolved
|
|
|
|
end
|
|
|
|
|
2013-11-05 18:01:17 +08:00
|
|
|
def listen_for_changes
|
|
|
|
puts "@@@@@@@@@@@@ listen_for_changes" if @debug
|
|
|
|
|
2023-01-21 02:52:49 +08:00
|
|
|
options = { ignore: %r{\Alib/autospec} }
|
2013-11-02 06:57:50 +08:00
|
|
|
|
2013-11-05 18:01:17 +08:00
|
|
|
if @opts[:force_polling]
|
2013-11-02 06:57:50 +08:00
|
|
|
options[:force_polling] = true
|
2013-11-05 18:01:17 +08:00
|
|
|
options[:latency] = @opts[:latency] || 3
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
|
2019-05-03 11:50:47 +08:00
|
|
|
path = root_path
|
2015-11-02 10:24:24 +08:00
|
|
|
|
2017-04-26 00:12:39 +08:00
|
|
|
if ENV["VIM_AUTOSPEC"]
|
|
|
|
STDERR.puts "Using VIM file listener"
|
|
|
|
|
|
|
|
socket_path = (Rails.root + "tmp/file_change.sock").to_s
|
|
|
|
FileUtils.rm_f(socket_path)
|
|
|
|
server = SocketServer.new(socket_path)
|
|
|
|
server.start do |line|
|
|
|
|
file, line = line.split(" ")
|
2019-05-03 11:50:47 +08:00
|
|
|
file = reverse_symlink(file)
|
2019-05-03 06:17:27 +08:00
|
|
|
file = file.sub(Rails.root.to_s + "/", "")
|
2021-05-21 09:43:47 +08:00
|
|
|
# process_change can acquire a mutex and block
|
2017-04-26 00:12:39 +08:00
|
|
|
# the acceptor
|
|
|
|
Thread.new do
|
2023-01-21 02:52:49 +08:00
|
|
|
if file =~ /(es6|js)\z/
|
2017-05-03 04:02:00 +08:00
|
|
|
process_change([[file]])
|
|
|
|
else
|
|
|
|
process_change([[file, line]])
|
|
|
|
end
|
2017-04-26 00:12:39 +08:00
|
|
|
end
|
|
|
|
"OK"
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-11-02 10:24:24 +08:00
|
|
|
# to speed up boot we use a thread
|
2015-12-17 10:17:24 +08:00
|
|
|
%w[spec lib app config test vendor plugins].each do |watch|
|
|
|
|
puts "@@@@@@@@@ Listen to #{path}/#{watch} #{options}" if @debug
|
|
|
|
Thread.new do
|
|
|
|
begin
|
2017-04-13 22:26:07 +08:00
|
|
|
listener =
|
|
|
|
Listen.to("#{path}/#{watch}", options) do |modified, added, _|
|
2015-12-17 10:17:24 +08:00
|
|
|
paths = [modified, added].flatten
|
|
|
|
paths.compact!
|
2019-05-03 11:50:47 +08:00
|
|
|
paths.map! do |long|
|
|
|
|
long = reverse_symlink(long)
|
|
|
|
long[(path.length + 1)..-1]
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2015-12-17 10:17:24 +08:00
|
|
|
process_change(paths)
|
2019-05-03 11:50:47 +08:00
|
|
|
end
|
2017-04-13 22:26:07 +08:00
|
|
|
listener.start
|
|
|
|
sleep
|
2015-12-17 10:17:24 +08:00
|
|
|
rescue => e
|
|
|
|
puts "FAILED to listen on changes to #{path}/#{watch}"
|
|
|
|
puts e
|
2015-11-02 10:24:24 +08:00
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_change(files)
|
|
|
|
return if files.length == 0
|
2013-11-05 18:01:17 +08:00
|
|
|
|
|
|
|
puts "@@@@@@@@@@@@ process_change --> #{files}" if @debug
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
specs = []
|
|
|
|
hit = false
|
|
|
|
|
2017-04-26 00:12:39 +08:00
|
|
|
files.each do |file, line|
|
2013-11-02 06:57:50 +08:00
|
|
|
@runners.each do |runner|
|
|
|
|
# reloaders
|
|
|
|
runner.reloaders.each do |k|
|
|
|
|
if k.match(file)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ #{file} matched a reloader for #{runner}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
runner.reload
|
2023-12-07 06:25:00 +08:00
|
|
|
# rubocop:disable Lint/NonLocalExitFromIterator
|
2013-11-02 06:57:50 +08:00
|
|
|
return
|
2023-12-07 06:25:00 +08:00
|
|
|
# rubocop:enable Lint/NonLocalExitFromIterator
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# watchers
|
|
|
|
runner.watchers.each do |k, v|
|
|
|
|
if m = k.match(file)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ #{file} matched a watcher for #{runner}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
hit = true
|
|
|
|
spec = v ? (v.arity == 1 ? v.call(m) : v.call) : file
|
2017-04-26 00:12:39 +08:00
|
|
|
with_line = spec
|
|
|
|
with_line = spec + ":" << line.to_s if spec == file && line
|
2022-01-06 01:45:08 +08:00
|
|
|
if File.exist?(spec) || Dir.exist?(spec)
|
2017-04-26 00:12:39 +08:00
|
|
|
specs << [file, spec, runner] if with_line != spec
|
|
|
|
specs << [file, with_line, runner]
|
|
|
|
end
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
queue_specs(specs) if hit
|
|
|
|
rescue => e
|
|
|
|
fail(e, "failed in watcher")
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_specs(specs)
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ queue_specs --> #{specs}" if @debug
|
|
|
|
|
2013-11-02 06:57:50 +08:00
|
|
|
if specs.length == 0
|
|
|
|
locked = @mutex.try_lock
|
|
|
|
if locked
|
|
|
|
@signal.signal
|
|
|
|
@mutex.unlock
|
|
|
|
end
|
|
|
|
return
|
|
|
|
else
|
|
|
|
abort_runners
|
|
|
|
end
|
|
|
|
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ waiting for the mutex" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
@mutex.synchronize do
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ queueing specs" if @debug
|
|
|
|
puts "@@@@@@@@@@@@ #{@queue}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
specs.each do |file, spec, runner|
|
|
|
|
# make sure there's no other instance of this spec in the queue
|
2017-04-26 00:12:39 +08:00
|
|
|
@queue.delete_if { |_, s, r| s.strip.start_with?(spec.strip) && r == runner }
|
2013-11-02 06:57:50 +08:00
|
|
|
# deal with focused specs
|
|
|
|
if @queue.first && @queue.first[0] == "focus"
|
|
|
|
focus = @queue.shift
|
|
|
|
@queue.unshift([file, spec, runner])
|
2024-05-27 18:27:13 +08:00
|
|
|
if spec.exclude?(":") || focus[1].exclude?(spec.split(":")[0])
|
2017-06-09 06:02:30 +08:00
|
|
|
@queue.unshift(focus) if focus[1].include?(spec) || file != spec
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@queue.unshift([file, spec, runner])
|
|
|
|
end
|
2017-05-03 04:02:00 +08:00
|
|
|
|
|
|
|
# push run all specs to end of queue in correct order
|
2019-11-08 22:22:57 +08:00
|
|
|
ensure_all_specs_will_run(runner) if @auto_run_all
|
2013-11-02 06:57:50 +08:00
|
|
|
end
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ specs queued" if @debug
|
|
|
|
puts "@@@@@@@@@@@@ #{@queue}" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
@signal.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_queue
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ process_queue" if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
if @queue.length == 0
|
2013-11-05 18:01:17 +08:00
|
|
|
puts "@@@@@@@@@@@@ queue is empty..." if @debug
|
2013-11-02 06:57:50 +08:00
|
|
|
ensure_all_specs_will_run
|
|
|
|
@signal.signal
|
|
|
|
else
|
|
|
|
current = @queue.first
|
|
|
|
runner = current[2]
|
|
|
|
specs = runner.failed_specs
|
|
|
|
puts
|
|
|
|
puts
|
|
|
|
if specs.length == 0
|
2019-11-08 22:23:12 +08:00
|
|
|
puts "No specs have failed yet! Aborting anyway"
|
2013-11-02 06:57:50 +08:00
|
|
|
puts
|
2019-11-08 22:23:12 +08:00
|
|
|
abort_runners
|
2013-11-02 06:57:50 +08:00
|
|
|
else
|
|
|
|
puts "The following specs have failed:"
|
|
|
|
specs.each { |s| puts s }
|
|
|
|
puts
|
|
|
|
specs = specs.map { |s| [s, s, runner] }
|
|
|
|
queue_specs(specs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail(exception, message = nil)
|
|
|
|
puts message if message
|
|
|
|
puts exception.message
|
|
|
|
puts exception.backtrace.join("\n")
|
|
|
|
end
|
|
|
|
end
|