2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2009-09-09 02:46:31 -04:00
|
|
|
require 'test/unit'
|
2011-08-31 04:28:19 -04:00
|
|
|
require 'tempfile'
|
2009-09-09 02:46:31 -04:00
|
|
|
|
|
|
|
class TestAutoload < Test::Unit::TestCase
|
|
|
|
def test_autoload_so
|
2014-11-26 08:31:23 -05:00
|
|
|
# Date is always available, unless excluded intentionally.
|
2009-09-09 02:46:31 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, [], [])
|
2014-11-26 08:31:23 -05:00
|
|
|
autoload :Date, "date"
|
|
|
|
begin Date; rescue LoadError; end
|
2009-09-09 02:46:31 -04:00
|
|
|
INPUT
|
|
|
|
end
|
2011-02-04 20:09:39 -05:00
|
|
|
|
|
|
|
def test_non_realpath_in_loadpath
|
|
|
|
require 'tmpdir'
|
|
|
|
tmpdir = Dir.mktmpdir('autoload')
|
|
|
|
tmpdirs = [tmpdir]
|
|
|
|
tmpdirs.unshift(tmpdir + '/foo')
|
|
|
|
Dir.mkdir(tmpdirs[0])
|
|
|
|
tmpfiles = [tmpdir + '/foo.rb', tmpdir + '/foo/bar.rb']
|
|
|
|
open(tmpfiles[0] , 'w') do |f|
|
|
|
|
f.puts <<-INPUT
|
|
|
|
$:.unshift(File.expand_path('..', __FILE__)+'/./foo')
|
|
|
|
module Foo
|
|
|
|
autoload :Bar, 'bar'
|
|
|
|
end
|
2011-02-05 08:48:45 -05:00
|
|
|
p Foo::Bar
|
2011-02-04 20:09:39 -05:00
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
open(tmpfiles[1], 'w') do |f|
|
|
|
|
f.puts 'class Foo::Bar; end'
|
|
|
|
end
|
2011-02-05 08:48:45 -05:00
|
|
|
assert_in_out_err([tmpfiles[0]], "", ["Foo::Bar"], [])
|
2011-02-04 20:09:39 -05:00
|
|
|
ensure
|
|
|
|
File.unlink(*tmpfiles) rescue nil if tmpfiles
|
|
|
|
tmpdirs.each {|dir| Dir.rmdir(dir)}
|
|
|
|
end
|
2011-04-14 08:23:32 -04:00
|
|
|
|
|
|
|
def test_autoload_p
|
|
|
|
bug4565 = '[ruby-core:35679]'
|
|
|
|
|
|
|
|
require 'tmpdir'
|
2011-06-14 06:51:25 -04:00
|
|
|
Dir.mktmpdir('autoload') {|tmpdir|
|
|
|
|
tmpfile = tmpdir + '/foo.rb'
|
|
|
|
a = Module.new do
|
|
|
|
autoload :X, tmpfile
|
|
|
|
end
|
|
|
|
b = Module.new do
|
|
|
|
include a
|
|
|
|
end
|
|
|
|
assert_equal(true, a.const_defined?(:X))
|
|
|
|
assert_equal(true, b.const_defined?(:X))
|
|
|
|
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
|
|
|
assert_equal(tmpfile, b.autoload?(:X), bug4565)
|
|
|
|
}
|
2011-04-14 08:23:32 -04:00
|
|
|
end
|
2011-08-31 04:28:19 -04:00
|
|
|
|
2015-05-16 15:02:38 -04:00
|
|
|
def test_autoload_with_unqualified_file_name # [ruby-core:69206]
|
|
|
|
lp = $LOAD_PATH.dup
|
|
|
|
lf = $LOADED_FEATURES.dup
|
|
|
|
|
|
|
|
Dir.mktmpdir('autoload') { |tmpdir|
|
|
|
|
$LOAD_PATH << tmpdir
|
|
|
|
|
|
|
|
Dir.chdir(tmpdir) do
|
|
|
|
eval <<-END
|
|
|
|
class ::Object
|
|
|
|
module A
|
|
|
|
autoload :C, 'b'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
END
|
|
|
|
|
|
|
|
File.open('b.rb', 'w') {|file| file.puts 'module A; class C; end; end'}
|
|
|
|
assert_kind_of Class, ::A::C
|
|
|
|
end
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
$LOAD_PATH.replace lp
|
|
|
|
$LOADED_FEATURES.replace lf
|
|
|
|
Object.send(:remove_const, :A) if Object.const_defined?(:A)
|
|
|
|
end
|
|
|
|
|
2011-08-31 04:28:19 -04:00
|
|
|
def test_require_explicit
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'class Object; AutoloadTest = 1; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert(require file.path)
|
|
|
|
assert_equal(1, ::AutoloadTest)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_threaded_accessing_constant
|
2014-06-01 11:25:12 -04:00
|
|
|
# Suppress "warning: loading in progress, circular require considered harmful"
|
2014-06-01 18:31:30 -04:00
|
|
|
EnvUtil.default_warning {
|
2014-06-01 11:41:42 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'sleep 0.5; class AutoloadTest; X = 1; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
|
|
|
t1 = Thread.new { ::AutoloadTest::X }
|
|
|
|
t2 = Thread.new { ::AutoloadTest::X }
|
|
|
|
[t1, t2].each(&:join)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
end
|
2014-06-01 11:41:42 -04:00
|
|
|
}
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_threaded_accessing_inner_constant
|
2014-06-01 08:09:08 -04:00
|
|
|
# Suppress "warning: loading in progress, circular require considered harmful"
|
2014-06-01 18:31:30 -04:00
|
|
|
EnvUtil.default_warning {
|
2014-06-01 11:41:42 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'class AutoloadTest; sleep 0.5; X = 1; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
|
|
|
t1 = Thread.new { ::AutoloadTest::X }
|
|
|
|
t2 = Thread.new { ::AutoloadTest::X }
|
|
|
|
[t1, t2].each(&:join)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
end
|
2014-06-01 11:41:42 -04:00
|
|
|
}
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nameerror_when_autoload_did_not_define_the_constant
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts ''
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
assert_raise(NameError) do
|
|
|
|
AutoloadTest
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_override_autoload
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts ''
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
eval %q(class AutoloadTest; end)
|
|
|
|
assert_equal(Class, AutoloadTest.class)
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
|
|
|
end
|
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_override_while_autoloading
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'class AutoloadTest; sleep 0.5; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
# while autoloading...
|
|
|
|
t = Thread.new { AutoloadTest }
|
|
|
|
sleep 0.1
|
|
|
|
# override it
|
|
|
|
EnvUtil.suppress_warning {
|
|
|
|
eval %q(AutoloadTest = 1)
|
|
|
|
}
|
|
|
|
t.join
|
|
|
|
assert_equal(1, AutoloadTest)
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
|
|
|
end
|
|
|
|
}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
2015-11-05 17:05:06 -05:00
|
|
|
def ruby_impl_require
|
2016-02-19 02:58:09 -05:00
|
|
|
Kernel.module_eval do
|
|
|
|
alias old_require require
|
|
|
|
end
|
2015-05-14 17:57:33 -04:00
|
|
|
called_with = []
|
|
|
|
Kernel.send :define_method, :require do |path|
|
|
|
|
called_with << path
|
|
|
|
old_require path
|
|
|
|
end
|
2015-11-05 17:05:06 -05:00
|
|
|
yield called_with
|
2015-05-14 17:57:33 -04:00
|
|
|
ensure
|
2016-02-19 02:58:09 -05:00
|
|
|
Kernel.module_eval do
|
|
|
|
undef require
|
|
|
|
alias require old_require
|
|
|
|
undef old_require
|
|
|
|
end
|
2015-05-14 17:57:33 -04:00
|
|
|
end
|
|
|
|
|
2015-11-05 17:05:06 -05:00
|
|
|
def test_require_implemented_in_ruby_is_called
|
|
|
|
ruby_impl_require do |called_with|
|
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'class AutoloadTest; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
assert(Object::AutoloadTest)
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
|
|
|
end
|
|
|
|
assert_equal [file.path], called_with
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-09 16:18:42 -05:00
|
|
|
def test_autoload_while_autoloading
|
|
|
|
ruby_impl_require do |called_with|
|
|
|
|
Tempfile.create(%w(a .rb)) do |a|
|
|
|
|
Tempfile.create(%w(b .rb)) do |b|
|
|
|
|
a.puts "require '#{b.path}'; class AutoloadTest; end"
|
|
|
|
b.puts "class AutoloadTest; module B; end; end"
|
|
|
|
[a, b].each(&:flush)
|
|
|
|
add_autoload(a.path)
|
|
|
|
begin
|
|
|
|
assert(Object::AutoloadTest)
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
|
|
|
end
|
|
|
|
assert_equal [a.path, b.path], called_with
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-07 00:26:37 -04:00
|
|
|
def test_bug_13526
|
|
|
|
script = File.join(__dir__, 'bug-13526.rb')
|
|
|
|
assert_ruby_status([script], '', '[ruby-core:81016] [Bug #13526]')
|
|
|
|
end
|
|
|
|
|
2018-02-13 04:36:35 -05:00
|
|
|
def test_autoload_private_constant
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
|
|
|
File.write(tmpdir+"/zzz.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
ZZZ = :ZZZ
|
|
|
|
private_constant :ZZZ
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[ruby-core:85516] [Bug #14469]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
autoload :ZZZ, "zzz.rb"
|
|
|
|
end
|
|
|
|
assert_raise(NameError, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_autoload_deprecate_constant
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
|
|
|
File.write(tmpdir+"/zzz.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
ZZZ = :ZZZ
|
|
|
|
deprecate_constant :ZZZ
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[ruby-core:85516] [Bug #14469]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
autoload :ZZZ, "zzz.rb"
|
|
|
|
end
|
|
|
|
assert_warning(/ZZZ is deprecated/, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-19 23:22:26 -04:00
|
|
|
def test_autoload_fork
|
|
|
|
EnvUtil.default_warning do
|
|
|
|
Tempfile.create(['autoload', '.rb']) {|file|
|
|
|
|
file.puts 'sleep 0.3; class AutoloadTest; end'
|
|
|
|
file.close
|
|
|
|
add_autoload(file.path)
|
|
|
|
begin
|
|
|
|
thrs = []
|
|
|
|
3.times do
|
|
|
|
thrs << Thread.new { AutoloadTest; nil }
|
|
|
|
thrs << Thread.new { fork { AutoloadTest } }
|
|
|
|
end
|
|
|
|
thrs.each(&:join)
|
|
|
|
thrs.each do |th|
|
|
|
|
pid = th.value or next
|
|
|
|
_, status = Process.waitpid2(pid)
|
|
|
|
assert_predicate status, :success?
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
remove_autoload_constant
|
|
|
|
assert_nil $!, '[ruby-core:86410] [Bug #14634]'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end if Process.respond_to?(:fork)
|
|
|
|
|
2018-05-10 04:54:26 -04:00
|
|
|
def test_autoload_same_file
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
|
|
|
File.write("#{tmpdir}/b.rb", "#{<<~'begin;'}\n#{<<~'end;'}")
|
|
|
|
begin;
|
|
|
|
module Foo; end
|
|
|
|
module Bar; end
|
|
|
|
end;
|
|
|
|
3.times do # timing-dependent, needs a few times to hit [Bug #14742]
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-'begin;'}\n#{<<-'end;'}")
|
|
|
|
begin;
|
|
|
|
autoload :Foo, 'b'
|
|
|
|
autoload :Bar, 'b'
|
|
|
|
t1 = Thread.new do Foo end
|
|
|
|
t2 = Thread.new do Bar end
|
|
|
|
t1.join
|
|
|
|
t2.join
|
|
|
|
bug = '[ruby-core:86935] [Bug #14742]'
|
|
|
|
assert_instance_of Module, t1.value, bug
|
|
|
|
assert_instance_of Module, t2.value, bug
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_leak
|
|
|
|
assert_no_memory_leak([], '', <<~'end;', 'many autoloads', timeout: 30)
|
|
|
|
200000.times do |i|
|
|
|
|
m = Module.new
|
|
|
|
m.instance_eval do
|
|
|
|
autoload :Foo, 'x'
|
|
|
|
autoload :Bar, i.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2011-08-31 04:28:19 -04:00
|
|
|
def add_autoload(path)
|
2012-04-12 03:16:14 -04:00
|
|
|
(@autoload_paths ||= []) << path
|
2015-05-18 05:41:21 -04:00
|
|
|
::Object.class_eval {autoload(:AutoloadTest, path)}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_autoload_constant
|
2012-04-12 03:16:14 -04:00
|
|
|
$".replace($" - @autoload_paths)
|
2015-05-18 05:41:21 -04:00
|
|
|
::Object.class_eval {remove_const(:AutoloadTest)}
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
2009-09-09 02:46:31 -04:00
|
|
|
end
|