From 870e8f037bf2bd17ee4f078620aab18f0822f120 Mon Sep 17 00:00:00 2001 From: Colin Kelley Date: Thu, 5 Nov 2020 20:50:25 -0800 Subject: [PATCH] issue #504: address many rubocop offenses --- lib/listen/event/processor.rb | 8 ++--- lib/listen/thread.rb | 8 ++--- spec/acceptance/listen_spec.rb | 18 +++++------ spec/lib/listen/directory_spec.rb | 10 +++--- spec/lib/listen/thread_spec.rb | 51 +++++++++++++++++++++---------- spec/support/acceptance_helper.rb | 2 +- 6 files changed, 58 insertions(+), 39 deletions(-) diff --git a/lib/listen/event/processor.rb b/lib/listen/event/processor.rb index 6e67745a..657c1a9d 100644 --- a/lib/listen/event/processor.rb +++ b/lib/listen/event/processor.rb @@ -70,22 +70,22 @@ def _sleep(seconds) end def _remember_time_of_first_unprocessed_event - @first_unprocessed_event_time ||= _timestamp + @_remember_time_of_first_unprocessed_event ||= _timestamp end def _reset_no_unprocessed_events - @first_unprocessed_event_time = nil + @_remember_time_of_first_unprocessed_event = nil end def _deadline - @first_unprocessed_event_time + @latency + @_remember_time_of_first_unprocessed_event + @latency end # blocks until event is popped # returns the event or `nil` when the event_queue is closed def _wait_until_events config.event_queue.pop.tap do |_event| - @first_unprocessed_event_time ||= _timestamp + @_remember_time_of_first_unprocessed_event ||= _timestamp end end diff --git a/lib/listen/thread.rb b/lib/listen/thread.rb index 4f395d8e..c999a7bd 100644 --- a/lib/listen/thread.rb +++ b/lib/listen/thread.rb @@ -38,12 +38,12 @@ def _log_exception(ex, thread_name, caller_stack: nil) Listen.logger.error(message) end - def _exception_with_causes(ex) - result = +"#{ex.class}: #{ex}" - if ex.cause + def _exception_with_causes(exception) + result = +"#{exception.class}: #{exception}" + if exception.cause result << "\n" result << "--- Caused by: ---\n" - result << _exception_with_causes(ex.cause) + result << _exception_with_causes(exception.cause) end result end diff --git a/spec/acceptance/listen_spec.rb b/spec/acceptance/listen_spec.rb index f4cfb77e..a803f70f 100644 --- a/spec/acceptance/listen_spec.rb +++ b/spec/acceptance/listen_spec.rb @@ -18,15 +18,15 @@ let(:paths) { Pathname.new(Dir.pwd) } around { |example| fixtures { example.run } } - modes = - case ENV['TEST_LISTEN_ADAPTER_MODES'] || 'both' - when 'polling' - [true] - when 'native' - [false] - else - [false, true] - end + modes = + case ENV['TEST_LISTEN_ADAPTER_MODES'] + when 'polling' + [true] + when 'native' + [false] + else + [false, true] + end # TODO: make it configurable # TODO: restore diff --git a/spec/lib/listen/directory_spec.rb b/spec/lib/listen/directory_spec.rb index de2cff49..46cd8f7f 100644 --- a/spec/lib/listen/directory_spec.rb +++ b/spec/lib/listen/directory_spec.rb @@ -13,14 +13,14 @@ def fake_dir_stat(name, options = {}) instance_double(::File::Stat, name, defaults.merge(options)) end - def fake_children(ex, dir, *args, &block) + def fake_children(exception, dir, *args, &block) if block_given? - ex.send(:allow, dir).to receive(:children, &block) + exception.send(:allow, dir).to receive(:children, &block) else - ex.send(:allow, dir).to receive(:children).and_return(*args) + exception.send(:allow, dir).to receive(:children).and_return(*args) end - ex.send(:allow, dir).to receive(:exist?).and_return(true) - ex.send(:allow, dir).to receive(:directory?).and_return(true) + exception.send(:allow, dir).to receive(:exist?).and_return(true) + exception.send(:allow, dir).to receive(:directory?).and_return(true) end let(:dir) { double(:dir) } diff --git a/spec/lib/listen/thread_spec.rb b/spec/lib/listen/thread_spec.rb index cf8bf387..778eb623 100644 --- a/spec/lib/listen/thread_spec.rb +++ b/spec/lib/listen/thread_spec.rb @@ -42,25 +42,39 @@ -> { raise ArgumentError, 'boom!' } end - it "rescues and logs exceptions" do - expect(Listen.logger).to receive(:error). - with(/Exception rescued in listen-worker_thread:\nArgumentError: boom!\n.*\/listen\/thread_spec\.rb/) - subject.join - end + it "rescues and logs exceptions" do + pattern = <<~EOS.strip + Exception rescued in listen-worker_thread: + ArgumentError: boom! + .*\\/listen\\/thread_spec\\.rb + EOS + expect(Listen.logger).to receive(:error).with(/#{pattern}/) + subject.join + end - it "rescues and logs backtrace + exception backtrace" do - expect(Listen.logger).to receive(:error). - with(/Exception rescued in listen-worker_thread:\nArgumentError: boom!\n.*\/listen\/thread\.rb.*--- Thread.new ---.*\/listen\/thread_spec\.rb/m) - subject.join - end + it "rescues and logs backtrace + exception backtrace" do + pattern = <<~EOS.strip + Exception rescued in listen-worker_thread: + ArgumentError: boom! + .*\\/listen\\/thread\\.rb.*--- Thread.new ---.*\\/listen\\/thread_spec\\.rb + EOS + expect(Listen.logger).to receive(:error).with(/#{pattern}/m) + subject.join end + end context "when nested exceptions raised" do let(:block) { raise_nested_exception_block } it "details exception causes" do - expect(Listen.logger).to receive(:error). - with(/RuntimeError: nested outer\n--- Caused by: ---\nRuntimeError: nested inner\n--- Caused by: ---\nArgumentError: boom!/) + pattern = <<~EOS + RuntimeError: nested outer + --- Caused by: --- + RuntimeError: nested inner + --- Caused by: --- + ArgumentError: boom! + EOS + expect(Listen.logger).to receive(:error).with(/#{pattern}/) subject.join end end @@ -77,10 +91,15 @@ describe '.rescue_and_log' do it 'rescues and logs nested exceptions' do - expect(Listen.logger).to receive(:error). - with(/Exception rescued in method:\nRuntimeError: nested outer\n--- Caused by: ---\nRuntimeError: nested inner\n--- Caused by: ---\nArgumentError: boom!/) do |message| - expect(message).to_not match(/Thread\.new/) - end + pattern = <<~EOS + Exception rescued in method: + RuntimeError: nested outer + --- Caused by: --- + RuntimeError: nested inner + --- Caused by: --- + ArgumentError: boom! + EOS + expect(Listen.logger).to receive(:error).with(/#{pattern}/) described_class.rescue_and_log("method", &raise_nested_exception_block) end diff --git a/spec/support/acceptance_helper.rb b/spec/support/acceptance_helper.rb index 99e11d2a..07358e8e 100644 --- a/spec/support/acceptance_helper.rb +++ b/spec/support/acceptance_helper.rb @@ -143,7 +143,7 @@ def allow_changes(reset_queue = true) # Conveniently wrap a Listener instance for testing class ListenerWrapper - attr_reader :listener, :changes + attr_reader :listener attr_accessor :lag def initialize(callback, paths, *args)