Skip to content

Commit

Permalink
Don't fallback to polling
Browse files Browse the repository at this point in the history
If the user has specified `Spring.watch_method = :listen`, but listen is
not available, just let it throw a `LoadError`.
  • Loading branch information
jonleighton committed Jun 14, 2013
1 parent 4a783d7 commit 20adbef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 51 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ source 'https://rubygems.org'
gemspec

gem 'listen', :require => false
gem 'rb-inotify', :require => false
gem 'rb-fsevent', :require => false
38 changes: 16 additions & 22 deletions lib/spring/watcher.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
require "spring/watcher/abstract"
require "spring/watcher/listen"
require "spring/watcher/polling"

module Spring
class << self
attr_accessor :watch_interval, :watch_method
attr_accessor :watch_interval
attr_writer :watcher
attr_reader :watch_method
end

def self.watch_method=(method)
case method
when :polling
require_relative "watcher/polling"
@watch_method = Watcher::Polling
when :listen
require_relative "watcher/listen"
@watch_method = Watcher::Listen
else
@watch_method = method
end
end

self.watch_interval = 0.2
self.watch_method = :polling

def self.watcher
@watcher ||= watcher_class.new(Spring.application_root_path, watch_interval)
end

def self.watcher_class
if watch_method.to_s == 'listen'
if Watcher::Listen.available?
Watcher::Listen
else
puts %Q{Listen gem was not found, please add this to your Gemfile. `gem 'listen', group: ['test','development']`
Falling back to Wather::Polling}
Watcher::Polling
end
elsif watch_method.to_s == 'polling'
Watcher::Polling
elsif watch_method.kind_of?(Class)
watch_method
else
raise NotImplementedError
end
@watcher ||= watch_method.new(Spring.application_root_path, watch_interval)
end

def self.watch(*items)
Expand Down
11 changes: 3 additions & 8 deletions lib/spring/watcher/listen.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
require "listen"
require "listen/version"

module Spring
module Watcher
class Listen < Abstract
attr_reader :listener

def self.available?
require "listen"
require "listen/version"
true
rescue LoadError
false
end

def start
unless @listener
@listener = ::Listen.to(*base_directories, relative_paths: false)
Expand Down
5 changes: 1 addition & 4 deletions test/apps/rails-3-2/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ gem 'rails', '~> 3.2.0'
gem 'sqlite3'
gem 'rspec'

# gem 'listen'
gem 'rb-inotify', :require => false # linux
gem 'rb-fsevent', :require => false # mac os x
gem 'rb-kqueue', :require => false # bsd
# gem 'listen', '~> 1.0'
19 changes: 4 additions & 15 deletions test/unit/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "fileutils"
require "active_support/core_ext/numeric/time"
require "spring/watcher"

raise "listen not loaded" unless Spring::Watcher::Listen.available?
require "spring/watcher/polling"
require "spring/watcher/listen"

module WatcherTests
LATENCY = 0.001
Expand Down Expand Up @@ -141,8 +141,7 @@ class ListenWatcherTest < ActiveSupport::TestCase
include WatcherTests

def watcher_class
Spring.watch_method = :listen
Spring.watcher_class
Spring::Watcher::Listen
end

test "root directories" do
Expand All @@ -168,16 +167,6 @@ class PollingWatcherTest < ActiveSupport::TestCase
include WatcherTests

def watcher_class
Spring.watch_method = :polling
Spring.watcher_class
end
end

class CustomWatcherTest < ActiveSupport::TestCase
class DummyListener; end

def test_watcher_class
Spring.watch_method = DummyListener
assert_equal Spring.watcher_class, DummyListener
Spring::Watcher::Polling
end
end

0 comments on commit 20adbef

Please sign in to comment.