3 describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do
4 def branch_a_list(include_branch)
7 #{"* master" unless include_branch.nil?}
8 #{"* " + include_branch unless !include_branch}
14 let(:resource) { Puppet::Type.type(:vcsrepo).new({
19 :source => 'git@repo',
24 let(:provider) { resource.provider }
27 Puppet::Util.stubs(:which).with('git').returns('/usr/bin/git')
31 context "with a revision that is a remote branch" do
32 it "should execute 'git clone' and 'git checkout -b'" do
33 resource[:revision] = 'only/remote'
34 Dir.expects(:chdir).with('/').at_least_once.yields
35 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
36 provider.expects(:git).with('clone', resource.value(:source), resource.value(:path))
37 provider.expects(:update_submodules)
38 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
39 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
44 context "with a remote not named 'origin'" do
45 it "should execute 'git clone --origin not_origin" do
46 resource[:remote] = 'not_origin'
47 Dir.expects(:chdir).with('/').at_least_once.yields
48 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
49 provider.expects(:git).with('clone', '--origin', 'not_origin', resource.value(:source), resource.value(:path))
50 provider.expects(:update_submodules)
51 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
52 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
57 context "with shallow clone enable" do
58 it "should execute 'git clone --depth 1'" do
59 resource[:revision] = 'only/remote'
61 Dir.expects(:chdir).with('/').at_least_once.yields
62 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
63 provider.expects(:git).with('clone', '--depth', '1', resource.value(:source), resource.value(:path))
64 provider.expects(:update_submodules)
65 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
66 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
71 context "with a revision that is not a remote branch" do
72 it "should execute 'git clone' and 'git reset --hard'" do
73 resource[:revision] = 'a-commit-or-tag'
74 Dir.expects(:chdir).with('/').at_least_once.yields
75 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
76 provider.expects(:git).with('clone', resource.value(:source), resource.value(:path))
77 provider.expects(:update_submodules)
78 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
79 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
83 it "should execute 'git clone' and submodule commands" do
84 resource.delete(:revision)
85 provider.expects(:git).with('clone', resource.value(:source), resource.value(:path))
86 provider.expects(:update_submodules)
91 context "with an ensure of bare" do
92 context "with revision" do
93 it "should just execute 'git clone --bare'" do
94 resource[:ensure] = :bare
95 provider.expects(:git).with('clone', '--bare', resource.value(:source), resource.value(:path))
99 context "without revision" do
100 it "should just execute 'git clone --bare'" do
101 resource[:ensure] = :bare
102 resource.delete(:revision)
103 provider.expects(:git).with('clone', '--bare', resource.value(:source), resource.value(:path))
109 context "when a source is not given" do
110 context "when the path does not exist" do
111 it "should execute 'git init'" do
112 resource[:ensure] = :present
113 resource.delete(:source)
116 expects_directory?(false)
118 provider.expects(:bare_exists?).returns(false)
119 provider.expects(:git).with('init')
124 context "when the path is a bare repository" do
125 it "should convert it to a working copy" do
126 resource[:ensure] = :present
127 resource.delete(:source)
128 provider.expects(:bare_exists?).returns(true)
129 provider.expects(:convert_bare_to_working_copy)
134 context "when the path is not empty and not a repository" do
135 it "should raise an exception" do
136 provider.expects(:path_exists?).returns(true)
137 provider.expects(:path_empty?).returns(false)
138 proc { provider.create }.should raise_error(Puppet::Error)
143 context "when the path does not exist" do
144 it "should execute 'git init --bare'" do
145 resource[:ensure] = :bare
146 resource.delete(:source)
149 expects_directory?(false)
150 provider.expects(:working_copy_exists?).returns(false)
151 provider.expects(:git).with('init', '--bare')
156 context "when the path is a working copy repository" do
157 it "should convert it to a bare repository" do
158 resource[:ensure] = :bare
159 resource.delete(:source)
160 provider.expects(:working_copy_exists?).returns(true)
161 provider.expects(:convert_working_copy_to_bare)
164 it "should clone overtop it using force" do
165 resource[:force] = true
166 Dir.expects(:chdir).with('/').at_least_once.yields
167 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
168 provider.expects(:path_exists?).returns(true)
169 provider.expects(:path_empty?).returns(false)
171 provider.expects(:git).with('clone',resource.value(:source), resource.value(:path))
172 provider.expects(:update_submodules)
173 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
174 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
179 context "when the path is not empty and not a repository" do
180 it "should raise an exception" do
181 provider.expects(:path_exists?).returns(true)
182 provider.expects(:path_empty?).returns(false)
183 provider.expects(:working_copy_exists?).returns(false)
184 proc { provider.create }.should raise_error(Puppet::Error)
190 context 'destroying' do
191 it "it should remove the directory" do
197 context "checking the revision property" do
199 expects_chdir('/tmp/test')
200 resource[:revision] = 'currentsha'
201 resource.delete(:source)
202 provider.expects(:git).with('rev-parse', 'HEAD').returns('currentsha')
205 context "when its SHA is not different than the current SHA" do
206 it "should return the ref" do
207 provider.expects(:git).with('config', 'remote.origin.url').returns('')
208 provider.expects(:git).with('fetch', 'origin') # FIXME
209 provider.expects(:git).with('fetch', '--tags', 'origin')
210 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('currentsha')
211 provider.expects(:git).with('tag', '-l').returns("Hello")
212 provider.revision.should == resource.value(:revision)
216 context "when its SHA is different than the current SHA" do
217 it "should return the current SHA" do
218 provider.expects(:git).with('config', 'remote.origin.url').returns('')
219 provider.expects(:git).with('fetch', 'origin') # FIXME
220 provider.expects(:git).with('fetch', '--tags', 'origin')
221 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('othersha')
222 provider.expects(:git).with('tag', '-l').returns("Hello")
223 provider.revision.should == 'currentsha'
227 context "when its a ref to a remote head" do
228 it "should return the revision" do
229 provider.expects(:git).with('config', 'remote.origin.url').returns('')
230 provider.expects(:git).with('fetch', 'origin') # FIXME
231 provider.expects(:git).with('fetch', '--tags', 'origin')
232 provider.expects(:git).with('tag', '-l').returns("Hello")
233 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('')
234 provider.expects(:git).with('ls-remote', '--heads', '--tags', 'origin', resource.value(:revision)).returns("newsha refs/heads/#{resource.value(:revision)}")
235 provider.revision.should == 'currentsha'
239 context "when its a ref to non existant remote head" do
241 provider.expects(:git).with('config', 'remote.origin.url').returns('')
242 provider.expects(:git).with('fetch', 'origin') # FIXME
243 provider.expects(:git).with('fetch', '--tags', 'origin')
244 provider.expects(:git).with('tag', '-l').returns("Hello")
245 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('')
246 provider.expects(:git).with('ls-remote', '--heads', '--tags', 'origin', resource.value(:revision)).returns('')
247 expect { provider.revision }.to raise_error(Puppet::Error, /not a local or remote ref$/)
251 context "when the source is modified" do
252 it "should update the origin url" do
253 resource[:source] = 'git://git@foo.com/bar.git'
254 provider.expects(:git).with('config', 'remote.origin.url').returns('old')
255 provider.expects(:git).with('config', 'remote.origin.url', 'git://git@foo.com/bar.git')
256 provider.expects(:git).with('fetch', 'origin') # FIXME
257 provider.expects(:git).with('fetch', '--tags', 'origin')
258 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('currentsha')
259 provider.expects(:git).with('tag', '-l').returns("Hello")
260 provider.revision.should == resource.value(:revision)
265 context "setting the revision property" do
269 context "when it's an existing local branch" do
270 it "should use 'git fetch' and 'git reset'" do
271 resource[:revision] = 'feature/foo'
272 provider.expects(:update_submodules)
273 provider.expects(:git).with('branch', '-a').at_least_once.returns(branch_a_list(resource.value(:revision)))
274 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
275 provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}")
276 provider.revision = resource.value(:revision)
279 context "when it's a remote branch" do
280 it "should use 'git fetch' and 'git reset'" do
281 resource[:revision] = 'only/remote'
282 provider.expects(:update_submodules)
283 provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision))
284 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
285 provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}")
286 provider.revision = resource.value(:revision)
289 context "when it's a commit or tag" do
290 it "should use 'git fetch' and 'git reset'" do
291 resource[:revision] = 'a-commit-or-tag'
292 provider.expects(:git).with('branch', '-a').at_least_once.returns(fixture(:git_branch_a))
293 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
294 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
295 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
296 provider.expects(:git).with('submodule', 'update', '--init', '--recursive')
297 provider.revision = resource.value(:revision)
302 context "updating references" do
303 it "should use 'git fetch --tags'" do
304 resource.delete(:source)
306 provider.expects(:git).with('config', 'remote.origin.url').returns('')
307 provider.expects(:git).with('fetch', 'origin')
308 provider.expects(:git).with('fetch', '--tags', 'origin')
309 provider.update_references
313 context "checking if revision" do
316 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
318 context "is a local branch" do
319 context "when it's listed in 'git branch -a'" do
320 it "should return true" do
321 resource[:revision] = 'feature/foo'
322 provider.should be_local_branch_revision
325 context "when it's not listed in 'git branch -a'" do
326 it "should return false" do
327 resource[:revision] = 'feature/notexist'
328 provider.should_not be_local_branch_revision
332 context "is a remote branch" do
333 context "when it's listed in 'git branch -a' with an 'origin/' prefix" do
334 it "should return true" do
335 resource[:revision] = 'only/remote'
336 provider.should be_remote_branch_revision
339 context "when it's not listed in 'git branch -a' with an 'origin/' prefix" do
340 it "should return false" do
341 resource[:revision] = 'only/local'
342 provider.should_not be_remote_branch_revision
348 context "retrieving the current revision" do
351 provider.expects(:git).with('branch','-a').returns("* foo")
354 it "will strip trailing newlines" do
355 provider.expects(:get_revision).with('origin/foo')
360 describe 'latest?' do
362 expects_chdir('/tmp/test')
364 context 'when true' do
366 provider.expects(:revision).returns('testrev')
367 provider.expects(:latest).returns('testrev')
368 provider.latest?.should be_true
371 context 'when false' do
373 provider.expects(:revision).returns('master')
374 provider.expects(:latest).returns('testrev')
375 provider.latest?.should be_false
382 provider.expects(:get_revision).returns('master')
385 context 'on master' do
387 provider.expects(:git).with('branch','-a').returns("* master")
388 provider.latest.should == 'master'
391 context 'no branch' do
393 provider.expects(:git).with('branch','-a').returns("* master")
395 provider.latest.should == 'master'
398 context 'feature/bar' do
400 provider.expects(:git).with('branch','-a').returns("* master")
401 provider.latest.should == 'master'
406 describe 'convert_working_copy_to_bare' do
408 FileUtils.expects(:mv).returns(true)
409 FileUtils.expects(:rm_rf).returns(true)
410 FileUtils.expects(:mv).returns(true)
412 provider.instance_eval { convert_working_copy_to_bare }
416 describe 'convert_bare_to_working_copy' do
418 FileUtils.expects(:mv).returns(true)
419 FileUtils.expects(:mkdir).returns(true)
420 FileUtils.expects(:mv).returns(true)
421 provider.expects(:commits_in?).returns(true)
422 # If you forget to stub these out you lose 3 hours of rspec work.
423 provider.expects(:reset).with('HEAD').returns(true)
424 provider.expects(:git_with_identity).returns(true)
425 provider.expects(:update_owner_and_excludes).returns(true)
427 provider.instance_eval { convert_bare_to_working_copy }