-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathrepo_apply_test.rb
169 lines (125 loc) · 4.8 KB
/
repo_apply_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
require 'test_helper'
module RepositoryApplyTestHelpers
def assert_workdir_content(path, test_value, repo = nil)
repo = @repo if repo.nil?
assert_equal File.read(File.join(repo.workdir, path)), test_value
end
def assert_index_content(path, test_value, repo = nil)
repo = @repo if repo.nil?
assert_equal repo.lookup(repo.index[path][:oid]).content, test_value
end
def blob_contents(repo, path)
repo.lookup(repo.head.target.tree[path][:oid]).content
end
def update_file(repo, path)
original = blob_contents(repo, path)
content = new_content(original)
blob = repo.write(content, :blob)
index = repo.index
index.read_tree(repo.head.target.tree)
index.add(:path => path, :oid => blob, :mode => 0100644)
new_tree = index.write_tree(repo)
oid = do_commit(repo, new_tree, "Update #{path}")
return repo.lookup(oid), content, original
end
def do_commit(repo, tree, message)
Rugged::Commit.create(repo,
:tree => tree,
:author => person,
:committer => person,
:message => message,
:parents => repo.empty? ? [] : [ repo.head.target ].compact,
:update_ref => 'HEAD',
)
end
def new_content(original)
"#{original}Some new line\n"
end
def person
{:name => 'Scott', :email => 'schacon@gmail.com', :time => Time.now }
end
end
class RepositoryApplyTest < Rugged::TestCase
include RepositoryApplyTestHelpers
def setup
@repo = FixtureRepo.from_libgit2("testrepo")
@original_commit = @repo.head.target.oid
end
def test_apply_index
new_commit, new_content, original_content = update_file(@repo, 'README')
assert_index_content 'README', new_content
diff = @repo.diff(new_commit, @original_commit)
assert_equal true, @repo.apply(diff, {:location => :index})
assert_index_content 'README', original_content
end
def test_apply_workdir
new_commit, new_content, original_content = update_file(@repo, 'README')
@repo.checkout_head(:strategy => :force)
assert_workdir_content 'README', new_content
diff = @repo.diff(new_commit, @original_commit)
assert_equal true, @repo.apply(diff)
assert_workdir_content 'README', original_content
end
def test_apply_both
new_commit, new_content, original_content = update_file(@repo, 'README')
@repo.checkout_head(:strategy => :force)
assert_index_content 'README', new_content
assert_workdir_content 'README', new_content
diff = @repo.diff(new_commit, @original_commit)
assert_equal true, @repo.apply(diff, :location => :both)
assert_index_content 'README', original_content
assert_workdir_content 'README', original_content
end
def test_bare_repository_defaults_to_index
bare_repo = Rugged::Repository.clone_at(@repo.path, Dir.mktmpdir('rugged-apply_bare'), :bare => true)
original_commit = bare_repo.head.target.oid
new_commit, new_content, original_content = update_file(bare_repo, 'README')
assert_index_content 'README', new_content, bare_repo
diff = bare_repo.diff(new_commit, original_commit)
assert_equal true, bare_repo.apply(diff)
assert_index_content 'README', original_content, bare_repo
end
def test_location_option
new_commit, _new_content, _original_content = update_file(@repo, 'README')
diff = @repo.diff(new_commit, @original_commit)
assert_raises TypeError do
@repo.apply(diff, :location => :invalid)
end
end
def test_callbacks
new_commit, new_content, original_content = update_file(@repo, 'README')
assert_equal @repo.lookup(@repo.index["README"][:oid]).content, new_content
diff = @repo.diff(new_commit, @original_commit)
callsback = 0
hunk_cb = Proc.new { |hunk|
callsback += 1
assert hunk.is_a?(Rugged::Diff::Hunk)
}
delta_cb = Proc.new { |delta|
callsback += 1
assert delta.is_a?(Rugged::Diff::Delta)
}
assert_equal true, @repo.apply(diff, {:location => :index, :hunk_callback => hunk_cb, :delta_callback => delta_cb})
assert_index_content 'README', original_content
assert_equal callsback, 2
hunk_skip_all = Proc.new {
false
}
new_commit, new_content, original_content = update_file(@repo, 'README')
diff = @repo.diff(new_commit, @original_commit)
assert_equal true, @repo.apply(diff, {:location => :index, :hunk_callback => hunk_skip_all})
assert_index_content 'README', new_content
delta_fail = Proc.new {
nil
}
assert_raises RuntimeError do
@repo.apply(diff, {:location => :index, :delta_callback => delta_fail})
end
assert_raises ArgumentError do
@repo.apply(diff, {:location => :index, :delta_callback => 'this is not a callable object'})
end
assert_raises ArgumentError do
@repo.apply(diff, {:location => :index, :hunk_callback => 'this is not a callable object'})
end
end
end