Skip to content

Commit

Permalink
issue #504: rubocop -a -A
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Nov 27, 2020
1 parent c181cfe commit a9137ac
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 52 deletions.
6 changes: 2 additions & 4 deletions bin/listen
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
require 'listen'
require 'listen/cli'

unless defined?(JRUBY_VERSION)
if Signal.list.keys.include?('INT')
Signal.trap('INT') { Thread.new { Listen.stop } }
end
if !defined?(JRUBY_VERSION) && Signal.list.keys.include?('INT')
Signal.trap('INT') { Thread.new { Listen.stop } }
end

Listen::CLI.start
6 changes: 2 additions & 4 deletions lib/listen/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
module Listen
module Adapter
class Base
attr_reader :options
attr_reader :options, :config

# TODO: only used by tests
DEFAULTS = {}.freeze

attr_reader :config

def initialize(config)
@started = false
@config = config
Expand Down Expand Up @@ -75,7 +73,7 @@ def start
@started = true

@run_thread = Listen::Thread.new("run_thread") do
@snapshots.values.each do |snapshot|
@snapshots.each_value do |snapshot|
_timed('Record.build()') { snapshot.record.build }
end
_run
Expand Down
5 changes: 1 addition & 4 deletions lib/listen/adapter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
module Listen
module Adapter
class Config
attr_reader :directories
attr_reader :silencer
attr_reader :queue
attr_reader :adapter_options
attr_reader :directories, :silencer, :queue, :adapter_options

def initialize(directories, queue, silencer, adapter_options)
# Default to current directory if no directories are supplied
Expand Down
2 changes: 1 addition & 1 deletion lib/listen/adapter/darwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _run

def _process_changes(dirs)
dirs.each do |dir|
dir = Pathname.new(dir.sub(%r{\/$}, ''))
dir = Pathname.new(dir.sub(%r{/$}, ''))

@callbacks.each do |watched_dir, callback|
if watched_dir.eql?(dir) || Listen::Directory.ascendant_of?(watched_dir, dir)
Expand Down
5 changes: 3 additions & 2 deletions lib/listen/adapter/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ def _process_event(dir, event)
_queue_change(:file, dir, rel_path, options)
end
when :dir
if change.type == :removed
case change.type
when :removed
# TODO: check if watched dir?
_queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {})
elsif change.type == :added
when :added
_queue_change(:dir, dir, rel_path, {})
# do nothing - changed directory means either:
# - removed subdirs (handled above)
Expand Down
1 change: 1 addition & 0 deletions lib/listen/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def start

class Forwarder
attr_reader :logger

def initialize(options)
@options = options
@logger = ::Logger.new(STDOUT, level: ::Logger::INFO)
Expand Down
4 changes: 1 addition & 3 deletions lib/listen/event/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module Listen
module Event
class Config
attr_reader :listener
attr_reader :event_queue
attr_reader :min_delay_between_events
attr_reader :listener, :event_queue, :min_delay_between_events

def initialize(
listener,
Expand Down
3 changes: 1 addition & 2 deletions lib/listen/event/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def <<(args)

private

attr_reader :event_queue
attr_reader :config
attr_reader :event_queue, :config

def _safe_relative_from_cwd(dir)
return dir unless config.relative?
Expand Down
4 changes: 1 addition & 3 deletions lib/listen/listener/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def relative?
@relative
end

attr_reader :min_delay_between_events

attr_reader :silencer_rules
attr_reader :min_delay_between_events, :silencer_rules

def adapter_instance_options(klass)
valid_keys = klass.const_get('DEFAULTS').keys
Expand Down
6 changes: 3 additions & 3 deletions lib/listen/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Options
def initialize(opts, defaults)
@options = {}
given_options = opts.dup
defaults.keys.each do |key|
defaults.each_key do |key|
@options[key] = given_options.delete(key) || defaults[key]
end

Expand All @@ -17,11 +17,11 @@ def initialize(opts, defaults)
end

def respond_to_missing?(name, *_)
@options.has_key?(name)
super || @options.has_key?(name)
end

def method_missing(name, *_)
respond_to_missing?(name) or raise NameError, "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})"
super || respond_to_missing?(name) or raise NameError, "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})"
@options[name]
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/listen/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Record
# TODO: deprecate

attr_reader :root

def initialize(directory)
@tree = _auto_hash
@root = directory.to_s
Expand Down Expand Up @@ -49,9 +50,9 @@ def dir_entries(rel_path)
_sub_tree(rel_path)
end

subtree.each_with_object({}) do |(key, values), result|
subtree.transform_values do |values|
# only get data for file entries
result[key] = values.key?(:mtime) ? values : {}
values.key?(:mtime) ? values : {}
end
end

Expand Down
14 changes: 8 additions & 6 deletions lib/listen/silencer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ def configure(options)
def silenced?(relative_path, type)
path = relative_path.to_s

if only_patterns && type == :file
return true unless only_patterns.any? { |pattern| path =~ pattern }
end

ignore_patterns.any? { |pattern| path =~ pattern }
_ignore?(path) || (only_patterns && type == :file && !_only?(path))
end

private

attr_reader :options
def _ignore?(path)
ignore_patterns.any? { |pattern| path =~ pattern }
end

def _only?(path)
only_patterns.any? { |pattern| path =~ pattern }
end

def _init_ignores(ignores, overrides)
patterns = []
Expand Down
2 changes: 1 addition & 1 deletion listen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
}

