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 expect { provider.create }.to 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))
180 context "when the path is not empty and not a repository" do
181 it "should raise an exception" do
182 provider.expects(:path_exists?).returns(true)
183 provider.expects(:path_empty?).returns(false)
184 provider.expects(:working_copy_exists?).returns(false)
185 expect { provider.create }.to raise_error(Puppet::Error)
191 context 'destroying' do
192 it "it should remove the directory" do
198 context "checking the revision property" do
200 expects_chdir('/tmp/test')
201 resource[:revision] = 'currentsha'
202 resource.delete(:source)
203 provider.stubs(:git).with('config', 'remote.origin.url').returns('')
204 provider.stubs(:git).with('fetch', 'origin') # FIXME
205 provider.stubs(:git).with('fetch', '--tags', 'origin')
206 provider.stubs(:git).with('rev-parse', 'HEAD').returns('currentsha')
207 provider.stubs(:git).with('branch', '-a').returns(branch_a_list(resource.value(:revision)))
208 provider.stubs(:git).with('tag', '-l').returns("Hello")
211 context "when its SHA is not different than the current SHA" do
212 it "should return the ref" do
213 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
214 expect(provider.revision).to eq(resource.value(:revision))
218 context "when its SHA is different than the current SHA" do
219 it "should return the current SHA" do
220 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('othersha')
221 expect(provider.revision).to eq(resource.value(:revision))
225 context "when its a ref to a remote head" do
226 it "should return the revision" do
227 provider.stubs(:git).with('branch', '-a').returns(" remotes/origin/#{resource.value(:revision)}")
228 provider.expects(:git).with('rev-parse', "origin/#{resource.value(:revision)}").returns("newsha")
229 expect(provider.revision).to eq(resource.value(:revision))
233 context "when its a ref to non existant remote head" do
235 provider.expects(:git).with('branch', '-a').returns(branch_a_list)
236 provider.expects(:git).with('rev-parse', '--revs-only', resource.value(:revision)).returns('')
238 expect { provider.revision }.to raise_error(Puppet::Error, /not a local or remote ref$/)
242 context "when the source is modified" do
243 it "should update the origin url" do
244 resource[:source] = 'git://git@foo.com/bar.git'
245 provider.expects(:git).with('config', 'remote.origin.url').returns('old')
246 provider.expects(:git).with('config', 'remote.origin.url', 'git://git@foo.com/bar.git')
247 provider.expects(:git).with('rev-parse', resource.value(:revision)).returns('currentsha')
248 expect(provider.revision).to eq(resource.value(:revision))
253 context "setting the revision property" do
257 context "when it's an existing local branch" do
258 it "should use 'git fetch' and 'git reset'" do
259 resource[:revision] = 'feature/foo'
260 provider.expects(:update_submodules)
261 provider.expects(:git).with('branch', '-a').at_least_once.returns(branch_a_list(resource.value(:revision)))
262 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
263 provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}")
264 provider.revision = resource.value(:revision)
267 context "when it's a remote branch" do
268 it "should use 'git fetch' and 'git reset'" do
269 resource[:revision] = 'only/remote'
270 provider.expects(:update_submodules)
271 provider.expects(:git).with('branch', '-a').at_least_once.returns(resource.value(:revision))
272 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
273 provider.expects(:git).with('reset', '--hard', "origin/#{resource.value(:revision)}")
274 provider.revision = resource.value(:revision)
277 context "when it's a commit or tag" do
278 it "should use 'git fetch' and 'git reset'" do
279 resource[:revision] = 'a-commit-or-tag'
280 provider.expects(:git).with('branch', '-a').at_least_once.returns(fixture(:git_branch_a))
281 provider.expects(:git).with('checkout', '--force', resource.value(:revision))
282 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
283 provider.expects(:git).with('branch', '-a').returns(fixture(:git_branch_a))
284 provider.expects(:git).with('submodule', 'update', '--init', '--recursive')
285 provider.revision = resource.value(:revision)
290 context "updating references" do
291 it "should use 'git fetch --tags'" do
292 resource.delete(:source)
294 provider.expects(:git).with('config', 'remote.origin.url').returns('')
295 provider.expects(:git).with('fetch', 'origin')
296 provider.expects(:git).with('fetch', '--tags', 'origin')
297 provider.update_references
301 describe 'latest?' do
302 context 'when true' do
304 provider.expects(:revision).returns('testrev')
305 provider.expects(:latest_revision).returns('testrev')
306 expect(provider.latest?).to be_truthy
309 context 'when false' do
311 provider.expects(:revision).returns('master')
312 provider.expects(:latest_revision).returns('testrev')
313 expect(provider.latest?).to be_falsey
318 describe 'convert_working_copy_to_bare' do
320 FileUtils.expects(:mv).returns(true)
321 FileUtils.expects(:rm_rf).returns(true)
322 FileUtils.expects(:mv).returns(true)
324 provider.instance_eval { convert_working_copy_to_bare }
328 describe 'convert_bare_to_working_copy' do
330 FileUtils.expects(:mv).returns(true)
331 FileUtils.expects(:mkdir).returns(true)
332 FileUtils.expects(:mv).returns(true)
333 provider.expects(:commits_in?).returns(true)
334 # If you forget to stub these out you lose 3 hours of rspec work.
335 provider.expects(:reset).with('HEAD').returns(true)
336 provider.expects(:git_with_identity).returns(true)
337 provider.expects(:update_owner_and_excludes).returns(true)
339 provider.instance_eval { convert_bare_to_working_copy }