-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathindex_test.rb
454 lines (362 loc) · 12.3 KB
/
index_test.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
require "test_helper"
require 'base64'
require 'tempfile'
require 'fileutils'
class IndexTest < Rugged::TestCase
def self.new_index_entry
now = Time.now
{
:path => "new_path",
:oid => "d385f264afb75a56a5bec74243be9b367ba4ca08",
:mtime => now,
:ctime => now,
:file_size => 1000,
:dev => 234881027,
:ino => 88888,
:mode => 33188,
:uid => 502,
:gid => 502,
:stage => 3,
}
end
def setup
path = File.dirname(__FILE__) + '/fixtures/testrepo.git/index'
@index = Rugged::Index.new(path)
end
def test_iteration
enum = @index.each
assert enum.kind_of? Enumerable
i = 0
@index.each { |e| i += 1 }
assert_equal @index.count, i
end
def test_index_size
assert_equal 2, @index.count
end
def test_empty_index
@index.clear
assert_equal 0, @index.count
end
def test_remove_entries
@index.remove 'new.txt'
assert_equal 1, @index.count
end
def test_remove_dir
@index.remove_dir 'does-not-exist'
assert_equal 2, @index.count
@index.remove_dir '', 2
assert_equal 2, @index.count
@index.remove_dir ''
assert_equal 0, @index.count
end
def test_get_entry_data
e = @index[0]
assert_equal 'README', e[:path]
assert_equal '1385f264afb75a56a5bec74243be9b367ba4ca08', e[:oid]
assert_equal 1273360380, e[:mtime].to_i
assert_equal 1273360380, e[:ctime].to_i
assert_equal 4, e[:file_size]
assert_equal 234881026, e[:dev]
assert_equal 6674088, e[:ino]
assert_equal 33188, e[:mode]
assert_equal 501, e[:uid]
assert_equal 0, e[:gid]
assert_equal false, e[:valid]
assert_equal 0, e[:stage]
e = @index[1]
assert_equal 'new.txt', e[:path]
assert_equal 'fa49b077972391ad58037050f2a75f74e3671e92', e[:oid]
end
def test_iterate_entries
itr_test = @index.sort { |a, b| a[:oid] <=> b[:oid] }.map { |e| e[:path] }.join(':')
assert_equal "README:new.txt", itr_test
end
def test_update_entries
now = Time.at Time.now.to_i
e = @index[0]
e[:oid] = "12ea3153a78002a988bb92f4123e7e831fd1138a"
e[:mtime] = now
e[:ctime] = now
e[:file_size] = 1000
e[:dev] = 234881027
e[:ino] = 88888
e[:mode] = 33188
e[:uid] = 502
e[:gid] = 502
e[:stage] = 3
@index.add(e)
new_e = @index.get e[:path], 3
assert_equal e, new_e
end
def test_add_new_entries
e = IndexTest.new_index_entry
@index << e
assert_equal 3, @index.count
itr_test = @index.sort { |a, b| a[:oid] <=> b[:oid] }.map { |x| x[:path] }.join(':')
assert_equal "README:new_path:new.txt", itr_test
end
end
class IndexWriteTest < Rugged::TestCase
def setup
path = File.dirname(__FILE__) + '/fixtures/testrepo.git/index'
@tmpfile = Tempfile.new('index', Dir.tmpdir, encoding: "binary")
@tmpfile.write(File.binread(path))
@tmpfile.close
@index = Rugged::Index.new(@tmpfile.path)
end
def teardown
@tmpfile.unlink
end
def test_raises_when_writing_invalid_entries
assert_raises TypeError do
@index.add(21)
end
end
def test_can_write_index
e = IndexTest.new_index_entry
@index << e
e[:path] = "else.txt"
@index << e
@index.write
index2 = Rugged::Index.new(@tmpfile.path)
itr_test = index2.sort { |a, b| a[:oid] <=> b[:oid] }.map { |x| x[:path] }.join(':')
assert_equal "README:else.txt:new_path:new.txt", itr_test
assert_equal 4, index2.count
end
end
class IndexWorkdirTest < Rugged::TestCase
def setup
@repo = FixtureRepo.empty
@index = @repo.index
end
def test_adding_a_path
File.open(File.join(@repo.workdir, 'test.txt'), 'w') do |f|
f.puts "test content"
end
@index.add('test.txt')
@index.write
index2 = Rugged::Index.new(File.join(@repo.workdir, '.git', 'index'))
assert_equal index2[0][:path], 'test.txt'
end
def test_reloading_index
File.open(File.join(@repo.workdir, 'test.txt'), 'w') do |f|
f.puts "test content"
end
@index.add('test.txt')
@index.write
rindex = Rugged::Index.new(File.join(@repo.workdir, '.git', 'index'))
e = rindex['test.txt']
assert_equal 0, e[:stage]
rindex << IndexTest.new_index_entry
rindex.write
assert_equal 1, @index.count
@index.reload
assert_equal 2, @index.count
e = @index.get 'new_path', 3
assert_equal e[:mode], 33188
end
end
class IndexConflictsTest < Rugged::TestCase
def setup
@repo = FixtureRepo.from_libgit2("mergedrepo")
end
def test_conflicts?
assert @repo.index.conflicts?
end
def test_conflicts
conflicts = @repo.index.conflicts
assert_equal 2, conflicts.size
assert_equal "conflicts-one.txt", conflicts[0][:ancestor][:path]
assert_equal "conflicts-one.txt", conflicts[0][:ours][:path]
assert_equal "conflicts-one.txt", conflicts[0][:theirs][:path]
assert_equal 1, conflicts[0][:ancestor][:stage]
assert_equal 2, conflicts[0][:ours][:stage]
assert_equal 3, conflicts[0][:theirs][:stage]
assert_equal "conflicts-two.txt", conflicts[1][:ancestor][:path]
assert_equal "conflicts-two.txt", conflicts[1][:ours][:path]
assert_equal "conflicts-two.txt", conflicts[1][:theirs][:path]
assert_equal 1, conflicts[1][:ancestor][:stage]
assert_equal 2, conflicts[1][:ours][:stage]
assert_equal 3, conflicts[1][:theirs][:stage]
end
def test_conflict_get
conflict = @repo.index.conflict_get("conflicts-one.txt")
assert_equal "conflicts-one.txt", conflict[:ancestor][:path]
assert_equal "conflicts-one.txt", conflict[:ours][:path]
assert_equal "conflicts-one.txt", conflict[:theirs][:path]
assert_equal 1, conflict[:ancestor][:stage]
assert_equal 2, conflict[:ours][:stage]
assert_equal 3, conflict[:theirs][:stage]
refute @repo.index.conflict_get("conflict-does-not-exists.txt")
end
def test_conflict_remove
@repo.index.conflict_remove("conflicts-one.txt")
assert_equal @repo.index.conflicts.size, 1
@repo.index.conflict_remove("conflicts-two.txt")
assert_equal @repo.index.conflicts.size, 0
refute @repo.index.conflicts?
end
def test_conflict_add
conflict = @repo.index.conflict_get("conflicts-one.txt")
conflict[:ancestor][:path] = conflict[:ours][:path] = conflict[:theirs][:path] = "new-conflict.txt"
@repo.index.conflict_add(conflict)
assert_equal @repo.index.conflicts.size, 3
conflict[:ancestor] = nil
conflict[:ours][:path] = conflict[:theirs][:path] = "another-new-conflict.txt"
@repo.index.conflict_add(conflict)
assert_equal @repo.index.conflicts.size, 4
end
def test_conflict_cleanup
@repo.index.conflict_cleanup
assert_equal @repo.index.conflicts.size, 0
refute @repo.index.conflicts?
end
end
class IndexMergeFileTest < Rugged::TestCase
def setup
@repo = FixtureRepo.from_libgit2("mergedrepo")
end
def test_merge_file
merge_file_result = @repo.index.merge_file("conflicts-one.txt")
assert !merge_file_result[:automergeable]
assert_equal merge_file_result[:path], "conflicts-one.txt"
assert_equal merge_file_result[:data], "<<<<<<< conflicts-one.txt\nThis is most certainly a conflict!\n=======\nThis is a conflict!!!\n>>>>>>> conflicts-one.txt\n"
end
def test_merge_file_with_labels
merge_file_result = @repo.index.merge_file("conflicts-one.txt", our_label: "ours", their_label: "theirs")
assert !merge_file_result[:automergeable]
assert_equal merge_file_result[:path], "conflicts-one.txt"
assert_equal merge_file_result[:data], "<<<<<<< ours\nThis is most certainly a conflict!\n=======\nThis is a conflict!!!\n>>>>>>> theirs\n"
end
def test_merge_file_without_ancestor
# remove the stage 1 (ancestor), this is now an add/add conflict
@repo.index.remove("conflicts-one.txt", 1)
merge_file_result = @repo.index.merge_file("conflicts-one.txt", our_label: "ours", their_label: "theirs")
assert !merge_file_result[:automergeable]
assert_equal merge_file_result[:path], "conflicts-one.txt"
assert_equal merge_file_result[:data], "<<<<<<< ours\nThis is most certainly a conflict!\n=======\nThis is a conflict!!!\n>>>>>>> theirs\n"
end
def test_merge_file_without_ours
# turn this into a modify/delete conflict
@repo.index.remove("conflicts-one.txt", 2)
assert_raises RuntimeError do
@repo.index.merge_file("conflicts-one.txt", our_label: "ours", their_label: "theirs")
end
end
def test_merge_file_without_theirs
# turn this into a modify/delete conflict
@repo.index.remove("conflicts-one.txt", 3)
assert_raises RuntimeError do
@repo.index.merge_file("conflicts-one.txt", our_label: "ours", their_label: "theirs")
end
end
end
class IndexRepositoryTest < Rugged::TestCase
def setup
@source_repo = FixtureRepo.from_rugged("testrepo.git")
@repo = FixtureRepo.clone(@source_repo)
@path = @repo.workdir
end
def test_idempotent_read_write
head_sha = @repo.references['HEAD'].resolve.target_id
tree = @repo.lookup(head_sha).tree
index = @repo.index
index.read_tree(tree)
index_tree_sha = index.write_tree
index_tree = @repo.lookup(index_tree_sha)
assert_equal tree.oid, index_tree.oid
end
def test_build_tree_from_index
head_sha = @repo.references['refs/remotes/origin/packed'].resolve.target_id
tree = @repo.lookup(head_sha).tree
index = @repo.index
index.read_tree(tree)
index.remove('second.txt')
new_tree_sha = index.write_tree
assert head_sha != new_tree_sha
assert_nil @repo.lookup(new_tree_sha)['second.txt']
end
def test_read_tree_with_not_a_tree
head_sha = @repo.references['refs/remotes/origin/packed'].resolve.target_id
commit = @repo.lookup(head_sha)
index = @repo.index
assert_raises TypeError do
index.read_tree(commit)
end
end
end
class IndexAddAllTest < Rugged::TestCase
def setup
@repo = FixtureRepo.empty
Dir.chdir(@repo.workdir) do
File.open("file.foo", "w") { |f| f.write "a file" }
File.open("file.bar", "w") { |f| f.write "another file" }
File.open("file.zzz", "w") { |f| f.write "yet another one" }
File.open("other.zzz", "w") { |f| f.write "yet another one" }
File.open("more.zzz", "w") { |f| f.write "yet another one" }
File.open(".gitignore", "w") { |f| f.write "*.foo\n" }
end
end
def test_add_all_lifecycle
Dir.chdir(@repo.workdir) do
@repo.index.add_all("file.*")
assert @repo.index["file.bar"]
assert @repo.index["file.zzz"]
refute @repo.index["file.foo"]
refute @repo.index["other.zzz"]
refute @repo.index["more.zzz"]
@repo.index.add_all("*.zzz")
assert @repo.index["file.bar"]
assert @repo.index["file.zzz"]
assert @repo.index["other.zzz"]
assert @repo.index["more.zzz"]
refute @repo.index["file.foo"]
end
end
def test_add_all_dry_run
Dir.chdir(@repo.workdir) do
yielded = []
@repo.index.add_all do |path, pathspec|
yielded << path
false
end
assert_equal [".gitignore", "file.bar", "file.zzz", "more.zzz", "other.zzz"], yielded
yielded.each do |path|
refute @repo.index[path]
end
yielded = []
@repo.index.add_all(["file.*", "*.zzz"]) do |path, pathspec|
yielded << [path, pathspec]
false
end
assert_equal [
["file.bar", "file.*"],
["file.zzz", "file.*"],
["more.zzz", "*.zzz"],
["other.zzz", "*.zzz"]
], yielded
end
end
def test_update_all
Dir.chdir(@repo.workdir) do
@repo.index.add_all("file.*")
File.open("file.bar", "w") { |f| f.write "new content for file" }
@repo.index.update_all("file.*")
assert @repo.index["file.bar"]
assert_equal "new content for file", @repo.lookup(@repo.index["file.bar"][:oid]).content
refute @repo.index["other.zzz"], "#update_all should only update files in the index"
refute @repo.index["more.zzz"], "#update_all should only update files in the index"
File.unlink("file.bar")
@repo.index.update_all
refute @repo.index["file.bar"], "#update_all should remove index entries that are removed from the workdir"
end
end
def test_remove_all
Dir.chdir(@repo.workdir) do
@repo.index.add_all("file.*")
@repo.index.remove_all("*.zzz")
assert @repo.index["file.bar"]
refute @repo.index["file.zzz"]
end
end
end