Merge pull request #273 from hunner/fix_trustcert
[puppet_vcsrepo.git] / spec / unit / puppet / provider / vcsrepo / svn_spec.rb
index 3af06f2..6a37c20 100644 (file)
-require 'pathname'; Pathname.new(__FILE__).realpath.ascend { |x| begin; require (x + 'spec_helper.rb'); break; rescue LoadError; end }
+require 'spec_helper'
 
-provider_class = Puppet::Type.type(:vcsrepo).provider(:svn)
+describe Puppet::Type.type(:vcsrepo).provider(:svn) do
 
-describe provider_class do
+  let(:resource) { Puppet::Type.type(:vcsrepo).new({
+    :name     => 'test',
+    :ensure   => :present,
+    :provider => :svn,
+    :path     => '/tmp/vcsrepo',
+  })}
+
+  let(:provider) { resource.provider }
 
   before :each do
-    @resource = stub("resource")
-    @provider = provider_class.new(@resource)
-    @path = '/tmp/vcsrepo'
+    Puppet::Util.stubs(:which).with('git').returns('/usr/bin/git')
   end
 
-  context 'when creating' do
-    context "when a source is given" do
-      context "and when a revision is given" do
-        it "should execute 'svn checkout' with a revision" do
-          @resource.expects(:value).with(:path).returns(@path).at_least_once
-          @resource.expects(:value).with(:source).returns('svn://example.com/repo').at_least_once
-          @resource.expects(:value).with(:revision).returns('1234').at_least_once
-          @provider.expects(:svn).with('checkout', '-r', '1234', 'svn://example.com/repo', @path)
-          @provider.create
-        end        
+  describe 'creating' do
+    context 'with source and revision' do
+      it "should execute 'svn checkout' with a revision" do
+        resource[:source] = 'exists'
+        resource[:revision] = '1'
+        provider.expects(:svn).with('--non-interactive', 'checkout', '-r',
+          resource.value(:revision),
+          resource.value(:source),
+          resource.value(:path))
+        provider.create
+      end
+    end
+    context 'with source' do
+      it "should just execute 'svn checkout' without a revision" do
+        resource[:source] = 'exists'
+        provider.expects(:svn).with('--non-interactive', 'checkout',
+          resource.value(:source),
+          resource.value(:path))
+        provider.create
       end
-      context "and when a revision is not given" do
-        it "should just execute 'svn checkout' without a revision" do
-          @resource.expects(:value).with(:path).returns(@path).at_least_once
-          @resource.expects(:value).with(:source).returns('svn://example.com/repo').at_least_once
-          @resource.expects(:value).with(:revision).returns(nil).at_least_once
-          @provider.expects(:svn).with('checkout','svn://example.com/repo', @path)
-          @provider.create
-        end        
+    end
+
+    context 'with fstype' do
+      it "should execute 'svnadmin create' with an '--fs-type' option" do
+        resource[:fstype] = 'ext4'
+        provider.expects(:svnadmin).with('create', '--fs-type',
+                                          resource.value(:fstype),
+                                          resource.value(:path))
+        provider.create
       end
     end
-    context "when a source is not given" do
-      context "when a fstype is given" do
-        it "should execute 'svnadmin create' with an '--fs-type' option" do
-          @resource.expects(:value).with(:path).returns(@path).at_least_once
-          @resource.expects(:value).with(:fstype).returns('fsfs').at_least_once
-          @resource.expects(:value).with(:source).returns(nil)
-          @provider.expects(:svnadmin).with('create', '--fs-type', 'fsfs', @path)
-          @provider.create
-        end
+    context 'without fstype' do
+      it "should execute 'svnadmin create' without an '--fs-type' option" do
+        provider.expects(:svnadmin).with('create', resource.value(:path))
+        provider.create
       end
