-
Notifications
You must be signed in to change notification settings - Fork 120
/
check.rb
executable file
·1603 lines (1422 loc) · 41.8 KB
/
check.rb
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# frozen_string_literal: true
# See bbatsov/rubocop#3326
# rubocop:disable all
exec ruby "-S" "-x" "$0" "$@"
#! ruby
# rubocop:enable all
# Check the Ruby version.
if RUBY_VERSION < "1.8.0"
warn("ruby 1.8 required for the test suite")
exit(1)
end
require "fileutils"
require "open3"
require "ostruct"
require "optparse"
require "set"
require "tmpdir"
# Show an error message and exit.
def fatal(message, file = nil, lineno = nil)
if !file.nil? && !lineno.nil?
$stderr.puts("#{file}:#{lineno}: error: #{message}")
elsif !file.nil?
$stderr.puts("#{file}: error: #{message}")
else
$stderr.puts("error: #{message}")
end
exit(1)
end
# Show a warning message.
def warn(message, file = nil, lineno = nil)
if !file.nil? && !lineno.nil?
$stderr.puts("#{file}:#{lineno}: warning: #{message}")
elsif !file.nil?
$stderr.puts("#{file}: warning: #{message}")
else
$stderr.puts("warning: #{message}")
end
end
# Get a string from the given environment variable.
def read_env_str(key, default_value)
value = default_value
if ENV.key?(key)
value = ENV[key]
end
value
end
# Get a positive integer from the given environment variable.
def read_env_positive_int(key, default_value)
value = default_value
if ENV.key?(key)
begin
value = Integer(ENV[key])
rescue ArgumentError, TypeError
warn("environment variable ignored: #{key} is not integer: #{ENV[key]}")
end
end
if value <= 0
warn("environment variable ignored: #{key} must be positive: #{ENV[key]}")
value = default_value
end
value
end
# The default prefix for the root temporary directory. See TempDir.root.
TMPDIR_PREFIX = "form_check_"
# The default extra options for mpirun.
DEFAULT_MPIRUN_OPTS = read_env_str("FORM_CHECK_MPIRUN_OPTS", nil)
# The default extra options for valgrind.
DEFAULT_VALGRIND_OPTS = read_env_str("FORM_CHECK_VALGRIND_OPTS", nil)
# The default maximal running time in seconds of FORM jobs before they get terminated.
DEFAULT_TIMEOUT = read_env_positive_int("FORM_CHECK_DEFAULT_TIMEOUT", 10)
# The factor multiplied to DEFAULT_TIMEOUT when Valgrind is used.
VALGRIND_TIME_DILATION = read_env_positive_int("FORM_CHECK_VALGRIND_TIME_DILATION", 30)
# The default directory for searching test cases.
TESTDIR = File.dirname(__FILE__)
# Routines for temporary directories.
class TempDir
@root = nil
# Return the root temporary directory name.
def self.root
if @root.nil?
@root = Dir.mktmpdir(TMPDIR_PREFIX)
end
@root
end
# Create a temporary directory under the root temporary directory, and return
# the directory name.
def self.mktmpdir(prefix)
Dir.mktmpdir(prefix, root)
end
# Clean up the all temporary directory.
def self.cleanup
return if @root.nil?
# The first try.
FileUtils.rm_rf(@root)
# Wait up to 5 seconds.
50.times do
# If the directory still remains, try to remove it after 0.1 seconds.
if !FileTest.directory?(@root)
return
end
sleep(0.1)
FileUtils.rm_rf(@root)
end
# Failed.
if FileTest.directory?(@root)
warn("failed to delete the temporary directory '#{@root}'")
end
@root = nil
end
# We need to register the cleanup function before loading test/unit.
at_exit { TempDir.cleanup }
end
# Register a finalization function before loading test/unit.
at_exit { defined?(finalize) && finalize }
# We use test/unit, which is now not in the standard library.
begin
require "test/unit"
rescue LoadError
warn("test/unit required for the test suite")
exit(1)
end
# Find the path to a program.
def which(name)
result = nil
if name != File.basename(name)
# Convert the relative path to the absolute path.
result = File.expand_path(name)
else
# Search from $PATH.
ENV["PATH"].split(":").each do |path|
candidate = File.join(path, name)
if File.executable?(candidate)
result = File.expand_path(candidate)
break
end
end
end
result = name if result.nil? # Fallback.
result
end
# To be mixed-in all FORM tests.
module FormTest
# Interplay with globals.
@cfg = nil
@tests = nil
def self.cfg=(val)
@cfg = val
end
def self.cfg
@cfg
end
def self.tests=(val)
@tests = val
end
def self.tests
@tests
end
def info
FormTest.tests.classes_info[self.class.name]
end
# Accessors to the configuration.
def timeout
FormTest.cfg.timeout
end
def ncpu
FormTest.cfg.ncpu
end
def serial?
FormTest.cfg.serial?
end
def threaded?
FormTest.cfg.threaded?
end
def mpi?
FormTest.cfg.mpi?
end
def valgrind?
!FormTest.cfg.valgrind.nil?
end
def wordsize
FormTest.cfg.wordsize
end
def cygwin?
RUBY_PLATFORM =~ /cygwin/i
end
def mac?
RUBY_PLATFORM =~ /darwin/i
end
def linux?
RUBY_PLATFORM =~ /linux/i
end
def unix?
cygwin? || mac? || linux?
end
def travis?
ENV["TRAVIS"] == "true"
end
def github?
ENV["GITHUB_ACTIONS"] == "true"
end
# Override methods in Test::Unit::TestCase.
def setup
super
@tmpdir = nil
@filename = nil
end
def teardown
cleanup_files
super
end
# Set up the working directory and put FORM files.
def setup_files
cleanup_files
@tmpdir = TempDir.mktmpdir("#{self.class.name}_")
nfiles.times do |i|
File.open(File.join(@tmpdir, "#{i + 1}.frm"), "w") do |file|
file.write(info.sources[i])
end
end
end
# Delete the working directory.
def cleanup_files
if !@tmpdir.nil?
FileUtils.rm_rf(@tmpdir)
end
@tmpdir = nil
end
# Called from derived classes' test_* methods.
def do_test(&block)
if !requires
info.status = "SKIPPED"
if defined?(omit)
omit(requires_str, &block)
elsif defined?(skip)
skip(requires_str)
end
return
end
if !FormTest.cfg.full && pendings
info.status = "SKIPPED"
if defined?(pend)
pend(pendings_str) do
assert(false)
yield
end
elsif defined?(skip)
skip(requires_str)
end
return
end
FormTest.cfg.retries.times do |t|
setup_files
prepare
@stdout = ""
@stderr = ""
begin
nfiles.times do |i|
ENV["FORM"] = FormTest.cfg.form_cmd
@filename = "#{i + 1}.frm"
execute("#{FormTest.cfg.form_cmd} #{@filename}")
if !finished?
info.status = "TIMEOUT"
assert(false, "timeout (= #{timeout} sec) in #{@filename} of #{info.desc}")
end
if return_value != 0
break
end
end
yield
# NOTE: Here we catch all exceptions, though it is a very bad style. This
# is because, in Ruby 1.9, test/unit is implemented based on
# minitest and MiniTest::Assertion is not a subclass of
# StandardError.
rescue Exception => e # rubocop:disable Lint/RescueException
if info.status == "TIMEOUT" && t < FormTest.cfg.retries - 1
warn("timeout (= #{timeout} sec) in #{@filename} of #{info.desc}, retry")
info.status = nil
info.times = nil
next
end
$stderr.puts
$stderr.puts("=" * 79)
$stderr.puts("#{info.desc} FAILED")
$stderr.puts("=" * 79)
$stderr.puts(@stdout)
$stderr.puts("=" * 79)
$stderr.puts
if info.status.nil?
if (defined?(MiniTest::Assertion) && e.is_a?(MiniTest::Assertion)) ||
(defined?(Test::Unit::AssertionFailedError) && e.is_a?(Test::Unit::AssertionFailedError))
info.status = "FAILED"
else
info.status = "ERROR"
end
end
raise e
else
if FormTest.cfg.verbose
$stderr.puts
$stderr.puts("=" * 79)
$stderr.puts("#{info.desc} SUCCEEDED")
$stderr.puts("=" * 79)
$stderr.puts(@stdout)
$stderr.puts("=" * 79)
$stderr.puts
end
info.status = "OK"
end
break
end
end
# Execute a FORM job.
def execute(cmdline)
if FormTest.cfg.verbose
$stderr.puts("Command: #{cmdline}")
end
@finished = false
@exit_status = nil
t0 = Time.now
begin
execute_popen3(cmdline, timeout)
ensure
t1 = Time.now
dt = t1 - t0
if info.times.nil?
info.times = []
end
info.times.push(dt)
end
end
# An implementation by popen3. Should work with Ruby 1.8 on Unix.
#
# tested on:
# ruby 1.8.5 (2006-08-25) [x86_64-linux]
# ruby 1.8.7 (2013-12-22 patchlevel 375) [x86_64-linux]
# ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
# ruby 1.9.3p545 (2014-02-24) [i386-cygwin]
# ruby 1.9.3p545 (2014-02-24) [x86_64-cygwin]
# ruby 2.0.0p247 (2013-06-27) [x86_64-linux]
# ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
# ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]
#
# segfault at IO#gets
# ruby 1.8.6 (2010-09-02 patchlevel 420) [x86_64-linux]
# ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]
#
def execute_popen3(cmdline, timeout)
cmdline = "echo pid=$$;cd #{@tmpdir};#{cmdline};echo exit_status=$?"
stdout = []
stderr = []
Open3.popen3(cmdline) do |stdinstream, stdoutstream, stderrstream|
stdinstream.close
out = Thread.new do
while (line = stdoutstream.gets)
stdout << line
end
end
err = Thread.new do
while (line = stderrstream.gets)
stderr << line
# We print both stdout and stderr when a test fails. An easy way to
# implement this is to copy messages in stderr to those in stdout.
# Unfortunately their orders are not preserved.
stdout << line
end
end
begin
runner = Thread.current
killer = Thread.new(timeout) do |timeout1|
sleep(timeout1)
runner.raise
end
out.join
err.join
killer.kill
rescue StandardError
while out.alive? && stdout.empty?
sleep(0.01)
end
if !stdout.empty? && stdout[0] =~ /pid=([0-9]+)/
pid = $1.to_i
Process.kill("KILL", pid)
else
warn("failed to kill FORM job at timeout (unknown pid)")
end
else
@finished = true
ensure
out.kill # avoid SEGFAULT at IO#close in some old versions
err.kill
end
end
if !stdout.empty? && stdout[0] =~ /pid=([0-9]+)/
stdout.shift
end
if !FormTest.cfg.valgrind.nil? && (@finished && !stdout.empty? && !stdout[-1].start_with?("exit_status="))
# The exit status may be in the middle of the output (sometimes annoyingly
# happened on Travis CI).
i = stdout.map { |x| x.start_with?("exit_status") }.rindex(true)
if !i.nil?
s = stdout[i]
stdout.delete_at(i)
stdout << s
end
end
if @finished && !stdout.empty? && stdout[-1] =~ /exit_status=([0-9]+)/
@exit_status = $1.to_i
stdout.pop
end
# We exclude the Valgrind warnings "Warning: set address range perms: ..."
# from the standard output, which can happen when the program allocates
# big memory chunks.
stdout = stdout.reject { |l| l =~ /Warning: set address range perms/ }
@stdout += stdout.join
@stderr += stderr.join
end
# Default assertions.
def default_check
if return_value != 0
assert(false, "nonzero return value (= #{return_value}) from #{@filename} of #{info.desc}")
elsif warning?
assert(false, "warning in #{@filename} of #{info.desc}")
else
assert(true)
end
end
# Methods to be overridden in derived classes.
# The number of FORM files attached to the test.
def nfiles
1
end
# The required condition. The test will be skipped if the condition does not
# hold.
def requires
true
end
# The string representation for the required condition.
def requires_str
"true"
end
# The pending condition. The test will be skipped if the condition holds.
def pendings
false
end
# The string representation for the pending condition.
def pendings_str
"false"
end
# The method to be called before the test.
def prepare
# Can be overridden in child classes.
end
# Test-result functions.
# The exit status as a number
def return_value
@exit_status
end
# The verbatim result keeping line breaks and whitespaces.
# Must be in the default output format.
def exact_result(exprname, index = -1)
matches = @stdout.scan(/^[ \t]+#{Regexp.escape(exprname)}\s*=(.+?);/m)
return matches[index].first if !matches.empty? && !matches[index].nil?
""
end
# The result on one line with multiple whitespaces reduced to one.
# Must be in the default output format.
def result(exprname, index = -1)
r = exact_result(exprname, index)
return r.gsub(/\s+/, "") if !r.nil?
""
end
# The number of terms in the given expression.
# Must be in the default statistics format.
def nterms(exprname, index = -1)
matches = @stdout.scan(/^[ \t]+#{exprname}\s*Terms in output\s*=\s*(\d+)\s*Bytes used\s*=\s*\d+/m)
return matches[index].first.to_i if !matches.empty? && !matches[index].nil?
-1
end
# The size in byte.
# Must be in the default statistics format.
def bytesize(exprname, index = -1)
matches = @stdout.scan(/^[ \t]+#{exprname}\s*Terms in output\s*=\s*\d+\s*Bytes used\s*=\s*(\d+)/m)
return matches[index].first.to_i if !matches.empty? && !matches[index].nil?
-1
end
# The file contents as a string (in the working directory).
def file(filename)
begin
File.open(File.join(@tmpdir, filename), "r") do |f|
return f.read
end
rescue StandardError
$stderr.puts("warning: failed to read '#{filename}'")
end
""
end
# Same as file(filename).
def read(filename)
file filename
end
# Write to a file (in the working directory).
def write(filename, text)
fname = File.join(@tmpdir, filename)
FileUtils.mkdir_p(File.dirname(fname))
File.open(fname, "w") do |f|
f.write(text)
end
end
# The working directory for the test.
def workdir
@tmpdir
end
# The standard output of the FORM job as a string.
def stdout
@stdout
end
# The standard error of the FORM job as a string.
def stderr
@stderr
end
# Test-result functions to be used in assertions.
# true if the FORM job finished in timeout.
def finished?
@finished
end
# true if the FORM job put warning messages.
def warning?
@stdout =~ /(^|\R)\S+ Line \d+ --> Warning/
end
# true if the FORM job put preprocessor errors.
def preprocess_error?
@stdout =~ /(^|\R)\S+ Line \d+ ==>/
end
# true if the FORM job put compile-time errors.
def compile_error?
@stdout =~ /(^|\R)\S+ Line \d+ -->/
end
# true if the FORM job put run-time errors.
# NOTE: indeed this implementation detects abnormal terminations
# via "Terminate()", which also happens for preprocessor/compiler errors.
def runtime_error?
if serial?
@stdout =~ /Program terminating at \S+ Line \d+ -->/
elsif threaded?
@stdout =~ /Program terminating in thread \d+ at \S+ Line \d+ -->/
elsif mpi?
@stdout =~ /Program terminating in process \d+ at \S+ Line \d+ -->/
end
end
# true if the FORM job completed without any warnings/errors and
# the exit code was 0.
def succeeded?
if finished? && !warning? && !preprocess_error? && !compile_error? && !runtime_error? && return_value == 0
if FormTest.cfg.valgrind.nil?
if @stderr.empty?
return true
end
@stdout += "!!! stderr is not empty"
return false
end
# Check for Valgrind errors.
ok = !@stderr.include?("Invalid read") &&
!@stderr.include?("Invalid write") &&
!@stderr.include?("Invalid free") &&
!@stderr.include?("Mismatched free") &&
!@stderr.include?("Use of uninitialised value") &&
!@stderr.include?("Conditional jump or move depends on uninitialised value") &&
!@stderr.include?("points to unaddressable byte") &&
!@stderr.include?("points to uninitialised byte") &&
!@stderr.include?("contains uninitialised byte") &&
!@stderr.include?("Source and destination overlap in memcpy") &&
!@stderr.include?("has a fishy") &&
@stderr !~ /definitely lost: [1-9]/ &&
@stderr !~ /indirectly lost: [1-9]/ &&
@stderr !~ /possibly lost: [1-9]/
if !ok
@stdout += "!!! Valgrind test failed"
end
return ok
end
false
end
# Utility functions for pattern matching.
# A pattern from the given string with escaping any special characters.
def exact_pattern(str)
san_str = Regexp.quote(str)
Regexp.new(san_str)
end
# The same as #exact_pattern but ignores whitespaces.
def pattern(str)
san_str = Regexp.quote(str.gsub(/\s+/, ""))
Regexp.new(san_str)
end
# Same as #pattern but matches only with the whole expression.
# Assumes the default output format.
def expr(str)
san_str = Regexp.quote(str.gsub(/\s+/, ""))
Regexp.new("^#{san_str}$")
end
end
# Information of a test case.
class TestInfo
def initialize
@classname = nil
@where = nil # where the test is defined
@foldname = nil # fold name of the test
@enabled = nil # enabled or not
@sources = [] # FORM sources
@time_dilation = nil
@status = nil # status
@times = nil # elapsed time (array)
end
attr_accessor :classname, :where, :foldname, :enabled, :sources, :time_dilation,
:status, :times
# Return the description of the test.
def desc
"#{@foldname} (#{@where})"
end
end
# List of test cases.
class TestCases
def initialize
@files = [] # Ruby files
@classes = [] # test class names (unsorted)
@classes_info = {} # TestInfo objects, key: Ruby class name
@name_patterns = []
@exclude_patterns = []
end
attr_reader :classes_info
attr_accessor :name_patterns, :exclude_patterns
# Return a list containing info objects for enabled tests.
def classes_info_list
infos = []
@classes.each do |c|
info = @classes_info["Test_#{c}"]
if info.enabled
infos.push(info)
end
end
infos
end
# Convert a .frm file to a .rb file and load it.
def make_ruby_file(filename)
# Check existing files.
inname = File.basename(filename)
outname = "#{File.basename(filename, '.frm')}.rb"
if @files.include?(outname)
fatal("duplicate output file name", inname)
end
@files.push(outname)
outname = File.join(TempDir.root, outname)
File.open(filename, "r") do |infile|
File.open(outname, "w") do |outfile|
lineno = 0
level = 0
classname = nil
info = nil
block = nil
blockno = 0
fileno = 0
skipping = false
heredoc = nil
requires = nil
pendings = nil
prepares = nil
time_dilation = nil
infile.each_line do |line|
line.chop!
lineno += 1
if level == 0
case line
when /^\*..#\[\s*([^:]*)/
# fold open: start a class
fold = $1.strip
if fold.empty?
fatal("empty fold", inname, lineno)
end
classname = canonical_name(fold)
info = TestInfo.new
@classes.push(classname)
@classes_info["Test_#{classname}"] = info
info.classname = classname
info.where = "#{inname}:#{lineno}"
info.foldname = fold
info.enabled = test_enabled?(classname)
level += 1
block = ""
blockno = 0
fileno = 0
skipping = !info.enabled
heredoc = nil
requires = nil
pendings = nil
prepares = nil
time_dilation = nil
if skipping
line = ""
else
line = "class Test_#{classname} < Test::Unit::TestCase; include FormTest"
end
when /^\*..#\]/
# unexpected fold close
fatal("unexpected fold close", inname, lineno)
else
# as commentary
line = ""
end
elsif heredoc.nil? && line =~ /^\*..#\]\s*([^:]*)/ && level == 1
# fold close: end of the class
fold = $1.strip
foldname = info.foldname
if !fold.empty? && fold != foldname
warn("unmatched fold '#{fold}', which should be '#{foldname}'", inname, lineno)
end
line = ""
if !skipping
if fileno == 0
# no .end
blockno.times do
outfile.write("\n")
end
block += ".end\n"
fileno += 1
info.sources.push(block)
line += "def test_#{classname}; do_test { default_check } end; "
else
outfile.write("def test_#{classname}; do_test {\n" + block)
line = "} end; "
end
line += "def nfiles; #{fileno} end; " if fileno != 1
if !requires.nil?
requires = requires.map { |s| "(#{s})" }.join(" && ")
line += "def requires; #{requires} end; "
line += "def requires_str; %(#{requires}) end; "
end
if !pendings.nil?
pendings = pendings.map { |s| "(#{s})" }.join(" || ")
line += "def pendings; #{pendings} end; "
line += "def pendings_str; %(#{pendings}) end; "
end
if !prepares.nil?
prepares = prepares.join("; ")
line += "def prepare; #{prepares} end; "
end
if !time_dilation.nil?
line += "def timeout; super() * #{time_dilation} end;"
end
line += "end"
end
level = 0
classname = nil
info = nil
elsif heredoc.nil? && line =~ /^\s*\.end/
# .end
if skipping
line = ""
else
blockno += 1 if fileno > 0 # previous .end
blockno.times do
outfile.write("\n")
end
block += "#{line}\n"
fileno += 1
info.sources.push(block)
block = ""
blockno = 0
line = nil # later
end
elsif heredoc.nil? && line =~ /^\s*#\s*require\s+(.*)/
# #require <condition>
line = ""
if requires.nil?
requires = []
end
requires << $1
elsif heredoc.nil? && line =~ /^\s*#\s*pend_if\s+(.*)/
# #pend_if <condition>
line = ""
if pendings.nil?
pendings = []
end
pendings << $1
elsif heredoc.nil? && line =~ /^\s*#\s*prepare\s+(.*)/
# #prepare <statement>
line = ""
if prepares.nil?
prepares = []
end
prepares << $1
elsif heredoc.nil? && line =~ /^\s*#\s*time_dilation\s+(.*)/
# #time_dilation <dilation>
line = ""
if !time_dilation.nil?
fatal("attempted to set time_dilation twice", inname, lineno)
end
time_dilation = $1.to_f
if time_dilation <= 0
fatal("invalid time_dilation", inname, lineno)
end
info.time_dilation = time_dilation
elsif heredoc.nil? && line =~ /^\*\s*#\s*(require|prepare|pend_if|time_dilation)\s+(.*)/
# *#require/prepare/pend_if/time_dilation, commented out in the FORM way
line = ""
else
if heredoc.nil?
if line =~ /^\*..#\[/
# fold open
level += 1
elsif line =~ /^\*..#\]\s*([^:]*)/
# fold close
level -= 1
elsif line =~ /<</ && (line =~ /<<-?(\w+)/ ||
line =~ /<<-?"(\w+)"/ ||
line =~ /<<-?'(\w+)'/ ||
line =~ /<<-?`(\w+)`/)
# start here document
heredoc = Regexp.new($1)
# NOTE: Currently, we don't support more than one << operators
# in the same line.
end
elsif line =~ heredoc
# end here document
heredoc = nil
end
if skipping
line = ""
else
# some typical assertions
if line =~ /^\s*assert\s+(succeeded\?|finished\?)\s*$/
line = "assert(#{$1}, 'Failed for #{$1}')"
end
block += "#{line}\n"
blockno += 1
line = nil
end
end
if !line.nil?
outfile.write("#{line}\n")
end
end
if level >= 1
fatal("expected fold close", inname, lineno)
end
end
end
require outname
end
# true if the test is enabled
def test_enabled?(name)
# construct regular expressions (wildcards: '*' and '?')
@name_patterns.length.times do |i|
if !@name_patterns[i].is_a?(Regexp)
s = @name_patterns[i].to_s.gsub("\*", ".*").tr("\?", ".")
s = "^#{s}$"
@name_patterns[i] = Regexp.new(s)
end
end
@exclude_patterns.length.times do |i|
if !@exclude_patterns[i].is_a?(Regexp)
s = @exclude_patterns[i].to_s.gsub("\*", ".*").tr("\?", ".")
s = "^#{s}$"
@exclude_patterns[i] = Regexp.new(s)
end
end
# check --name NAME
ok = true
if !@name_patterns.empty?
ok = false
@name_patterns.each do |pat|
if name =~ pat
ok = true
break
end
end
end
if !ok
return false
end