* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / swig / ruby / test / test_ra.rb
blob34ced56807c24cc75e0049e10541c3175a68404c
1 require "util"
3 require "svn/ra"
5 class SvnRaTest < Test::Unit::TestCase
6   include SvnTestUtil
8   def setup
9     setup_basic
10   end
12   def teardown
13     teardown_basic
14   end
16   def test_version
17     assert_equal(Svn::Core.subr_version, Svn::Ra.version)
18   end
20   def test_uuid
21     session = Svn::Ra::Session.open(@repos_uri)
22     assert_equal(File.read(File.join(@repos_path, "db", "uuid")).strip,
23                  session.uuid)
24   end
26   def test_open_without_callback
27     assert_nothing_raised do
28       Svn::Ra::Session.open(@repos_uri)
29     end
30   end
32   def test_session
33     log = "sample log"
34     log2 = "sample log2"
35     file = "sample.txt"
36     src = "sample source"
37     path = File.join(@wc_path, file)
38     ctx = make_context(log)
39     config = {}
40     path_props = {"my-prop" => "value"}
41     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
42     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
44     assert_equal(youngest_rev, session.latest_revnum)
45     assert_equal(@repos_uri, session.repos_root)
47     File.open(path, "w") {|f| f.print(src)}
48     ctx.add(path)
49     info = ctx.ci(@wc_path)
50     rev1 = info.revision
52     assert_equal(info.revision, session.dated_revision(info.date))
53     content, props = session.file(file, info.revision)
54     assert_equal(src, content)
55     assert_equal([
56                    Svn::Core::PROP_ENTRY_COMMITTED_DATE,
57                    Svn::Core::PROP_ENTRY_UUID,
58                    Svn::Core::PROP_ENTRY_LAST_AUTHOR,
59                    Svn::Core::PROP_ENTRY_COMMITTED_REV,
60                  ].sort,
61                  props.keys.sort)
62     entries, props = session.dir("", info.revision)
63     assert_equal([file], entries.keys)
64     assert(entries[file].file?)
65     assert_equal([
66                    Svn::Core::PROP_ENTRY_COMMITTED_DATE,
67                    Svn::Core::PROP_ENTRY_UUID,
68                    Svn::Core::PROP_ENTRY_LAST_AUTHOR,
69                    Svn::Core::PROP_ENTRY_COMMITTED_REV,
70                  ].sort,
71                  props.keys.sort)
73     entries, props = session.dir("", info.revision, Svn::Core::DIRENT_KIND)
74     assert_equal(Svn::Core::NODE_FILE, entries[file].kind)
75     entries, props = session.dir("", info.revision, 0)
76     assert_equal(Svn::Core::NODE_NONE, entries[file].kind)
78     ctx = make_context(log2)
79     File.open(path, "w") {|f| f.print(src * 2)}
80     path_props.each do |key, value|
81       ctx.prop_set(key, value, path)
82     end
83     info = ctx.ci(@wc_path)
84     rev2 = info.revision
86     logs = []
87     receiver = Proc.new do |changed_paths, revision, author, date, message|
88       logs << [revision, message]
89     end
90     session.log([file], rev1, rev2, rev2 - rev1 + 1, &receiver)
91     assert_equal([
92                    [rev1, log],
93                    [rev2, log2],
94                  ].sort_by {|rev, log| rev},
95                  logs.sort_by {|rev, log| rev})
97     assert_equal(Svn::Core::NODE_FILE, session.check_path(file))
98     assert_equal(Svn::Core::NODE_FILE, session.stat(file).kind)
100     assert_equal({
101                    rev1 => "/#{file}",
102                    rev2 => "/#{file}",
103                  },
104                  session.locations(file, [rev1, rev2]))
106     infos = []
107     session.file_revs(file, rev1, rev2) do |_path, rev, rev_props, prop_diffs|
108       hashed_prop_diffs = {}
109       prop_diffs.each do |prop|
110         hashed_prop_diffs[prop.name] = prop.value
111       end
112       infos << [rev, _path, hashed_prop_diffs]
113     end
114     assert_equal([
115                    [rev1, "/#{file}", {}],
116                    [rev2, "/#{file}", path_props],
117                  ],
118                  infos)
120     infos = []
121     session.file_revs2(file, rev1, rev2) do |_path, rev, rev_props, prop_diffs|
122       infos << [rev, _path, prop_diffs]
123     end
124     assert_equal([
125                    [rev1, "/#{file}", {}],
126                    [rev2, "/#{file}", path_props],
127                  ],
128                  infos)
130     assert_equal({}, session.get_locks(""))
131     locks = []
132     session.lock({file => rev2}) do |_path, do_lock, lock, ra_err|
133       locks << [_path, do_lock, lock, ra_err]
134     end
135     assert_equal([file],
136                  locks.collect{|_path, *rest| _path}.sort)
137     lock = locks.assoc(file)[2]
138     assert_equal(["/#{file}"],
139                  session.get_locks("").collect{|_path, *rest| _path})
140     assert_equal(lock.token, session.get_lock(file).token)
141     assert_equal([lock.token],
142                  session.get_locks(file).values.collect{|l| l.token})
143     session.unlock({file => lock.token})
144     assert_equal({}, session.get_locks(file))
145   end
147   def assert_property_access
148     log = "sample log"
149     file = "sample.txt"
150     path = File.join(@wc_path, file)
151     ctx = make_context(log)
152     config = {}
153     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
154     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
156     FileUtils.touch(path)
157     ctx.add(path)
158     info = ctx.commit(@wc_path)
160     assert_equal(@author, yield(session, :get, Svn::Core::PROP_REVISION_AUTHOR))
161     assert_equal(log, yield(session, :get, Svn::Core::PROP_REVISION_LOG))
162     assert_equal([
163                    Svn::Core::PROP_REVISION_AUTHOR,
164                    Svn::Core::PROP_REVISION_DATE,
165                    Svn::Core::PROP_REVISION_LOG,
166                  ].sort,
167                  yield(session, :list).keys.sort)
168     yield(session, :set, Svn::Core::PROP_REVISION_LOG, nil)
169     assert_nil(yield(session, :get ,Svn::Core::PROP_REVISION_LOG))
170     assert_equal([
171                    Svn::Core::PROP_REVISION_AUTHOR,
172                    Svn::Core::PROP_REVISION_DATE,
173                  ].sort,
174                  yield(session, :list).keys.sort)
175   end
177   def test_prop
178     assert_property_access do |session, action, *args|
179       case action
180       when :get
181         key, = args
182         session[key]
183       when :set
184         key, value = args
185         session[key] = value
186       when :list
187         session.properties
188       end
189     end
190   end
192   def test_prop_with_no_ruby_way
193     assert_property_access do |session, action, *args|
194       case action
195       when :get
196         key, = args
197         session.prop(key)
198       when :set
199         key, value = args
200         session.set_prop(key, value)
201       when :list
202         session.proplist
203       end
204     end
205   end
207   def test_callback
208     log = "sample log"
209     file = "sample.txt"
210     src1 = "sample source1"
211     src2 = "sample source2"
212     path = File.join(@wc_path, file)
213     path_in_repos = "/#{file}"
214     ctx = make_context(log)
215     config = {}
216     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
217     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
219     File.open(path, "w") {|f| f.print(src1)}
220     ctx.add(path)
221     rev1 = ctx.ci(@wc_path).revision
223     File.open(path, "w") {|f| f.print(src2)}
224     rev2 = ctx.ci(@wc_path).revision
226     ctx.up(@wc_path)
228     editor, editor_baton = session.commit_editor(log) {}
229     reporter = session.update(rev2, "", editor, editor_baton)
230     reporter.abort_report
232     editor, editor_baton = session.commit_editor(log) {}
233     reporter = session.update2(rev2, "", editor)
234     reporter.abort_report
235   end
237   def test_diff
238     log = "sample log"
239     file = "sample.txt"
240     src1 = "a\nb\nc\nd\ne\n"
241     src2 = "a\nb\nC\nd\ne\n"
242     path = File.join(@wc_path, file)
243     path_in_repos = "/#{file}"
244     ctx = make_context(log)
245     config = {}
246     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
247     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
249     File.open(path, "w") {|f| f.print(src1)}
250     ctx.add(path)
251     rev1 = ctx.ci(@wc_path).revision
253     File.open(path, "w") {|f| f.print(src2)}
254     rev2 = ctx.ci(@wc_path).revision
256     ctx.up(@wc_path)
258     editor = Svn::Delta::BaseEditor.new # dummy
259     reporter = session.diff(rev2, "", @repos_uri, editor)
260     reporter.set_path("", rev1, false, nil)
261     reporter.finish_report
262   end
264   def test_commit_editor
265     log = "sample log"
266     dir_uri = "/test"
267     config = {}
268     ctx = make_context(log)
269     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
270     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
271     actual_info = nil
272     actual_date = nil
274     expected_info = [1, @author]
275     start_time = Time.now
276     gc_disable do
277       editor, baton = session.commit_editor(log) do |rev, date, author|
278         actual_info = [rev, author]
279         actual_date = date
280       end
281       editor.baton = baton
283       root = editor.open_root(-1)
284       editor.add_directory(dir_uri, root, nil, -1)
285       gc_enable do
286         GC.start
287         editor.close_edit
288       end
289       assert_equal(expected_info, actual_info)
290       assert_operator(start_time..(Time.now), :include?, actual_date)
291     end
292   end
294   def test_commit_editor2
295     log = "sample log"
296     dir_uri = "/test"
297     config = {}
298     ctx = make_context(log)
299     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
300     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
301     actual_info = nil
302     actual_date = nil
304     expected_info = [1, @author]
305     start_time = Time.now
306     gc_disable do
307       editor = session.commit_editor2(log) do |info|
308         actual_info = [info.revision, info.author]
309         actual_date = info.date
310       end
312       root = editor.open_root(-1)
313       editor.add_directory(dir_uri, root, nil, -1)
314       gc_enable do
315         GC.start
316         editor.close_edit
317       end
318       assert_equal(expected_info, actual_info)
319       assert_operator(start_time..(Time.now), :include?, actual_date)
320     end
321   end
323   def test_reparent
324     log = "sample log"
325     dir = "dir"
326     deep_dir = "deep"
327     dir_path = File.join(@wc_path, dir)
328     deep_dir_path = File.join(dir_path, deep_dir)
329     config = {}
330     ctx = make_context(log)
332     ctx.mkdir(dir_path)
333     ctx.ci(@wc_path)
335     ctx.mkdir(deep_dir_path)
336     ctx.ci(@wc_path)
338     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
339     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
341     entries, props = session.dir(dir, nil)
342     assert_equal([deep_dir], entries.keys)
343     assert_raise(Svn::Error::FS_NOT_FOUND) do
344       session.dir(deep_dir)
345     end
347     session.reparent("#{@repos_uri}/#{dir}")
348     assert_raise(Svn::Error::FS_NOT_FOUND) do
349       session.dir(dir)
350     end
351     entries, props = session.dir(deep_dir)
352     assert_equal([], entries.keys)
354     assert_raise(Svn::Error::RA_ILLEGAL_URL) do
355       session.reparent("file:///tmp/xxx")
356     end
357   end
359   def test_mergeinfo
360     log = "sample log"
361     file = "sample.txt"
362     src = "sample\n"
363     config = {}
364     trunk = File.join(@wc_path, "trunk")
365     branch = File.join(@wc_path, "branch")
366     trunk_path = File.join(trunk, file)
367     branch_path = File.join(branch, file)
368     session_relative_trunk_path = "trunk"
369     repository_branch_path = "/branch"
371     ctx = make_context(log)
372     ctx.mkdir(trunk, branch)
373     File.open(trunk_path, "w") {}
374     File.open(branch_path, "w") {}
375     ctx.add(trunk_path)
376     ctx.add(branch_path)
377     rev1 = ctx.commit(@wc_path).revision
379     File.open(branch_path, "w") {|f| f.print(src)}
380     rev2 = ctx.commit(@wc_path).revision
382     callbacks = Svn::Ra::Callbacks.new(ctx.auth_baton)
383     session = Svn::Ra::Session.open(@repos_uri, config, callbacks)
385     assert_nil(session.mergeinfo(session_relative_trunk_path))
386     ctx.merge(branch, rev1, branch, rev2, trunk)
387     assert_nil(session.mergeinfo(session_relative_trunk_path))
389     rev3 = ctx.commit(@wc_path).revision
390     mergeinfo = session.mergeinfo(session_relative_trunk_path)
391     assert_equal([session_relative_trunk_path], mergeinfo.keys)
392     trunk_mergeinfo = mergeinfo[session_relative_trunk_path]
393     assert_equal([repository_branch_path], trunk_mergeinfo.keys)
394     assert_equal([[1, 2, true]],
395                  trunk_mergeinfo[repository_branch_path].collect {|range|
396                     range.to_a})
398     ctx.rm(branch_path)
399     rev4 = ctx.commit(@wc_path).revision
401     ctx.merge(branch, rev3, branch, rev4, trunk)
402     assert(!File.exist?(trunk_path))
403     rev5 = ctx.commit(@wc_path).revision
405     mergeinfo = session.mergeinfo(session_relative_trunk_path, rev5)
406     assert_equal([session_relative_trunk_path], mergeinfo.keys)
407     trunk_mergeinfo = mergeinfo[session_relative_trunk_path]
408     assert_equal([repository_branch_path], trunk_mergeinfo.keys)
409     assert_equal([[1, 2, true], [3, 4, true]],
410                  trunk_mergeinfo[repository_branch_path].collect {|range|
411                     range.to_a})
412   end