[ruby/reline] test_dumb_terminal: "ruby" command is not always available

Fixes the same issue at https://github.com/ruby/ruby/pull/5417

`ruby` is not always available in certain build environments and
configure options (e.g. --program-suffix)

This patch tries to choose an appropriate command line for spawning a
fresh Ruby process, based on EnvUtil implementation in ruby/ruby's test
suite.

Plus when this library is directly mirrored into ruby/ruby, prefer EnvUtil
available there over the implementation in this library's test suite.

https://github.com/ruby/reline/commit/278327d2e9
This commit is contained in:
Sorah Fukumori 2023-02-06 04:38:33 +09:00 коммит произвёл git
Родитель 8a474b344e
Коммит 8a29419b7f
2 изменённых файлов: 42 добавлений и 1 удалений

Просмотреть файл

@ -5,6 +5,19 @@ ENV['TERM'] = 'xterm' # for some CI environments
require 'reline' require 'reline'
require 'test/unit' require 'test/unit'
begin
require 'rbconfig'
rescue LoadError
end
begin
# This should exist and available in load path when this file is mirrored to ruby/ruby and running at there
if File.exist?(File.expand_path('../../tool/lib/envutil.rb', __dir__))
require 'envutil'
end
rescue LoadError
end
module Reline module Reline
class <<self class <<self
def test_mode(ansi: false) def test_mode(ansi: false)
@ -25,6 +38,34 @@ module Reline
const_set('IOGate', Reline::GeneralIO) const_set('IOGate', Reline::GeneralIO)
Reline.instance_variable_set(:@core, nil) Reline.instance_variable_set(:@core, nil)
end end
# Return a executable name to spawn Ruby process. In certain build configuration,
# "ruby" may not be available.
def test_rubybin
# When this test suite is running in ruby/ruby, prefer EnvUtil result over original implementation
if const_defined?(:EnvUtil)
return EnvUtil.rubybin
end
# The following is a simplified port of EnvUtil.rubybin in ruby/ruby
if ruby = ENV["RUBY"]
return ruby
end
ruby = "ruby"
exeext = RbConfig::CONFIG["EXEEXT"]
rubyexe = (ruby + exeext if exeext and !exeext.empty?)
if File.exist? ruby and File.executable? ruby and !File.directory? ruby
return File.expand_path(ruby)
end
if rubyexe and File.exist? rubyexe and File.executable? rubyexe
return File.expand_path(rubyexe)
end
if defined?(RbConfig.ruby)
RbConfig.ruby
else
"ruby"
end
end
end end
end end

Просмотреть файл

@ -370,7 +370,7 @@ class Reline::Test < Reline::TestCase
def test_dumb_terminal def test_dumb_terminal
lib = File.expand_path("../../lib", __dir__) lib = File.expand_path("../../lib", __dir__)
out = IO.popen([{"TERM"=>"dumb"}, "ruby", "-I#{lib}", "-rreline", "-e", "p Reline::IOGate"], &:read) out = IO.popen([{"TERM"=>"dumb"}, Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", "p Reline::IOGate"], &:read)
assert_equal("Reline::GeneralIO", out.chomp) assert_equal("Reline::GeneralIO", out.chomp)
end end