Tests for Git revision property, cleanup
[puppet_vcsrepo.git] / spec / unit / puppet / provider / vcsrepo / git_spec.rb
1 require 'pathname'; Pathname.new(__FILE__).realpath.ascend { |x| begin; require (x + 'spec_helper.rb'); break; rescue LoadError; end }
2
3 provider_class = Puppet::Type.type(:vcsrepo).provider(:git)
4
5 describe provider_class do
6
7   before :each do
8     @resource = stub("resource")
9     @provider = provider_class.new(@resource)
10     @path = '/tmp/vcsrepo'
11   end
12
13   describe 'when creating' do
14     context "when a source is given" do
15       context "and when a revision is given" do
16         it "should execute 'git clone' and 'git reset'" do
17           @resource.expects(:value).with(:path).returns(@path).at_least_once
18           @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
19           @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
20           @resource.expects(:value).with(:revision).returns('abcdef').at_least_once
21           Dir.expects(:chdir).with(@path).yields
22           @provider.expects('git').with('reset', '--hard', 'abcdef')
23           @provider.create
24         end        
25       end
26       context "and when a revision is not given" do
27         it "should just execute 'git clone'" do
28           @resource.expects(:value).with(:path).returns(@path).at_least_once
29           @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
30           @resource.expects(:value).with(:revision).returns(nil).at_least_once
31           @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
32           @provider.create
33         end        
34       end
35     end
36     context "when a source is not given" do
37       it "should execute 'git init'" do
38         @resource.expects(:value).with(:path).returns(@path).at_least_once
39         @resource.expects(:value).with(:source).returns(nil)
40         Dir.expects(:chdir).with(@path).yields
41         @provider.expects(:git).with('init')
42         @provider.create
43       end
44     end
45   end
46
47   describe 'when destroying' do
48     it "it should remove the directory" do
49       @resource.expects(:value).with(:path).returns(@path).at_least_once
50       FileUtils.expects(:rm_rf).with(@path)
51       @provider.destroy
52     end
53   end
54
55   describe "when checking existence" do
56     it "should check for the directory" do
57       @resource.expects(:value).with(:path).returns(@path)
58       File.expects(:directory?).with(@path)
59       @provider.exists?
60     end
61   end
62
63   describe "when checking the revision property" do
64     context "when given a non-SHA ref as the resource revision" do
65       context "when its SHA is not different than the curent SHA" do
66         it "should return the ref" do
67           @resource.expects(:value).with(:path).returns(@path).at_least_once
68           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
69           @resource.expects(:value).with(:revision).returns('a-tag').at_least_once
70           @provider.expects(:git).with('rev-parse', 'a-tag').returns('currentsha')
71           Dir.expects(:chdir).with(@path).twice.yields
72           @provider.revision.should == 'a-tag'
73         end
74       end
75       context "when its SHA is different than the current SHA" do
76         it "should return the current SHA" do
77           @resource.expects(:value).with(:path).returns(@path).at_least_once
78           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
79           @resource.expects(:value).with(:revision).returns('a-tag').at_least_once
80           @provider.expects(:git).with('rev-parse', 'a-tag').returns('othersha')
81           Dir.expects(:chdir).with(@path).twice.yields
82           @provider.revision.should == 'currentsha'
83         end          
84       end
85     end
86     context "when given a SHA ref as the resource revision" do
87       context "when it is the same as the current SHA" do
88         it "should return it" do
89           @resource.expects(:value).with(:path).returns(@path).at_least_once
90           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
91           @resource.expects(:value).with(:revision).returns('currentsha').at_least_once
92           @provider.expects(:git).with('rev-parse', 'currentsha').returns('currentsha')
93           Dir.expects(:chdir).with(@path).twice.yields
94           @provider.revision.should == 'currentsha'
95         end
96       end
97       context "when it is not the same as the current SHA" do
98         it "should return the current SHA" do
99           @resource.expects(:value).with(:path).returns(@path).at_least_once
100           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
101           @resource.expects(:value).with(:revision).returns('othersha').at_least_once
102           @provider.expects(:git).with('rev-parse', 'othersha').returns('othersha')
103           Dir.expects(:chdir).with(@path).twice.yields
104           @provider.revision.should == 'currentsha'
105         end
106       end
107     end
108   end
109   
110   describe "when setting the revision property" do
111     it "should use 'git fetch' and 'git reset'" do
112       @resource.expects(:value).with(:path).returns(@path).at_least_once
113       @provider.expects('git').with('fetch', 'origin')
114       Dir.expects(:chdir).with(@path).at_least_once.yields
115       @provider.expects('git').with('reset', '--hard', 'carcar')
116       @provider.revision = 'carcar'
117     end
118   end
119
120 end