summaryrefslogtreecommitdiff
path: root/subversion/bindings/swig/ruby/test/test_ra.rb
blob: 1d20ca49146bf07ad746f2f5709ece75e17a9a05 (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
# ====================================================================
#    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 "util"

require "svn/ra"

class SvnRaTest < 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::Ra.version)
  end

  def test_uuid
    Svn::Ra::Session.open(@repos_uri) do |session|
      assert_equal(@repos.fs.uuid, session.uuid)
    end
  end

  def test_open_without_callback
    assert_nothing_raised do
      Svn::Ra::Session.open(@repos_uri).close
    end
  end

  def test_session
    log = "sample log"
    log2 = "sample log2"
    file = "sample.txt"
    src = "sample source"
    path = File.join(@wc_path, file)

    session = nil
    rev1 = nil
    path_props = nil

    make_context(log) do |ctx0|
      config = {}
      path_props = {"my-prop" => "value"}
      callbacks = Svn::Ra::Callbacks.new(ctx0.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config,callbacks) do |session|

        assert_equal(youngest_rev, session.latest_revnum)
        assert_equal(@repos_uri, session.repos_root)

        File.open(path, "w") {|f| f.print(src)}
        ctx0.add(path)
        info = ctx0.ci(@wc_path)
        rev1 = info.revision

        assert_equal(info.revision, session.dated_revision(info.date))
        content, props = session.file(file, info.revision)
        assert_equal(src, content)
        assert_equal([
                       Svn::Core::PROP_ENTRY_COMMITTED_DATE,
                       Svn::Core::PROP_ENTRY_UUID,
                       Svn::Core::PROP_ENTRY_LAST_AUTHOR,
                       Svn::Core::PROP_ENTRY_COMMITTED_REV,
                     ].sort,
                     props.keys.sort)
        entries, props = session.dir("", info.revision)
        assert_equal([file], entries.keys)
        assert(entries[file].file?)
        assert_equal([
                       Svn::Core::PROP_ENTRY_COMMITTED_DATE,
                       Svn::Core::PROP_ENTRY_UUID,
                       Svn::Core::PROP_ENTRY_LAST_AUTHOR,
                       Svn::Core::PROP_ENTRY_COMMITTED_REV,
                     ].sort,
                     props.keys.sort)

        entries, props = session.dir("", info.revision, Svn::Core::DIRENT_KIND)
        assert_equal(Svn::Core::NODE_FILE, entries[file].kind)
        entries, props = session.dir("", info.revision, 0)
        assert_equal(Svn::Core::NODE_UNKNOWN, entries[file].kind)

        make_context(log2) do |ctx|
          File.open(path, "w") {|f| f.print(src * 2)}
          path_props.each do |key, value|
            ctx.prop_set(key, value, path)
          end
          info = ctx.ci(@wc_path)
          rev2 = info.revision

          logs = []
          receiver = Proc.new do |changed_paths, revision, author, date, message|
            logs << [revision, message]
          end
          session.log([file], rev1, rev2, rev2 - rev1 + 1, &receiver)
          assert_equal([
                         [rev1, log],
                         [rev2, log2],
                       ].sort_by {|rev, log| rev},
                       logs.sort_by {|rev, log| rev})

          assert_equal(Svn::Core::NODE_FILE, session.check_path(file))
          assert_equal(Svn::Core::NODE_FILE, session.stat(file).kind)

          assert_equal({
                         rev1 => "/#{file}",
                         rev2 => "/#{file}",
                       },
                       session.locations(file, [rev1, rev2]))

          infos = []
          session.file_revs(file, rev1, rev2) do |_path, rev, rev_props, prop_diffs|
            hashed_prop_diffs = {}
            prop_diffs.each do |prop|
              hashed_prop_diffs[prop.name] = prop.value
            end
            infos << [rev, _path, hashed_prop_diffs]
          end
          assert_equal([
                         [rev1, "/#{file}", {}],
                         [rev2, "/#{file}", path_props],
                       ],
                       infos)

          infos = []
          session.file_revs2(file, rev1, rev2) do |_path, rev, rev_props, prop_diffs|
            infos << [rev, _path, prop_diffs]
          end
          assert_equal([
                         [rev1, "/#{file}", {}],
                         [rev2, "/#{file}", path_props],
                       ],
                       infos)

          assert_equal({}, session.get_locks(""))
          locks = []
          session.lock({file => rev2}) do |_path, do_lock, lock, ra_err|
            locks << [_path, do_lock, lock, ra_err]
          end
          assert_equal([file],
                       locks.collect{|_path, *rest| _path}.sort)
          lock = locks.assoc(file)[2]
          assert_equal(["/#{file}"],
                       session.get_locks("").collect{|_path, *rest| _path})
          assert_equal(lock.token, session.get_lock(file).token)
          assert_equal([lock.token],
                       session.get_locks(file).values.collect{|l| l.token})
          session.unlock({file => lock.token})
          assert_equal({}, session.get_locks(file))
        end
      end
    end
  end

  def assert_property_access
    log = "sample log"
    file = "sample.txt"
    path = File.join(@wc_path, file)
    make_context(log) do |ctx|
      config = {}
      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|

        FileUtils.touch(path)
        ctx.add(path)
        info = ctx.commit(@wc_path)

        assert_equal(@author, yield(session, :get, Svn::Core::PROP_REVISION_AUTHOR))
        assert_equal(log, yield(session, :get, Svn::Core::PROP_REVISION_LOG))
        assert_equal([
                       Svn::Core::PROP_REVISION_AUTHOR,
                       Svn::Core::PROP_REVISION_DATE,
                       Svn::Core::PROP_REVISION_LOG,
                     ].sort,
                     yield(session, :list).keys.sort)
        yield(session, :set, Svn::Core::PROP_REVISION_LOG, nil)
        assert_nil(yield(session, :get ,Svn::Core::PROP_REVISION_LOG))
        assert_equal([
                       Svn::Core::PROP_REVISION_AUTHOR,
                       Svn::Core::PROP_REVISION_DATE,
                     ].sort,
                     yield(session, :list).keys.sort)
      end
    end
  end

  def test_prop
    assert_property_access do |session, action, *args|
      case action
      when :get
        key, = args
        session[key]
      when :set
        key, value = args
        session[key] = value
      when :list
        session.properties
      end
    end
  end

  def test_prop_with_no_ruby_way
    assert_property_access do |session, action, *args|
      case action
      when :get
        key, = args
        session.prop(key)
      when :set
        key, value = args
        session.set_prop(key, value)
      when :list
        session.proplist
      end
    end
  end

  def test_callback
    log = "sample log"
    file = "sample.txt"
    src1 = "sample source1"
    src2 = "sample source2"
    path = File.join(@wc_path, file)
    path_in_repos = "/#{file}"
    make_context(log) do |ctx|
      config = {}
      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|

        File.open(path, "w") {|f| f.print(src1)}
        ctx.add(path)
        rev1 = ctx.ci(@wc_path).revision

        File.open(path, "w") {|f| f.print(src2)}
        rev2 = ctx.ci(@wc_path).revision

        ctx.up(@wc_path)

        editor, editor_baton = session.commit_editor(log) {}
        reporter = session.update(rev2, "", editor, editor_baton)
        reporter.abort_report

        editor, editor_baton = session.commit_editor(log) {}
        reporter = session.update2(rev2, "", editor)
        reporter.abort_report
      end
    end
  end

  def test_diff
    log = "sample log"
    file = "sample.txt"
    src1 = "a\nb\nc\nd\ne\n"
    src2 = "a\nb\nC\nd\ne\n"
    path = File.join(@wc_path, file)
    path_in_repos = "/#{file}"
    make_context(log) do |ctx|
      config = {}
      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|

        File.open(path, "w") {|f| f.print(src1)}
        ctx.add(path)
        rev1 = ctx.ci(@wc_path).revision

        File.open(path, "w") {|f| f.print(src2)}
        rev2 = ctx.ci(@wc_path).revision

        ctx.up(@wc_path)

        editor = Svn::Delta::BaseEditor.new # dummy
        reporter = session.diff(rev2, "", @repos_uri, editor)
        reporter.set_path("", rev1, false, nil)
        reporter.finish_report
      end
    end
  end

  def test_commit_editor
    log = "sample log"
    dir_uri = "/test"
    config = {}
    make_context(log) do |ctx|
      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|
        actual_info = nil
        actual_date = nil

        expected_info = [1, @author]
        start_time = Time.now
        gc_disable do
          editor, baton = session.commit_editor(log) do |rev, date, author|
            actual_info = [rev, author]
            actual_date = date
          end
          editor.baton = baton

          root = editor.open_root(-1)
          editor.add_directory(dir_uri, root, nil, -1)
          gc_enable do
            GC.start
            editor.close_edit
          end
          assert_equal(expected_info, actual_info)
          assert_operator(start_time..(Time.now), :include?, actual_date)
        end
      end
    end
  end

  def test_commit_editor2
    log = "sample log"
    dir_uri = "/test"
    config = {}
    make_context(log) do |ctx|
      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|
        actual_info = nil
        actual_date = nil

        expected_info = [1, @author]
        start_time = Time.now
        gc_disable do
          editor = session.commit_editor2(log) do |info|
            actual_info = [info.revision, info.author]
            actual_date = info.date
          end

          root = editor.open_root(-1)
          editor.add_directory(dir_uri, root, nil, -1)
          gc_enable do
            GC.start
            editor.close_edit
          end
          assert_equal(expected_info, actual_info)
          assert_operator(start_time..(Time.now), :include?, actual_date)
        end
      end
    end
  end

  def test_reparent
    log = "sample log"
    dir = "dir"
    deep_dir = "deep"
    dir_path = File.join(@wc_path, dir)
    deep_dir_path = File.join(dir_path, deep_dir)
    config = {}
    make_context(log) do |ctx|

      ctx.mkdir(dir_path)
      ctx.ci(@wc_path)

      ctx.mkdir(deep_dir_path)
      ctx.ci(@wc_path)

      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|

        entries, props = session.dir(dir, nil)
        assert_equal([deep_dir], entries.keys)
        assert_raise(Svn::Error::FS_NOT_FOUND) do
          session.dir(deep_dir)
        end

        session.reparent("#{@repos_uri}/#{dir}")
        assert_raise(Svn::Error::FS_NOT_FOUND) do
          session.dir(dir)
        end
        entries, props = session.dir(deep_dir)
        assert_equal([], entries.keys)

        assert_raise(Svn::Error::RA_ILLEGAL_URL) do
          session.reparent("file:///tmp/xxx")
        end
      end
    end
  end

  def test_mergeinfo
    log = "sample log"
    file = "sample.txt"
    src = "sample\n"
    config = {}
    trunk = File.join(@wc_path, "trunk")
    branch = File.join(@wc_path, "branch")
    trunk_path = File.join(trunk, file)
    branch_path = File.join(branch, file)
    session_relative_trunk_path = "trunk"
    repository_branch_path = "/branch"

    make_context(log) do |ctx|
      ctx.mkdir(trunk, branch)
      File.open(trunk_path, "w") {}
      File.open(branch_path, "w") {}
      ctx.add(trunk_path)
      ctx.add(branch_path)
      rev1 = ctx.commit(@wc_path).revision

      File.open(branch_path, "w") {|f| f.print(src)}
      rev2 = ctx.commit(@wc_path).revision

      callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
      Svn::Ra::Session.open(@repos_uri, config, callbacks) do |session|

        assert_nil(session.mergeinfo(session_relative_trunk_path))
        ctx.merge(branch, rev1, branch, rev2, trunk)
        assert_nil(session.mergeinfo(session_relative_trunk_path))

        rev3 = ctx.commit(@wc_path).revision
        mergeinfo = session.mergeinfo(session_relative_trunk_path)
        assert_equal([session_relative_trunk_path], mergeinfo.keys)
        trunk_mergeinfo = mergeinfo[session_relative_trunk_path]
        assert_equal([repository_branch_path], trunk_mergeinfo.keys)
        assert_equal([[1, 2, true]],
                     trunk_mergeinfo[repository_branch_path].collect {|range|
                        range.to_a})

        ctx.rm(branch_path)
        rev4 = ctx.commit(@wc_path).revision

        ctx.merge(branch, rev3, branch, rev4, trunk)
        assert(!File.exist?(trunk_path))
        rev5 = ctx.commit(@wc_path).revision

        mergeinfo = session.mergeinfo(session_relative_trunk_path, rev5)
        assert_equal([session_relative_trunk_path], mergeinfo.keys)
        trunk_mergeinfo = mergeinfo[session_relative_trunk_path]
        assert_equal([repository_branch_path], trunk_mergeinfo.keys)
        assert_equal([[1, 2, true], [3, 4, true]],
                     trunk_mergeinfo[repository_branch_path].collect {|range|
                        range.to_a})
      end
    end
  end
end