Skip to content

Fixing delete method to work with multiple files #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/filewatch/watched_files_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def delete(paths)
Array(paths).each do |f|
index = @pointers.delete(f)
@files.delete_at(index)
refresh_pointers
end
@sort_method.call
end
Expand Down
27 changes: 24 additions & 3 deletions spec/filewatch/watched_files_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
module FileWatch
describe WatchedFilesCollection do
let(:time) { Time.now }
let(:filepath1){"/var/log/z.log"}
let(:filepath2){"/var/log/m.log"}
let(:filepath3){"/var/log/a.log"}
let(:stat1) { double("stat1", :size => 98, :modified_at => time - 30, :identifier => nil, :inode => 234567, :inode_struct => InodeStruct.new("234567", 3, 2)) }
let(:stat2) { double("stat2", :size => 99, :modified_at => time - 20, :identifier => nil, :inode => 234568, :inode_struct => InodeStruct.new("234568", 3, 2)) }
let(:stat3) { double("stat3", :size => 100, :modified_at => time, :identifier => nil, :inode => 234569, :inode_struct => InodeStruct.new("234569", 3, 2)) }
let(:wf1) { WatchedFile.new("/var/log/z.log", stat1, Settings.new) }
let(:wf2) { WatchedFile.new("/var/log/m.log", stat2, Settings.new) }
let(:wf3) { WatchedFile.new("/var/log/a.log", stat3, Settings.new) }
let(:wf1) { WatchedFile.new(filepath1, stat1, Settings.new) }
let(:wf2) { WatchedFile.new(filepath2, stat2, Settings.new) }
let(:wf3) { WatchedFile.new(filepath3, stat3, Settings.new) }

context "sort by last_modified in ascending order" do
let(:sort_by) { "last_modified" }
Expand Down Expand Up @@ -70,5 +73,23 @@ module FileWatch
expect(collection.values).to eq([wf1, wf2, wf3])
end
end

context "when delete called" do
let(:sort_by) { "path" }
let(:sort_direction) { "desc" }

it "is able to delete multiple files at once" do
collection = described_class.new(Settings.from_options(:file_sort_by => sort_by, :file_sort_direction => sort_direction))
collection.add(wf1)
collection.add(wf2)
collection.add(wf3)
expect(collection.keys).to eq([filepath1, filepath2, filepath3])

collection.delete([filepath2,filepath3])
expect(collection.keys).to eq([filepath1])

end
end

end
end