-
Notifications
You must be signed in to change notification settings - Fork 72
/
git_spec.rb
295 lines (258 loc) · 11.9 KB
/
git_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require File.expand_path('../spec_helper', __FILE__)
module Pod
module Downloader
describe 'Git' do
def fixture_url(name)
'file://' + fixture(name).to_s
end
before do
tmp_folder.rmtree if tmp_folder.exist?
end
describe 'In general' do
it 'checks out a specific commit' do
options = { :git => fixture('git-repo'), :commit => '7ad3a6c' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'first commit'
end
it 'checks out a specific branch' do
options = { :git => fixture('git-repo'), :branch => 'topic_branch' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'topic_branch'
end
it 'checks out a specific branch with forced encoding' do
options = { :git => fixture('git-repo'), :branch => '中文分支' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == '中文分支'
end
it 'checks out a specific tag' do
options = { :git => fixture('git-repo'), :tag => 'v1.0' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'v1.0'
end
it 'downloads the head of a repo' do
options = { :git => fixture('git-repo') }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download_head
Dir.chdir(tmp_folder) do
`git rev-list HEAD`.chomp.should.include '98cbf14'
end
end
it 'downloads the head of a repo if no specific options are provided' do
options = { :git => fixture('git-repo') }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
Dir.chdir(tmp_folder) do
`git rev-list HEAD`.chomp.should.include '98cbf14'
end
end
it "doesn't initializes submodules by default" do
options = { :git => fixture('git-repo'), :commit => 'd7f4104' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'added submodule'
tmp_folder('submodule/README').should.not.exist?
end
it 'initializes submodules when requested' do
FileUtils.rm_rf('/tmp/git-submodule-repo')
FileUtils.cp_r(fixture('git-submodule-repo'), '/tmp/')
options = { :git => fixture('git-repo'), :commit => 'd7f4104', :submodules => true }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'added submodule'
tmp_folder('submodule/README').read.strip.should == 'submodule'
FileUtils.rm_rf('/tmp/git-submodule-repo')
downloader.checkout_options.should == {
:git => fixture('git-repo'),
:commit => 'd7f410490dabf7a6bde665ba22da102c3acf1bd9',
:submodules => true,
}
end
it 'returns whether it supports the download of the head' do
options = { :git => fixture('git-repo') }
downloader = Downloader.for_target(tmp_folder('checkout'), options)
downloader.head_supported?.should.be.true
end
it 'returns whether the provided options are specific' do
Downloader.for_target('path', :git => 'url').options_specific?.should.be.false
Downloader.for_target('path', :git => 'url', :branch => '').options_specific?.should.be.false
Downloader.for_target('path', :git => 'url', :submodules => '').options_specific?.should.be.false
Downloader.for_target('path', :git => 'url', :commit => '').options_specific?.should.be.true
Downloader.for_target('path', :git => 'url', :tag => '').options_specific?.should.be.true
end
it 'returns the checked out revision' do
options = { :git => fixture('git-repo') }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
downloader.checkout_options.should == {
:git => fixture('git-repo'),
:commit => '98cbf14201a78b56c6b7290f6cac840a7597a1c2',
}
end
end
describe 'Shallow cloning' do
def ensure_only_one_ref(folder)
Dir.chdir(folder) do
`git rev-list --count HEAD`.strip.should == '1'
end
end
before do
FileUtils.rm_rf('/tmp/git-submodule-repo')
FileUtils.cp_r(fixture('git-submodule-repo'), '/tmp/')
end
it 'uses shallow clone' do
options = { :git => fixture_url('git-repo') }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
ensure_only_one_ref(tmp_folder)
end
it 'clones a specific branch' do
options = { :git => fixture_url('git-repo'), :branch => 'topic_branch' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'topic_branch'
ensure_only_one_ref(tmp_folder)
end
it 'clones a specific tag' do
options = { :git => fixture_url('git-repo'), :tag => 'v1.0' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
tmp_folder('README').read.strip.should == 'v1.0'
ensure_only_one_ref(tmp_folder)
end
it 'clones a specific commit' do
options = { :git => fixture_url('git-repo'), :commit => '407e385' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download
Dir.chdir(tmp_folder) do
`git rev-list HEAD`.chomp.should.include '407e385'
end
end
end
describe 'Robustness' do
it 'supports path contains quotes or spaces' do
options = { :git => fixture('git-repo'), :commit => '7ad3a6c' }
downloader = Downloader.for_target(tmp_folder_with_quotes, options)
downloader.download
tmp_folder_with_quotes('README').read.strip.should == 'first commit'
end
it "raises if it can't find the url" do
options = { :git => 'missing-repo' }
downloader = Downloader.for_target(tmp_folder, options)
lambda { downloader.download }.should.raise DownloaderError
end
it "raises if it can't find a commit" do
options = { :git => fixture('git-repo'), :commit => 'aaaaaa' }
downloader = Downloader.for_target(tmp_folder, options)
lambda { downloader.download }.should.raise DownloaderError
end
it "raises if it can't find a tag" do
options = { :git => fixture('git-repo'), :tag => 'aaaaaa' }
downloader = Downloader.for_target(tmp_folder, options)
lambda { downloader.download }.should.raise DownloaderError
end
it "raises if it can't find a reference" do
options = { :git => fixture('git-repo'), :commit => 'aaaaaa' }
downloader = Downloader.for_target(tmp_folder, options)
lambda { downloader.download }.should.raise DownloaderError
end
it 'is not confused by specific options in download head' do
options = { :git => fixture_url('git-repo'), :tag => 'v1.0' }
downloader = Downloader.for_target(tmp_folder, options)
downloader.download_head
Dir.chdir(tmp_folder) do
`git rev-list HEAD`.chomp.should.include '98cbf14'
end
end
describe 'will retry if a remote does not support shallow clones' do
it 'with git <= 2.10.x' do
options = { :git => fixture_url('git-repo'), :tag => 'v1.0' }
downloader = Downloader.for_target(tmp_folder, options)
message = '/usr/local/bin/git clone URL directory --single-branch ' \
"--depth 1\nCloning into 'directory'...\n" \
'fatal: dumb http transport does not support --depth'
dumb_remote_error = Pod::Downloader::DownloaderError.new(message)
downloader.stubs(:git!).raises(dumb_remote_error).then.returns(true)
should.not.raise { downloader.download }
end
it 'with git >= 2.11.x' do
options = { :git => fixture_url('git-repo'), :tag => 'v1.0' }
downloader = Downloader.for_target(tmp_folder, options)
message = '/usr/local/bin/git clone URL directory --single-branch ' \
"--depth 1\nCloning into 'directory'...\n" \
'fatal: dumb http transport does not support shallow capabilities'
dumb_remote_error = Pod::Downloader::DownloaderError.new(message)
downloader.stubs(:git!).raises(dumb_remote_error).then.returns(true)
should.not.raise { downloader.download }
end
end
end
describe ':commit_from_ls_remote' do
it 'finds commit for a branch' do
test_commit = 'abcde'
test_branch = 'stable'
test_output = "#{test_commit}\trefs/heads/#{test_branch}"
result = Git.send(:commit_from_ls_remote, test_output, test_branch)
result.should == test_commit
end
it 'returns nil for no match' do
test_commit = 'abcde'
test_branch = 'stable'
test_output = "#{test_commit}\trefs/heads/other_branch"
result = Git.send(:commit_from_ls_remote, test_output, test_branch)
result.should.nil?
end
it 'can match a commit for a tag' do
test_commit = '12345'
test_branch = 'stable'
test_output = "#{test_commit}\trefs/tags/#{test_branch}"
result = Git.send(:commit_from_ls_remote, test_output, test_branch)
result.should == test_commit
end
it 'finds correct commit from multiple ls-remote branches' do
test_commit1 = 'abcde'
test_branch1 = 'stable'
test_commit2 = '12345'
test_branch2 = 'release/stable'
test_output = "#{test_commit1}\trefs/heads/#{test_branch1}\n#{test_commit2}\trefs/heads/#{test_branch2}"
result1 = Git.send(:commit_from_ls_remote, test_output, test_branch1)
result1.should == test_commit1
result2 = Git.send(:commit_from_ls_remote, test_output, test_branch2)
result2.should == test_commit2
end
it 'returns a branch over a tag' do
test_commit1 = 'abcde'
test_branch = 'stable'
test_commit2 = '12345'
test_output = "#{test_commit1}\trefs/heads/#{test_branch}\n#{test_commit2}\trefs/tags/#{test_branch}"
result = Git.send(:commit_from_ls_remote, test_output, test_branch)
result.should == test_commit1
end
it 'handles nil inputs' do
Git.send(:commit_from_ls_remote, nil, 'test_branch').should.nil?
Git.send(:commit_from_ls_remote, "123\trefs/heads/abc", nil).should.nil?
end
end
describe '::preprocess_options' do
it 'skips non-branch requests' do
options = { :git => fixture_url('git-repo'), :commit => 'aaaa' }
new_options = Downloader.preprocess_options(options)
options.should == new_options
end
it 'resolves master to a commit' do
options = { :git => fixture_url('git-repo'), :branch => 'master' }
new_options = Downloader.preprocess_options(options)
new_options[:commit].should == '98cbf14201a78b56c6b7290f6cac840a7597a1c2'
end
it 'ignores invalid branches' do
options = { :git => fixture_url('git-repo'), :branch => 'aaaa' }
new_options = Downloader.preprocess_options(options)
new_options[:branch].should == 'aaaa'
end
end
end
end
end