* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / swig / ruby / test / util.rb
blob9611087b75484e53f4a29b9db780b68e83fd8c06
1 require "fileutils"
2 require "pathname"
4 require "my-assertions"
6 class Time
7   unless instance_methods.include?("to_int")
8     alias to_int to_i
9   end
10 end
12 require 'greek_tree'
14 module SvnTestUtil
15   def setup_default_variables
16     test_dir = Pathname.new(File.dirname(__FILE__))
17     pwd = Pathname.new(Dir.pwd)
18     @base_dir = test_dir.relative_path_from(pwd).to_s
19     @author = ENV["USER"] || "sample-user"
20     @password = "sample-password"
21     @realm = "sample realm"
22     @repos_path = File.join(@base_dir, "repos")
23     @full_repos_path = File.expand_path(@repos_path)
24     @repos_uri = "file://#{@full_repos_path.sub(/^\/?/, '/')}"
25     @svnserve_host = "127.0.0.1"
26     @svnserve_ports = (64152..64282).collect{|x| x.to_s}
27     @wc_base_dir = File.join(@base_dir, "wc-tmp")
28     @wc_path = File.join(@wc_base_dir, "wc")
29     @full_wc_path = File.expand_path(@wc_path)
30     @tmp_path = File.join(@base_dir, "tmp")
31     @config_path = File.join(@base_dir, "config")
32     @greek = Greek.new(@tmp_path, @wc_path, @repos_uri)
33   end
35   def setup_basic(need_svnserve=false)
36     @need_svnserve = need_svnserve
37     setup_default_variables
38     setup_tmp
39     setup_repository
40     add_hooks
41     setup_svnserve if @need_svnserve
42     setup_config
43     setup_wc
44     add_authentication
45     GC.stress = true if GC.respond_to?(:stress=) and $DEBUG
46   end
48   def teardown_basic
49     GC.stress = false if GC.respond_to?(:stress=)
50     teardown_svnserve if @need_svnserve
51     teardown_repository
52     teardown_wc
53     teardown_config
54     teardown_tmp
55     gc
56   end
58   def gc
59     if $DEBUG
60       before_pools = Svn::Core::Pool.number_of_pools
61       puts
62       puts "before pools: #{before_pools}"
63     end
64     gc_enable do
65       GC.start
66     end
67     if $DEBUG
68       after_pools = Svn::Core::Pool.number_of_pools
69       puts "after pools: #{after_pools}"
70       STDOUT.flush
71     end
72   end
74   def change_gc_status(prev_disabled)
75     begin
76       yield
77     ensure
78       if prev_disabled
79         GC.disable
80       else
81         GC.enable
82       end
83     end
84   end
86   def gc_disable(&block)
87     change_gc_status(GC.disable, &block)
88   end
90   def gc_enable(&block)
91     change_gc_status(GC.enable, &block)
92   end
94   def setup_tmp(path=@tmp_path)
95     FileUtils.rm_rf(path)
96     FileUtils.mkdir_p(path)
97   end
99   def teardown_tmp(path=@tmp_path)
100     FileUtils.rm_rf(path)
101   end
103   def setup_repository(path=@repos_path, config={}, fs_config={})
104     FileUtils.rm_rf(path)
105     FileUtils.mkdir_p(File.dirname(path))
106     Svn::Repos.create(path, config, fs_config)
107     @repos = Svn::Repos.open(@repos_path)
108     @fs = @repos.fs
109   end
111   def teardown_repository(path=@repos_path)
112     @fs.close unless @fs.nil?
113     @repos.close unless @repos.nil?
114     Svn::Repos.delete(path) if File.exists?(path)
115     @repos = nil
116     @fs = nil
117   end
119   def setup_wc
120     teardown_wc
121     make_context("").checkout(@repos_uri, @wc_path)
122   end
124   def teardown_wc
125     FileUtils.rm_rf(@wc_base_dir)
126   end
128   def setup_config
129     teardown_config
130     Svn::Core::Config.ensure(@config_path)
131   end
133   def teardown_config
134     FileUtils.rm_rf(@config_path)
135   end
137   def add_authentication
138     passwd_file = "passwd"
139     File.open(@repos.svnserve_conf, "w") do |conf|
140       conf.print <<-CONF
141 [general]
142 anon-access = none
143 auth-access = write
144 password-db = #{passwd_file}
145 realm = #{@realm}
146       CONF
147     end
148     File.open(File.join(@repos.conf_dir, passwd_file), "w") do |f|
149       f.print <<-PASSWD
150 [users]
151 #{@author} = #{@password}
152       PASSWD
153     end
154   end
156   def add_hooks
157     add_pre_revprop_change_hook
158   end
160   def youngest_rev
161     @fs.youngest_rev
162   end
164   def root(rev=nil)
165     @fs.root(rev)
166   end
168   def prop(name, rev=nil)
169     @fs.prop(name, rev)
170   end
172   def make_context(log)
173     ctx = Svn::Client::Context.new
174     ctx.set_log_msg_func do |items|
175       [true, log]
176     end
177     ctx.add_username_prompt_provider(0) do |cred, realm, username, may_save|
178       cred.username = @author
179       cred.may_save = false
180     end
181     setup_auth_baton(ctx.auth_baton)
182     ctx
183   end
185   def setup_auth_baton(auth_baton)
186     auth_baton[Svn::Core::AUTH_PARAM_CONFIG_DIR] = @config_path
187     auth_baton[Svn::Core::AUTH_PARAM_DEFAULT_USERNAME] = @author
188   end
190   def normalize_line_break(str)
191     if windows?
192       str.gsub(/\n/, "\r\n")
193     else
194       str
195     end
196   end
198   def setup_greek_tree
199     @greek.setup(make_context("setup greek tree"))
200   end
202   module_function
203   def windows?
204     /cygwin|mingw|mswin32|bccwin32/.match(RUBY_PLATFORM)
205   end
207   module Svnserve
208     def setup_svnserve
209       @svnserve_port = nil
210       @repos_svnserve_uri = nil
212       # Look through the list of potential ports until we're able to
213       # successfully start svnserve on a free one.
214       @svnserve_ports.each do |port|
215         @svnserve_pid = fork {
216           STDERR.close
217           exec("svnserve",
218                "--listen-host", @svnserve_host,
219                "--listen-port", port,
220                "-d", "--foreground")
221         }
222         pid, status = Process.waitpid2(@svnserve_pid, Process::WNOHANG)
223         if status and status.exited?
224           if $DEBUG
225             STDERR.puts "port #{port} couldn't be used for svnserve"
226           end
227         else
228           # svnserve started successfully.  Note port number and cease
229           # startup attempts.
230           @svnserve_port = port
231           @repos_svnserve_uri =
232             "svn://#{@svnserve_host}:#{@svnserve_port}#{@full_repos_path}"
233           break
234         end
235       end
236       if @svnserve_port.nil?
237         msg = "Can't run svnserve because available port "
238         msg << "isn't exist in [#{@svnserve_ports.join(', ')}]"
239         raise msg
240       end
241     end
243     def teardown_svnserve
244       if @svnserve_pid
245         Process.kill(:TERM, @svnserve_pid)
246         begin
247           Process.waitpid(@svnserve_pid)
248         rescue Errno::ECHILD
249         end
250       end
251     end
253     def add_pre_revprop_change_hook
254       File.open(@repos.pre_revprop_change_hook, "w") do |hook|
255         hook.print <<-HOOK
256 #!/bin/sh
257 REPOS="$1"
258 REV="$2"
259 USER="$3"
260 PROPNAME="$4"
262 if [ "$PROPNAME" = "#{Svn::Core::PROP_REVISION_LOG}" -a \
263      "$USER" = "#{@author}" ]; then
264   exit 0
267 exit 1
268         HOOK
269       end
270       FileUtils.chmod(0755, @repos.pre_revprop_change_hook)
271     end
272   end
274   module SetupEnvironment
275     def setup_test_environment(top_dir, base_dir, ext_dir)
276       svnserve_dir = File.join(top_dir, 'subversion', 'svnserve')
277       ENV["PATH"] = "#{svnserve_dir}:#{ENV['PATH']}"
278       FileUtils.ln_sf(File.join(base_dir, ".libs"), ext_dir)
279     end
280   end
282   if windows?
283     require 'windows_util'
284     include Windows::Svnserve
285     extend Windows::SetupEnvironment
286   else
287     include Svnserve
288     extend SetupEnvironment
289   end