Permalink
Browse files

require :absolute option for absolute targets

  • Loading branch information...
1 parent 6187762 commit 759e810b75c14dfbc9ddda30c984de3eeec0b9f2 @marcisme committed Jul 23, 2012
Showing with 76 additions and 3 deletions.
  1. +4 −3 README.md
  2. +12 −0 lib/guard/copy.rb
  3. +5 −0 lib/guard/copy/target.rb
  4. +16 −0 spec/guard/copy/target_spec.rb
  5. +39 −0 spec/guard/copy_spec.rb
View
@@ -58,12 +58,13 @@ the directory specified by the `:from` option to that specified by the
``` ruby
:from => 'source' # directory to copy files from
-:to => 'target' # directory or glob to copy files to; can be an array
+:to => 'target' # directory or glob to copy files to; string or array
:glob => :newest # how to handle globs; default: :all
# :newest - copy to only the newest directory
# :all - copy to all directories
-:delete => true # delete files from target directories
-:verbose => true # log all operations as info messages
+:delete => true # delete files from target directories; default: false
+:verbose => true # log all operations as info messages; default: false
+:absolute => true # allow absolute paths for :to; default: false
```
## Watchers
View
@@ -28,6 +28,7 @@ def start
validate_presence_of(:from)
validate_from_is_directory
validate_presence_of(:to)
+ validate_to_patterns_are_not_absolute
validate_to_does_not_include_from
resolve_targets
validate_no_targets_are_files
@@ -159,6 +160,17 @@ def validate_no_targets_are_files
end
end
+ def validate_to_patterns_are_not_absolute
+ targets.each do |target|
+ if target.absolute? && !options[:absolute]
+ UI.error('Guard::Copy - :to contains an absolute path:')
+ UI.error(" #{target.pattern}")
+ UI.error('Set the :absolute option to allow absolute target paths')
+ throw :task_has_failed
+ end
+ end
+ end
+
def display_target_paths
if target_paths.any?
UI.info("Guard::Copy - files in:")
View
@@ -32,6 +32,11 @@ def resolve
@paths.any?
end
+ # @return [Boolean] true if the pattern is an absolute path
+ def absolute?
+ @pattern.start_with?('/')
+ end
+
end
end
end
@@ -107,5 +107,21 @@ module Guard
end
+ describe '#absolute?' do
+
+ context 'when absolute' do
+ it 'returns true' do
+ Copy::Target.new('/absolute/path').should be_absolute
+ end
+ end
+
+ context 'when not absolute' do
+ it 'returns false' do
+ Copy::Target.new('relative/path').should_not be_absolute
+ end
+ end
+
+ end
+
end
end
View
@@ -135,6 +135,15 @@ module Guard
expect { guard.start }.to throw_symbol(:task_has_failed)
end
+ it 'throws :task_has_failed when :to is absolute without option' do
+ dir('source')
+ guard = Copy.new([], :from => 'source', :to => '/absolute/path')
+ UI.should_receive(:error).with('Guard::Copy - :to contains an absolute path:')
+ UI.should_receive(:error).with(' /absolute/path')
+ UI.should_receive(:error).with('Set the :absolute option to allow absolute target paths')
+ expect { guard.start }.to throw_symbol(:task_has_failed)
+ end
+
end
end
@@ -211,6 +220,21 @@ module Guard
File.should be_file('t2/foo')
end
+ context 'when :absolute is true' do
+ it 'copies files to absolute paths' do
+ file('source/foo')
+ dir('/t1')
+ dir('/t2')
+ guard = Copy.new([], :from => 'source', :to => ['/t1', '/t2'], :absolute => true)
+ guard.start
+
+ guard.run_on_changes(['source/foo'])
+
+ File.should be_file('/t1/foo')
+ File.should be_file('/t2/foo')
+ end
+ end
+
context 'when :verbose is false (or nil)' do
it 'does not log copy operation' do
file('source/foo')
@@ -278,6 +302,21 @@ module Guard
end
end
+ context 'when :delete and :absolute are true' do
+ it 'deletes the absolute target files' do
+ file('source/foo')
+ file('/t1/foo')
+ file('/t2/foo')
+ guard = Copy.new([], :from => 'source', :to => ['/t1', '/t2'], :delete => true, :absolute => true)
+ guard.start
+
+ guard.run_on_removals(['source/foo'])
+
+ File.should_not be_file('/t1/foo')
+ File.should_not be_file('/t2/foo')
+ end
+ end
+
context 'when :delete is false (or nil)' do
it 'does not delete the target file' do
file('source/foo')

0 comments on commit 759e810

Please sign in to comment.