forked from libgit2/rugged
/
repo_test.rb
173 lines (140 loc) · 4.93 KB
/
repo_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
require 'test_helper'
require 'base64'
class RepositoryTest < Rugged::TestCase
include Rugged::RepositoryAccess
def test_last_commit
assert @repo.respond_to? :last_commit
assert "36060c58702ed4c2a40832c51758d5344201d89a", @repo.last_commit.oid
end
def test_fails_to_open_unexisting_repos
assert_raises IOError, Rugged::OSError do
Rugged::Repository.new("fakepath/123/")
end
assert_raises Rugged::RepositoryError do
Rugged::Repository.new("/home/")
end
end
def test_can_check_if_objects_exist
assert @repo.exists?("8496071c1b46c854b31185ea97743be6a8774479")
assert @repo.exists?("1385f264afb75a56a5bec74243be9b367ba4ca08")
assert !@repo.exists?("ce08fe4884650f067bd5703b6a59a8b3b3c99a09")
assert !@repo.exists?("8496071c1c46c854b31185ea97743be6a8774479")
end
def test_can_read_a_raw_object
rawobj = @repo.read("8496071c1b46c854b31185ea97743be6a8774479")
assert_match 'tree 181037049a54a1eb5fab404658a3a250b44335d7', rawobj.data
assert_equal 172, rawobj.len
assert_equal :commit, rawobj.type
end
def test_can_read_object_headers
hash = @repo.read_header("8496071c1b46c854b31185ea97743be6a8774479")
assert_equal 172, hash[:len]
assert_equal :commit, hash[:type]
end
def test_check_reads_fail_on_missing_objects
assert_raises Rugged::OdbError do
@repo.read("a496071c1b46c854b31185ea97743be6a8774471")
end
end
def test_check_read_headers_fail_on_missing_objects
assert_raises Rugged::OdbError do
@repo.read_header("a496071c1b46c854b31185ea97743be6a8774471")
end
end
def test_walking_with_block
oid = "a4a7dce85cf63874e984719f4fdd239f5145052f"
list = []
@repo.walk(oid) { |c| list << c }
assert list.map {|c| c.oid[0,5] }.join('.'), "a4a7d.c4780.9fd73.4a202.5b5b0.84960"
end
def test_walking_without_block
commits = @repo.walk('a4a7dce85cf63874e984719f4fdd239f5145052f')
assert commits.kind_of?(Enumerable)
assert commits.count > 0
end
def test_lookup_object
object = @repo.lookup("8496071c1b46c854b31185ea97743be6a8774479")
assert object.kind_of?(Rugged::Commit)
end
def test_find_reference
ref = @repo.ref('refs/heads/master')
assert ref.kind_of?(Rugged::Reference)
assert_equal 'refs/heads/master', ref.name
end
def test_match_all_refs
refs = @repo.refs 'refs/heads'
assert_equal 2, refs.length
end
def test_return_all_ref_names
refs = @repo.ref_names
refs.each {|name| assert name.kind_of?(String)}
assert_equal 5, refs.count
end
def test_return_all_tags
tags = @repo.tags
assert_equal 2, tags.count
end
def test_return_matching_tags
tags = @repo.tags 'v0.9'
assert_equal 1, tags.count
end
def test_return_all_remotes
remotes = @repo.remotes
assert_equal 1, remotes.count
end
def test_lookup_head
head = @repo.head
assert_equal "36060c58702ed4c2a40832c51758d5344201d89a", head.target
assert_equal :direct, head.type
end
def test_access_a_file
sha = '36060c58702ed4c2a40832c51758d5344201d89a'
blob = @repo.blob_at(sha, 'new.txt')
assert_equal "new file\n", blob.content
end
def test_garbage_collection
Rugged::Repository.new(@path)
ObjectSpace.garbage_collect
end
def test_enumerate_all_objects
assert_equal 33, @repo.each_id.to_a.length
end
def test_loading_alternates
alt_path = File.dirname(__FILE__) + '/fixtures/alternate/objects'
repo = Rugged::Repository.new(@path, :alternates => [alt_path])
assert_equal 36, repo.each_id.to_a.length
assert repo.read('146ae76773c91e3b1d00cf7a338ec55ae58297e2')
end
def test_find_merge_base_between_oids
commit1 = 'a4a7dce85cf63874e984719f4fdd239f5145052f'
commit2 = '36060c58702ed4c2a40832c51758d5344201d89a'
base = '5b5b025afb0b4c913b4c338a42934a3863bf3644'
assert_equal base, @repo.merge_base(commit1, commit2)
end
def test_find_merge_base_between_commits
commit1 = @repo.lookup('a4a7dce85cf63874e984719f4fdd239f5145052f')
commit2 = @repo.lookup('36060c58702ed4c2a40832c51758d5344201d89a')
base = '5b5b025afb0b4c913b4c338a42934a3863bf3644'
assert_equal base, @repo.merge_base(commit1, commit2)
end
def test_find_merge_base_between_ref_and_oid
commit1 = 'a4a7dce85cf63874e984719f4fdd239f5145052f'
commit2 = "refs/heads/master"
base = '5b5b025afb0b4c913b4c338a42934a3863bf3644'
assert_equal base, @repo.merge_base(commit1, commit2)
end
end
class RepositoryWriteTest < Rugged::TestCase
include Rugged::TempRepositoryAccess
TEST_CONTENT = "my test data\n"
TEST_CONTENT_TYPE = 'blob'
def test_can_hash_data
oid = Rugged::Repository::hash(TEST_CONTENT, TEST_CONTENT_TYPE)
assert_equal "76b1b55ab653581d6f2c7230d34098e837197674", oid
end
def test_write_to_odb
oid = @repo.write(TEST_CONTENT, TEST_CONTENT_TYPE)
assert_equal "76b1b55ab653581d6f2c7230d34098e837197674", oid
assert @repo.exists?("76b1b55ab653581d6f2c7230d34098e837197674")
end
end