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