From e21066c3d51624947676dc8327dbdefa0290ed42 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Sat, 23 Apr 2016 00:03:27 +0200 Subject: [PATCH] fix lots of RuboCop offenses --- lib/listen.rb | 5 +-- lib/listen/adapter.rb | 52 +++++++++++++------------ lib/listen/adapter/base.rb | 10 +++-- lib/listen/adapter/bsd.rb | 6 +-- lib/listen/adapter/darwin.rb | 4 +- lib/listen/adapter/linux.rb | 8 ++-- lib/listen/adapter/polling.rb | 2 +- lib/listen/adapter/windows.rb | 11 +++--- lib/listen/backend.rb | 15 +++---- lib/listen/change.rb | 18 ++++----- lib/listen/event/config.rb | 8 +--- lib/listen/event/queue.rb | 11 ++---- lib/listen/file.rb | 2 +- lib/listen/fsm.rb | 3 +- lib/listen/listener/config.rb | 10 ++--- lib/listen/queue_optimizer.rb | 8 ++-- lib/listen/record.rb | 4 +- lib/listen/record/entry.rb | 4 +- lib/listen/record/symlink_detector.rb | 4 +- lib/listen/silencer.rb | 4 +- lib/listen/version.rb | 2 +- listen.gemspec | 4 +- spec/acceptance/listen_spec.rb | 1 - spec/lib/listen/adapter/base_spec.rb | 1 - spec/lib/listen/adapter/config_spec.rb | 2 +- spec/lib/listen/adapter/darwin_spec.rb | 4 +- spec/lib/listen/adapter_spec.rb | 1 - spec/lib/listen/change_spec.rb | 1 - spec/lib/listen/event/loop_spec.rb | 2 +- spec/lib/listen/event/processor_spec.rb | 4 +- spec/lib/listen/file_spec.rb | 34 ++++++++-------- spec/lib/listen/listener/config_spec.rb | 2 +- spec/lib/listen/listener_spec.rb | 5 +-- spec/lib/listen/record_spec.rb | 10 ++--- spec/lib/listen/silencer_spec.rb | 4 +- spec/support/acceptance_helper.rb | 10 ++--- 36 files changed, 125 insertions(+), 151 deletions(-) diff --git a/lib/listen.rb b/lib/listen.rb index 57d4a1ad..a4888432 100644 --- a/lib/listen.rb +++ b/lib/listen.rb @@ -45,10 +45,7 @@ def stop @listeners ||= [] # TODO: should use a mutex for this - @listeners.each do |listener| - # call stop to halt the main loop - listener.stop - end + @listeners.each(&:stop) @listeners = nil end end diff --git a/lib/listen/adapter.rb b/lib/listen/adapter.rb index 288bcb45..a1954d90 100644 --- a/lib/listen/adapter.rb +++ b/lib/listen/adapter.rb @@ -7,37 +7,39 @@ module Listen module Adapter - OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows] + OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows].freeze POLLING_FALLBACK_MESSAGE = 'Listen will be polling for changes.'\ - 'Learn more at https://github.com/guard/listen#listen-adapters.' + 'Learn more at https://github.com/guard/listen#listen-adapters.'.freeze - def self.select(options = {}) - _log :debug, 'Adapter: considering polling ...' - return Polling if options[:force_polling] - _log :debug, 'Adapter: considering optimized backend...' - return _usable_adapter_class if _usable_adapter_class - _log :debug, 'Adapter: falling back to polling...' - _warn_polling_fallback(options) - Polling - rescue - _log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect, - $ERROR_POSITION * "\n") - raise - end + class << self + def select(options = {}) + _log :debug, 'Adapter: considering polling ...' + return Polling if options[:force_polling] + _log :debug, 'Adapter: considering optimized backend...' + return _usable_adapter_class if _usable_adapter_class + _log :debug, 'Adapter: falling back to polling...' + _warn_polling_fallback(options) + Polling + rescue + _log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect, + $ERROR_POSITION * "\n") + raise + end - private + private - def self._usable_adapter_class - OPTIMIZED_ADAPTERS.detect(&:usable?) - end + def _usable_adapter_class + OPTIMIZED_ADAPTERS.detect(&:usable?) + end - def self._warn_polling_fallback(options) - msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE) - Kernel.warn "[Listen warning]:\n #{msg}" if msg - end + def _warn_polling_fallback(options) + msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE) + Kernel.warn "[Listen warning]:\n #{msg}" if msg + end - def self._log(type, message) - Listen::Logger.send(type, message) + def _log(type, message) + Listen::Logger.send(type, message) + end end end end diff --git a/lib/listen/adapter/base.rb b/lib/listen/adapter/base.rb index 2f59d9b3..e33349a5 100644 --- a/lib/listen/adapter/base.rb +++ b/lib/listen/adapter/base.rb @@ -8,7 +8,7 @@ class Base attr_reader :options # TODO: only used by tests - DEFAULTS = {} + DEFAULTS = {}.freeze attr_reader :config @@ -129,8 +129,12 @@ def _log_exception(msg, caller_stack) _log(:error, formatted) end - def self._log(*args, &block) - Listen::Logger.send(*args, &block) + class << self + private + + def _log(*args, &block) + Listen::Logger.send(*args, &block) + end end end end diff --git a/lib/listen/adapter/bsd.rb b/lib/listen/adapter/bsd.rb index b5590492..7db424f9 100644 --- a/lib/listen/adapter/bsd.rb +++ b/lib/listen/adapter/bsd.rb @@ -16,7 +16,7 @@ class BSD < Base :rename # :link, :revoke ] - } + }.freeze BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '') Please add the following to your Gemfile to avoid polling for changes: @@ -38,9 +38,9 @@ def self.usable? private - def _configure(directory, &_callback) + def _configure(directory, &callback) @worker ||= KQueue::Queue.new - @callback = _callback + @callback = callback # use Record to make a snapshot of dir, so we # can detect new files _find(directory.to_s) { |path| _watch_file(path, @worker) } diff --git a/lib/listen/adapter/darwin.rb b/lib/listen/adapter/darwin.rb index afffc4f6..d65779f4 100644 --- a/lib/listen/adapter/darwin.rb +++ b/lib/listen/adapter/darwin.rb @@ -9,7 +9,7 @@ class Darwin < Base OS_REGEXP = /darwin(?1\d+)/i # The default delay between checking for changes. - DEFAULTS = { latency: 0.1 } + DEFAULTS = { latency: 0.1 }.freeze INCOMPATIBLE_GEM_VERSION = <<-EOS.gsub(/^ {8}/, '') rb-fsevent > 0.9.4 no longer supports OS X 10.6 through 10.8. @@ -58,7 +58,7 @@ def _run def _process_event(dir, event) _log :debug, "fsevent: processing event: #{event.inspect}" event.each do |path| - new_path = Pathname.new(path.sub(/\/$/, '')) + new_path = Pathname.new(path.sub(%r{\/$}, '')) _log :debug, "fsevent: #{new_path}" # TODO: does this preserve symlinks? rel_path = new_path.relative_path_from(dir).to_s diff --git a/lib/listen/adapter/linux.rb b/lib/listen/adapter/linux.rb index 54564d5d..ec5fe6d1 100644 --- a/lib/listen/adapter/linux.rb +++ b/lib/listen/adapter/linux.rb @@ -14,12 +14,12 @@ class Linux < Base :close_write ], wait_for_delay: 0.1 - } + }.freeze private WIKI_URL = 'https://github.com/guard/listen'\ - '/wiki/Increasing-the-amount-of-inotify-watchers' + '/wiki/Increasing-the-amount-of-inotify-watchers'.freeze INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '') FATAL: Listen error: unable to monitor directories for changes. @@ -50,10 +50,8 @@ def _process_event(dir, event) if /1|true/ =~ ENV['LISTEN_GEM_SIMULATE_FSEVENT'] if (event.flags & [:moved_to, :moved_from]) || _dir_event?(event) rel_path = path.dirname.relative_path_from(dir).to_s - _queue_change(:dir, dir, rel_path, {}) - else - _queue_change(:dir, dir, rel_path, {}) end + _queue_change(:dir, dir, rel_path, {}) return end diff --git a/lib/listen/adapter/polling.rb b/lib/listen/adapter/polling.rb index d1d57be6..486eafe4 100644 --- a/lib/listen/adapter/polling.rb +++ b/lib/listen/adapter/polling.rb @@ -8,7 +8,7 @@ module Adapter class Polling < Base OS_REGEXP = // # match every OS - DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 } + DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }.freeze private diff --git a/lib/listen/adapter/windows.rb b/lib/listen/adapter/windows.rb index b15fb4c5..35dcca7b 100644 --- a/lib/listen/adapter/windows.rb +++ b/lib/listen/adapter/windows.rb @@ -24,21 +24,21 @@ def self.usable? private - def _configure(dir, &callback) + def _configure(dir) require 'wdm' _log :debug, 'wdm - starting...' @worker ||= WDM::Monitor.new @worker.watch_recursively(dir.to_s, :files) do |change| - callback.call([:file, change]) + yield([:file, change]) end @worker.watch_recursively(dir.to_s, :directories) do |change| - callback.call([:dir, change]) + yield([:dir, change]) end events = [:attributes, :last_write] @worker.watch_recursively(dir.to_s, *events) do |change| - callback.call([:attr, change]) + yield([:attr, change]) end end @@ -70,7 +70,6 @@ def _process_event(dir, event) _queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {}) elsif change.type == :added _queue_change(:dir, dir, rel_path, {}) - else # do nothing - changed directory means either: # - removed subdirs (handled above) # - added subdirs (handled above) @@ -81,7 +80,7 @@ def _process_event(dir, event) end rescue details = event.inspect - _log :error, format('wdm - callback (%): %s:%s', details, $ERROR_INFO, + _log :error, format('wdm - callback (%s): %s:%s', details, $ERROR_INFO, $ERROR_POSITION * "\n") raise end diff --git a/lib/listen/backend.rb b/lib/listen/backend.rb index 21125407..97a029c4 100644 --- a/lib/listen/backend.rb +++ b/lib/listen/backend.rb @@ -6,6 +6,8 @@ # from exploding with huge test setup blocks module Listen class Backend + extend Forwardable + def initialize(directories, queue, silencer, config) adapter_select_opts = config.adapter_select_options @@ -22,17 +24,10 @@ def initialize(directories, queue, silencer, config) @adapter = adapter_class.new(aconfig) end - def start - adapter.start - end + delegate start: :adapter + delegate stop: :adapter - def stop - adapter.stop - end - - def min_delay_between_events - @min_delay_between_events - end + attr_reader :min_delay_between_events private diff --git a/lib/listen/change.rb b/lib/listen/change.rb index b73028d7..3d49c79e 100644 --- a/lib/listen/change.rb +++ b/lib/listen/change.rb @@ -35,7 +35,7 @@ def invalidate(type, rel_path, options) cookie = options[:cookie] if !cookie && config.silenced?(rel_path, type) - Listen::Logger.debug { "(silenced): #{rel_path.inspect}" } + Listen::Logger.debug { "(silenced): #{rel_path.inspect}" } return end @@ -49,16 +49,14 @@ def invalidate(type, rel_path, options) if change options = cookie ? { cookie: cookie } : {} config.queue(type, change, watched_dir, rel_path, options) + elsif type == :dir + # NOTE: POSSIBLE RECURSION + # TODO: fix - use a queue instead + Directory.scan(self, rel_path, options) else - if type == :dir - # NOTE: POSSIBLE RECURSION - # TODO: fix - use a queue instead - Directory.scan(self, rel_path, options) - else - change = File.change(record, rel_path) - return if !change || options[:silence] - config.queue(:file, change, watched_dir, rel_path) - end + change = File.change(record, rel_path) + return if !change || options[:silence] + config.queue(:file, change, watched_dir, rel_path) end rescue RuntimeError => ex msg = format( diff --git a/lib/listen/event/config.rb b/lib/listen/event/config.rb index ee13bd65..d49a8f25 100644 --- a/lib/listen/event/config.rb +++ b/lib/listen/event/config.rb @@ -27,9 +27,7 @@ def timestamp Time.now.to_f end - def event_queue - @event_queue - end + attr_reader :event_queue def callable? @block @@ -39,9 +37,7 @@ def optimize_changes(changes) @queue_optimizer.smoosh_changes(changes) end - def min_delay_between_events - @min_delay_between_events - end + attr_reader :min_delay_between_events def stopped? listener.state == :stopped diff --git a/lib/listen/event/queue.rb b/lib/listen/event/queue.rb index 553d3ffe..06ffaa13 100644 --- a/lib/listen/event/queue.rb +++ b/lib/listen/event/queue.rb @@ -3,6 +3,8 @@ module Listen module Event class Queue + extend Forwardable + class Config def initialize(relative) @relative = relative @@ -31,13 +33,8 @@ def <<(args) block.call(args) if block end - def empty? - event_queue.empty? - end - - def pop - event_queue.pop - end + delegate empty?: :event_queue + delegate pop: :event_queue private diff --git a/lib/listen/file.rb b/lib/listen/file.rb index d5dbc93e..32fc6c39 100644 --- a/lib/listen/file.rb +++ b/lib/listen/file.rb @@ -26,7 +26,7 @@ def self.change(record, rel_path) end return if /1|true/ =~ ENV['LISTEN_GEM_DISABLE_HASHING'] - return unless self.inaccurate_mac_time?(lstat) + return unless inaccurate_mac_time?(lstat) # Check if change happened within 1 second (maybe it's even # too much, e.g. 0.3-0.5 could be sufficient). diff --git a/lib/listen/fsm.rb b/lib/listen/fsm.rb index 88946f23..882cc00a 100644 --- a/lib/listen/fsm.rb +++ b/lib/listen/fsm.rb @@ -111,7 +111,8 @@ class State attr_reader :name, :transitions def initialize(name, transitions = nil, &block) - @name, @block = name, block + @name = name + @block = block @transitions = nil @transitions = Array(transitions).map(&:to_sym) if transitions end diff --git a/lib/listen/listener/config.rb b/lib/listen/listener/config.rb index 8db77a4f..d1353227 100644 --- a/lib/listen/listener/config.rb +++ b/lib/listen/listener/config.rb @@ -10,7 +10,7 @@ class Config # Backend selecting options force_polling: false, polling_fallback_message: nil - } + }.freeze def initialize(opts) @options = DEFAULTS.merge(opts) @@ -23,13 +23,9 @@ def relative? @relative end - def min_delay_between_events - @min_delay_between_events - end + attr_reader :min_delay_between_events - def silencer_rules - @silencer_rules - end + attr_reader :silencer_rules def adapter_instance_options(klass) valid_keys = klass.const_get('DEFAULTS').keys diff --git a/lib/listen/queue_optimizer.rb b/lib/listen/queue_optimizer.rb index 4fc7b890..752297a7 100644 --- a/lib/listen/queue_optimizer.rb +++ b/lib/listen/queue_optimizer.rb @@ -88,7 +88,7 @@ def _calculate_add_remove_difference(actions, path, default_if_exists) # editor rename() call (e.g. Kate and Sublime) def _reinterpret_related_changes(cookies) table = { moved_to: :added, moved_from: :removed } - cookies.map do |_, changes| + cookies.flat_map do |_, changes| data = _detect_possible_editor_save(changes) if data to_dir, to_file = data @@ -101,7 +101,7 @@ def _reinterpret_related_changes(cookies) [table.fetch(change, change), dir, path] end end - end.flatten(1) + end end def _detect_possible_editor_save(changes) @@ -113,9 +113,9 @@ def _detect_possible_editor_save(changes) changes.each do |data| case data[1] when :moved_from - from_type, from_change, _, from, _ = data + from_type, from_change, _, from, = data when :moved_to - to_type, to_change, to_dir, to, _ = data + to_type, to_change, to_dir, to, = data else return nil end diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 70e37970..ad374cec 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -74,9 +74,7 @@ def _auto_hash Hash.new { |h, k| h[k] = Hash.new } end - def tree - @tree - end + attr_reader :tree def _fast_update_file(dirname, basename, data) if [nil, '', '.'].include? dirname diff --git a/lib/listen/record/entry.rb b/lib/listen/record/entry.rb index 1080977c..1fcabb04 100644 --- a/lib/listen/record/entry.rb +++ b/lib/listen/record/entry.rb @@ -6,7 +6,9 @@ class Entry # file: "/home/me/watched_dir", "app/models", "foo.rb" # dir, "/home/me/watched_dir", "." def initialize(root, relative, name = nil) - @root, @relative, @name = root, relative, name + @root = root + @relative = relative + @name = name end attr_reader :root, :relative, :name diff --git a/lib/listen/record/symlink_detector.rb b/lib/listen/record/symlink_detector.rb index e021a1ae..58840925 100644 --- a/lib/listen/record/symlink_detector.rb +++ b/lib/listen/record/symlink_detector.rb @@ -4,9 +4,9 @@ module Listen # @private api class Record class SymlinkDetector - WIKI = 'https://github.com/guard/listen/wiki/Duplicate-directory-errors' + WIKI = 'https://github.com/guard/listen/wiki/Duplicate-directory-errors'.freeze - SYMLINK_LOOP_ERROR = <<-EOS + SYMLINK_LOOP_ERROR = <<-EOS.freeze ** ERROR: directory is already being watched! ** Directory: %s diff --git a/lib/listen/silencer.rb b/lib/listen/silencer.rb index 3c9f84b0..4addd34a 100644 --- a/lib/listen/silencer.rb +++ b/lib/listen/silencer.rb @@ -15,7 +15,7 @@ class Silencer )(/|$)}x # The default list of files that get ignored. - DEFAULT_IGNORED_EXTENSIONS = /(?: + DEFAULT_IGNORED_EXTENSIONS = %r{(?: # Kate's tmp\/swp files \..*\d+\.new | \.kate-swp @@ -50,7 +50,7 @@ class Silencer | \.DS_Store | \.tmp | ~ - )$/x + )$}x attr_accessor :only_patterns, :ignore_patterns diff --git a/lib/listen/version.rb b/lib/listen/version.rb index 6da3a21d..172c7f26 100644 --- a/lib/listen/version.rb +++ b/lib/listen/version.rb @@ -1,3 +1,3 @@ module Listen - VERSION = '3.0.6' + VERSION = '3.0.6'.freeze end diff --git a/listen.gemspec b/listen.gemspec index 2a4429bd..f478e6d6 100644 --- a/listen.gemspec +++ b/listen.gemspec @@ -14,8 +14,8 @@ Gem::Specification.new do |s| s.description = 'The Listen gem listens to file modifications and '\ 'notifies you about the changes. Works everywhere!' - s.files = `git ls-files -z`.split("\x0").select do |f| - /^(?:bin|lib)\// =~ f + s.files = `git ls-files -z`.split("\x0").select do |f| + %r{^(?:bin|lib)\/} =~ f end + %w(CHANGELOG.md CONTRIBUTING.md LICENSE.txt README.md) s.test_files = [] diff --git a/spec/acceptance/listen_spec.rb b/spec/acceptance/listen_spec.rb index 3eaad602..ca1c5ac2 100644 --- a/spec/acceptance/listen_spec.rb +++ b/spec/acceptance/listen_spec.rb @@ -65,7 +65,6 @@ context 'with default ignore options' do context 'with nothing in listen dir' do - it { is_expected.to process_addition_of('file.rb') } it { is_expected.to process_addition_of('.hidden') } diff --git a/spec/lib/listen/adapter/base_spec.rb b/spec/lib/listen/adapter/base_spec.rb index 7d0741f3..f9df49cd 100644 --- a/spec/lib/listen/adapter/base_spec.rb +++ b/spec/lib/listen/adapter/base_spec.rb @@ -1,5 +1,4 @@ RSpec.describe Listen::Adapter::Base do - class FakeAdapter < described_class def initialize(config) @my_callbacks = {} diff --git a/spec/lib/listen/adapter/config_spec.rb b/spec/lib/listen/adapter/config_spec.rb index 9de5c43c..4a3315cc 100644 --- a/spec/lib/listen/adapter/config_spec.rb +++ b/spec/lib/listen/adapter/config_spec.rb @@ -63,7 +63,7 @@ end context 'when not resolved' do - let(:directories) { ['symlinked_dir1', 'symlinked_dir2'] } + let(:directories) { %w(symlinked_dir1 symlinked_dir2) } it 'returns array of resolved pathnames' do expect(subject.directories).to eq([real_path1, real_path2]) end diff --git a/spec/lib/listen/adapter/darwin_spec.rb b/spec/lib/listen/adapter/darwin_spec.rb index a75d3e4c..1c156492 100644 --- a/spec/lib/listen/adapter/darwin_spec.rb +++ b/spec/lib/listen/adapter/darwin_spec.rb @@ -160,9 +160,7 @@ Timeout.timeout(max_test_time) do subject.start - until started.size == expectations.size - sleep 0.1 - end + sleep 0.1 until started.size == expectations.size end running << started.pop until started.empty? diff --git a/spec/lib/listen/adapter_spec.rb b/spec/lib/listen/adapter_spec.rb index 6b1fd92c..e894ab3b 100644 --- a/spec/lib/listen/adapter_spec.rb +++ b/spec/lib/listen/adapter_spec.rb @@ -1,5 +1,4 @@ RSpec.describe Listen::Adapter do - let(:listener) { instance_double(Listen::Listener, options: {}) } before do allow(Listen::Adapter::BSD).to receive(:usable?) { false } diff --git a/spec/lib/listen/change_spec.rb b/spec/lib/listen/change_spec.rb index 95598c4c..73bed147 100644 --- a/spec/lib/listen/change_spec.rb +++ b/spec/lib/listen/change_spec.rb @@ -42,7 +42,6 @@ end context 'with unknown change' do - it 'calls Listen::File#change' do expect(Listen::File).to receive(:change).with(record, 'file.rb') subject.invalidate(:file, 'file.rb', {}) diff --git a/spec/lib/listen/event/loop_spec.rb b/spec/lib/listen/event/loop_spec.rb index 959ab359..03984f81 100644 --- a/spec/lib/listen/event/loop_spec.rb +++ b/spec/lib/listen/event/loop_spec.rb @@ -48,7 +48,7 @@ allow(subject).to receive(:_nice_error) do |ex| indent = "\n -- " - backtrace = ex.backtrace.reject { |line| line =~ /\/gems\// } + backtrace = ex.backtrace.reject { |line| line =~ %r{\/gems\/} } fail "error called: #{ex}: #{indent}#{backtrace * indent}" end end diff --git a/spec/lib/listen/event/processor_spec.rb b/spec/lib/listen/event/processor_spec.rb index a0301aee..fd459355 100644 --- a/spec/lib/listen/event/processor_spec.rb +++ b/spec/lib/listen/event/processor_spec.rb @@ -61,9 +61,9 @@ def status_for_time(time) it 'does not sleep' do expect(config).to_not receive(:sleep) - t = Time.now + t = Time.now.to_f subject.loop_for(1) - diff = Time.now.to_f - t.to_f + diff = Time.now.to_f - t expect(diff).to be < 0.01 end end diff --git a/spec/lib/listen/file_spec.rb b/spec/lib/listen/file_spec.rb index d20c4c2f..c5fa5b41 100644 --- a/spec/lib/listen/file_spec.rb +++ b/spec/lib/listen/file_spec.rb @@ -108,17 +108,17 @@ let(:record_mtime) { stat_mtime.to_f } context 'with accurate stat times' do - let(:stat_mtime) { Time.at(1_401_235_714.123) } - let(:stat_atime) { Time.at(1_401_235_714.123) } - let(:stat_ctime) { Time.at(1_401_235_714.123) } + let(:stat_mtime) { Time.at(1_401_235_714.123).utc } + let(:stat_atime) { Time.at(1_401_235_714.123).utc } + let(:stat_ctime) { Time.at(1_401_235_714.123).utc } let(:record_mtime) { stat_mtime.to_f } it { should be_nil } end context 'with inaccurate stat times' do - let(:stat_mtime) { Time.at(1_401_235_714.0) } - let(:stat_atime) { Time.at(1_401_235_714.0) } - let(:stat_ctime) { Time.at(1_401_235_714.0) } + let(:stat_mtime) { Time.at(1_401_235_714.0).utc } + let(:stat_atime) { Time.at(1_401_235_714.0).utc } + let(:stat_ctime) { Time.at(1_401_235_714.0).utc } let(:record_mtime) { stat_mtime.to_f } @@ -127,7 +127,7 @@ # NOTE: if real mtime is ???14.99, the # saved mtime is ???14.0 - let(:now) { Time.at(1_401_235_716.00) } + let(:now) { Time.at(1_401_235_716.00).utc } it { should be_nil } end @@ -136,7 +136,7 @@ # so saved mtime at ???14.0 means it could be # ???14.999, so ???15.999 could still be within 1 second # range - let(:now) { Time.at(1_401_235_715.999999) } + let(:now) { Time.at(1_401_235_715.999999).utc } before { allow(Time).to receive(:now) { now } } @@ -222,23 +222,23 @@ subject { Listen::File.inaccurate_mac_time?(stat) } context 'with no accurate times' do - let(:mtime) { Time.at(1_234_567.0) } - let(:atime) { Time.at(1_234_567.0) } - let(:ctime) { Time.at(1_234_567.0) } + let(:mtime) { Time.at(1_234_567.0).utc } + let(:atime) { Time.at(1_234_567.0).utc } + let(:ctime) { Time.at(1_234_567.0).utc } it { should be_truthy } end context 'with all accurate times' do - let(:mtime) { Time.at(1_234_567.89) } - let(:atime) { Time.at(1_234_567.89) } - let(:ctime) { Time.at(1_234_567.89) } + let(:mtime) { Time.at(1_234_567.89).utc } + let(:atime) { Time.at(1_234_567.89).utc } + let(:ctime) { Time.at(1_234_567.89).utc } it { should be_falsey } end context 'with one accurate time' do - let(:mtime) { Time.at(1_234_567.0) } - let(:atime) { Time.at(1_234_567.89) } - let(:ctime) { Time.at(1_234_567.0) } + let(:mtime) { Time.at(1_234_567.0).utc } + let(:atime) { Time.at(1_234_567.89).utc } + let(:ctime) { Time.at(1_234_567.0).utc } it { should be_falsey } end end diff --git a/spec/lib/listen/listener/config_spec.rb b/spec/lib/listen/listener/config_spec.rb index ec92a867..2f658ad6 100644 --- a/spec/lib/listen/listener/config_spec.rb +++ b/spec/lib/listen/listener/config_spec.rb @@ -12,7 +12,7 @@ it 'extracts adapter options' do klass = Class.new do - DEFAULTS = { latency: 5.4321 } + DEFAULTS = { latency: 5.4321 }.freeze end expected = { latency: 1.234 } expect(subject.adapter_instance_options(klass)).to eq(expected) diff --git a/spec/lib/listen/listener_spec.rb b/spec/lib/listen/listener_spec.rb index 0fdace43..6e6678ad 100644 --- a/spec/lib/listen/listener_spec.rb +++ b/spec/lib/listen/listener_spec.rb @@ -1,7 +1,6 @@ include Listen RSpec.describe Listener do - let(:realdir1) { fake_path('/foo/dir1', children: []) } let(:realdir2) { fake_path('/foo/dir2', children: []) } @@ -85,7 +84,7 @@ context 'with a block' do let(:myblock) { instance_double(Proc) } - let(:block) { proc { myblock.call } } + let(:block) { proc { myblock.call } } subject do described_class.new('dir1') do |*args| myblock.call(*args) @@ -108,7 +107,7 @@ it 'passes directories to backend' do allow(Backend).to receive(:new). - with(['dir1', 'dir2'], anything, anything, anything). + with(%w(dir1 dir2), anything, anything, anything). and_return(backend) subject end diff --git a/spec/lib/listen/record_spec.rb b/spec/lib/listen/record_spec.rb index b7a7a7ac..285aa8bb 100644 --- a/spec/lib/listen/record_spec.rb +++ b/spec/lib/listen/record_spec.rb @@ -32,7 +32,7 @@ def realpath(path) end def symlink(hash_or_dir) - if String === hash_or_dir + if hash_or_dir.is_a?(String) allow(::File).to receive(:realpath).with(hash_or_dir). and_return(hash_or_dir) else @@ -277,14 +277,14 @@ def record_tree(record) real_directory('/dir/dir2' => []) end - it 'builds record' do + it 'builds record' do record.build expect(record_tree(record)). to eq( 'dir1' => {}, 'dir1/foo' => { 'bar' => { mtime: 2.3, mode: 0755 } }, 'dir2' => {}, - ) + ) end end @@ -300,7 +300,7 @@ def record_tree(record) allow(::File).to receive(:realpath) { |path| path } end - it 'builds record' do + it 'builds record' do record.build expect(record_tree(record)). to eq( @@ -309,7 +309,7 @@ def record_tree(record) 'dir1/foo/bar' => {}, 'dir1/foo/baz' => {}, 'dir2' => {}, - ) + ) end end diff --git a/spec/lib/listen/silencer_spec.rb b/spec/lib/listen/silencer_spec.rb index f32cb889..bfd154de 100644 --- a/spec/lib/listen/silencer_spec.rb +++ b/spec/lib/listen/silencer_spec.rb @@ -61,7 +61,7 @@ end context 'when ignoring foo/bar* and *.pid' do - let(:options) { { ignore: [/^foo\/bar/, /\.pid$/] } } + let(:options) { { ignore: [%r{^foo\/bar}, /\.pid$/] } } it { should_not accept(:file, 'foo/bar/baz') } it { should_not accept(:file, 'foo.pid') } end @@ -79,7 +79,7 @@ end context 'when accepting only foo/* and *.txt' do - let(:options) { { only: [/^foo\//, /\.txt$/] } } + let(:options) { { only: [%r{^foo\/}, /\.txt$/] } } it { should accept(:file, 'foo/bar.rb') } it { should accept(:file, 'bar.txt') } it { should_not accept(:file, 'bar/baz.rb') } diff --git a/spec/support/acceptance_helper.rb b/spec/support/acceptance_helper.rb index 8f36d432..8c76a6a0 100644 --- a/spec/support/acceptance_helper.rb +++ b/spec/support/acceptance_helper.rb @@ -5,7 +5,6 @@ queued_modification: :modified, queued_addition: :added }.each do |description, type| - RSpec::Matchers.define "process_#{description}_of".to_sym do |expected| match do |actual| # Use cases: @@ -94,7 +93,7 @@ def change_fs(type, path) def _sleep_until_next_second(path) Listen::File.inaccurate_mac_time?(path) - t = Time.now + t = Time.now.utc diff = t.to_f - t.to_i sleep(1.05 - diff) @@ -165,7 +164,7 @@ def initialize(callback, paths, *args) @timed_changes = TimedChanges.new if callback - @listener = Listen.send(*args) do |modified, added, removed| + @listener = Listen.send(*args) do |modified, added, removed| # Add changes to trigger frozen Hash error, making sure lag is enough _add_changes(:modified, modified, changes) _add_changes(:added, added, changes) @@ -190,7 +189,6 @@ def listen(reset_queue = true) sleep lag @timed_changes.allow_changes(reset_queue) do - yield # Polling sleep (default: 1s) @@ -234,8 +232,8 @@ def _relative_path(changes) changes.map do |change| unfrozen_copy = change.dup [@paths].flatten.each do |path| - sub = path.sub(/\/$/, '').to_s - unfrozen_copy.gsub!(/^#{sub}\//, '') + sub = path.sub(%r{\/$}, '').to_s + unfrozen_copy.gsub!(%r{^#{sub}\/}, '') end unfrozen_copy end