Skip to content

Commit

Permalink
Raise argument error when passing a file path
Browse files Browse the repository at this point in the history
The Listen gem doesn't support listening on changes of individual files,
but currently if we pass a file path to `Listen.on`, the listener is
successfully initialized, without actually listening on given files. To
improve clarity, we notify the user they need to pass a directory by
raising an argument error.
  • Loading branch information
janko authored and ColinDKelley committed Jan 6, 2023
1 parent abb90a5 commit 4f30208
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/listen/adapter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def initialize(directories, queue, silencer, adapter_options)
Pathname.new(directory.to_s).realpath
end

@directories.each do |pathname|
unless pathname.directory?
fail ArgumentError, "must be a directory: #{pathname}"
end
end

@silencer = silencer
@queue = queue
@adapter_options = adapter_options
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/listen/adapter/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# Here's what may be passed to initializer
let(:path1) { fake_path('/real/path1', realpath: real_path1) }
let(:path2) { fake_path('/real/path2', realpath: real_path2) }
let(:path3) { fake_path('/real/path3', realpath: real_path3) }

let(:current_path) do
fake_path('/real/current_path', realpath: real_current_path)
Expand All @@ -29,6 +30,7 @@
# something useful)
let(:real_path1) { fake_path('/real/path1') }
let(:real_path2) { fake_path('/real/path2') }
let(:real_path3) { fake_path('/real/path3', directory?: false) }
let(:real_current_path) { fake_path('/real/current_path') }

before do
Expand All @@ -38,6 +40,7 @@

allow(Pathname).to receive(:new).with('/real/path1').and_return(path1)
allow(Pathname).to receive(:new).with('/real/path2').and_return(path2)
allow(Pathname).to receive(:new).with('/real/path3').and_return(path3)

allow(Pathname).to receive(:new).with(path1).and_return(path1)
allow(Pathname).to receive(:new).with(path2).and_return(path2)
Expand Down Expand Up @@ -90,6 +93,13 @@
expect(subject.directories).to eq([real_current_path])
end
end

context 'with file path' do
let(:directories) { ['/real/path3'] }
it 'raises argument error requesting a directory' do
expect { subject }.to raise_error(ArgumentError, /must be a directory/)
end
end
end

describe '#adapter_options' do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def ci?

module SpecHelpers
def fake_path(str, options = {})
instance_double(Pathname, str, { to_s: str }.merge(options))
instance_double(Pathname, str, { to_s: str, directory?: true }.merge(options))
end
end

Expand Down

0 comments on commit 4f30208

Please sign in to comment.