[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / tool / test-bundled-gems.rb
blobeeec1d7aedbf55f1df0ebd5c355e5e9523a74159
1 require 'rbconfig'
2 require 'timeout'
3 require 'fileutils'
4 require_relative 'lib/colorize'
6 ENV.delete("GNUMAKEFLAGS")
8 github_actions = ENV["GITHUB_ACTIONS"] == "true"
10 allowed_failures = ENV['TEST_BUNDLED_GEMS_ALLOW_FAILURES'] || ''
11 allowed_failures = allowed_failures.split(',').reject(&:empty?)
13 ENV["GEM_PATH"] = [File.realpath('.bundle'), File.realpath('../.bundle', __dir__)].join(File::PATH_SEPARATOR)
15 colorize = Colorize.new
16 rake = File.realpath("../../.bundle/bin/rake", __FILE__)
17 gem_dir = File.realpath('../../gems', __FILE__)
18 rubylib = [gem_dir+'/lib', ENV["RUBYLIB"]].compact.join(File::PATH_SEPARATOR)
19 exit_code = 0
20 ruby = ENV['RUBY'] || RbConfig.ruby
21 failed = []
22 File.foreach("#{gem_dir}/bundled_gems") do |line|
23   next if /^\s*(?:#|$)/ =~ line
24   gem = line.split.first
25   next if ARGV.any? {|pat| !File.fnmatch?(pat, gem)}
26   # 93(bright yellow) is copied from .github/workflows/mingw.yml
27   puts "#{github_actions ? "::group::\e\[93m" : "\n"}Testing the #{gem} gem#{github_actions ? "\e\[m" : ""}"
29   test_command = "#{ruby} -C #{gem_dir}/src/#{gem} #{rake} test"
30   first_timeout = 600 # 10min
32   toplib = gem
33   case gem
34   when "resolv-replace"
35     # Skip test suite
36     next
37   when "typeprof"
39   when "rbs"
40     # TODO: We should skip test file instead of test class/methods
41     skip_test_files = %w[
42     ]
44     skip_test_files.each do |file|
45       path = "#{gem_dir}/src/#{gem}/#{file}"
46       File.unlink(path) if File.exist?(path)
47     end
49     test_command << " stdlib_test validate RBS_SKIP_TESTS=#{__dir__}/rbs_skip_tests SKIP_RBS_VALIDATION=true"
50     first_timeout *= 3
52   when "debug"
53     # Since debug gem requires debug.so in child processes without
54     # activating the gem, we preset necessary paths in RUBYLIB
55     # environment variable.
56     load_path = true
58   when "test-unit"
59     test_command = "#{ruby} -C #{gem_dir}/src/#{gem} test/run-test.rb"
61   when /\Anet-/
62     toplib = gem.tr("-", "/")
64   end
66   if load_path
67     libs = IO.popen([ruby, "-e", "old = $:.dup; require '#{toplib}'; puts $:-old"], &:read)
68     next unless $?.success?
69     puts libs
70     ENV["RUBYLIB"] = [libs.split("\n"), rubylib].join(File::PATH_SEPARATOR)
71   else
72     ENV["RUBYLIB"] = rubylib
73   end
75   print "[command]" if github_actions
76   puts test_command
77   pid = Process.spawn(test_command, "#{/mingw|mswin/ =~ RUBY_PLATFORM ? 'new_' : ''}pgroup": true)
78   {nil => first_timeout, INT: 30, TERM: 10, KILL: nil}.each do |sig, sec|
79     if sig
80       puts "Sending #{sig} signal"
81       Process.kill("-#{sig}", pid)
82     end
83     begin
84       break Timeout.timeout(sec) {Process.wait(pid)}
85     rescue Timeout::Error
86     end
87   rescue Interrupt
88     exit_code = Signal.list["INT"]
89     Process.kill("-KILL", pid)
90     Process.wait(pid)
91     break
92   end
94   print "::endgroup::\n" if github_actions
95   unless $?.success?
97     mesg = "Tests failed " +
98            ($?.signaled? ? "by SIG#{Signal.signame($?.termsig)}" :
99               "with exit code #{$?.exitstatus}")
100     puts colorize.decorate(mesg, "fail")
101     if allowed_failures.include?(gem)
102       mesg = "Ignoring test failures for #{gem} due to \$TEST_BUNDLED_GEMS_ALLOW_FAILURES"
103       puts colorize.decorate(mesg, "skip")
104     else
105       failed << gem
106       exit_code = $?.exitstatus if $?.exitstatus
107     end
108   end
111 puts "Failed gems: #{failed.join(', ')}" unless failed.empty?
112 exit exit_code