1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/irb/test_workspace.rb
Stan Lo a923203811 [ruby/irb] Provide a base test class and let tests restore encodings
conveniently
(https://github.com/ruby/irb/pull/429)

* Create a base TestIRB::TestCase class

* Save/restore encodings for tests that initializes InputMethod classes

Because `RelineInputMethod#initializes` calls `set_encoding`, which
changes stdio/out/err and Encoding's default encoding values, we need to
make sure any test that directly or indirectly (e.g. through Context)
initializes `RelineInputMethod` restores encodings.

`ReadlineInputMethod` also changes encodings but currently no tests
cover it.

* Remove unnecessary TestHelper module

Since we now have a base TestCase, without_rdoc can just live there.

https://github.com/ruby/irb/commit/c2874ec121
2022-11-03 22:13:11 +00:00

129 lines
3.3 KiB
Ruby

# frozen_string_literal: false
require 'tempfile'
require 'rubygems'
require 'irb'
require 'irb/workspace'
require 'irb/color'
require_relative "helper"
module TestIRB
class TestWorkSpace < TestCase
def test_code_around_binding
IRB.conf[:USE_COLORIZE] = false
Tempfile.create('irb') do |f|
code = <<~RUBY
# 1
# 2
IRB::WorkSpace.new(binding) # 3
# 4
# 5
RUBY
f.print(code)
f.close
workspace = eval(code, binding, f.path)
assert_equal(<<~EOS, without_term { workspace.code_around_binding })
From: #{f.path} @ line 3 :
1: # 1
2: # 2
=> 3: IRB::WorkSpace.new(binding) # 3
4: # 4
5: # 5
EOS
end
ensure
IRB.conf.delete(:USE_COLORIZE)
end
def test_code_around_binding_with_existing_unreadable_file
pend 'chmod cannot make file unreadable on windows' if windows?
pend 'skipped in root privilege' if Process.uid == 0
Tempfile.create('irb') do |f|
code = "IRB::WorkSpace.new(binding)\n"
f.print(code)
f.close
File.chmod(0, f.path)
workspace = eval(code, binding, f.path)
assert_equal(nil, workspace.code_around_binding)
end
end
def test_code_around_binding_with_script_lines__
IRB.conf[:USE_COLORIZE] = false
with_script_lines do |script_lines|
Tempfile.create('irb') do |f|
code = "IRB::WorkSpace.new(binding)\n"
script_lines[f.path] = code.split(/^/)
workspace = eval(code, binding, f.path)
assert_equal(<<~EOS, without_term { workspace.code_around_binding })
From: #{f.path} @ line 1 :
=> 1: IRB::WorkSpace.new(binding)
EOS
end
end
ensure
IRB.conf.delete(:USE_COLORIZE)
end
def test_code_around_binding_on_irb
workspace = eval("IRB::WorkSpace.new(binding)", binding, "(irb)")
assert_equal(nil, workspace.code_around_binding)
end
def test_toplevel_binding_local_variables
pend if RUBY_ENGINE == 'truffleruby'
bug17623 = '[ruby-core:102468]'
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
top_srcdir = "#{__dir__}/../.."
irb_path = nil
%w[exe libexec].find do |dir|
irb_path = "#{top_srcdir}/#{dir}/irb"
File.exist?(irb_path)
end or omit 'irb command not found'
assert_in_out_err(bundle_exec + ['-W0', "-C#{top_srcdir}", '-e', <<~RUBY , '--', '-f', '--'], 'binding.local_variables', /\[:_\]/, [], bug17623)
version = 'xyz' # typical rubygems loading file
load('#{irb_path}')
RUBY
end
private
def with_script_lines
script_lines = nil
debug_lines = {}
Object.class_eval do
if defined?(SCRIPT_LINES__)
script_lines = SCRIPT_LINES__
remove_const :SCRIPT_LINES__
end
const_set(:SCRIPT_LINES__, debug_lines)
end
yield debug_lines
ensure
Object.class_eval do
remove_const :SCRIPT_LINES__
const_set(:SCRIPT_LINES__, script_lines) if script_lines
end
end
def without_term
env = ENV.to_h.dup
ENV.delete('TERM')
yield
ensure
ENV.replace(env)
end
end
end