-      context "when a fstype is not given" do
-        it "should execute 'svnadmin create' without an '--fs-type' option" do
-          @resource.expects(:value).with(:path).returns(@path).at_least_once
-          @resource.expects(:value).with(:source).returns(nil)
-          @resource.expects(:value).with(:fstype).returns(nil).at_least_once
-          @provider.expects(:svnadmin).with('create', @path)
-          @provider.create
-        end
+    end
+    
+    context "with depth" do
+      it "should execute 'svn checkout' with a depth" do
+        resource[:source] = 'exists'
+        resource[:depth] = 'infinity'
+        provider.expects(:svn).with('--non-interactive', 'checkout', '--depth', 'infinity',
+          resource.value(:source),
+          resource.value(:path))
+        provider.create
+      end
+    end
+    
+    context "with trust_server_cert" do
+      it "should execute 'svn checkout' without a trust-server-cert" do
+        resource[:source] = 'exists'
+        resource[:trust_server_cert] = :false
+        provider.expects(:svn).with('--non-interactive', 'checkout',
+          resource.value(:source),
+          resource.value(:path))
+        provider.create
+      end
+      it "should execute 'svn checkout' with a trust-server-cert" do
+        resource[:source] = 'exists'
+        resource[:trust_server_cert] = :true
+        provider.expects(:svn).with('--non-interactive', '--trust-server-cert', 'checkout',
+          resource.value(:source),
+          resource.value(:path))
+        provider.create
       end
     end
   end
 
-  context 'when destroying' do
+  describe 'destroying' do
     it "it should remove the directory" do
-      @resource.expects(:value).with(:path).returns(@path).at_least_once
-      FileUtils.expects(:rm_rf).with(@path)
-      @provider.destroy
+      expects_rm_rf
+      provider.destroy
     end
   end
 
-  context "when checking existence" do
+  describe "checking existence" do
     it "should check for the directory" do
-      @resource.expects(:value).with(:path).returns(@path)
-      File.expects(:directory?).with(@path)
-      @provider.exists?
+      expects_directory?(true, resource.value(:path))
+      expects_directory?(true, File.join(resource.value(:path), '.svn'))
+      provider.exists?
+    end
+  end
+
+  describe "checking the revision property" do
+    before do
+      provider.expects(:svn).with('--non-interactive', 'info').returns(fixture(:svn_info))
+    end
+    it "should use 'svn info'" do
+      expects_chdir
+      expect(provider.revision).to eq('4') # From 'Revision', not 'Last Changed Rev'
     end
   end
 
-  describe "revision property" do
-    context "when checking" do
-      it "should use 'svn info'" do
-        @resource.expects(:value).with(:path).returns(@path)
-        p fixture(:svn_info)[/^Revision:\s+(\d+)/m, 1]
-        @provider.expects('svn').with('info').returns(fixture(:svn_info))
-        Dir.expects(:chdir).with(@path).yields
-        @provider.revision.should == '4'
+  describe "setting the revision property" do
+    before do
+      @revision = '30'
+    end
+    context 'with conflict' do
+      it "should use 'svn update'" do
+        resource[:conflict] = 'theirs-full'
+        expects_chdir
+        provider.expects(:svn).with('--non-interactive', 'update',
+                                    '-r', @revision,
+                                    '--accept', resource.value(:conflict))
+        provider.revision = @revision
       end
     end
-    context "when setting" do
+    context 'without conflict' do
       it "should use 'svn update'" do
-        @resource.expects(:value).with(:path).returns(@path)
-        @provider.expects('svn').with('update', '-r', '30')
-        Dir.expects(:chdir).with(@path).yields
-        @provider.revision = '30'
+        expects_chdir
+        provider.expects(:svn).with('--non-interactive', 'update', '-r', @revision)
+        provider.revision = @revision
+      end
+    end
+  end
+
+  describe "setting the revision property and repo source" do
+    before do
+      @revision = '30'
+    end
+    context 'with conflict' do
+      it "should use 'svn switch'" do
+        resource[:source] = 'an-unimportant-value'
+        resource[:conflict] = 'theirs-full'
+        expects_chdir
+        provider.expects(:svn).with('--non-interactive', 'switch',
+                                    '-r', @revision, 'an-unimportant-value',
+                                    '--accept', resource.value(:conflict))
+        provider.revision = @revision
+      end
+    end
+    context 'without conflict' do
+      it "should use 'svn switch'" do
+        resource[:source] = 'an-unimportant-value'
+        expects_chdir
+        provider.expects(:svn).with('--non-interactive', 'switch', '-r', @revision, 'an-unimportant-value')
+        provider.revision = @revision
       end
     end
   end