gem.files = `git ls-files -z`.split("\x0").select do |f|
%r{^(?:bin|lib)\/} =~ f
%r{^(?:bin|lib)/} =~ f
end + %w[CHANGELOG.md CONTRIBUTING.md LICENSE.txt README.md]

gem.test_files = []
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/listen/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
it { should be :modified }

it 'sets path in record with expected data' do
expected = expected_data. merge(md5: md5)
expected = expected_data.merge(md5: md5)
expect(record).to receive(:update_file).
with('file.rb', expected)
subject
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/listen/queue_optimizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@

context 'with cookie' do
context 'when single moved' do
let(:changes) { [[:file, :moved_to, dir, 'foo', cookie: 4321]] }
let(:changes) { [[:file, :moved_to, dir, 'foo', { cookie: 4321 }]] }
it { is_expected.to eq(modified: [], added: ['foo'], removed: []) }
end

context 'when related moved_to' do
let(:changes) do
[
[:file, :moved_from, dir, 'foo', cookie: 4321],
[:file, :moved_to, dir, 'bar', cookie: 4321]
[:file, :moved_from, dir, 'foo', { cookie: 4321 }],
[:file, :moved_to, dir, 'bar', { cookie: 4321 }]
]
end
it { is_expected.to eq(modified: [], added: ['bar'], removed: []) }
Expand All @@ -93,8 +93,8 @@
context 'when related moved_to with ignored moved_from' do
let(:changes) do
[
[:file, :moved_from, dir, 'ignored', cookie: 4321],
[:file, :moved_to, dir, 'foo', cookie: 4321]
[:file, :moved_from, dir, 'ignored', { cookie: 4321 }],
[:file, :moved_to, dir, 'foo', { cookie: 4321 }]
]
end
it { is_expected.to eq(modified: ['foo'], added: [], removed: []) }
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/listen/silencer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
before { subject.configure(options) }

describe '#silenced?' do
it { should accept(:file, Pathname('some_dir') + 'some_file.rb') }
it { should accept(:file, Pathname('some_dir').join("some_file.rb")) }

context 'with default ignore' do
hidden_ignored = %w[.git .svn .hg .rbx .bundle]
Expand Down Expand Up @@ -69,7 +69,7 @@
end

context 'when ignoring foo/bar* and *.pid' do
let(:options) { { ignore: [%r{^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
Expand All @@ -87,7 +87,7 @@
end

context 'when accepting only foo/* and *.txt' do
let(:options) { { only: [%r{^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') }
Expand Down
14 changes: 6 additions & 8 deletions spec/support/acceptance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
# 2. keep the queue if we're testing for existing accumulated changes

# if were testing the queue (e.g. after unpause), don't reset
reset_queue = /queued_/ !~ description

actual.listen(reset_queue) do
actual.listen(reset_queue: /queued_/ !~ description) do
change_fs(type, expected) if reset_queue
end
actual.changes[type].include? expected
Expand Down Expand Up @@ -116,7 +114,7 @@ def freeze_offset
end

# Allow changes only during specific time wine
def allow_changes(reset_queue = true)
def allow_changes(reset_queue: true)
@freeze_time = nil
if reset_queue
# Clear to prepare for collecting new FS events
Expand Down Expand Up @@ -178,12 +176,12 @@ def changes
@timed_changes.changes
end

def listen(reset_queue = true)
def listen(reset_queue: true)
# Give previous events time to be received, queued and processed
# so they complete and don't interfere
sleep lag

@timed_changes.allow_changes(reset_queue) do
@timed_changes.allow_changes(reset_queue: reset_queue) do
yield

# Polling sleep (default: 1s)
Expand Down Expand Up @@ -225,8 +223,8 @@ def _relative_path(changes)
changes.map do |change|
unfrozen_copy = change.dup
[@paths].flatten.each do |path|
sub = path.sub(%r{\/$}, '').to_s
unfrozen_copy.gsub!(%r{^#{sub}\/}, '')
sub = path.sub(%r{/$}, '').to_s
unfrozen_copy.gsub!(%r{^#{sub}/}, '')
end
unfrozen_copy
end
Expand Down

0 comments on commit a9137ac

Please sign in to comment.