Skip to content

Commit

Permalink
Be sure to reset timestamp for new files
Browse files Browse the repository at this point in the history
  • Loading branch information
rymai committed Jan 8, 2012
1 parent 919bde5 commit 50b7275
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/guard/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def file_modified?(path, last_event)
file_content_modified?(path, sha1_checksum(path))
elsif mtime > last_event.to_i
set_sha1_checksums_hash(path, sha1_checksum(path))
set_file_timestamp_hash(path) if watch_all_modifications?
true
elsif watch_all_modifications?
ts = file_timestamp(path)
Expand Down
Empty file removed spec/fixtures/folder1/file1.txt
Empty file.
102 changes: 77 additions & 25 deletions spec/guard/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,41 @@

before { listen_to subject }

context 'for a new file' do
it 'catches the creation' do
FileUtils.rm(file1) if File.exists?(file1)
File.exists?(file1).should be_false

watch do
FileUtils.touch(file1)
end

subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/file1.txt']
end
end

context 'without the :all option' do
it 'finds modified files only in the directory supplied' do
watch do
FileUtils.touch([file1, file2, file3])
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']
end

subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']
end
end

context 'with the :all options' do
it 'finds modified files within subdirectories' do
watch do
FileUtils.touch([file1, file2, file3])
subject.modified_files([fixture('folder1')], { :all => true }).should =~
['spec/fixtures/folder1/deletedfile1.txt',
'spec/fixtures/folder1/file1.txt',
'spec/fixtures/folder1/folder2/file2.txt']
end

subject.modified_files([fixture('folder1')], { :all => true }).should =~
['spec/fixtures/folder1/deletedfile1.txt',
'spec/fixtures/folder1/file1.txt',
'spec/fixtures/folder1/folder2/file2.txt']
end
end

Expand All @@ -198,7 +214,7 @@
watch do
FileUtils.touch([file1, file2, file3])
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']

subject.update_last_event

Expand All @@ -215,23 +231,21 @@
watch do
FileUtils.touch([file1, file2, file3])
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']
['spec/fixtures/folder1/deletedfile1.txt', 'spec/fixtures/folder1/file1.txt']

subject.update_last_event

FileUtils.touch([file2, file3])
File.open(file1, 'w') { |f| f.write('changed content') }
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/file1.txt']
['spec/fixtures/folder1/file1.txt']
end
end
end

context 'without the :watch_all_modifications option' do
after { FileUtils.touch(file3) }

it 'defaults to false' do
subject.watch_all_modifications?.should eql false
subject.watch_all_modifications?.should be_false
end

context 'for a deleted file' do
Expand All @@ -241,25 +255,26 @@
File.exists?(file3).should be_true

watch do
FileUtils.remove_file(file3)
FileUtils.rm(file3)
end

subject.modified_files([fixture('folder1')], {}).should =~ []
subject.modified_files([fixture('folder1')], {}).should eq []
end
end

context 'for a moved file' do
after { FileUtils.move(file4, file1) }
after { FileUtils.rm(file4) }

it 'does not catch the move' do
FileUtils.touch(file1)
File.exists?(file1).should be_true
File.exists?(file4).should be_false

watch do
FileUtils.move(file1, file4)
FileUtils.mv(file1, file4)
end

subject.modified_files([@fixture_path.join('folder1')], {}).should =~ []
subject.modified_files([fixture('folder1')], {}).should eq []
end
end
end
Expand All @@ -268,12 +283,50 @@
subject { described_class.new(Dir.pwd, :watch_all_modifications => true) }

before do
FileUtils.touch(file1)
subject.timestamp_files
subject.update_last_event
end

it 'should be true when set' do
subject.watch_all_modifications?.should eql true
subject.watch_all_modifications?.should be_true
end

context 'for a new file then deleted then re-created and re-deleted' do
it 'catches all the events' do
FileUtils.rm(file1) if File.exists?(file1)
File.exists?(file1).should be_false

watch do
FileUtils.touch(file1)
File.exists?(file1).should be_true
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/file1.txt']

subject.update_last_event

FileUtils.rm(file1)
File.exists?(file1).should be_false
subject.modified_files([fixture('folder1')], {}).should =~
['!spec/fixtures/folder1/file1.txt']

subject.update_last_event
sleep(sleep_time)

FileUtils.touch(file1)
File.exists?(file1).should be_true
subject.modified_files([fixture('folder1')], {}).should =~
['spec/fixtures/folder1/file1.txt']

subject.update_last_event

FileUtils.rm(file1)
File.exists?(file1).should be_false
subject.modified_files([fixture('folder1')], {}).should =~
['!spec/fixtures/folder1/file1.txt']
end

end
end

context 'for a deleted file' do
Expand All @@ -283,27 +336,26 @@
File.exists?(file3).should be_true

watch do
FileUtils.remove_file(file3)
FileUtils.rm(file3)
end

subject.modified_files([fixture('folder1')], {}).should =~
['!spec/fixtures/folder1/deletedfile1.txt']
['!spec/fixtures/folder1/deletedfile1.txt']
end
end

context 'for a moved file' do
after { FileUtils.move(file4, file1) }

after { FileUtils.rm(file4) }
it 'catches the move' do
File.exists?(file1).should be_true
File.exists?(file4).should be_false

watch do
FileUtils.move(file1, file4)
FileUtils.mv(file1, file4)
end

subject.modified_files([@fixture_path.join('folder1')], {}).should =~
['!spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/movedfile1.txt']
subject.modified_files([fixture('folder1')], {}).should =~
['!spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/movedfile1.txt']
end
end
end
Expand Down

0 comments on commit 50b7275

Please sign in to comment.