Permalink
Browse files

Fix setting listener options. (Fixes #173)

  • Loading branch information...
1 parent 691b473 commit 8f1cf71a658cdd3aeee52969704b28074f1b4685 @netzpirat netzpirat committed Oct 31, 2011
Showing with 86 additions and 6 deletions.
  1. +2 −0 CHANGELOG.md
  2. +19 −6 lib/guard/listener.rb
  3. +65 −0 spec/guard/listener_spec.rb
View
@@ -2,6 +2,7 @@
### Bug fix
+- [#173](https://github.com/guard/guard/issues/173): Cannot set the watch_all_modifications option. (reported by [@sutherland][], fixed by [@netzpirat][]
- Fix `guard init` when a guard name is given. ([@rymai][])
### Improvements
@@ -366,6 +367,7 @@
[@stereobooster]: https://github.com/stereobooster
[@stouset]: https://github.com/stouset
[@sunaku]: https://github.com/sunaku
+[@sutherland]: https://github.com/sutherland
[@thibaudgg]: https://github.com/thibaudgg
[@thierryhenrio]: https://github.com/thierryhenrio
[@tinogomes]: https://github.com/tinogomes
View
@@ -48,18 +48,22 @@ def self.select_and_init(*args)
#
# @param [String] directory the root directory to listen to
# @option options [Boolean] relativize_paths use only relative paths
+ # @option options [Boolean] watch_all_modifications to enable deleted and moved file listening.
# @option options [Array<String>] ignore_paths the paths to ignore by the listener
#
def initialize(directory = Dir.pwd, options = {})
- @directory = directory.to_s
@sha1_checksums_hash = {}
@file_timestamp_hash = {}
- @relativize_paths = options.fetch(:relativize_paths, true)
@changed_files = []
@paused = false
+
+ @directory = directory.to_s
+
+ @relativize_paths = options.fetch('relativize_paths', true)
+ @watch_all_modifications = options.fetch('watch_all_modifications', false)
+
@ignore_paths = DEFAULT_IGNORE_PATHS
@ignore_paths |= options[:ignore_paths] if options[:ignore_paths]
- @watch_all_modifications = options.fetch(:watch_all_modifications, false)
update_last_event
start_reactor
@@ -143,7 +147,7 @@ def update_last_event
def modified_files(dirs, options = {})
last_event = @last_event
files = []
- if @watch_all_modifications
+ if watch_all_modifications?
deleted_files = @file_timestamp_hash.collect do |path, ts|
unless File.exists?(path)
@sha1_checksums_hash.delete(path)
@@ -196,10 +200,19 @@ def relativize_paths?
!!@relativize_paths
end
+ # test if the listener should also watch for deleted and
+ # moved files
+ #
+ # @return [Boolean] whether to watch all file modifications or not
+ #
+ def watch_all_modifications?
+ !!@watch_all_modifications
+ end
+
# Populate initial timestamp file hash to watch for deleted or moved files.
#
def timestamp_files
- all_files.each {|path| set_file_timestamp_hash(path, file_timestamp(path)) } if @watch_all_modifications
+ all_files.each {|path| set_file_timestamp_hash(path, file_timestamp(path)) } if watch_all_modifications?
end
# Removes the ignored paths from the directory list.
@@ -258,7 +271,7 @@ def file_modified?(path, last_event)
elsif mtime > last_event.to_i
set_sha1_checksums_hash(path, sha1_checksum(path))
true
- elsif @watch_all_modifications
+ elsif watch_all_modifications?
ts = file_timestamp(path)
if ts != @file_timestamp_hash[path]
set_file_timestamp_hash(path, ts)
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'thor/core_ext/hash_with_indifferent_access'
describe Guard::Listener do
@@ -37,6 +38,70 @@
end
end
+ describe '#initialize' do
+ context 'with a directory parameter' do
+ it 'ensures the directory is a String' do
+ listener = described_class.new(Pathname.new('/tmp'))
+ listener.directory.should eql '/tmp'
+ end
+ end
+
+ context 'without a directory parameter' do
+ it 'takes the current working directory' do
+ listener = described_class.new()
+ listener.directory.should eql Dir.pwd.to_s
+ end
+ end
+
+ context 'with the relativize_paths option' do
+ let(:options) { Thor::CoreExt::HashWithIndifferentAccess.new('relativize_paths' => false) }
+
+ it 'takes the passed option value' do
+ listener = described_class.new('/tmp', options)
+ listener.relativize_paths?.should be_false
+ end
+ end
+
+ context 'without the relativize_paths option' do
+ it 'sets it to true as default' do
+ listener = described_class.new
+ listener.relativize_paths?.should be_true
+ end
+ end
+
+ context 'with the watch_all_modifications option' do
+ let(:options) { Thor::CoreExt::HashWithIndifferentAccess.new('watch_all_modifications' => true) }
+
+ it 'takes the passed option value' do
+ listener = described_class.new('/tmp', options)
+ listener.watch_all_modifications?.should be_true
+ end
+ end
+
+ context 'without the watch_all_modifications option' do
+ it 'sets it to false as default' do
+ listener = described_class.new
+ listener.watch_all_modifications?.should be_false
+ end
+ end
+
+ context 'without the ignored_paths options' do
+ it 'sets the default ignore paths' do
+ listener = described_class.new
+ listener.ignore_paths.should =~ %w[. .. .bundle .git log tmp vendor]
+ end
+ end
+
+ context 'with the ignored_paths options' do
+ let(:options) { Thor::CoreExt::HashWithIndifferentAccess.new('ignore_paths' => %w[.idea coverage]) }
+
+ it 'adds the paths to the default ignore paths' do
+ listener = described_class.new('/tmp', options)
+ listener.ignore_paths.should =~ %w[. .. .bundle .git log tmp vendor .idea coverage]
+ end
+ end
+ end
+
describe '#all_files' do
subject { described_class.new(@fixture_path) }

0 comments on commit 8f1cf71

Please sign in to comment.