22require 'base64'
33
44module 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+
513 def blob_contents ( repo , path )
614 repo . lookup ( repo . head . target . tree [ path ] [ :oid ] ) . content
715 end
@@ -46,56 +54,89 @@ class RepositoryApplyTest < Rugged::TestCase
4654
4755 def setup
4856 @repo = FixtureRepo . from_libgit2 ( "testrepo" )
57+ @original_commit = @repo . head . target . oid
4958 end
5059
5160 def test_apply_index
52- original_commit = @repo . head . target . oid
5361 new_commit , new_content , original_content = update_file ( @repo , 'README' )
5462
55- assert_equal @repo . lookup ( @repo . index [ " README" ] [ :oid ] ) . content , new_content
63+ assert_index_content ' README' , new_content
5664
57- diff = @repo . diff ( new_commit , original_commit )
65+ diff = @repo . diff ( new_commit , @ original_commit)
5866
5967 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
6169 end
6270
6371 def test_apply_workdir
64- original_commit = @repo . head . target . oid
6572 new_commit , new_content , original_content = update_file ( @repo , 'README' )
6673
6774 @repo . checkout_head ( :strategy => :force )
68- assert_equal File . read ( File . join ( @repo . workdir , 'README' ) ) , new_content
75+ assert_workdir_contents 'README' , new_content
6976
70- diff = @repo . diff ( new_commit , original_commit )
77+ diff = @repo . diff ( new_commit , @ original_commit)
7178
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
7481 end
7582
7683 def test_apply_both
77- original_commit = @repo . head . target . oid
7884 new_commit , new_content , original_content = update_file ( @repo , 'README' )
7985
8086 @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
8389
84- diff = @repo . diff ( new_commit , original_commit )
90+ diff = @repo . diff ( new_commit , @ original_commit)
8591
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
8995 end
9096
9197 def test_location_option
92- original_commit = @repo . head . target . oid
9398 new_commit , new_content , original_content = update_file ( @repo , 'README' )
9499
95- diff = @repo . diff ( new_commit , original_commit )
100+ diff = @repo . diff ( new_commit , @ original_commit)
96101
97102 assert_raises TypeError do
98103 @repo . apply ( diff , :location => :invalid )
99104 end
100105 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
101142end
0 commit comments