Fix compiler warning due to missing function prototype.
[svn.git] / subversion / bindings / swig / ruby / test / windows_util.rb
blobcba3b2412702bf323f54e72bacebe057f39336bc
1 require 'etc'
2 require 'fileutils'
4 module SvnTestUtil
5   module Windows
6     module Svnserve
7       SERVICE_NAME = 'test-svn-server'
9       class << self
10         def escape_value(value)
11           escaped_value = value.gsub(/"/, '\\"') # "
12           "\"#{escaped_value}\""
13         end
14       end
16       def service_control(command, args={})
17         args = args.collect do |key, value|
18           "#{key}= #{Svnserve.escape_value(value)}"
19         end.join(" ")
20         result = `sc #{command} #{SERVICE_NAME} #{args}`
21         if result.match(/FAILED/)
22           raise "Failed to #{command} #{SERVICE_NAME}: #{args}"
23         end
24         /^\s*STATE\s*:\s\d+\s*(.*?)\s*$/ =~ result
25         $1
26       end
28       def grant_everyone_full_access(dir)
29         dir = dir.tr(File::SEPARATOR, File::ALT_SEPARATOR)
30         `cacls #{Svnserve.escape_value(dir)} /T /E /P Everyone:F`
31       end
33       def service_exists?
34         begin
35           service_control("query")
36           true
37         rescue
38           false
39         end
40       end
42       def service_stopped?
43         "STOPPED" == service_control("query") rescue true
44       end
46       def setup_svnserve
47         @svnserve_port = @svnserve_ports.first
48         @repos_svnserve_uri = "svn://#{@svnserve_host}:#{@svnserve_port}"
49         grant_everyone_full_access(@full_repos_path)
51         @@service_created ||= begin
52           @@service_created = true
53           service_control('stop') unless service_stopped?
54           service_control('delete') if service_exists?
56           svnserve_dir = File.expand_path(File.join(@base_dir, "svnserve"))
57           FileUtils.mkdir_p(svnserve_dir)
58           at_exit do
59             service_control('stop') unless service_stopped?
60             service_control('delete') if service_exists?
61             FileUtils.rm_rf(svnserve_dir)
62           end
63           trap("INT") do
64             service_control('stop') unless service_stopped?
65             service_control('delete') if service_exists?
66             FileUtils.rm_rf(svnserve_dir)
67           end
68           targets = %w(svnserve.exe libsvn_subr-1.dll libsvn_repos-1.dll
69                        libsvn_fs-1.dll libsvn_delta-1.dll
70                        libaprutil.dll libapr.dll)
71           ENV["PATH"].split(";").each do |path|
72             found_targets = []
73             targets.each do |target|
74               target_path = "#{path}\\#{target}"
75               if File.exists?(target_path)
76                 found_targets << target
77                 FileUtils.cp(target_path, svnserve_dir)
78               end
79             end
80             targets -= found_targets
81             break if targets.empty?
82           end
83           unless targets.empty?
84             raise "can't find libraries to work svnserve: #{targets.join(' ')}"
85           end
87           grant_everyone_full_access(svnserve_dir)
89           svnserve_path = File.join(svnserve_dir, "svnserve.exe")
90           svnserve_path = svnserve_path.tr(File::SEPARATOR,
91                                            File::ALT_SEPARATOR)
92           svnserve_path = Svnserve.escape_value(svnserve_path)
94           root = @full_repos_path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
96           args = ["--service", "--root", Svnserve.escape_value(root),
97                   "--listen-host", @svnserve_host,
98                   "--listen-port", @svnserve_port]
99           user = ENV["USERNAME"] || Etc.getlogin
100           service_control('create',
101                           [["binPath", "#{svnserve_path} #{args.join(' ')}"],
102                            ["DisplayName", SERVICE_NAME],
103                            ["type", "own"]])
104         end
105         service_control('start')
106         true
107       end
109       def teardown_svnserve
110         service_control('stop') unless service_stopped?
111       end
113       def add_pre_revprop_change_hook
114         File.open("#{@repos.pre_revprop_change_hook}.cmd", "w") do |hook|
115           hook.print <<-HOOK
116 set REPOS=%1
117 set REV=%2
118 set USER=%3
119 set PROPNAME=%4
120 if "%PROPNAME%" == "#{Svn::Core::PROP_REVISION_LOG}" if "%USER%" == "#{@author}" exit 0
121 exit 1
122           HOOK
123         end
124       end
125     end
127     module SetupEnvironment
128       def setup_test_environment(top_dir, base_dir, ext_dir)
129         build_type = "Release"
131         FileUtils.mkdir_p(ext_dir)
133         relative_base_dir =
134           base_dir.sub(/^#{Regexp.escape(top_dir + File::SEPARATOR)}/, '')
135         build_base_dir = File.join(top_dir, build_type, relative_base_dir)
137         dll_dir = File.expand_path(build_base_dir)
138         subversion_dir = File.join(build_base_dir, "..", "..", "..")
139         subversion_dir = File.expand_path(subversion_dir)
141         util_name = "util"
142         build_conf = File.join(top_dir, "build.conf")
143         File.open(File.join(ext_dir, "#{util_name}.rb" ), 'w') do |util|
144           setup_dll_wrapper_util(dll_dir, util)
145           add_depended_dll_path_to_dll_wrapper_util(top_dir, build_type, util)
146           add_svn_dll_path_to_dll_wrapper_util(build_conf, subversion_dir, util)
147           setup_dll_wrappers(build_conf, ext_dir, dll_dir, util_name) do |lib|
148             svn_lib_dir = File.join(subversion_dir, "libsvn_#{lib}")
149             util.puts("add_path.call(#{svn_lib_dir.dump})")
150           end
152           svnserve_dir = File.join(subversion_dir, "svnserve")
153           util.puts("add_path.call(#{svnserve_dir.dump})")
154         end
155       end
157       private
158       def setup_dll_wrapper_util(dll_dir, util)
159         libsvn_swig_ruby_dll_dir = File.join(dll_dir, "libsvn_swig_ruby")
161         util.puts(<<-EOC)
162 paths = ENV["PATH"].split(';')
163 add_path = Proc.new do |path|
164   win_path = path.tr(File::SEPARATOR, File::ALT_SEPARATOR)
165   unless paths.include?(win_path)
166     ENV["PATH"] = "\#{win_path};\#{ENV['PATH']}"
167   end
170 add_path.call(#{dll_dir.dump})
171 add_path.call(#{libsvn_swig_ruby_dll_dir.dump})
173       end
175       def add_depended_dll_path_to_dll_wrapper_util(top_dir, build_type, util)
176         lines = []
177         gen_make_opts = File.join(top_dir, "gen-make.opts")
178         lines = File.read(gen_make_opts).to_a if File.exists?(gen_make_opts)
179         config = {}
180         lines.each do |line|
181           name, value = line.chomp.split(/\s*=\s*/, 2)
182           config[name] = value if value
183         end
185         [
186          ["apr", build_type],
187          ["apr-util", build_type],
188          ["apr-iconv", build_type],
189          ["berkeley-db", "bin"],
190         ].each do |lib, sub_dir|
191           lib_dir = config["--with-#{lib}"] || lib
192           dirs = [top_dir, lib_dir, sub_dir].compact
193           dll_dir = File.expand_path(File.join(*dirs))
194           util.puts("add_path.call(#{dll_dir.dump})")
195         end
196       end
198       def add_svn_dll_path_to_dll_wrapper_util(build_conf, subversion_dir, util)
199         File.open(build_conf) do |f|
200           f.each do |line|
201             if /^\[(libsvn_.+)\]\s*$/ =~ line
202               lib_name = $1
203               lib_dir = File.join(subversion_dir, lib_name)
204               util.puts("add_path.call(#{lib_dir.dump})")
205             end
206           end
207         end
208       end
210       def setup_dll_wrappers(build_conf, ext_dir, dll_dir, util_name)
211         File.open(build_conf) do |f|
212           f.each do |line|
213             if /^\[swig_(.+)\]\s*$/ =~ line
214               lib_name = $1
215               File.open(File.join(ext_dir, "#{lib_name}.rb" ), 'w') do |rb|
216                 rb.puts(<<-EOC)
217 require File.join(File.dirname(__FILE__), #{util_name.dump})
218 require File.join(#{dll_dir.dump}, File.basename(__FILE__, '.rb')) + '.so'
220               end
222               yield(lib_name)
223             end
224           end
225         end
226       end
227     end
228   end