forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_worktree_spec.rb
415 lines (346 loc) · 14.3 KB
/
git_worktree_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
describe GitWorktree do
context "repository" do
before do
@git_db = "TestGit.git"
@ae_db_dir = Dir.mktmpdir
@default_hash = {:a => "one", :b => "two", :c => "three"}
@dirnames = %w(A B c)
@repo_path = File.join(@ae_db_dir, @git_db)
@filenames = %w(A/File1.YamL B/File2.YamL c/File3.YAML)
@deleted_names = %w(A A/File1.YamL)
@conflict_file = 'A/File1.YamL'
@master_url = "file://#{@repo_path}"
@repo_options = {:path => @repo_path,
:username => "user1",
:email => "[email protected]",
:bare => true,
:new => true}
@ae_db = GitWorktree.new(@repo_options)
@original_commit = add_files_to_bare_repo(@filenames)
end
after do
FileUtils.rm_rf(@ae_db_dir) if Dir.exist?(@ae_db_dir)
end
def add_files_to_bare_repo(flist)
flist.each { |f| @ae_db.add(f, YAML.dump(@default_hash.merge(:fname => f))) }
@ae_db.save_changes("files added")
@ae_db.instance_variable_get('@repo').head.target.oid
end
def clone(url)
dir = Dir.mktmpdir
options = {:path => dir,
:url => url,
:username => "user1",
:email => "[email protected]",
:ssl_no_verify => true,
:bare => true,
:clone => true}
return dir, GitWorktree.new(options)
end
def open_existing_repo
options = {:path => @repo_path,
:username => "user1",
:email => "[email protected]"}
GitWorktree.new(options)
end
it "#delete_repo" do
expect(@ae_db.delete_repo).to be_truthy
expect(Dir.exist?(@repo_path)).to be_falsey
end
it "#remove of existing file" do
expect { @ae_db.remove('A/File1.YamL') }.to_not raise_error
end
it "#remove of non existing file" do
expect { @ae_db.remove('A/NotExistent.YamL') }.to raise_error(Rugged::IndexError)
end
it "#remove_dir existing directory" do
expect { @ae_db.remove_dir('A') }.to_not raise_error
expect(@ae_db.file_exists?('A/File1.YamL')).to be_truthy
end
it "#file_exists? missing" do
expect(@ae_db.file_exists?('A/nothing.YamL')).to be_falsey
end
it "#read_file that exists" do
fname = 'A/File1.YamL'
expect(YAML.load(@ae_db.read_file(fname))).to eq(@default_hash.merge(:fname => fname))
end
it "#file_attributes" do
fname = 'A/File1.YamL'
expect(@ae_db.file_attributes(fname).keys).to match_array([:updated_on, :updated_by])
end
it "#read_file that doesn't exist" do
expect { @ae_db.read_file('doesnotexist') }.to raise_error(GitWorktreeException::GitEntryMissing)
end
it "#entries" do
expect(@ae_db.entries("")).to match_array(@dirnames)
end
it "#entries in A" do
expect(@ae_db.entries("A")).to match_array(%w(File1.YamL))
end
it "get list of files" do
expect(@ae_db.file_list).to match_array(@filenames + @dirnames)
end
it "#directory_exists?" do
expect(@ae_db.directory_exists?('A')).to be_truthy
end
it "#nodes" do
node = @ae_db.nodes("A").first
expect(node[:full_name]).to eq("#{@git_db}/A/File1.YamL")
expect(node[:rel_path]).to eq("A/File1.YamL")
end
it "rename directory" do
filenames = %w(AAA/File1.YamL B/File2.YamL c/File3.YAML)
dirnames = %w(AAA B c)
@ae_db.mv_dir('A', "AAA")
@ae_db.save_changes("directories moved")
expect(@ae_db.file_list).to match_array(filenames + dirnames)
end
it "rename directory when target exists" do
expect { @ae_db.mv_dir('A', 'A') }.to raise_error(GitWorktreeException::DirectoryAlreadyExists)
end
it "move directories with similar names" do
filenames = %w(A/A/A/File1.YamL A/A/Aile2.YamL)
filenames.each { |f| @ae_db.add(f, YAML.dump(@default_hash.merge(:fname => f))) }
@ae_db.send(:commit, "extra files_added").tap { |cid| @ae_db.send(:merge, cid) }
@ae_db.mv_dir('A', "AAA")
@ae_db.save_changes("directories moved")
filenames = %w(AAA/File1.YamL B/File2.YamL c/File3.YAML AAA/A/A/File1.YamL AAA/A/Aile2.YamL)
dirnames = %w(AAA B c AAA/A AAA/A/A)
expect(@ae_db.file_list).to match_array(filenames + dirnames)
end
it "move intermediate directories with similar names" do
filenames = %w(A/A/A/File1.YamL A/A/Aile2.YamL)
filenames.each { |f| @ae_db.add(f, YAML.dump(@default_hash.merge(:fname => f))) }
@ae_db.send(:commit, "extra files_added").tap { |cid| @ae_db.send(:merge, cid) }
@ae_db.mv_dir('A/A', "AAA")
@ae_db.save_changes("directories moved")
filenames = %w(A/File1.YamL B/File2.YamL c/File3.YAML AAA/A/File1.YamL AAA/Aile2.YamL)
dirnames = %w(AAA B c AAA/A A)
expect(@ae_db.file_list).to match_array(filenames + dirnames)
end
it "get list of files from a specific commit" do
@ae_db.remove_dir("A")
@ae_db.save_changes("directories deleted")
expect(@ae_db.file_list).to match_array(@filenames + @dirnames - @deleted_names)
@repo_options[:commit_sha] = @original_commit
@repo_options[:new] = false
@orig_db = GitWorktree.new(@repo_options)
expect(@orig_db.file_list).to match_array(@filenames + @dirnames)
end
it "can delete directories" do
@dirnames.each { |d| @ae_db.remove_dir(d) }
@ae_db.save_changes("directories deleted")
@filenames.each { |f| expect(@ae_db.file_exists?(f)).to be_falsey }
end
it "rename file with new contents" do
filenames = %w(A/File11.YamL B/File2.YamL c/File3.YAML)
@ae_db.mv_file_with_new_contents('A/File1.YamL', 'A/File11.YamL', "Hello")
@ae_db.save_changes("file renamed")
expect(@ae_db.file_list).to match_array(filenames + @dirnames)
end
it "rename file" do
filenames = %w(A/File11.YamL B/File2.YamL c/File3.YAML)
@ae_db.mv_file('A/File1.YamL', 'A/File11.YamL')
@ae_db.save_changes("file renamed")
expect(@ae_db.file_list).to match_array(filenames + @dirnames)
end
it "manage conflicts" do
@ae_db.add(@conflict_file, YAML.dump(@default_hash.merge(:fname => "first_one")))
commit = @ae_db.send(:commit, "suspended commit")
new_db = open_existing_repo
new_db.add(@conflict_file, YAML.dump(@default_hash.merge(:fname => "second_one")))
new_db.save_changes("overlapping commit")
expect { @ae_db.send(:merge, commit) }.to raise_error { |error|
expect(error).to be_a(GitWorktreeException::GitConflicts)
}
end
it "clone repo" do
dirname, c_repo = clone(@master_url)
expect(c_repo.file_list).to match_array(@ae_db.file_list)
FileUtils.rm_rf(dirname) if Dir.exist?(dirname)
end
it "push changes to master repo" do
dirname, c_repo = clone(@master_url)
new_file = "A/File12.yamL"
c_repo.add(new_file, YAML.dump(@default_hash.merge(:fname => "new1")))
c_repo.save_changes("new file added on slave", :remote)
expect(c_repo.file_list).to match_array(@filenames + @dirnames + [new_file])
expect(c_repo.file_list).to match_array(@ae_db.file_list)
FileUtils.rm_rf(dirname) if Dir.exist?(dirname)
end
it "push changes to master with no conflicts" do
dirname, c_repo = clone(@master_url)
new_file_m = "A/File_on_master.yamL"
new_file_c = "A/File_on_slave.yamL"
@ae_db.add(new_file_m, YAML.dump(@default_hash.merge(:fname => "new on master")))
@ae_db.save_changes("new file added in master")
c_repo.add(new_file_c, YAML.dump(@default_hash.merge(:fname => "new1")))
c_repo.save_changes("new file added on slave", :remote)
expect(c_repo.file_list).to match_array(@filenames + @dirnames + [new_file_c, new_file_m])
expect(c_repo.file_list).to match_array(@ae_db.file_list)
FileUtils.rm_rf(dirname) if Dir.exist?(dirname)
end
it "push changes to master with conflicts" do
dirname, c_repo = clone(@master_url)
new_file = "conflict_file.yaml"
master_data = "on master"
@ae_db.add(new_file, master_data)
@ae_db.save_changes("updated on master")
c_repo.add(new_file, "on slave")
expect { c_repo.save_changes("updated on slave", :remote) }.to raise_error(GitWorktreeException::GitConflicts)
expect(@ae_db.file_list).to match_array(@filenames + @dirnames + [new_file])
expect(c_repo.file_list).to match_array(@ae_db.file_list)
expect(c_repo.read_entry(c_repo.find_entry(new_file))).to eql(master_data)
FileUtils.rm_rf(dirname) if Dir.exist?(dirname)
end
it "pull updates in a cloned repo" do
dirname, c_repo = clone(@master_url)
expect(c_repo.file_list).to match_array(@ae_db.file_list)
@ae_db.mv_file_with_new_contents('A/File1.YamL', 'A/File11.YamL', "Hello")
@ae_db.save_changes("file renamed in master")
c_repo.send(:pull)
expect(c_repo.file_list).to match_array(@ae_db.file_list)
FileUtils.rm_rf(dirname) if Dir.exist?(dirname)
end
end
describe "git branches" do
let(:git_repo_path) { Rails.root.join("spec/fixtures/git_repos/branch_and_tag.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }
describe "#branches" do
it "all branches" do
expect(test_repo.branches).to match_array(%w(master branch1 branch2))
end
it "local branches only" do
expect(test_repo.branches(:local)).to match_array(%w(master branch1 branch2))
end
it "remote branches only" do
expect(test_repo.branches(:remote)).to be_empty
end
end
describe "#file_list" do
it "get list of files in a branch" do
test_repo.branch = 'branch2'
expect(test_repo.file_list).to match_array(%w(file1 file2 file3 file4))
end
end
describe "#branch_info" do
it "get branch info" do
expect(test_repo.branch_info('branch2').keys).to match_array([:time, :message, :commit_sha])
end
end
describe "#branch" do
it "non existent branch" do
expect { test_repo.branch = 'nada' }.to raise_exception(GitWorktreeException::BranchMissing)
end
end
end
describe "git branches with no master" do
let(:git_repo_path) { Rails.root.join("spec/fixtures/git_repos/no_master.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }
describe "#branches" do
it "all branches" do
expect(test_repo.branches).to match_array(%w(branch1 branch2))
end
end
describe "#file_list" do
it "get list of files in a branch" do
test_repo.branch = 'branch2'
expect(test_repo.file_list).to match_array(%w(file1 file2 file3 file4))
end
end
end
describe 'git tags' do
let(:git_repo_path) { Rails.root.join("spec/fixtures/git_repos/branch_and_tag.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }
describe "#tags" do
it "get list of tags" do
expect(test_repo.tags).to match_array(%w(tag1 tag2))
end
end
describe "#file_list" do
it "get list of files in a tag" do
test_repo.tag = 'tag2'
expect(test_repo.file_list).to match_array(%w(file1 file2 file3 file4))
end
end
describe "#tag_info" do
it "get tag info" do
expect(test_repo.tag_info('tag2').keys).to match_array([:time, :message, :commit_sha])
end
end
describe "#tag" do
it "non existent tag" do
expect { test_repo.tag = 'nada' }.to raise_exception(GitWorktreeException::TagMissing)
end
end
end
describe "#new" do
let(:git_repo_path) { Rails.root.join("spec", "fixtures", "git_repos", "branch_and_tag.git") }
it "raises an exception if SSH requested, but rugged is not compiled with SSH support" do
require "rugged"
expect(Rugged).to receive(:features).and_return([:threads, :https])
expect {
GitWorktree.new(:path => git_repo_path, :ssh_private_key => "fake key\nfile content")
}.to raise_error(GitWorktreeException::InvalidCredentialType)
end
end
describe "#with_credential_options" do
let(:git_repo_path) { Rails.root.join("spec", "fixtures", "git_repos", "branch_and_tag.git") }
subject do
repo.with_credential_options do |cred_options|
cred_options[:credentials].call("url", nil, [])
end
end
describe "via plaintext" do
let(:repo) { described_class.new(:path => git_repo_path.to_s, :username => username, :password => password) }
let(:username) { "fred" }
let(:password) { "pa$$w0rd" }
it "with both username and password" do
expect(subject).to be_a Rugged::Credentials::UserPassword
end
context "with no username" do
let(:username) { nil }
it "raises an exception" do
expect { subject }.to raise_error(GitWorktreeException::InvalidCredentials, /provide username and password for/)
end
end
context "with no password" do
let(:password) { nil }
it "raises an exception" do
expect { subject }.to raise_error(GitWorktreeException::InvalidCredentials, /provide username and password for/)
end
end
end
describe "via SSH" do
let(:repo) { described_class.new(:path => git_repo_path.to_s, :username => username, :ssh_private_key => ssh_private_key, :password => password) }
let(:username) { "git" }
let(:ssh_private_key) { "fake key\nfile content" }
let(:password) { "pa$$w0rd" }
before do
require "rugged"
allow(Rugged).to receive(:features).and_return([:threads, :https, :ssh])
end
it "with username, ssh_private_key, and password" do
expect(subject).to be_a Rugged::Credentials::SshKey
end
context "with no username" do
let(:username) { nil }
it "raises an exception" do
expect { subject }.to raise_error(GitWorktreeException::InvalidCredentials, /provide username for/)
end
end
context "with no password" do
let(:password) { nil }
it "creates a password-less ssh key cred" do
expect(subject).to be_a Rugged::Credentials::SshKey
end
end
context "with no ssh_private_key" do
let(:ssh_private_key) { nil }
it "treats it like a user/pass" do
expect(subject).to be_a Rugged::Credentials::UserPassword
end
end
end
end
end