PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_require.rb

https://github.com/diabolo/ruby
Ruby | 307 lines | 270 code | 36 blank | 1 comment | 2 complexity | 156e5a43645e48c7a040fe5c8742d0c4 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. require 'test/unit'
  2. require 'tempfile'
  3. require_relative 'envutil'
  4. require 'tmpdir'
  5. class TestRequire < Test::Unit::TestCase
  6. def test_require_invalid_shared_object
  7. t = Tempfile.new(["test_ruby_test_require", ".so"])
  8. t.puts "dummy"
  9. t.close
  10. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  11. begin
  12. require \"#{ t.path }\"
  13. rescue LoadError
  14. p :ok
  15. end
  16. INPUT
  17. end
  18. def test_require_too_long_filename
  19. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  20. begin
  21. require '#{ "foo/" * 10000 }foo'
  22. rescue LoadError
  23. p :ok
  24. end
  25. INPUT
  26. begin
  27. assert_in_out_err(["-S", "foo/" * 10000 + "foo"], "") do |r, e|
  28. assert_equal([], r)
  29. assert_operator(2, :<=, e.size)
  30. assert_equal("openpath: pathname too long (ignored)", e.first)
  31. assert_match(/\(LoadError\)/, e.last)
  32. end
  33. rescue Errno::EINVAL
  34. # too long commandline may be blocked by OS.
  35. end
  36. end
  37. def test_require_path_home
  38. env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"]
  39. ENV["RUBYPATH"] = "~"
  40. ENV["HOME"] = "/foo" * 10000
  41. assert_in_out_err(%w(-S test_ruby_test_require), "", [], /^.+$/)
  42. ENV["RUBYPATH"] = "~" + "/foo" * 10000
  43. ENV["HOME"] = "/foo"
  44. assert_in_out_err(%w(-S test_ruby_test_require), "", [], /^.+$/)
  45. t = Tempfile.new(["test_ruby_test_require", ".rb"])
  46. t.puts "p :ok"
  47. t.close
  48. ENV["RUBYPATH"] = "~"
  49. ENV["HOME"], name = File.split(t.path)
  50. assert_in_out_err(["-S", name], "", %w(:ok), [])
  51. ensure
  52. env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH")
  53. env_home ? ENV["HOME"] = env_home : ENV.delete("HOME")
  54. end
  55. def test_require_with_unc
  56. assert(system(File.expand_path(EnvUtil.rubybin).sub(/\A(\w):/, '//localhost/\1$/'), "-rabbrev", "-e0"))
  57. end if /mswin|mingw/ =~ RUBY_PLATFORM
  58. def test_define_class
  59. begin
  60. require "socket"
  61. rescue LoadError
  62. return
  63. end
  64. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  65. BasicSocket = 1
  66. begin
  67. require 'socket'
  68. p :ng
  69. rescue TypeError
  70. p :ok
  71. end
  72. INPUT
  73. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  74. class BasicSocket; end
  75. begin
  76. require 'socket'
  77. p :ng
  78. rescue TypeError
  79. p :ok
  80. end
  81. INPUT
  82. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  83. class BasicSocket < IO; end
  84. begin
  85. require 'socket'
  86. p :ok
  87. rescue Exception
  88. p :ng
  89. end
  90. INPUT
  91. end
  92. def test_define_class_under
  93. begin
  94. require "zlib"
  95. rescue LoadError
  96. return
  97. end
  98. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  99. module Zlib; end
  100. Zlib::Error = 1
  101. begin
  102. require 'zlib'
  103. p :ng
  104. rescue TypeError
  105. p :ok
  106. end
  107. INPUT
  108. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  109. module Zlib; end
  110. class Zlib::Error; end
  111. begin
  112. require 'zlib'
  113. p :ng
  114. rescue NameError
  115. p :ok
  116. end
  117. INPUT
  118. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  119. module Zlib; end
  120. class Zlib::Error < StandardError; end
  121. begin
  122. require 'zlib'
  123. p :ok
  124. rescue Exception
  125. p :ng
  126. end
  127. INPUT
  128. end
  129. def test_define_module
  130. begin
  131. require "zlib"
  132. rescue LoadError
  133. return
  134. end
  135. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  136. Zlib = 1
  137. begin
  138. require 'zlib'
  139. p :ng
  140. rescue TypeError
  141. p :ok
  142. end
  143. INPUT
  144. end
  145. def test_define_module_under
  146. begin
  147. require "socket"
  148. rescue LoadError
  149. return
  150. end
  151. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  152. class BasicSocket < IO; end
  153. class Socket < BasicSocket; end
  154. Socket::Constants = 1
  155. begin
  156. require 'socket'
  157. p :ng
  158. rescue TypeError
  159. p :ok
  160. end
  161. INPUT
  162. end
  163. def test_load
  164. t = Tempfile.new(["test_ruby_test_require", ".rb"])
  165. t.puts "module Foo; end"
  166. t.puts "at_exit { p :wrap_end }"
  167. t.puts "at_exit { raise 'error in at_exit test' }"
  168. t.puts "p :ok"
  169. t.close
  170. assert_in_out_err([], <<-INPUT, %w(:ok :end :wrap_end), /error in at_exit test/)
  171. load(#{ t.path.dump }, true)
  172. GC.start
  173. p :end
  174. INPUT
  175. assert_raise(ArgumentError) { at_exit }
  176. end
  177. def test_load2 # [ruby-core:25039]
  178. t = Tempfile.new(["test_ruby_test_require", ".rb"])
  179. t.puts "Hello = 'hello'"
  180. t.puts "class Foo"
  181. t.puts " p Hello"
  182. t.puts "end"
  183. t.close
  184. assert_in_out_err([], <<-INPUT, %w("hello"), [])
  185. load(#{ t.path.dump }, true)
  186. INPUT
  187. end
  188. def test_tainted_loadpath
  189. t = Tempfile.new(["test_ruby_test_require", ".rb"])
  190. abs_dir, file = File.split(t.path)
  191. abs_dir = File.expand_path(abs_dir).untaint
  192. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  193. abs_dir = "#{ abs_dir }"
  194. $: << abs_dir
  195. require "#{ file }"
  196. p :ok
  197. INPUT
  198. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  199. abs_dir = "#{ abs_dir }"
  200. $: << abs_dir.taint
  201. require "#{ file }"
  202. p :ok
  203. INPUT
  204. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  205. abs_dir = "#{ abs_dir }"
  206. $: << abs_dir.taint
  207. $SAFE = 1
  208. begin
  209. require "#{ file }"
  210. rescue SecurityError
  211. p :ok
  212. end
  213. INPUT
  214. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  215. abs_dir = "#{ abs_dir }"
  216. $: << abs_dir.taint
  217. $SAFE = 1
  218. begin
  219. require "#{ file }"
  220. rescue SecurityError
  221. p :ok
  222. end
  223. INPUT
  224. assert_in_out_err([], <<-INPUT, %w(:ok), [])
  225. abs_dir = "#{ abs_dir }"
  226. $: << abs_dir << 'elsewhere'.taint
  227. require "#{ file }"
  228. p :ok
  229. INPUT
  230. end
  231. def test_relative
  232. load_path = $:.dup
  233. $:.delete(".")
  234. Dir.mktmpdir do |tmp|
  235. Dir.chdir(tmp) do
  236. Dir.mkdir('x')
  237. File.open('x/t.rb', 'wb') {}
  238. File.open('x/a.rb', 'wb') {|f| f.puts("require_relative('t.rb')")}
  239. assert require('./x/t.rb')
  240. assert !require(File.expand_path('x/t.rb'))
  241. assert_nothing_raised(LoadError) {require('./x/a.rb')}
  242. assert_raise(LoadError) {require('x/t.rb')}
  243. File.unlink(*Dir.glob('x/*'))
  244. Dir.rmdir("#{tmp}/x")
  245. $:.replace(load_path)
  246. load_path = nil
  247. assert(!require('tmpdir'))
  248. end
  249. end
  250. ensure
  251. $:.replace(load_path) if load_path
  252. end
  253. def test_relative_symlink
  254. Dir.mktmpdir {|tmp|
  255. Dir.chdir(tmp) {
  256. Dir.mkdir "a"
  257. Dir.mkdir "b"
  258. File.open("a/lib.rb", "w") {|f| f.puts 'puts "a/lib.rb"' }
  259. File.open("b/lib.rb", "w") {|f| f.puts 'puts "b/lib.rb"' }
  260. File.open("a/tst.rb", "w") {|f| f.puts 'require_relative "lib"' }
  261. begin
  262. File.symlink("../a/tst.rb", "b/tst.rb")
  263. result = IO.popen([EnvUtil.rubybin, "b/tst.rb"]).read
  264. assert_equal("a/lib.rb\n", result, "[ruby-dev:40040]")
  265. rescue NotImplementedError
  266. skip "File.symlink is not implemented"
  267. end
  268. }
  269. }
  270. end
  271. end