ensure = latest to support reference tracking.\n\nWorking with tags, more work needed...
[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 "and when a source is given" do
15       before do
16         @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
17       end
18       context "and when ensure = present" do
19         before do
20           @resource.expects(:value).with(:ensure).returns(:present).at_least_once
21         end
22         context "and when a revision is given" do
23           it "should execute 'git clone' and 'git reset'" do
24             @resource.expects(:value).with(:path).returns(@path).at_least_once
25             @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
26             @resource.expects(:value).with(:revision).returns('abcdef').at_least_once
27             Dir.expects(:chdir).with(@path).yields
28             @provider.expects('git').with('reset', '--hard', 'abcdef')
29             @provider.create
30           end        
31         end
32         context "and when a revision is not given" do
33           it "should just execute 'git clone'" do
34             @resource.expects(:value).with(:path).returns(@path).at_least_once
35             @resource.expects(:value).with(:revision).returns(nil).at_least_once
36             @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
37             @provider.create
38           end        
39         end
40       end
41       context "and when ensure = bare" do
42         before do
43           @resource.expects(:value).with(:ensure).returns(:bare).at_least_once
44         end
45         context "and when a revision is given" do
46           it "should just execute 'git clone --bare'" do
47             @resource.expects(:value).with(:path).returns(@path).at_least_once
48             @resource.expects(:value).with(:revision).returns(nil).at_least_once
49             @provider.expects(:git).with('clone', '--bare', 'git://example.com/repo.git', @path)
50             @provider.create
51           end        
52         end
53         context "and when a revision is not given" do
54           it "should just execute 'git clone --bare'" do
55             @resource.expects(:value).with(:path).returns(@path).at_least_once
56             @resource.expects(:value).with(:revision).returns(nil).at_least_once
57             @provider.expects(:git).with('clone', '--bare', 'git://example.com/repo.git', @path)
58             @provider.create
59           end        
60         end
61       end
62
63     end
64     context "when a source is not given" do
65       before do
66         @resource.expects(:value).with(:path).returns(@path).at_least_once
67         @resource.expects(:value).with(:source).returns(nil)
68       end
69       context "when ensure = present" do
70         before { @resource.expects(:value).with(:ensure).returns(:present).at_least_once }
71         context "when the path does not exist" do
72           it "should execute 'git init'" do
73             Dir.expects(:mkdir).with(@path)
74             Dir.expects(:chdir).with(@path).yields
75             @provider.expects(:bare_exists?).returns(false)
76             File.expects(:directory?).with(@path).returns(false)
77             @provider.expects(:git).with('init')
78             @provider.create
79           end
80         end
81         context "when the path is a bare repository" do
82           it "should convert it to a working copy" do
83             @provider.expects(:bare_exists?).returns(true)
84             @provider.expects(:convert_bare_to_working_copy)
85             @provider.create
86           end
87         end
88         context "when the path is not a repository" do
89           it "should raise an exception" do
90             File.expects(:directory?).with(@path).returns(true)
91             @provider.expects(:bare_exists?).returns(false)
92             proc {
93               @provider.create
94             }.should raise_error(Puppet::Error)
95           end
96         end
97       end
98       context "when ensure = bare" do
99         before { @resource.expects(:value).with(:ensure).returns(:bare).at_least_once } 
100         context "when the path does not exist" do
101           it "should execute 'git init --bare'" do
102             Dir.expects(:chdir).with(@path).yields
103             File.expects(:directory?).with(@path).returns(false)
104             FileUtils.expects(:mkdir).with(@path)
105             @provider.expects(:working_copy_exists?).returns(false)
106             @provider.expects(:git).with('init', '--bare')
107             @provider.create
108           end
109         end
110         context "when the path is a working copy repository" do
111           it "should convert it to a bare repository" do
112             @provider.expects(:working_copy_exists?).returns(true)
113             @provider.expects(:convert_working_copy_to_bare)
114             @provider.create
115           end
116         end
117         context "when the path is not a repository" do
118           it "should raise an exception" do
119             File.expects(:directory?).with(@path).returns(true)
120             @provider.expects(:working_copy_exists?).returns(false)
121             proc {
122               @provider.create
123             }.should raise_error(Puppet::Error)
124           end
125         end
126       end
127     end
128   end
129
130   describe 'when destroying' do
131     it "it should remove the directory" do
132       @resource.expects(:value).with(:path).returns(@path).at_least_once
133       FileUtils.expects(:rm_rf).with(@path)
134       @provider.destroy
135     end
136   end
137
138   describe "when checking the revision property" do
139     context "when given a non-SHA ref as the resource revision" do
140       context "when its SHA is not different than the curent SHA" do
141         it "should return the ref" do
142           @resource.expects(:value).with(:path).returns(@path).at_least_once
143           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
144           @resource.expects(:value).with(:revision).returns('a-tag').at_least_once
145           @provider.expects(:git).with('rev-parse', 'a-tag').returns('currentsha')
146           Dir.expects(:chdir).with(@path).twice.yields
147           @provider.revision.should == 'a-tag'
148         end
149       end
150       context "when its SHA is different than the current SHA" do
151         it "should return the current SHA" do
152           @resource.expects(:value).with(:path).returns(@path).at_least_once
153           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
154           @resource.expects(:value).with(:revision).returns('a-tag').at_least_once
155           @provider.expects(:git).with('rev-parse', 'a-tag').returns('othersha')
156           Dir.expects(:chdir).with(@path).twice.yields
157           @provider.revision.should == 'currentsha'
158         end          
159       end
160     end
161     context "when given a SHA ref as the resource revision" do
162       context "when it is the same as the current SHA" do
163         it "should return it" do
164           @resource.expects(:value).with(:path).returns(@path).at_least_once
165           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
166           @resource.expects(:value).with(:revision).returns('currentsha').at_least_once
167           @provider.expects(:git).with('rev-parse', 'currentsha').returns('currentsha')
168           Dir.expects(:chdir).with(@path).twice.yields
169           @provider.revision.should == 'currentsha'
170         end
171       end
172       context "when it is not the same as the current SHA" do
173         it "should return the current SHA" do
174           @resource.expects(:value).with(:path).returns(@path).at_least_once
175           @provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
176           @resource.expects(:value).with(:revision).returns('othersha').at_least_once
177           @provider.expects(:git).with('rev-parse', 'othersha').returns('othersha')
178           Dir.expects(:chdir).with(@path).twice.yields
179           @provider.revision.should == 'currentsha'
180         end
181       end
182     end
183   end
184
185   describe "when setting the revision property" do
186     it "should use 'git fetch' and 'git reset'" do
187       @resource.expects(:value).with(:path).returns(@path).at_least_once
188       @provider.expects('git').with('pull', 'origin')
189       Dir.expects(:chdir).with(@path).at_least_once.yields
190       @provider.expects('git').with('reset', '--hard', 'carcar')
191       @provider.revision = 'carcar'
192     end
193   end
194
195   describe "when updating references" do
196     it "should use 'git fetch --tags'" do
197       @resource.expects(:value).with(:path).returns(@path).at_least_once
198       @provider.expects('git').with('fetch', '--tags', 'origin')
199       Dir.expects(:chdir).with(@path).at_least_once.yields
200       @provider.update_references
201     end    
202   end
203
204 end