From 411111b5ff88b682d7e9cc54a47573bccc3e3edc Mon Sep 17 00:00:00 2001 From: John Iacona Date: Wed, 28 Aug 2013 14:32:05 -0400 Subject: update git provider to handle checking out into an existing (empty) dir --- lib/puppet/provider/vcsrepo.rb | 5 +++++ lib/puppet/provider/vcsrepo/git.rb | 2 +- spec/unit/puppet/provider/vcsrepo/git_spec.rb | 6 ++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/puppet/provider/vcsrepo.rb b/lib/puppet/provider/vcsrepo.rb index 2c026ba..05b92f8 100644 --- a/lib/puppet/provider/vcsrepo.rb +++ b/lib/puppet/provider/vcsrepo.rb @@ -17,6 +17,11 @@ class Puppet::Provider::Vcsrepo < Puppet::Provider File.directory?(@resource.value(:path)) end + def path_empty? + # Path is empty if the only entries are '.' and '..' + Dir.entries(@resource.value(:path)).size == 2 + end + # Note: We don't rely on Dir.chdir's behavior of automatically returning the # value of the last statement -- for easier stubbing. def at_path(&block) #:nodoc: diff --git a/lib/puppet/provider/vcsrepo/git.rb b/lib/puppet/provider/vcsrepo/git.rb index 58330f6..47e84d2 100644 --- a/lib/puppet/provider/vcsrepo/git.rb +++ b/lib/puppet/provider/vcsrepo/git.rb @@ -147,7 +147,7 @@ Puppet::Type.type(:vcsrepo).provide(:git, :parent => Puppet::Provider::Vcsrepo) end def check_force - if path_exists? + if path_exists? and not path_empty? if @resource.value(:force) notice "Removing %s to replace with vcsrepo." % @resource.value(:path) destroy diff --git a/spec/unit/puppet/provider/vcsrepo/git_spec.rb b/spec/unit/puppet/provider/vcsrepo/git_spec.rb index 68b6c0a..1a6d008 100644 --- a/spec/unit/puppet/provider/vcsrepo/git_spec.rb +++ b/spec/unit/puppet/provider/vcsrepo/git_spec.rb @@ -74,9 +74,10 @@ describe_provider :vcsrepo, :git, :resource => {:path => '/tmp/vcsrepo'} do end end - context "when the path is not a repository" do + context "when the path is not empty and not a repository" do it "should raise an exception" do provider.expects(:path_exists?).returns(true) + provider.expects(:path_empty?).returns(false) proc { provider.create }.should raise_error(Puppet::Error) end end @@ -102,9 +103,10 @@ describe_provider :vcsrepo, :git, :resource => {:path => '/tmp/vcsrepo'} do end end - context "when the path is not a repository" do + context "when the path is not empty and not a repository" do it "should raise an exception" do expects_directory?(true) + provider.expects(:path_empty?).returns(false) proc { provider.create }.should raise_error(Puppet::Error) end end -- cgit v1.2.3 From e69c6eac89fbbbd5c459388eef30b1320ab9d826 Mon Sep 17 00:00:00 2001 From: John Iacona Date: Wed, 28 Aug 2013 16:34:57 -0400 Subject: change path_empty? to not do full directory listing --- lib/puppet/provider/vcsrepo.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/vcsrepo.rb b/lib/puppet/provider/vcsrepo.rb index 05b92f8..8793e63 100644 --- a/lib/puppet/provider/vcsrepo.rb +++ b/lib/puppet/provider/vcsrepo.rb @@ -19,7 +19,10 @@ class Puppet::Provider::Vcsrepo < Puppet::Provider def path_empty? # Path is empty if the only entries are '.' and '..' - Dir.entries(@resource.value(:path)).size == 2 + d = Dir.new(@resource.value(:path)) + d.read # should return '.' + d.read # should return '..' + d.read.nil? end # Note: We don't rely on Dir.chdir's behavior of automatically returning the -- cgit v1.2.3