From 4f30208d5e22fb9e6d935d034f082d8eefc46f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Mon, 24 Oct 2022 12:26:23 +0200 Subject: [PATCH] Raise argument error when passing a file path 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. --- lib/listen/adapter/config.rb | 6 ++++++ spec/lib/listen/adapter/config_spec.rb | 10 ++++++++++ spec/spec_helper.rb | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/listen/adapter/config.rb b/lib/listen/adapter/config.rb index e33b13ef..5a068a7c 100644 --- a/lib/listen/adapter/config.rb +++ b/lib/listen/adapter/config.rb @@ -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 diff --git a/spec/lib/listen/adapter/config_spec.rb b/spec/lib/listen/adapter/config_spec.rb index 9cab26d6..ff715a8d 100644 --- a/spec/lib/listen/adapter/config_spec.rb +++ b/spec/lib/listen/adapter/config_spec.rb @@ -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) @@ -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 @@ -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) @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 676f51f2..7d4799ee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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