summaryrefslogtreecommitdiff
path: root/subversion/bindings/swig/ruby/test/test_delta.rb
blob: 113e01b384cd4712a27ae9146dfc115554e1b6a7 (plain)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# encoding: UTF-8
# ====================================================================
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied.  See the License for the
#    specific language governing permissions and limitations
#    under the License.
# ====================================================================

require "my-assertions"
require "util"
require "stringio"
require 'digest/md5'
require 'tempfile'

require "svn/info"

class SvnDeltaTest < Test::Unit::TestCase
  include SvnTestUtil

  def setup
    setup_basic
  end

  def teardown
    teardown_basic
  end

  def test_version
    assert_equal(Svn::Core.subr_version, Svn::Delta.version)
  end

  def test_txdelta_window
    s = ("a\nb\nc\nd\ne" + "\n" * 100) * 1000
    t = ("a\nb\nX\nd\ne" + "\n" * 100) * 1000
    source = StringIO.new(s)
    target = StringIO.new(t)
    stream = Svn::Delta::TextDeltaStream.new(source, target)
    assert_nil(stream.md5_digest)
    _my_assert_block do
      ret = stream.each do |window|
        window.ops.each do |op|
          op_size = op.offset + op.length
          case op.action_code
          when Svn::Delta::TXDELTA_SOURCE
            assert_operator(op_size, :<=, window.sview_len)
          when Svn::Delta::TXDELTA_NEW
            assert_operator(op_size, :<=, window.new_data.length)
          when Svn::Delta::TXDELTA_TARGET
            assert_operator(op_size, :<=, window.tview_len)
          else
            flunk
          end
        end
      end
      true if RUBY_VERSION > '1.9' # this block returns nil in > ruby '1.9'
    end
    assert_equal(Digest::MD5.hexdigest(t), stream.md5_digest)
  end

  def test_txdelta_window_compose
    s = ("a\nb\nc\nd\ne" + "\n" * 100) * 1000
    t = ("a\nb\nX\nd\ne" + "\n" * 100) * 1000
    source = StringIO.new(s)
    target = StringIO.new(t)
    stream = Svn::Delta::TextDeltaStream.new(source, target)
    composed_window = nil
    stream.each do |window|
      if composed_window.nil?
        composed_window = window
      else
        composed_window = window.compose(composed_window)
      end
    end

    _my_assert_block do
      composed_window.ops.each do |op|
        op_size = op.offset + op.length
        case op.action_code
        when Svn::Delta::TXDELTA_SOURCE
          assert_operator(op_size, :<=, composed_window.sview_len)
        when Svn::Delta::TXDELTA_NEW
          assert_operator(op_size, :<=, composed_window.new_data.length)
        when Svn::Delta::TXDELTA_TARGET
          assert_operator(op_size, :<=, composed_window.tview_len)
        else
          flunk
        end
      end
    end
  end

  def test_txdelta_apply_instructions
    s = ("a\nb\nc\nd\ne" + "\n" * 100) * 1000
    t = ("a\nb\nX\nd\ne" + "\n" * 100) * 1000
    source = StringIO.new(s)
    target = StringIO.new(t)
    stream = Svn::Delta::TextDeltaStream.new(source, target)

    result = ""
    offset = 0
    stream.each do |window|
      result << window.apply_instructions(s[offset, window.sview_len])
      offset += window.sview_len
    end
    assert_equal(t, result)
  end

  def test_push_target
    source = StringIO.new("abcde")
    target_content = "ZZZ" * 100
    data = ""
    finished = false
    handler = Proc.new do |window|
      if window
        data << window.new_data
      else
        finished = true
      end
    end
    target = Svn::Delta::TextDeltaStream.push_target(source, &handler)
    target.write(target_content)
    assert(!finished)
    target.close
    assert(finished)
    assert_equal(target_content, data)
  end

  def test_apply
    source_text = "abcde"
    target_text = "abXde"
    source = StringIO.new(source_text)
    target = StringIO.new(target_text)
    stream = Svn::Delta::TextDeltaStream.new(source, target)

    apply_source = StringIO.new(source_text)
    apply_result = StringIO.new("")

    handler, digest = Svn::Delta.apply(apply_source, apply_result)
    assert_nil(digest)
    handler.send(stream)
    apply_result.rewind
    assert_equal(target_text, apply_result.read)

    handler, digest = Svn::Delta.apply(apply_source, apply_result)
    handler.send(target_text)
    apply_result.rewind
    assert_equal(target_text * 2, apply_result.read)

    handler, digest = Svn::Delta.apply(apply_source, apply_result)
    handler.send(StringIO.new(target_text))
    apply_result.rewind
    assert_equal(target_text * 3, apply_result.read)
  end

  def get_regex(pattern, encoding='ASCII', options=0)
    Regexp.new(pattern.encode(encoding),options)
  end

  def test_svndiff
    source_text = "abcde"
    target_text = "abXde"
    source = StringIO.new(source_text)
    target = StringIO.new(target_text)
    stream = Svn::Delta::TextDeltaStream.new(source, target)

    if RUBY_VERSION > '1.9'
      output = StringIO.new("".encode('ASCII-8BIT'))
    else
      output = StringIO.new("")
    end
    handler = Svn::Delta.svndiff_handler(output)

    Svn::Delta.send(target_text, handler)
    output.rewind
    result = output.read
    if RUBY_VERSION > '1.9'
      regex = get_regex("\\ASVN.*#{target_text}\\Z".encode('utf-8'),result.encoding,16)
      assert_match(regex, result)
    else
      assert_match(/\ASVN.*#{target_text}\Z/, result)
    end
    # skip svndiff window
    input = StringIO.new(result[4..-1])
    window = Svn::Delta.read_svndiff_window(input, 0)
    assert_equal(target_text, window.new_data)

    finished = false
    data = ""
    stream = Svn::Delta.parse_svndiff do |window|
      if window
        data << window.new_data
      else
        finished = true
      end
    end
    stream.write(result)
    stream.close
    assert(finished)
    assert_equal(target_text, data)
  end

# Fails since r1852536: "delta path editor binding broken for root path"
#   https://issues.apache.org/jira/browse/SVN-4805
#
#  def test_path_driver
#    editor = Svn::Delta::BaseEditor.new
#    sorted_paths = []
#    callback = Proc.new do |parent_baton, path|
#      sorted_paths << path
#    end
#
#    targets = [
#      "/",
#      "/file1",
#      "/dir1",
#      "/dir2/file2",
#      "/dir2/dir3/file3",
#      "/dir2/dir3/file4"
#    ]
#    10.times do
#      x = rand(targets.size)
#      y = rand(targets.size)
#      targets[x], targets[y] = targets[y], targets[x]
#    end
#    Svn::Delta.path_driver(editor, 0, targets, &callback)
#    assert_equal(targets.sort, sorted_paths)
#  end

  def test_changed
    dir = "changed_dir"
    dir1 = "changed_dir1"
    dir2 = "changed_dir2"
    dir_path = File.join(@wc_path, dir)
    dir1_path = File.join(@wc_path, dir1)
    dir2_path = File.join(@wc_path, dir2)
    dir_svn_path = dir
    dir1_svn_path = dir1
    dir2_svn_path = dir2

    file1 = "changed1.txt"
    file2 = "changed2.txt"
    file3 = "changed3.txt"
    file4 = "changed4.txt"
    file5 = "changed5.txt"
    file1_path = File.join(@wc_path, file1)
    file2_path = File.join(dir_path, file2)
    file3_path = File.join(@wc_path, file3)
    file4_path = File.join(dir_path, file4)
    file5_path = File.join(@wc_path, file5)
    file1_svn_path = file1
    file2_svn_path = [dir_svn_path, file2].join("/")
    file3_svn_path = file3
    file4_svn_path = [dir_svn_path, file4].join("/")
    file5_svn_path = file5

    first_rev = nil

    log = "added 3 dirs\nanded 5 files"
    make_context(log) do |ctx|

      ctx.mkdir([dir_path, dir1_path, dir2_path])

      FileUtils.touch(file1_path)
      FileUtils.touch(file2_path)
      FileUtils.touch(file3_path)
      FileUtils.touch(file4_path)
      FileUtils.touch(file5_path)
      ctx.add(file1_path)
      ctx.add(file2_path)
      ctx.add(file3_path)
      ctx.add(file4_path)
      ctx.add(file5_path)

      commit_info = ctx.commit(@wc_path)
      first_rev = commit_info.revision

      editor = traverse(Svn::Delta::ChangedEditor, commit_info.revision, true)
      assert_equal([
                     file1_svn_path, file2_svn_path,
                     file3_svn_path, file4_svn_path,
                     file5_svn_path,
                   ].sort,
                   editor.added_files)
      assert_equal([], editor.updated_files)
      assert_equal([], editor.deleted_files)
      assert_equal([].sort, editor.updated_dirs)
      assert_equal([].sort, editor.deleted_dirs)
      assert_equal([
                     "#{dir_svn_path}/",
                     "#{dir1_svn_path}/",
                     "#{dir2_svn_path}/"
                   ].sort,
                   editor.added_dirs)
    end

    log = "deleted 2 dirs\nchanged 3 files\ndeleted 2 files\nadded 3 files"
    make_context(log) do |ctx|

      dir3 = "changed_dir3"
      dir4 = "changed_dir4"
      dir3_path = File.join(dir_path, dir3)
      dir4_path = File.join(@wc_path, dir4)
      dir3_svn_path = [dir_svn_path, dir3].join("/")
      dir4_svn_path = dir4

      file6 = "changed6.txt"
      file7 = "changed7.txt"
      file8 = "changed8.txt"
      file9 = "changed9.txt"
      file10 = "changed10.txt"
      file6_path = File.join(dir_path, file6)
      file7_path = File.join(@wc_path, file7)
      file8_path = File.join(dir_path, file8)
      file9_path = File.join(dir_path, file9)
      file10_path = File.join(dir_path, file10)
      file6_svn_path = [dir_svn_path, file6].join("/")
      file7_svn_path = file7
      file8_svn_path = [dir_svn_path, file8].join("/")
      file9_svn_path = [dir_svn_path, file9].join("/")
      file10_svn_path = [dir_svn_path, file10].join("/")

      File.open(file1_path, "w") {|f| f.puts "changed"}
      File.open(file2_path, "w") {|f| f.puts "changed"}
      File.open(file3_path, "w") {|f| f.puts "changed"}
      ctx.rm_f([file4_path, file5_path])
      FileUtils.touch(file6_path)
      FileUtils.touch(file7_path)
      FileUtils.touch(file8_path)
      ctx.add(file6_path)
      ctx.add(file7_path)
      ctx.add(file8_path)
      ctx.cp(file1_path, file9_path)
      ctx.cp(file2_path, file10_path)
      ctx.mv(dir2_path, dir3_path)
      ctx.cp(dir1_path, dir4_path)
      ctx.rm(dir1_path)

      commit_info = ctx.commit(@wc_path)
      second_rev = commit_info.revision

      editor = traverse(Svn::Delta::ChangedEditor, commit_info.revision, true)
      assert_equal([file1_svn_path, file2_svn_path, file3_svn_path].sort,
                   editor.updated_files)
      assert_equal([file4_svn_path, file5_svn_path].sort,
                   editor.deleted_files)
      assert_equal([file6_svn_path, file7_svn_path, file8_svn_path].sort,
                   editor.added_files)
      assert_equal([].sort, editor.updated_dirs)
      assert_equal([
                     [file9_svn_path, file1_svn_path, first_rev],
                     [file10_svn_path, file2_svn_path, first_rev],
                   ].sort_by{|x| x[0]},
                   editor.copied_files)
      assert_equal([
                     ["#{dir3_svn_path}/", "#{dir2_svn_path}/", first_rev],
                     ["#{dir4_svn_path}/", "#{dir1_svn_path}/", first_rev],
                   ].sort_by{|x| x[0]},
                   editor.copied_dirs)
      assert_equal(["#{dir1_svn_path}/", "#{dir2_svn_path}/"].sort,
                   editor.deleted_dirs)
      assert_equal([].sort, editor.added_dirs)
    end
  end

  def test_change_prop
    prop_name = "prop"
    prop_value = "value"

    dir = "dir"
    dir_path = File.join(@wc_path, dir)
    dir_svn_path = dir

    file1 = "file1.txt"
    file2 = "file2.txt"
    file1_path = File.join(@wc_path, file1)
    file2_path = File.join(dir_path, file2)

    log = "added 1 dirs\nanded 2 files"
    make_context(log) do |ctx|

      ctx.mkdir([dir_path])

      file1_svn_path = file1
      file2_svn_path = [dir_svn_path, file2].join("/")
      FileUtils.touch(file1_path)
      FileUtils.touch(file2_path)
      ctx.add(file1_path)
      ctx.add(file2_path)

      ctx.propset(prop_name, prop_value, dir_path)

      commit_info = ctx.commit(@wc_path)

      editor = traverse(Svn::Delta::ChangedDirsEditor, commit_info.revision)
      assert_equal(["", dir_svn_path].collect{|path| "#{path}/"}.sort,
                   editor.changed_dirs)
    end

    log = "prop changed"
    make_context(log) do |ctx|

      ctx.propdel(prop_name, dir_path)

      commit_info = ctx.commit(@wc_path)

      editor = traverse(Svn::Delta::ChangedDirsEditor, commit_info.revision)
      assert_equal([dir_svn_path].collect{|path| "#{path}/"}.sort,
                   editor.changed_dirs)


      ctx.propset(prop_name, prop_value, file1_path)

      commit_info = ctx.commit(@wc_path)

      editor = traverse(Svn::Delta::ChangedDirsEditor, commit_info.revision)
      assert_equal([""].collect{|path| "#{path}/"}.sort,
                   editor.changed_dirs)


      ctx.propdel(prop_name, file1_path)
      ctx.propset(prop_name, prop_value, file2_path)

      commit_info = ctx.commit(@wc_path)

      editor = traverse(Svn::Delta::ChangedDirsEditor, commit_info.revision)
      assert_equal(["", dir_svn_path].collect{|path| "#{path}/"}.sort,
                   editor.changed_dirs)
    end
  end

  def test_deep_copy
    dir1 = "dir1"
    dir2 = "dir2"
    dir1_path = File.join(@wc_path, dir1)
    dir2_path = File.join(dir1_path, dir2)
    dir1_svn_path = dir1
    dir2_svn_path = [dir1, dir2].join("/")

    first_rev = nil

    log = "added 2 dirs\nanded 3 files"
    make_context(log) do |ctx|

      ctx.mkdir([dir1_path, dir2_path])

      file1 = "file1.txt"
      file2 = "file2.txt"
      file3 = "file3.txt"
      file1_path = File.join(@wc_path, file1)
      file2_path = File.join(dir1_path, file2)
      file3_path = File.join(dir2_path, file3)
      file1_svn_path = file1
      file2_svn_path = [dir1_svn_path, file2].join("/")
      file3_svn_path = [dir2_svn_path, file3].join("/")
      FileUtils.touch(file1_path)
      FileUtils.touch(file2_path)
      FileUtils.touch(file3_path)
      ctx.add(file1_path)
      ctx.add(file2_path)
      ctx.add(file3_path)

      commit_info = ctx.commit(@wc_path)
      first_rev = commit_info.revision

      editor = traverse(Svn::Delta::ChangedEditor, commit_info.revision, true)
      assert_equal([
                     file1_svn_path, file2_svn_path,
                     file3_svn_path,
                   ].sort,
                   editor.added_files)
      assert_equal([].sort, editor.updated_files)
      assert_equal([].sort, editor.deleted_files)
      assert_equal([].sort, editor.updated_dirs)
      assert_equal([].sort, editor.deleted_dirs)
      assert_equal([
                     "#{dir1_svn_path}/",
                     "#{dir2_svn_path}/",
                   ].sort,
                   editor.added_dirs)
    end

    log = "copied top dir"
    make_context(log) do |ctx|

      dir3 = "dir3"
      dir3_path = File.join(@wc_path, dir3)
      dir3_svn_path = dir3

      ctx.cp(dir1_path, dir3_path)

      commit_info = ctx.commit(@wc_path)
      second_rev = commit_info.revision

      editor = traverse(Svn::Delta::ChangedEditor, commit_info.revision, true)
      assert_equal([].sort, editor.updated_files)
      assert_equal([].sort, editor.deleted_files)
      assert_equal([].sort, editor.added_files)
      assert_equal([].sort, editor.updated_dirs)
      assert_equal([].sort, editor.copied_files)
      assert_equal([
                     ["#{dir3_svn_path}/", "#{dir1_svn_path}/", first_rev]
                   ].sort_by{|x| x[0]},
                   editor.copied_dirs)
      assert_equal([].sort, editor.deleted_dirs)
      assert_equal([].sort, editor.added_dirs)
    end
  end

  private
  def traverse(editor_class, rev, pass_root=false)
    root = @fs.root
    base_rev = rev - 1
    base_root = @fs.root(base_rev)
    if pass_root
      editor = editor_class.new(root, base_root)
    else
      editor = editor_class.new
    end
    base_root.dir_delta("", "", root, "", editor)
    editor
  end
end