From eb9c1f28ed84c578ca683f65c8fc1a9feefdc874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 5 Nov 2013 11:01:17 +0100 Subject: [PATCH] add debug mode to autospec --- config/environments/test.rb | 8 ++++--- lib/autospec/manager.rb | 48 ++++++++++++++++++++++++++++++------- lib/tasks/autospec.rake | 8 ++++--- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/config/environments/test.rb b/config/environments/test.rb index f514a8e65..460911dae 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -10,9 +10,6 @@ Discourse::Application.configure do # Configure static asset server for tests with Cache-Control for performance config.serve_static_assets = true - # Log error messages when you accidentally call methods on nil - config.whiny_nils = true - # Show full error reports and disable caching config.consider_all_requests_local = true config.action_controller.perform_caching = false @@ -39,4 +36,9 @@ Discourse::Application.configure do # lower iteration count for test config.pbkdf2_iterations = 10 config.ember.variant = :development + + # silence deprecation warnings in test + config.whiny_nils = true unless rails4? + config.eager_load = false if rails4? + ActiveSupport::Deprecation.silenced = true end diff --git a/lib/autospec/manager.rb b/lib/autospec/manager.rb index 2d9760833..76eae8f55 100644 --- a/lib/autospec/manager.rb +++ b/lib/autospec/manager.rb @@ -9,18 +9,19 @@ module Autospec; end class Autospec::Manager def self.run(opts={}) - self.new.run(opts) + self.new(opts).run end - def initialize + def initialize(opts = {}) + @opts = opts + @debug = opts[:debug] @queue = [] @mutex = Mutex.new @signal = ConditionVariable.new + @runners = [ruby_runner, javascript_runner] end - def run(opts = {}) - @runners = [ruby_runner, javascript_runner] - + def run Signal.trap("HUP") { stop_runners; exit } Signal.trap("INT") { stop_runners; exit } @@ -59,6 +60,7 @@ class Autospec::Manager end def ensure_all_specs_will_run + puts "@@@@@@@@@@@@ ensure_all_specs_will_run" if @debug @runners.each do |runner| @queue << ['spec', 'spec', runner] unless @queue.any? { |f, s, r| s == "spec" && r == runner } end @@ -66,11 +68,13 @@ class Autospec::Manager [:start, :stop, :abort].each do |verb| define_method("#{verb}_runners") do + puts "@@@@@@@@@@@@ #{verb}_runners" if @debug @runners.each(&verb) end end def start_service_queue + puts "@@@@@@@@@@@@ start_service_queue" if @debug Thread.new do while true thread_loop @@ -80,11 +84,17 @@ class Autospec::Manager # the main loop, will run the specs in the queue till one fails or the queue is empty def thread_loop + puts "@@@@@@@@@@@@ thread_loop" if @debug @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 + if @debug + puts "@@@@@@@@@@@@ waiting because..." + puts "@@@@@@@@@@@@ ...current spec has failed" if last_failed + puts "@@@@@@@@@@@@ ...queue is empty" if @queue.length == 0 + end @signal.wait(@mutex) if @queue.length == 0 || last_failed end rescue => e @@ -93,6 +103,7 @@ class Autospec::Manager # will actually run the spec and check whether the spec has failed or not def process_spec(current) + puts "@@@@@@@@@@@@ process_spec --> #{current}" if @debug has_failed = false # retrieve the instance of the runner runner = current[2] @@ -100,9 +111,11 @@ class Autospec::Manager result = runner.run(current[1]).to_i if result == 0 + puts "@@@@@@@@@@@@ success" if @debug # remove the spec from the queue @queue.shift else + puts "@@@@@@@@@@@@ failure" if @debug has_failed = true if result > 0 focus_on_failed_tests(current) @@ -114,6 +127,7 @@ class Autospec::Manager end def focus_on_failed_tests(current) + puts "@@@@@@@@@@@@ focus_on_failed_tests --> #{current}" if @debug runner = current[2] # we only want 1 focus in the queue @queue.shift if current[0] == "focus" @@ -123,15 +137,17 @@ class Autospec::Manager @queue.unshift ["focus", failed_specs.join(" "), runner] if failed_specs.length > 0 end - def listen_for_changes(opts = {}) + def listen_for_changes + puts "@@@@@@@@@@@@ listen_for_changes" if @debug + options = { ignore: /^public|^lib\/autospec/, relative_paths: true, } - if opts[:force_polling] + if @opts[:force_polling] options[:force_polling] = true - options[:latency] = opts[:latency] || 3 + options[:latency] = @opts[:latency] || 3 end Thread.start do @@ -143,6 +159,9 @@ class Autospec::Manager def process_change(files) return if files.length == 0 + + puts "@@@@@@@@@@@@ process_change --> #{files}" if @debug + specs = [] hit = false @@ -151,6 +170,7 @@ class Autospec::Manager # reloaders runner.reloaders.each do |k| if k.match(file) + puts "@@@@@@@@@@@@ #{file} matched a reloader for #{runner}" if @debug runner.reload return end @@ -158,6 +178,7 @@ class Autospec::Manager # watchers runner.watchers.each do |k,v| if m = k.match(file) + puts "@@@@@@@@@@@@ #{file} matched a watcher for #{runner}" if @debug hit = true spec = v ? (v.arity == 1 ? v.call(m) : v.call) : file specs << [file, spec, runner] if File.exists?(spec) || Dir.exists?(spec) @@ -179,6 +200,8 @@ class Autospec::Manager end def queue_specs(specs) + puts "@@@@@@@@@@@@ queue_specs --> #{specs}" if @debug + if specs.length == 0 locked = @mutex.try_lock if locked @@ -190,7 +213,10 @@ class Autospec::Manager abort_runners end + puts "@@@@@@@@@@@@ waiting for the mutex" if @debug @mutex.synchronize do + puts "@@@@@@@@@@@@ queueing specs" if @debug + puts "@@@@@@@@@@@@ #{@queue}" if @debug specs.each do |file, spec, runner| # make sure there's no other instance of this spec in the queue @queue.delete_if { |f, s, r| s.strip == spec.strip && r == runner } @@ -205,12 +231,16 @@ class Autospec::Manager @queue.unshift([file, spec, runner]) end end + puts "@@@@@@@@@@@@ specs queued" if @debug + puts "@@@@@@@@@@@@ #{@queue}" if @debug @signal.signal end end def process_queue + puts "@@@@@@@@@@@@ process_queue" if @debug if @queue.length == 0 + puts "@@@@@@@@@@@@ queue is empty..." if @debug ensure_all_specs_will_run @signal.signal else @@ -220,7 +250,7 @@ class Autospec::Manager puts puts if specs.length == 0 - puts "No specs have failed yet!" + puts "No specs have failed yet! " puts else puts "The following specs have failed:" diff --git a/lib/tasks/autospec.rake b/lib/tasks/autospec.rake index 43455ac85..836aa51fc 100644 --- a/lib/tasks/autospec.rake +++ b/lib/tasks/autospec.rake @@ -6,8 +6,9 @@ desc "Run all specs automatically as needed" task "autospec" => :environment do require 'autospec/manager' - force_polling = ARGV.any?{ |a| a == "p" || a == "polling" } - latency = ((ARGV.find{ |a| a =~ /l=|latency=/ } || "").split("=")[1] || 3).to_i + debug = ARGV.any? { |a| a == "d" || a == "debug" } + force_polling = ARGV.any? { |a| a == "p" || a == "polling" } + latency = ((ARGV.find { |a| a =~ /l=|latency=/ } || "").split("=")[1] || 3).to_i if force_polling puts "Polling has been forced (slower) - checking every #{latency} #{"second".pluralize(latency)}" @@ -15,6 +16,7 @@ task "autospec" => :environment do puts "If file watching is not working, you can force polling with: bundle exec rake autospec p l=3" end - Autospec::Manager.run(force_polling: force_polling, latency: latency) + puts "@@@@@@@@@@@@ Running in debug mode" if debug + Autospec::Manager.run(force_polling: force_polling, latency: latency, debug: debug) end