moved timestamp hash creation into it's own method add initial specs for watching deleted files and fixed some minor formatting

This commit is contained in:
Darren Pearce 2011-09-13 16:18:32 -06:00
parent 02c4465940
commit 423610f22d
3 changed files with 102 additions and 10 deletions

View File

@ -198,6 +198,12 @@ $ guard --guardfile ~/.your_global_guardfile
$ guard -G ~/.your_global_guardfile # shortcut $ guard -G ~/.your_global_guardfile # shortcut
``` ```
Guard can optionally watch moved or deleted files with:
``` bash
$ guard -D
```
An exhaustive list of options is available with: An exhaustive list of options is available with:
``` bash ``` bash

View File

@ -30,14 +30,13 @@ module Guard
@sha1_checksums_hash = {} @sha1_checksums_hash = {}
@file_timestamp_hash = {} @file_timestamp_hash = {}
@relativize_paths = options.fetch(:relativize_paths, true) @relativize_paths = options.fetch(:relativize_paths, true)
@watch_deletions = options.fetch(:deletions, false) @watch_deletions = options.fetch(:deletions, false)
update_last_event update_last_event
end end
def start def start
watch(@directory) watch(@directory)
# populate initial sha1 hash to watch for deleted or moved files timestamp_files
all_files.each {|path| set_file_timestamp_hash(path, file_timestamp(path)) } if @watch_deletions
end end
def stop def stop
@ -93,6 +92,11 @@ module Guard
!!@relativize_paths !!@relativize_paths
end end
def timestamp_files
# populate initial timestamp file hash to watch for deleted or moved files
all_files.each {|path| set_file_timestamp_hash(path, file_timestamp(path)) } if @watch_deletions
end
private private
def potentially_modified_files(dirs, options={}) def potentially_modified_files(dirs, options={})
@ -123,11 +127,11 @@ module Guard
set_sha1_checksums_hash(path, sha1_checksum(path)) set_sha1_checksums_hash(path, sha1_checksum(path))
true true
elsif @watch_deletions elsif @watch_deletions
ts = file_timestamp(path) ts = file_timestamp(path)
if ts != @file_timestamp_hash[path] if ts != @file_timestamp_hash[path]
set_file_timestamp_hash(path, ts) set_file_timestamp_hash(path, ts)
true true
end end
end end
rescue rescue
false false

View File

@ -81,6 +81,8 @@ describe Guard::Listener do
let(:file1) { @fixture_path.join("folder1", "file1.txt") } let(:file1) { @fixture_path.join("folder1", "file1.txt") }
let(:file2) { @fixture_path.join("folder1", "folder2", "file2.txt") } let(:file2) { @fixture_path.join("folder1", "folder2", "file2.txt") }
let(:file3) { @fixture_path.join("folder1", "deletedfile1.txt") } let(:file3) { @fixture_path.join("folder1", "deletedfile1.txt") }
let(:file4) { @fixture_path.join("folder1", "movedfile1.txt") }
let(:destfile) { @fixture_path.join("folder1", "folder2","movedfile1.txt") }
before do before do
subject.update_last_event subject.update_last_event
@ -102,7 +104,7 @@ describe Guard::Listener do
end end
context "without updating the content" do context "without updating the content" do
it "ignores the files for the second time" do it "ignores the}fil)s for the second time" do
FileUtils.touch([file1, file2, file3]) FileUtils.touch([file1, file2, file3])
subject.modified_files([@fixture_path.join("folder1")], {}).should =~ ["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"] subject.modified_files([@fixture_path.join("folder1")], {}).should =~ ["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
subject.update_last_event subject.update_last_event
@ -125,6 +127,87 @@ describe Guard::Listener do
sleep 1 sleep 1
end end
end end
context "without watch_deletions" do
after { FileUtils.touch(file3) }
it "defaults to false" do
subject.instance_variable_get(:@watch_deletions).should eql false
end
it "it should not track deleted files" do
FileUtils.touch([file1, file2, file3])
subject.modified_files([@fixture_path.join("folder1")], {}).should =~ ["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
subject.update_last_event
FileUtils.rm(file3)
subject.modified_files([@fixture_path.join("folder1")], {}).should =~ []
sleep 1
end
end
context "with watch_deletions" do
subject { described_class.new(Dir.pwd, :deletions=>true) }
before :each do
subject.timestamp_files
sleep 1
subject.update_last_event
end
after :each do
FileUtils.touch([file1, file2, file3, file4])
FileUtils.rm_f([destfile])
end
it "should be true when set" do
subject.instance_variable_get(:@watch_deletions).should eql true
end
it "should track deleted files" do
FileUtils.touch([file1, file3])
subject.modified_files([@fixture_path.join("folder1")], {}).should =~
["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
subject.update_last_event
FileUtils.remove_file(file3)
subject.modified_files([@fixture_path.join("folder1")], {}).should =~
["!spec/fixtures/folder1/deletedfile1.txt"]
end
it "should track moved files" do
FileUtils.touch([file1, file3])
subject.modified_files([@fixture_path.join("folder1")], {}).should =~
["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
subject.update_last_event
FileUtils.move(file4, destfile)
subject.modified_files([@fixture_path.join("folder1")], {}).should =~
["!spec/fixtures/folder1/movedfile1.txt"]
end
it "should track deleted files with all option" do
FileUtils.touch([file1, file2])
subject.modified_files([@fixture_path.join("folder1")], {:all=>true}).should =~
["spec/fixtures/folder1/file1.txt", "spec/fixtures/folder1/folder2/file2.txt"]
subject.update_last_event
FileUtils.remove_file(file2)
subject.modified_files([@fixture_path.join("folder1")], {:all=>true}).should =~
["!spec/fixtures/folder1/folder2/file2.txt"]
end
it "should track moved files with all option" do
FileUtils.touch([file1, file2])
subject.modified_files([@fixture_path.join("folder1")], {:all=>true}).should =~
["spec/fixtures/folder1/file1.txt", "spec/fixtures/folder1/folder2/file2.txt"]
subject.update_last_event
FileUtils.move(file4, destfile)
subject.modified_files([@fixture_path.join("folder1")], {:all=>true}).should =~
["!spec/fixtures/folder1/movedfile1.txt","spec/fixtures/folder1/folder2/movedfile1.txt"]
end
end
end end
describe "working directory" do describe "working directory" do
@ -160,5 +243,4 @@ describe Guard::Listener do
end end
end end
end end