2017-11-23 22:53:27 -05:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
2020-07-14 22:21:14 -04:00
|
|
|
require 'rubygems'
|
2019-05-20 20:24:09 -04:00
|
|
|
require 'irb'
|
2017-11-23 22:53:27 -05:00
|
|
|
require 'irb/workspace'
|
2019-04-25 08:16:21 -04:00
|
|
|
require 'irb/color'
|
2017-11-23 22:53:27 -05:00
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestWorkSpace < Test::Unit::TestCase
|
2017-11-24 06:00:08 -05:00
|
|
|
def test_code_around_binding
|
2021-02-07 21:08:57 -05:00
|
|
|
IRB.conf[:USE_COLORIZE] = false
|
2019-04-26 05:28:54 -04:00
|
|
|
Tempfile.create('irb') do |f|
|
2017-11-23 22:53:27 -05:00
|
|
|
code = <<~RUBY
|
|
|
|
# 1
|
|
|
|
# 2
|
|
|
|
IRB::WorkSpace.new(binding) # 3
|
|
|
|
# 4
|
|
|
|
# 5
|
|
|
|
RUBY
|
|
|
|
f.print(code)
|
|
|
|
f.close
|
|
|
|
|
|
|
|
workspace = eval(code, binding, f.path)
|
2019-04-25 08:16:21 -04:00
|
|
|
assert_equal(<<~EOS, without_term { workspace.code_around_binding })
|
2017-11-23 22:53:27 -05:00
|
|
|
|
|
|
|
From: #{f.path} @ line 3 :
|
|
|
|
|
|
|
|
1: # 1
|
|
|
|
2: # 2
|
|
|
|
=> 3: IRB::WorkSpace.new(binding) # 3
|
|
|
|
4: # 4
|
|
|
|
5: # 5
|
|
|
|
|
|
|
|
EOS
|
|
|
|
end
|
2021-02-07 21:08:57 -05:00
|
|
|
ensure
|
|
|
|
IRB.conf.delete(:USE_COLORIZE)
|
2017-11-23 22:53:27 -05:00
|
|
|
end
|
|
|
|
|
2017-11-29 06:27:32 -05:00
|
|
|
def test_code_around_binding_with_existing_unreadable_file
|
2021-05-31 01:56:50 -04:00
|
|
|
pend 'chmod cannot make file unreadable on windows' if windows?
|
|
|
|
pend 'skipped in root privilege' if Process.uid == 0
|
2017-11-29 06:27:32 -05:00
|
|
|
|
2019-04-26 05:28:54 -04:00
|
|
|
Tempfile.create('irb') do |f|
|
2017-11-29 06:27:32 -05:00
|
|
|
code = "IRB::WorkSpace.new(binding)\n"
|
|
|
|
f.print(code)
|
|
|
|
f.close
|
2017-11-24 06:00:10 -05:00
|
|
|
|
2017-11-29 06:27:32 -05:00
|
|
|
File.chmod(0, f.path)
|
2017-11-24 06:00:10 -05:00
|
|
|
|
2017-11-29 06:27:32 -05:00
|
|
|
workspace = eval(code, binding, f.path)
|
|
|
|
assert_equal(nil, workspace.code_around_binding)
|
2017-11-24 06:00:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-24 06:00:08 -05:00
|
|
|
def test_code_around_binding_with_script_lines__
|
2021-02-07 21:08:57 -05:00
|
|
|
IRB.conf[:USE_COLORIZE] = false
|
2017-11-23 22:53:27 -05:00
|
|
|
with_script_lines do |script_lines|
|
2019-04-26 05:28:54 -04:00
|
|
|
Tempfile.create('irb') do |f|
|
2017-11-23 22:53:27 -05:00
|
|
|
code = "IRB::WorkSpace.new(binding)\n"
|
2017-11-24 00:46:55 -05:00
|
|
|
script_lines[f.path] = code.split(/^/)
|
2017-11-23 22:53:27 -05:00
|
|
|
|
|
|
|
workspace = eval(code, binding, f.path)
|
2019-04-25 08:16:21 -04:00
|
|
|
assert_equal(<<~EOS, without_term { workspace.code_around_binding })
|
2017-11-23 22:53:27 -05:00
|
|
|
|
|
|
|
From: #{f.path} @ line 1 :
|
|
|
|
|
|
|
|
=> 1: IRB::WorkSpace.new(binding)
|
|
|
|
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
2021-02-07 21:08:57 -05:00
|
|
|
ensure
|
|
|
|
IRB.conf.delete(:USE_COLORIZE)
|
2017-11-23 22:53:27 -05:00
|
|
|
end
|
|
|
|
|
2017-11-24 06:00:08 -05:00
|
|
|
def test_code_around_binding_on_irb
|
2017-11-23 22:53:27 -05:00
|
|
|
workspace = eval("IRB::WorkSpace.new(binding)", binding, "(irb)")
|
|
|
|
assert_equal(nil, workspace.code_around_binding)
|
|
|
|
end
|
|
|
|
|
2021-03-18 09:44:42 -04:00
|
|
|
|
|
|
|
def test_toplevel_binding_local_variables
|
2021-09-09 17:18:21 -04:00
|
|
|
pend if RUBY_ENGINE == 'truffleruby'
|
2021-03-18 09:44:42 -04:00
|
|
|
bug17623 = '[ruby-core:102468]'
|
|
|
|
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []
|
2021-09-09 19:07:08 -04:00
|
|
|
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)
|
2021-03-18 09:44:42 -04:00
|
|
|
version = 'xyz' # typical rubygems loading file
|
2021-09-09 18:43:01 -04:00
|
|
|
load('#{irb_path}')
|
2021-03-18 09:44:42 -04:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2017-11-23 22:53:27 -05:00
|
|
|
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
|
2019-04-25 08:16:21 -04:00
|
|
|
|
|
|
|
def without_term
|
|
|
|
env = ENV.to_h.dup
|
|
|
|
ENV.delete('TERM')
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ENV.replace(env)
|
|
|
|
end
|
2017-11-23 22:53:27 -05:00
|
|
|
end
|
|
|
|
end
|