source: valtobtest/subversion-1.6.2/subversion/bindings/swig/ruby/test/util.rb @ 3

Last change on this file since 3 was 3, checked in by valtob, 15 years ago

subversion source 1.6.2 as test

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