2
2
require 'base64'
3
3
4
4
module RepositoryApplyTestHelpers
5
+ def assert_workdir_contents ( path , test_value )
6
+ assert_equal File . read ( File . join ( @repo . workdir , path ) ) , test_value
7
+ end
8
+
9
+ def assert_index_content ( path , test_value )
10
+ assert_equal @repo . lookup ( @repo . index [ path ] [ :oid ] ) . content , test_value
11
+ end
12
+
5
13
def blob_contents ( repo , path )
6
14
repo . lookup ( repo . head . target . tree [ path ] [ :oid ] ) . content
7
15
end
@@ -46,56 +54,89 @@ class RepositoryApplyTest < Rugged::TestCase
46
54
47
55
def setup
48
56
@repo = FixtureRepo . from_libgit2 ( "testrepo" )
57
+ @original_commit = @repo . head . target . oid
49
58
end
50
59
51
60
def test_apply_index
52
- original_commit = @repo . head . target . oid
53
61
new_commit , new_content , original_content = update_file ( @repo , 'README' )
54
62
55
- assert_equal @repo . lookup ( @repo . index [ " README" ] [ :oid ] ) . content , new_content
63
+ assert_index_content ' README' , new_content
56
64
57
- diff = @repo . diff ( new_commit , original_commit )
65
+ diff = @repo . diff ( new_commit , @ original_commit)
58
66
59
67
assert_equal true , @repo . apply ( diff , { :location => :index } )
60
- assert_equal @repo . lookup ( @repo . index [ " README" ] [ :oid ] ) . content , original_content
68
+ assert_index_content ' README' , original_content
61
69
end
62
70
63
71
def test_apply_workdir
64
- original_commit = @repo . head . target . oid
65
72
new_commit , new_content , original_content = update_file ( @repo , 'README' )
66
73
67
74
@repo . checkout_head ( :strategy => :force )
68
- assert_equal File . read ( File . join ( @repo . workdir , 'README' ) ) , new_content
75
+ assert_workdir_contents 'README' , new_content
69
76
70
- diff = @repo . diff ( new_commit , original_commit )
77
+ diff = @repo . diff ( new_commit , @ original_commit)
71
78
72
- assert_equal true , @repo . apply ( diff , { :location => :workdir } )
73
- assert_equal File . read ( File . join ( @repo . workdir , 'README' ) ) , original_content
79
+ assert_equal true , @repo . apply ( diff )
80
+ assert_workdir_contents 'README' , original_content
74
81
end
75
82
76
83
def test_apply_both
77
- original_commit = @repo . head . target . oid
78
84
new_commit , new_content , original_content = update_file ( @repo , 'README' )
79
85
80
86
@repo . checkout_head ( :strategy => :force )
81
- assert_equal @repo . lookup ( @repo . index [ " README" ] [ :oid ] ) . content , new_content
82
- assert_equal File . read ( File . join ( @repo . workdir , 'README' ) ) , new_content
87
+ assert_index_content ' README' , new_content
88
+ assert_workdir_contents 'README' , new_content
83
89
84
- diff = @repo . diff ( new_commit , original_commit )
90
+ diff = @repo . diff ( new_commit , @ original_commit)
85
91
86
- assert_equal true , @repo . apply ( diff )
87
- assert_equal @repo . lookup ( @repo . index [ " README" ] [ :oid ] ) . content , original_content
88
- assert_equal File . read ( File . join ( @repo . workdir , 'README' ) ) , original_content
92
+ assert_equal true , @repo . apply ( diff , :location => :both )
93
+ assert_index_content ' README' , original_content
94
+ assert_workdir_contents 'README' , original_content
89
95
end
90
96
91
97
def test_location_option
92
- original_commit = @repo . head . target . oid
93
98
new_commit , new_content , original_content = update_file ( @repo , 'README' )
94
99
95
- diff = @repo . diff ( new_commit , original_commit )
100
+ diff = @repo . diff ( new_commit , @ original_commit)
96
101
97
102
assert_raises TypeError do
98
103
@repo . apply ( diff , :location => :invalid )
99
104
end
100
105
end
106
+
107
+ def test_callbacks
108
+ new_commit , new_content , original_content = update_file ( @repo , 'README' )
109
+
110
+ assert_equal @repo . lookup ( @repo . index [ "README" ] [ :oid ] ) . content , new_content
111
+
112
+ diff = @repo . diff ( new_commit , @original_commit )
113
+
114
+ hunk_cb = Proc . new { |hunk |
115
+ assert hunk . is_a? ( Rugged ::Diff ::Hunk )
116
+ }
117
+
118
+ delta_cb = Proc . new { |delta |
119
+ assert delta . is_a? ( Rugged ::Diff ::Delta )
120
+ }
121
+
122
+ assert_equal true , @repo . apply ( diff , { :location => :index , :hunk_callback => hunk_cb , :delta_callback => delta_cb } )
123
+ assert_index_content 'README' , original_content
124
+
125
+ hunk_skip_all = Proc . new {
126
+ false
127
+ }
128
+
129
+ new_commit , new_content , original_content = update_file ( @repo , 'README' )
130
+ diff = @repo . diff ( new_commit , @original_commit )
131
+ assert_equal true , @repo . apply ( diff , { :location => :index , :hunk_callback => hunk_skip_all } )
132
+ assert_index_content 'README' , new_content
133
+
134
+ delta_fail = Proc . new {
135
+ nil
136
+ }
137
+
138
+ assert_raises RuntimeError do
139
+ @repo . apply ( diff , { :location => :index , :delta_callback => delta_fail } )
140
+ end
141
+ end
101
142
end
0 commit comments