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