Skip to content

Commit bf2fc88

Browse files
committed
Raise an error when path is specified with :moved_from or :moved_to
If same path is specified with :moved_from or :moved_to additionally, :modify is not fired expectedly. require "rb-inotify" notifier = INotify::Notifier.new notifier.watch("test.log", :modify) do |event| puts "#{event} is modified" end notifier.watch("test.log", :moved_from) do |event| puts "#{event} is moved_from" end Above sample code does not work as expected. So, raise an error immediately.
1 parent 010f140 commit bf2fc88

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/rb-inotify/notifier.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ def to_io
195195
# @raise [SystemCallError] if the file or directory can't be watched,
196196
# e.g. if the file isn't found, read access is denied,
197197
# or the flags don't contain any events
198+
# @raise [ArgumentError] if the file is specified with :move_to.
198199
def watch(path, *flags, &callback)
200+
if File.file?(path) and (flags.include?(:moved_to) or flags.include?(:moved_from))
201+
raise ArgumentError.new, ":moved_to must not be specified with file #{path}"
202+
end
203+
199204
return Watcher.new(self, path, *flags, &callback) unless flags.include?(:recursive)
200205

201206
dir = Dir.new(path)

spec/notifier_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
expect(bar_events.first.name).to eq("test_two.txt")
7272
expect(bar_events.first.absolute_name).to eq(another_dir.join("test_two.txt").to_s)
7373
end
74+
75+
it "file should not specified with :moved_from or :moved_to" do
76+
dir.join("one.txt").write("hello world")
77+
expect {
78+
recording(dir.join("one.txt"), :moved_to)
79+
}.to raise_error(ArgumentError)
80+
expect {
81+
recording(dir.join("one.txt"), :moved_from)
82+
}.to raise_error(ArgumentError)
83+
end
7484
end
7585

7686
describe :run do

0 commit comments

Comments
 (0)