1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# $Id$
# NOTE:
# Never use optparse in this file.
# Never use test/unit in this file.
# Never use Ruby extensions in this file.
begin
require 'fileutils'
require 'tmpdir'
rescue LoadError
$:.unshift File.join(File.dirname(__FILE__), '../lib')
retry
end
if !Dir.respond_to?(:mktmpdir)
# copied from lib/tmpdir.rb
def Dir.mktmpdir(prefix="d", tmpdir=nil)
tmpdir ||= Dir.tmpdir
t = Time.now.strftime("%Y%m%d")
n = nil
begin
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
path << "-#{n}" if n
Dir.mkdir(path, 0700)
rescue Errno::EEXIST
n ||= 0
n += 1
retry
end
if block_given?
begin
yield path
ensure
FileUtils.remove_entry_secure path
end
else
path
end
end
end
def main
@ruby = File.expand_path('miniruby')
@verbose = false
dir = nil
quiet = false
tests = nil
ARGV.delete_if {|arg|
case arg
when /\A--ruby=(.*)/
@ruby = File.expand_path($1)
true
when /\A--sets=(.*)/
tests = Dir.glob("#{File.dirname($0)}/test_{#{$1}}*.rb")
puts tests.map {|path| File.basename(path) }.inspect
true
when /\A--dir=(.*)/
dir = $1
true
when /\A(-q|--q(uiet))\z/
quiet = true
true
when /\A(-v|--v(erbose))\z/
@verbose = true
when /\A(-h|--h(elp)?)\z/
puts(<<-End)
Usage: #{File.basename($0, '.*')} --ruby=PATH [--sets=NAME,NAME,...]
--sets=NAME,NAME,... Name of test sets.
--dir=DIRECTORY Working directory.
default: /tmp/bootstraptest.tmpwd
-v, --verbose Output test name before exec.
-q, --quiet Don\'t print header message.
-h, --help Print this message and quit.
End
exit 0
else
false
end
}
if tests and not ARGV.empty?
$stderr.puts "--tests and arguments are exclusive"
exit 1
end
tests ||= ARGV
tests = Dir.glob("#{File.dirname($0)}/test_*.rb") if tests.empty?
pathes = tests.map {|path| File.expand_path(path) }
unless quiet
puts Time.now
patchlevel = defined?(RUBY_PATCHLEVEL) ? " patchlevel #{RUBY_PATCHLEVEL}" : ''
puts "Driver is ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}#{patchlevel}) [#{RUBY_PLATFORM}]"
puts "Target is #{`#{@ruby} -v`}"
puts
$stdout.flush
end
in_temporary_working_directory(dir) {
exec_test pathes
}
end
def exec_test(pathes)
@count = 0
@error = 0
@errbuf = []
@location = nil
pathes.each do |path|
$stderr.print "\n#{File.basename(path)} "
load File.expand_path(path)
end
$stderr.puts
if @error == 0
$stderr.puts "PASS #{@count} tests"
exit 0
else
@errbuf.each do |msg|
$stderr.puts msg
end
$stderr.puts "FAIL #{@error}/#{@count} tests failed"
exit 1
end
end
def assert_check(testsrc, message = '')
$stderr.puts "\##{@count} #{@location}" if @verbose
result = get_result_string(testsrc)
check_coredump
faildesc = yield(result)
if !faildesc
$stderr.print '.'
else
$stderr.print 'F'
error faildesc, message
end
rescue Exception => err
$stderr.print 'E'
error err.message, message
end
def assert_equal(expected, testsrc, message = '')
newtest
assert_check(testsrc, message) {|result|
if expected == result
nil
else
desc = "#{result.inspect} (expected #{expected.inspect})"
pretty(testsrc, desc, result)
end
}
end
def assert_match(expected_pattern, testsrc, message = '')
newtest
assert_check(testsrc, message) {|result|
if expected_pattern =~ result
nil
else
desc = "#{expected_pattern.inspect} expected to be =~\n#{result.inspect}"
pretty(testsrc, desc, result)
end
}
end
def pretty(src, desc, result)
(/\n/ =~ src ? "\n#{adjust_indent(src)}" : src) + " #=> #{desc}"
end
INDENT = 27
def adjust_indent(src)
untabify(src).gsub(/^ {#{INDENT}}/o, '').gsub(/^/, ' ')
end
def untabify(str)
str.gsub(/^\t+/) {|tabs| ' ' * (8 * tabs.size) }
end
def get_result_string(src)
if @ruby
File.open('bootstraptest.tmp.rb', 'w') {|f|
f.puts "print(begin; #{src}; end)"
}
begin
`#{@ruby} -W0 bootstraptest.tmp.rb`
ensure
raise CoreDumpError, "core dumped" if $? and $?.coredump?
end
else
eval(src).to_s
end
end
def newtest
@location = File.basename(caller(2).first)
@count += 1
cleanup_coredump
end
def error(msg, additional_message)
@errbuf.push "\##{@count} #{@location}: #{msg} #{additional_message}"
@error += 1
end
def in_temporary_working_directory(dir)
if dir
Dir.mkdir dir
Dir.chdir(dir) {
yield
}
else
Dir.mktmpdir("bootstraptest.tmpwd") {|d|
Dir.chdir(d) {
yield
}
}
end
end
def cleanup_coredump
FileUtils.rm_f 'core'
FileUtils.rm_f Dir.glob('core.*')
FileUtils.rm_f @ruby+'.stackdump' if @ruby
end
class CoreDumpError < StandardError; end
def check_coredump
if File.file?('core') or not Dir.glob('core.*').empty? or
(@ruby and File.exist?(@ruby+'.stackdump'))
raise CoreDumpError, "core dumped"
end
end
main
|