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'
|
2019-05-07 06:00:57 -04:00
|
|
|
tmpfile2 = tmpdir + '/bar.rb'
|
2011-06-14 06:51:25 -04:00
|
|
|
a = Module.new do
|
|
|
|
autoload :X, tmpfile
|
2019-05-07 06:00:57 -04:00
|
|
|
autoload :Y, tmpfile2
|
2011-06-14 06:51:25 -04:00
|
|
|
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)
|
2019-05-07 06:52:24 -04:00
|
|
|
assert_equal(tmpfile, a.autoload?(:X, false))
|
|
|
|
assert_equal(tmpfile, a.autoload?(:X, nil))
|
|
|
|
assert_nil(b.autoload?(:X, false))
|
|
|
|
assert_nil(b.autoload?(:X, nil))
|
2019-05-07 06:00:57 -04:00
|
|
|
assert_equal(true, a.const_defined?("Y"))
|
|
|
|
assert_equal(true, b.const_defined?("Y"))
|
|
|
|
assert_equal(tmpfile2, a.autoload?("Y"))
|
|
|
|
assert_equal(tmpfile2, b.autoload?("Y"))
|
2011-06-14 06:51:25 -04:00
|
|
|
}
|
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]
|
2020-01-28 03:53:20 -05:00
|
|
|
Object.send(:remove_const, :A) if Object.const_defined?(:A)
|
|
|
|
|
2015-05-16 15:02:38 -04:00
|
|
|
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
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :C, 'test-ruby-core-69206'
|
2015-05-16 15:02:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
END
|
|
|
|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write("test-ruby-core-69206.rb", 'module A; class C; end; end')
|
2015-05-16 15:02:38 -04:00
|
|
|
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|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write(tmpdir+"/test-bug-14469.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
2018-02-13 04:36:35 -05:00
|
|
|
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
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-14469.rb"
|
2018-02-13 04:36:35 -05:00
|
|
|
end
|
|
|
|
assert_raise(NameError, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_autoload_deprecate_constant
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write(tmpdir+"/test-bug-14469.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
2018-02-13 04:36:35 -05:00
|
|
|
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
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-14469.rb"
|
2018-02-13 04:36:35 -05:00
|
|
|
end
|
|
|
|
assert_warning(/ZZZ is deprecated/, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-11 23:53:37 -04:00
|
|
|
def test_autoload_private_constant_before_autoload
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write(tmpdir+"/test-bug-11055.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
2019-08-11 23:53:37 -04:00
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
ZZZ = :ZZZ
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[Bug #11055]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-11055.rb"
|
2019-08-11 23:53:37 -04:00
|
|
|
private_constant :ZZZ
|
|
|
|
ZZZ
|
|
|
|
end
|
|
|
|
assert_raise(NameError, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[Bug #11055]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-11055.rb"
|
2019-08-11 23:53:37 -04:00
|
|
|
private_constant :ZZZ
|
|
|
|
end
|
|
|
|
assert_raise(NameError, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_autoload_deprecate_constant_before_autoload
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write(tmpdir+"/test-bug-11055.rb", "#{<<~"begin;"}\n#{<<~'end;'}")
|
2019-08-11 23:53:37 -04:00
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
|
|
|
ZZZ = :ZZZ
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[Bug #11055]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-11055.rb"
|
2019-08-11 23:53:37 -04:00
|
|
|
deprecate_constant :ZZZ
|
|
|
|
end
|
|
|
|
assert_warning(/ZZZ is deprecated/, bug) {class AutoloadTest; ZZZ; end}
|
|
|
|
assert_warning(/ZZZ is deprecated/, bug) {AutoloadTest::ZZZ}
|
|
|
|
end;
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-'end;'}")
|
|
|
|
bug = '[Bug #11055]'
|
|
|
|
begin;
|
|
|
|
class AutoloadTest
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :ZZZ, "test-bug-11055.rb"
|
2019-08-11 23:53:37 -04:00
|
|
|
deprecate_constant :ZZZ
|
|
|
|
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
|
2019-06-27 23:33:24 -04:00
|
|
|
thrs << Thread.new { AutoloadTest && nil }
|
2018-04-19 23:22:26 -04:00
|
|
|
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|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write("#{tmpdir}/test-bug-14742.rb", "#{<<~'begin;'}\n#{<<~'end;'}")
|
2018-05-10 04:54:26 -04:00
|
|
|
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;
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :Foo, 'test-bug-14742'
|
|
|
|
autoload :Bar, 'test-bug-14742'
|
2018-05-10 04:54:26 -04:00
|
|
|
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
|
|
|
|
|
2019-11-19 03:34:43 -05:00
|
|
|
def test_autoload_same_file_with_raise
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
2019-12-10 04:31:01 -05:00
|
|
|
File.write("#{tmpdir}/test-bug-16177.rb", "#{<<~'begin;'}\n#{<<~'end;'}")
|
2019-11-19 03:34:43 -05:00
|
|
|
begin;
|
|
|
|
raise '[ruby-core:95055] [Bug #16177]'
|
|
|
|
end;
|
|
|
|
assert_raise(RuntimeError, '[ruby-core:95055] [Bug #16177]') do
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-'begin;'}\n#{<<-'end;'}")
|
|
|
|
begin;
|
2019-12-10 04:31:01 -05:00
|
|
|
autoload :Foo, 'test-bug-16177'
|
|
|
|
autoload :Bar, 'test-bug-16177'
|
2019-11-19 03:34:43 -05:00
|
|
|
t1 = Thread.new do Foo end
|
|
|
|
t2 = Thread.new do Bar end
|
|
|
|
t1.join
|
|
|
|
t2.join
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 08:35:28 -04:00
|
|
|
def test_source_location
|
|
|
|
bug = "Bug16764"
|
|
|
|
Dir.mktmpdir('autoload') do |tmpdir|
|
|
|
|
path = "#{tmpdir}/test-#{bug}.rb"
|
2020-05-15 01:27:06 -04:00
|
|
|
File.write(path, "C::#{bug} = __FILE__\n")
|
|
|
|
assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-"end;"}")
|
|
|
|
begin;
|
|
|
|
class C; end
|
|
|
|
C.autoload(:Bug16764, #{path.dump})
|
|
|
|
assert_equal [__FILE__, __LINE__-1], C.const_source_location(#{bug.dump})
|
|
|
|
assert_equal #{path.dump}, C.const_get(#{bug.dump})
|
|
|
|
assert_equal [#{path.dump}, 1], C.const_source_location(#{bug.dump})
|
|
|
|
end;
|
2020-04-07 08:35:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 13:53:39 -04:00
|
|
|
def test_no_memory_leak
|
|
|
|
assert_no_memory_leak([], '', "#{<<~"begin;"}\n#{<<~'end;'}", 'many autoloads', timeout: 60)
|
|
|
|
begin;
|
2018-05-10 04:54:26 -04:00
|
|
|
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)}
|
2020-01-27 21:58:09 -05:00
|
|
|
TestAutoload.class_eval {remove_const(:AutoloadTest)} if defined? TestAutoload::AutoloadTest
|
2011-08-31 04:28:19 -04:00
|
|
|
end
|
2009-09-09 02:46:31 -04:00
|
|
|
end
|