From 8b419794f93b275ed6b4aa62b86dd647d8ca39dc Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Wed, 1 May 2013 10:29:39 +1000
Subject: [PATCH] added option to force polling instead of messing with the
 detection

---
 lib/autospec/runner.rb  | 20 +++++++-------------
 lib/tasks/autospec.rake | 11 ++++++++++-
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/lib/autospec/runner.rb b/lib/autospec/runner.rb
index 9063c2ff5f6..d03afd09e22 100644
--- a/lib/autospec/runner.rb
+++ b/lib/autospec/runner.rb
@@ -25,8 +25,8 @@ class Autospec::Runner
   watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
 
 
-  def self.run
-    self.new.run
+  def self.run(opts={})
+    self.new.run(opts)
   end
 
   def initialize
@@ -36,7 +36,7 @@ class Autospec::Runner
     start_service_queue
   end
 
-  def run
+  def run(opts = {})
     if already_running?(pid_file)
       puts "autospec appears to be running, it is possible the pid file is old"
       puts "if you are sure it is not running, delete #{pid_file}"
@@ -44,23 +44,17 @@ class Autospec::Runner
     end
     write_pid_file(pid_file, Process.pid)
 
-    # launching spork is forever going to take longer than this test
-    force_polling = true
-    Thread.new do
-      force_polling = force_polling?
-    end
-
     start_spork
     Signal.trap("HUP") {stop_spork; exit }
     Signal.trap("SIGINT") {stop_spork; exit }
 
-    puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if force_polling
+    puts "Forced polling (slower) - inotify does not work on network filesystems, use local filesystem to avoid" if opts[:force_polling]
 
     options = {filter: /^app|^spec|^lib/, relative_paths: true}
 
-    if force_polling
-      options[:force_polling] = force_polling
-      options[:latency] = 3
+    if opts[:force_polling]
+      options[:force_polling] = true
+      options[:latency] = opts[:latency] || 3
     end
 
     Thread.start do
diff --git a/lib/tasks/autospec.rake b/lib/tasks/autospec.rake
index 20c5db253f1..d0a623e7897 100644
--- a/lib/tasks/autospec.rake
+++ b/lib/tasks/autospec.rake
@@ -4,6 +4,15 @@
 
 desc "Run all specs automatically as needed"
 task "autospec" => :environment do
+  puts "If file watching is not working you can force polling with: bundle exec rake autospec p l=3"
   require 'autospec/runner'
-  Autospec::Runner.run
+
+  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)}"
+  end
+
+  Autospec::Runner.run(force_polling: force_polling, latency: latency)
 end