3 describe Puppet::Type.type(:vcsrepo).provider(:git_provider) do
4 def branch_a_list(include_branch = nil?)
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 raise an error" do
94 resource[:ensure] = :bare
95 expect { provider.create }.to raise_error Puppet::Error, /cannot set a revision.+bare/i
98 context "without revision" do
99 it "should just execute 'git clone --bare'" do
100 resource[:ensure] = :bare
101 resource.delete(:revision)
102 provider.expects(:git).with('clone', '--bare', resource.value(:source), resource.value(:path))
108 context "when a source is not given" do
109 context "when the path does not exist" do
110 it "should execute 'git init'" do
111 resource[:ensure] = :present
112 resource.delete(:source)
115 expects_directory?(false)
117 provider.expects(:bare_exists?).returns(false)
118 provider.expects(:git).with('init')
123 context "when the path is a bare repository" do
124 it "should convert it to a working copy" do
125 resource[:ensure] = :present
126 resource.delete(:source)
127 provider.expects(:bare_exists?).returns(true)
128 provider.expects(:convert_bare_to_working_copy)
133 context "when the path is not empty and not a repository" do
134 it "should raise an exception" do
135 provider.expects(:path_exists?).returns(true)
136 provider.expects(:path_empty?).returns(false)
137 proc { provider.create }.should raise_error(Puppet::Error)
142 context "when the path does not exist" do
143 it "should execute 'git init --bare'" do
144 resource[:ensure] = :bare
145 resource.delete(:source)
146 resource.delete(:revision)
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 resource.delete(:revision)
161 provider.expects(:working_copy_exists?).returns(true)
162 provider.expects(:convert_working_copy_to_bare)
165 it "should clone overtop it using force" do
166 resource[:force] = true
167 Dir.expects(:chdir).with('/').at_least_once.yields
168 Dir.expects(:chdir).with('/tmp/test').at_least_once.yields
169 provider.expects(:path_exists?).returns(true)
170 provider.expects(:path_empty?).returns(false)
172 provider.expects(:git).with('clone',resource.value(:source), resource.value(:path))
173 provider.expects(:update_submodules)
174 provider.expects(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
175 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
178 it "should warn about destroying it using force and noop attribute" do
179 resource[:force] = true
180 resource[:noop] = true
181 resource.delete(:revision)
182 provider.expects(:working_copy_exists?).returns(true)
184 provider.expects(:destroy).never
185 provider.expects(:create).never
186 Puppet::Type::Vcsrepo::Ensure.any_instance.expects(:send_log).with(:notice, "Noop Mode - Would have deleted repository and re-created from latest")
187 provider.resource.retrieve
189 it "should warn about destroying it using force and global noop" do
190 resource[:force] = true
192 resource.delete(:revision)
193 provider.expects(:working_copy_exists?).returns(true)
195 provider.expects(:destroy).never
196 provider.expects(:create).never
197 Puppet::Type::Vcsrepo::Ensure.any_instance.expects(:send_log).with(:notice, "Noop Mode - Would have deleted repository and re-created from latest")
198 provider.resource.retrieve
202 context "when the path is not empty and not a repository" do
203 it "should raise an exception" do
204 provider.expects(:path_exists?).returns(true)
205 provider.expects(:path_empty?).returns(false)
206 provider.expects(:working_copy_exists?).returns(false)
207 proc { provider.create }.should raise_error(Puppet::Error)
213 context 'destroying' do
214 it "it should remove the directory" do
220 context "checking the revision property" do
222 expects_chdir('/tmp/test')
223 resource[:revision] = 'currentsha'
224 resource.delete(:source)
225 provider.stubs(:git).with('config', 'remote.origin.url').returns('')
226 provider.stubs(:git).with('fetch', 'origin') # FIXME
227 provider.stubs(:git).with('fetch', '--tags', 'origin')
228 provider.stubs(:git).with('rev-parse', 'HEAD').returns('currentsha')
229 provider.stubs(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
230 provider.stubs(:git).with('tag', '-l').returns("Hello")
233 context "when its SHA is not different than the current SHA" do
234 it "should return the ref" do
235 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
236 provider.revision.should == resource.value(:revision)
240 context "when its SHA is different than the current SHA" do
241 it "should return the current SHA" do
242 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('othersha')
243 provider.revision.should == resource.value(:revision)
247 context "when its a ref to a remote head" do
248 it "should return the revision" do
249 provider.stubs(:git).with('branch', '-a').returns(" remotes/origin/#{resource.value(:revision)}")
250 provider.expects(:git).with('rev-parse', "origin/#{resource.value(:revision)}").returns("newsha")
251 provider.revision.should == resource.value(:revision)
255 context "when its a ref to non existant remote head" do
257 provider.expects(:git).with('branch', '-a').returns(branch_a_list)
258 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('')
260 expect { provider.revision }.to raise_error(Puppet::Error, /not a local or remote ref$/)
264 context "when the source is modified" do
265 it "should update the origin url" do
266 resource[:source] = 'git://git@foo.com/bar.git'
267 provider.expects(:git).with('config', 'remote.origin.url').returns('old')
268 provider.expects(:git).with('config', 'remote.origin.url', 'git://git@foo.com/bar.git')
269 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
270 provider.revision.should == resource.value(:revision)
275 context "setting the revision property" do
279 context "when it's an existing local branch" do
280 it "should use 'git fetch' and 'git reset'" do
281 resource[:revision] = 'feature/foo'
282 provider.expects(:update_submodules)
283 provider.expects(:git).with('branch', '-a').at_least_once.returns(branch_a_list(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 remote branch" do
290 it "should use 'git fetch' and 'git reset'" do
291 resource[:revision] = 'only/remote'
292 provider.expects(:update_submodules)
293 provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision))
294 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
295 provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}")
296 provider.revision = resource.value(:revision)
299 context "when it's a commit or tag" do
300 it "should use 'git fetch' and 'git reset'" do
301 resource[:revision] = 'a-commit-or-tag'
302 provider.expects(:git).with('branch', '-a').at_least_once.returns(fixture(:git_branch_a))
303 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
304 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
305 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
306 provider.expects(:git).with('submodule', 'update', '--init', '--recursive')
307 provider.revision = resource.value(:revision)
312 context "updating references" do
313 it "should use 'git fetch --tags'" do
314 resource.delete(:source)
316 provider.expects(:git).with('config', 'remote.origin.url').returns('')
317 provider.expects(:git).with('fetch', 'origin')
318 provider.expects(:git).with('fetch', '--tags', 'origin')
319 provider.update_references
323 context "checking if revision" do
326 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
328 context "is a local branch" do
329 context "when it's listed in 'git branch -a'" do
330 it "should return true" do
331 resource[:revision] = 'feature/foo'
332 provider.should be_local_branch_revision
335 context "when it's not listed in 'git branch -a'" do
336 it "should return false" do
337 resource[:revision] = 'feature/notexist'
338 provider.should_not be_local_branch_revision
342 context "is a remote branch" do
343 context "when it's listed in 'git branch -a' with an 'origin/' prefix" do
344 it "should return true" do
345 resource[:revision] = 'only/remote'
346 provider.should be_remote_branch_revision
349 context "when it's not listed in 'git branch -a' with an 'origin/' prefix" do
350 it "should return false" do
351 resource[:revision] = 'only/local'
352 provider.should_not be_remote_branch_revision
358 describe 'latest?' do
359 context 'when true' do
361 provider.expects(:revision).returns('testrev')
362 provider.expects(:latest_revision).returns('testrev')
363 provider.latest?.should be_true
366 context 'when false' do
368 provider.expects(:revision).returns('master')
369 provider.expects(:latest_revision).returns('testrev')
370 provider.latest?.should be_false
375 describe 'convert_working_copy_to_bare' do
377 FileUtils.expects(:mv).returns(true)
378 FileUtils.expects(:rm_rf).returns(true)
379 FileUtils.expects(:mv).returns(true)
381 provider.instance_eval { convert_working_copy_to_bare }
385 describe 'convert_bare_to_working_copy' do
387 FileUtils.expects(:mv).returns(true)
388 FileUtils.expects(:mkdir).returns(true)
389 FileUtils.expects(:mv).returns(true)
390 provider.expects(:commits_in?).returns(true)
391 # If you forget to stub these out you lose 3 hours of rspec work.
392 provider.expects(:reset).with('HEAD').returns(true)
393 provider.expects(:git_with_identity).returns(true)
394 provider.expects(:update_owner_and_excludes).returns(true)
396 provider.instance_eval { convert_bare_to_working_copy }