2009-09-09 02:46:31 -04:00
|
|
|
require 'test/unit'
|
2011-08-31 04:28:19 -04:00
|
|
|
require 'tempfile'
|
|
|
|
require 'thread'
|
2009-09-09 02:46:31 -04:00
|
|
|
require_relative 'envutil'
|
|
|
|
|
|
|
|
class TestAutoload < Test::Unit::TestCase
|
|
|
|
def test_autoload_so
|
|
|
|
# Continuation is always available, unless excluded intentionally.
|
|
|
|
assert_in_out_err([], <<-INPUT, [], [])
|
|
|
|
autoload :Continuation, "continuation"
|
|
|
|
begin Continuation; rescue LoadError; end
|
|
|
|
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
|
|
|
|
|
|
|
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
|
* 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 '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
|
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_inner_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 '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
|
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_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
|
|
|
|
|
|
|
|
def add_autoload(path)
|
2012-04-12 03:16:14 -04:00
|
|
|
(@autoload_paths ||= []) << path
|
2011-08-31 04:28:19 -04:00
|
|
|
eval <<-END
|
|
|
|
class ::Object
|
|
|
|
autoload :AutoloadTest, #{path.dump}
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_autoload_constant
|
2012-04-12 03:16:14 -04:00
|
|
|
$".replace($" - @autoload_paths)
|
2011-08-31 04:28:19 -04:00
|
|
|
eval <<-END
|
|
|
|
class ::Object
|
|
|
|
remove_const(:AutoloadTest)
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
2009-09-09 02:46:31 -04:00
|
|
|
end
|