Permalink
Browse files

create target directory when :mkpath is set

This change addresses issue #10.

  * move resolution failure warning to Target class
  * use Target#pattern without checking file system when :mkpath is true
  • Loading branch information...
1 parent af60296 commit 414c484b35256e015597f5de32a5468714a01f6c @marcisme committed Dec 11, 2012
Showing with 69 additions and 45 deletions.
  1. +0 −1 features/copy.feature
  2. +3 −7 lib/guard/copy.rb
  3. +29 −13 lib/guard/copy/target.rb
  4. +36 −15 spec/guard/copy/target_spec.rb
  5. +1 −9 spec/guard/copy_spec.rb
View
1 features/copy.feature
@@ -73,7 +73,6 @@ Feature: Copy files
Scenario: Create target directory on demand
Given a directory named "source/nes/ted/dir/ectory"
- And a directory named "target"
And I have run guard with this Guardfile:
"""
guard :copy, :from => 'source', :to => 'target', :mkpath => true
View
10 lib/guard/copy.rb
@@ -30,7 +30,7 @@ def start
validate_presence_of(:to)
validate_to_patterns_are_not_absolute
validate_to_does_not_include_from
- resolve_targets
+ resolve_targets!
validate_no_targets_are_files
display_target_paths
@@ -151,12 +151,8 @@ def validate_to_file(to_file)
end
end
- def resolve_targets
- @targets.each do |target|
- unless target.resolve
- UI.warning("Guard::Copy - '#{target.pattern}' does not match a valid directory")
- end
- end
+ def resolve_targets!
+ @targets.each(&:resolve!)
end
def validate_no_targets_are_files
View
42 lib/guard/copy/target.rb
@@ -2,7 +2,7 @@ module Guard
class Copy
class Target
- attr_reader :pattern, :options, :paths
+ attr_reader :pattern, :paths, :glob
# Initialize a new target
#
@@ -13,28 +13,44 @@ def initialize(pattern, options = {})
raise ArgumentError, 'pattern cannot be nil' unless pattern
raise ArgumentError, 'pattern cannot be empty' if pattern.empty?
@pattern = pattern
- @options = {
- :glob => :all
- }.merge(options)
+ @glob = options[:glob] || :all
+ @expand_pattern = !options[:mkpath]
@paths = []
end
# Resolve the target into one or more paths
- #
- # @return [Boolean] true if the pattern resolved to any paths
- def resolve
- @paths.clear
- if @options[:glob] == :newest
- @paths.concat(Dir[@pattern].sort_by { |f| File.mtime(f) }.last(1))
+ def resolve!
+ paths.clear
+ if expand_pattern?
+ expand_pattern
else
- @paths.concat(Dir[@pattern])
+ paths << pattern
end
- @paths.any?
+ warn_if_empty
end
# @return [Boolean] true if the pattern is an absolute path
def absolute?
- @pattern.start_with?('/')
+ pattern.start_with?('/')
+ end
+
+ private
+
+ def expand_pattern?; @expand_pattern; end
+
+ def expand_pattern
+ case glob
+ when :newest
+ paths.concat(Dir[pattern].sort_by { |f| File.mtime(f) }.last(1))
+ when :all
+ paths.concat(Dir[pattern])
+ end
+ end
+
+ def warn_if_empty
+ unless paths.any?
+ UI.warning("Guard::Copy - '#{pattern}' does not match a valid directory")
+ end
end
end
View
51 spec/guard/copy/target_spec.rb
@@ -18,7 +18,7 @@ module Guard
end
it 'sets default :glob option to :all' do
- target.options[:glob].should == :all
+ target.glob.should == :all
end
end
@@ -37,7 +37,7 @@ module Guard
end
- describe '#resolve' do
+ describe '#resolve!' do
context 'with valid pattern' do
@@ -46,13 +46,9 @@ module Guard
target = Copy::Target.new('target')
end
- it 'returns true' do
- target.resolve.should be_true
- end
-
it 'resets paths' do
- target.resolve
- target.resolve
+ target.resolve!
+ target.resolve!
target.paths.size.should == 1
end
@@ -62,14 +58,30 @@ module Guard
let(:target) { Copy::Target.new('non_existent_target') }
- it 'returns false' do
- target.resolve.should be_false
- end
-
it 'does not set any paths' do
+ target.resolve!
target.paths.size.should == 0
end
+ it 'warns' do
+ UI.should_receive(:warning).with("Guard::Copy - 'non_existent_target' does not match a valid directory")
+ target.resolve!
+ end
+
+ end
+
+ context 'with invalid pattern and :mkpath option' do
+
+ let(:target) do
+ Copy::Target.new('invalid_path', :mkpath => true)
+ end
+
+ it 'resets paths' do
+ target.resolve!
+ target.resolve!
+ target.paths.size.should == 1
+ end
+
end
end
@@ -80,7 +92,7 @@ module Guard
it 'returns single path' do
dir('target')
target = Copy::Target.new('target')
- target.resolve
+ target.resolve!
target.paths.should == ['target']
end
end
@@ -90,7 +102,7 @@ module Guard
dir('t1')
dir('t2')
target = Copy::Target.new('t*')
- target.resolve
+ target.resolve!
target.paths.should == ['t1', 't2']
end
end
@@ -100,11 +112,20 @@ module Guard
dir('target_old', 1978)
dir('target_new', 2012)
target = Copy::Target.new('t*', :glob => :newest)
- target.resolve
+ target.resolve!
target.paths.should == ['target_new']
end
end
+ context 'with invalid pattern and :mkpath option' do
+ it 'returns invalid path' do
+ dir('invalid_path')
+ target = Copy::Target.new('invalid_path', :mkpath => true)
+ target.resolve!
+ target.paths.should == ['invalid_path']
+ end
+ end
+
end
describe '#absolute?' do
View
10 spec/guard/copy_spec.rb
@@ -119,14 +119,6 @@ module Guard
expect { guard.start }.to throw_symbol(:task_has_failed)
end
- it 'warns when a :to directory does not exist' do
- dir('source')
- dir('target')
- guard = Copy.new([], :from => 'source', :to => ['target', 'v*'])
- UI.should_receive(:warning).with("Guard::Copy - 'v*' does not match a valid directory")
- guard.start
- end
-
it 'throws :task_has_failed when :to contains a file' do
dir('source')
file('target')
@@ -151,7 +143,7 @@ module Guard
it 'resolves targets' do
dir('source')
guard = Copy.new([], :from => 'source', :to => ['t1', 't2', 't3'])
- guard.targets.each { |t| t.should_receive(:resolve) }
+ guard.targets.each { |t| t.should_receive(:resolve!) }
guard.start
end

0 comments on commit 414c484

Please sign in to comment.