2017-11-23 22:53:27 -05:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
|
|
|
require 'irb/workspace'
|
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestWorkSpace < Test::Unit::TestCase
|
2017-11-24 06:00:08 -05:00
|
|
|
def test_code_around_binding
|
2017-11-23 22:53:27 -05:00
|
|
|
Tempfile.create 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, 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
|
|
|
|
end
|
|
|
|
|
2017-11-29 06:27:32 -05:00
|
|
|
def test_code_around_binding_with_existing_unreadable_file
|
|
|
|
skip 'chmod cannot make file unreadable on windows' if windows?
|
2018-01-10 05:39:09 -05:00
|
|
|
skip 'skipped in root privilege' if Process.uid == 0
|
2017-11-29 06:27:32 -05:00
|
|
|
|
|
|
|
Tempfile.create do |f|
|
|
|
|
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__
|
2017-11-23 22:53:27 -05:00
|
|
|
with_script_lines do |script_lines|
|
|
|
|
Tempfile.create do |f|
|
|
|
|
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)
|
|
|
|
assert_equal(<<~EOS, workspace.code_around_binding)
|
|
|
|
|
|
|
|
From: #{f.path} @ line 1 :
|
|
|
|
|
|
|
|
=> 1: IRB::WorkSpace.new(binding)
|
|
|
|
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|