PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_require.rb

http://github.com/ruby/ruby
Ruby | 861 lines | 765 code | 92 blank | 4 comment | 24 complexity | de01706cbf0c0f8ad602eb257c1b3a65 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'tempfile'
  4. require 'tmpdir'
  5. class TestRequire < Test::Unit::TestCase
  6. def test_load_error_path
  7. filename = "should_not_exist"
  8. error = assert_raise(LoadError) do
  9. require filename
  10. end
  11. assert_equal filename, error.path
  12. end
  13. def test_require_invalid_shared_object
  14. Tempfile.create(["test_ruby_test_require", ".so"]) {|t|
  15. t.puts "dummy"
  16. t.close
  17. assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}")
  18. begin;
  19. $:.replace([IO::NULL])
  20. assert_raise(LoadError) do
  21. require \"#{ t.path }\"
  22. end
  23. end;
  24. }
  25. end
  26. def test_require_too_long_filename
  27. assert_separately(["RUBYOPT"=>nil], "#{<<~"begin;"}\n#{<<~"end;"}")
  28. begin;
  29. $:.replace([IO::NULL])
  30. assert_raise(LoadError) do
  31. require '#{ "foo/" * 10000 }foo'
  32. end
  33. end;
  34. begin
  35. assert_in_out_err(["-S", "-w", "foo/" * 1024 + "foo"], "") do |r, e|
  36. assert_equal([], r)
  37. assert_operator(2, :<=, e.size)
  38. assert_match(/warning: openpath: pathname too long \(ignored\)/, e.first)
  39. assert_match(/\(LoadError\)/, e.last)
  40. end
  41. rescue Errno::EINVAL
  42. # too long commandline may be blocked by OS.
  43. end
  44. end
  45. def test_require_nonascii
  46. bug3758 = '[ruby-core:31915]'
  47. ["\u{221e}", "\x82\xa0".force_encoding("cp932")].each do |path|
  48. assert_raise_with_message(LoadError, /#{path}\z/, bug3758) {require path}
  49. end
  50. end
  51. def test_require_nonascii_path
  52. bug8165 = '[ruby-core:53733] [Bug #8165]'
  53. encoding = 'filesystem'
  54. assert_require_nonascii_path(encoding, bug8165)
  55. end
  56. def test_require_nonascii_path_utf8
  57. bug8676 = '[ruby-core:56136] [Bug #8676]'
  58. encoding = Encoding::UTF_8
  59. return if Encoding.find('filesystem') == encoding
  60. assert_require_nonascii_path(encoding, bug8676)
  61. end
  62. def test_require_nonascii_path_shift_jis
  63. bug8676 = '[ruby-core:56136] [Bug #8676]'
  64. encoding = Encoding::Shift_JIS
  65. return if Encoding.find('filesystem') == encoding
  66. assert_require_nonascii_path(encoding, bug8676)
  67. end
  68. case RUBY_PLATFORM
  69. when /cygwin/, /mswin/, /mingw/, /darwin/
  70. def self.ospath_encoding(path)
  71. Encoding::UTF_8
  72. end
  73. else
  74. def self.ospath_encoding(path)
  75. path.encoding
  76. end
  77. end
  78. def prepare_require_path(dir, encoding)
  79. require 'enc/trans/single_byte'
  80. Dir.mktmpdir {|tmp|
  81. begin
  82. require_path = File.join(tmp, dir, 'foo.rb').encode(encoding)
  83. rescue
  84. skip "cannot convert path encoding to #{encoding}"
  85. end
  86. Dir.mkdir(File.dirname(require_path))
  87. open(require_path, "wb") {|f| f.puts '$:.push __FILE__'}
  88. begin
  89. load_path = $:.dup
  90. features = $".dup
  91. yield require_path
  92. ensure
  93. $:.replace(load_path)
  94. $".replace(features)
  95. end
  96. }
  97. end
  98. def assert_require_nonascii_path(encoding, bug)
  99. prepare_require_path("\u3042" * 5, encoding) {|require_path|
  100. begin
  101. # leave paths for require encoding objects
  102. bug = "#{bug} require #{encoding} path"
  103. require_path = "#{require_path}"
  104. $:.clear
  105. assert_nothing_raised(LoadError, bug) {
  106. assert(require(require_path), bug)
  107. assert_equal(self.class.ospath_encoding(require_path), $:.last.encoding, '[Bug #8753]')
  108. assert(!require(require_path), bug)
  109. }
  110. end
  111. }
  112. end
  113. def test_require_path_home_1
  114. env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"]
  115. pathname_too_long = /pathname too long \(ignored\).*\(LoadError\)/m
  116. ENV["RUBYPATH"] = "~"
  117. ENV["HOME"] = "/foo" * 1024
  118. assert_in_out_err(%w(-S -w test_ruby_test_require), "", [], pathname_too_long)
  119. ensure
  120. env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH")
  121. env_home ? ENV["HOME"] = env_home : ENV.delete("HOME")
  122. end
  123. def test_require_path_home_2
  124. env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"]
  125. pathname_too_long = /pathname too long \(ignored\).*\(LoadError\)/m
  126. ENV["RUBYPATH"] = "~" + "/foo" * 1024
  127. ENV["HOME"] = "/foo"
  128. assert_in_out_err(%w(-S -w test_ruby_test_require), "", [], pathname_too_long)
  129. ensure
  130. env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH")
  131. env_home ? ENV["HOME"] = env_home : ENV.delete("HOME")
  132. end
  133. def test_require_path_home_3
  134. env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"]
  135. Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|
  136. t.puts "p :ok"
  137. t.close
  138. ENV["RUBYPATH"] = "~"
  139. ENV["HOME"] = t.path
  140. assert_in_out_err(%w(-S test_ruby_test_require), "", [], /\(LoadError\)/)
  141. ENV["HOME"], name = File.split(t.path)
  142. assert_in_out_err(["-S", name], "", %w(:ok), [])
  143. }
  144. ensure
  145. env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH")
  146. env_home ? ENV["HOME"] = env_home : ENV.delete("HOME")
  147. end
  148. def test_require_with_unc
  149. Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|
  150. t.puts "puts __FILE__"
  151. t.close
  152. path = File.expand_path(t.path).sub(/\A(\w):/, '//127.0.0.1/\1$')
  153. skip "local drive #$1: is not shared" unless File.exist?(path)
  154. args = ['--disable-gems', "-I#{File.dirname(path)}"]
  155. assert_in_out_err(args, "#{<<~"END;"}", [path], [])
  156. begin
  157. require '#{File.basename(path)}'
  158. rescue Errno::EPERM
  159. end
  160. END;
  161. }
  162. end if /mswin|mingw/ =~ RUBY_PLATFORM
  163. def test_require_twice
  164. Dir.mktmpdir do |tmp|
  165. req = File.join(tmp, "very_long_file_name.rb")
  166. File.write(req, "p :ok\n")
  167. assert_file.exist?(req)
  168. req[/.rb$/i] = ""
  169. assert_in_out_err(['--disable-gems'], <<-INPUT, %w(:ok), [])
  170. require "#{req}"
  171. require "#{req}"
  172. INPUT
  173. end
  174. end
  175. def assert_syntax_error_backtrace
  176. loaded_features = $LOADED_FEATURES.dup
  177. Dir.mktmpdir do |tmp|
  178. req = File.join(tmp, "test.rb")
  179. File.write(req, ",\n")
  180. e = assert_raise_with_message(SyntaxError, /unexpected/) {
  181. yield req
  182. }
  183. assert_not_nil(bt = e.backtrace, "no backtrace")
  184. assert_not_empty(bt.find_all {|b| b.start_with? __FILE__}, proc {bt.inspect})
  185. end
  186. $LOADED_FEATURES.replace loaded_features
  187. end
  188. def test_require_syntax_error
  189. assert_syntax_error_backtrace {|req| require req}
  190. end
  191. def test_require_syntax_error_rescued
  192. assert_syntax_error_backtrace do |req|
  193. assert_raise_with_message(SyntaxError, /unexpected/) {require req}
  194. require req
  195. end
  196. end
  197. def test_load_syntax_error
  198. assert_syntax_error_backtrace {|req| load req}
  199. end
  200. def test_define_class
  201. begin
  202. require "socket"
  203. rescue LoadError
  204. return
  205. end
  206. assert_separately([], <<-INPUT)
  207. BasicSocket = 1
  208. assert_raise(TypeError) do
  209. require 'socket'
  210. end
  211. INPUT
  212. assert_separately([], <<-INPUT)
  213. class BasicSocket; end
  214. assert_raise(TypeError) do
  215. require 'socket'
  216. end
  217. INPUT
  218. assert_separately([], <<-INPUT)
  219. class BasicSocket < IO; end
  220. assert_nothing_raised do
  221. require 'socket'
  222. end
  223. INPUT
  224. end
  225. def test_define_class_under
  226. begin
  227. require "zlib"
  228. rescue LoadError
  229. return
  230. end
  231. assert_separately([], <<-INPUT)
  232. module Zlib; end
  233. Zlib::Error = 1
  234. assert_raise(TypeError) do
  235. require 'zlib'
  236. end
  237. INPUT
  238. assert_separately([], <<-INPUT)
  239. module Zlib; end
  240. class Zlib::Error; end
  241. assert_raise(TypeError) do
  242. require 'zlib'
  243. end
  244. INPUT
  245. assert_separately([], <<-INPUT)
  246. module Zlib; end
  247. class Zlib::Error < StandardError; end
  248. assert_nothing_raised do
  249. require 'zlib'
  250. end
  251. INPUT
  252. end
  253. def test_define_module
  254. begin
  255. require "zlib"
  256. rescue LoadError
  257. return
  258. end
  259. assert_separately([], <<-INPUT)
  260. Zlib = 1
  261. assert_raise(TypeError) do
  262. require 'zlib'
  263. end
  264. INPUT
  265. end
  266. def test_define_module_under
  267. begin
  268. require "socket"
  269. rescue LoadError
  270. return
  271. end
  272. assert_separately([], <<-INPUT)
  273. class BasicSocket < IO; end
  274. class Socket < BasicSocket; end
  275. Socket::Constants = 1
  276. assert_raise(TypeError) do
  277. require 'socket'
  278. end
  279. INPUT
  280. end
  281. def test_load
  282. Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|
  283. t.puts "module Foo; end"
  284. t.puts "at_exit { p :wrap_end }"
  285. t.puts "at_exit { raise 'error in at_exit test' }"
  286. t.puts "p :ok"
  287. t.close
  288. assert_in_out_err([], <<-INPUT, %w(:ok :end :wrap_end), /error in at_exit test/)
  289. load(#{ t.path.dump }, true)
  290. GC.start
  291. p :end
  292. INPUT
  293. assert_raise(ArgumentError) { at_exit }
  294. }
  295. end
  296. def test_require_in_wrapped_load
  297. Dir.mktmpdir do |tmp|
  298. File.write("#{tmp}/1.rb", "require_relative '2'\n")
  299. File.write("#{tmp}/2.rb", "class Foo\n""end\n")
  300. assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
  301. path = ""#{tmp.dump}"/1.rb"
  302. begin;
  303. load path, true
  304. assert_instance_of(Class, Foo)
  305. end;
  306. end
  307. end
  308. def test_load_scope
  309. bug1982 = '[ruby-core:25039] [Bug #1982]'
  310. Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|
  311. t.puts "Hello = 'hello'"
  312. t.puts "class Foo"
  313. t.puts " p Hello"
  314. t.puts "end"
  315. t.close
  316. assert_in_out_err([], <<-INPUT, %w("hello"), [], bug1982)
  317. load(#{ t.path.dump }, true)
  318. INPUT
  319. }
  320. end
  321. def test_load_ospath
  322. bug = '[ruby-list:49994] path in ospath'
  323. base = "test_load\u{3042 3044 3046 3048 304a}".encode(Encoding::Windows_31J)
  324. path = nil
  325. Tempfile.create([base, ".rb"]) do |t|
  326. path = t.path
  327. assert_raise_with_message(LoadError, /#{base}/) {
  328. load(File.join(File.dirname(path), base))
  329. }
  330. t.puts "warn 'ok'"
  331. t.close
  332. assert_include(path, base)
  333. assert_warn("ok\n", bug) {
  334. assert_nothing_raised(LoadError, bug) {
  335. load(path)
  336. }
  337. }
  338. end
  339. end
  340. def test_relative
  341. load_path = $:.dup
  342. loaded_featrures = $LOADED_FEATURES.dup
  343. $:.delete(".")
  344. Dir.mktmpdir do |tmp|
  345. Dir.chdir(tmp) do
  346. Dir.mkdir('x')
  347. File.open('x/t.rb', 'wb') {}
  348. File.open('x/a.rb', 'wb') {|f| f.puts("require_relative('t.rb')")}
  349. assert require('./x/t.rb')
  350. assert !require(File.expand_path('x/t.rb'))
  351. assert_nothing_raised(LoadError) {require('./x/a.rb')}
  352. assert_raise(LoadError) {require('x/t.rb')}
  353. File.unlink(*Dir.glob('x/*'))
  354. Dir.rmdir("#{tmp}/x")
  355. $:.replace(load_path)
  356. load_path = nil
  357. assert(!require('tmpdir'))
  358. end
  359. end
  360. ensure
  361. $:.replace(load_path) if load_path
  362. $LOADED_FEATURES.replace loaded_featrures
  363. end
  364. def test_relative_symlink
  365. Dir.mktmpdir {|tmp|
  366. Dir.chdir(tmp) {
  367. Dir.mkdir "a"
  368. Dir.mkdir "b"
  369. File.open("a/lib.rb", "w") {|f| f.puts 'puts "a/lib.rb"' }
  370. File.open("b/lib.rb", "w") {|f| f.puts 'puts "b/lib.rb"' }
  371. File.open("a/tst.rb", "w") {|f| f.puts 'require_relative "lib"' }
  372. begin
  373. File.symlink("../a/tst.rb", "b/tst.rb")
  374. result = IO.popen([EnvUtil.rubybin, "b/tst.rb"], &:read)
  375. assert_equal("a/lib.rb\n", result, "[ruby-dev:40040]")
  376. rescue NotImplementedError, Errno::EACCES
  377. skip "File.symlink is not implemented"
  378. end
  379. }
  380. }
  381. end
  382. def test_frozen_loaded_features
  383. bug3756 = '[ruby-core:31913]'
  384. assert_in_out_err(['-e', '$LOADED_FEATURES.freeze; require "ostruct"'], "",
  385. [], /\$LOADED_FEATURES is frozen; cannot append feature \(RuntimeError\)$/,
  386. bug3756)
  387. end
  388. def test_race_exception
  389. bug5754 = '[ruby-core:41618]'
  390. path = nil
  391. stderr = $stderr
  392. verbose = $VERBOSE
  393. Tempfile.create(%w"bug5754 .rb") {|tmp|
  394. path = tmp.path
  395. tmp.print "#{<<~"begin;"}\n#{<<~"end;"}"
  396. begin;
  397. th = Thread.current
  398. t = th[:t]
  399. scratch = th[:scratch]
  400. if scratch.empty?
  401. scratch << :pre
  402. Thread.pass until t.stop?
  403. raise RuntimeError
  404. else
  405. scratch << :post
  406. end
  407. end;
  408. tmp.close
  409. class << (output = "")
  410. alias write concat
  411. end
  412. $stderr = output
  413. start = false
  414. scratch = []
  415. t1_res = nil
  416. t2_res = nil
  417. t1 = Thread.new do
  418. Thread.pass until start
  419. begin
  420. Kernel.send(:require, path)
  421. rescue RuntimeError
  422. end
  423. t1_res = require(path)
  424. end
  425. t2 = Thread.new do
  426. Thread.pass until scratch[0]
  427. t2_res = Kernel.send(:require, path)
  428. end
  429. t1[:scratch] = t2[:scratch] = scratch
  430. t1[:t] = t2
  431. t2[:t] = t1
  432. $VERBOSE = true
  433. start = true
  434. assert_nothing_raised(ThreadError, bug5754) {t1.join}
  435. assert_nothing_raised(ThreadError, bug5754) {t2.join}
  436. $VERBOSE = false
  437. assert_equal(true, (t1_res ^ t2_res), bug5754 + " t1:#{t1_res} t2:#{t2_res}")
  438. assert_equal([:pre, :post], scratch, bug5754)
  439. assert_match(/circular require/, output)
  440. assert_match(/in #{__method__}'$/o, output)
  441. }
  442. ensure
  443. $VERBOSE = verbose
  444. $stderr = stderr
  445. $".delete(path)
  446. end
  447. def test_loaded_features_encoding
  448. bug6377 = '[ruby-core:44750]'
  449. loadpath = $:.dup
  450. features = $".dup
  451. $".clear
  452. $:.clear
  453. Dir.mktmpdir {|tmp|
  454. $: << tmp
  455. open(File.join(tmp, "foo.rb"), "w") {}
  456. require "foo"
  457. assert_send([Encoding, :compatible?, tmp, $"[0]], bug6377)
  458. }
  459. ensure
  460. $:.replace(loadpath)
  461. $".replace(features)
  462. end
  463. def test_require_changed_current_dir
  464. bug7158 = '[ruby-core:47970]'
  465. Dir.mktmpdir {|tmp|
  466. Dir.chdir(tmp) {
  467. Dir.mkdir("a")
  468. Dir.mkdir("b")
  469. open(File.join("a", "foo.rb"), "w") {}
  470. open(File.join("b", "bar.rb"), "w") {|f|
  471. f.puts "p :ok"
  472. }
  473. assert_in_out_err([], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7158)
  474. begin;
  475. $:.replace([IO::NULL])
  476. $: << "."
  477. Dir.chdir("a")
  478. require "foo"
  479. Dir.chdir("../b")
  480. p :ng unless require "bar"
  481. Dir.chdir("..")
  482. p :ng if require "b/bar"
  483. end;
  484. }
  485. }
  486. end
  487. def test_require_not_modified_load_path
  488. bug7158 = '[ruby-core:47970]'
  489. Dir.mktmpdir {|tmp|
  490. Dir.chdir(tmp) {
  491. open("foo.rb", "w") {}
  492. assert_in_out_err([], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7158)
  493. begin;
  494. $:.replace([IO::NULL])
  495. a = Object.new
  496. def a.to_str
  497. "#{tmp}"
  498. end
  499. $: << a
  500. require "foo"
  501. last_path = $:.pop
  502. p :ok if last_path == a && last_path.class == Object
  503. end;
  504. }
  505. }
  506. end
  507. def test_require_changed_home
  508. bug7158 = '[ruby-core:47970]'
  509. Dir.mktmpdir {|tmp|
  510. Dir.chdir(tmp) {
  511. open("foo.rb", "w") {}
  512. Dir.mkdir("a")
  513. open(File.join("a", "bar.rb"), "w") {}
  514. assert_in_out_err([], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7158)
  515. begin;
  516. $:.replace([IO::NULL])
  517. $: << '~'
  518. ENV['HOME'] = "#{tmp}"
  519. require "foo"
  520. ENV['HOME'] = "#{tmp}/a"
  521. p :ok if require "bar"
  522. end;
  523. }
  524. }
  525. end
  526. def test_require_to_path_redefined_in_load_path
  527. bug7158 = '[ruby-core:47970]'
  528. Dir.mktmpdir {|tmp|
  529. Dir.chdir(tmp) {
  530. open("foo.rb", "w") {}
  531. assert_in_out_err([{"RUBYOPT"=>nil}, '--disable-gems'], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7158)
  532. begin;
  533. $:.replace([IO::NULL])
  534. a = Object.new
  535. def a.to_path
  536. "bar"
  537. end
  538. $: << a
  539. begin
  540. require "foo"
  541. p [:ng, $LOAD_PATH, ENV['RUBYLIB']]
  542. rescue LoadError => e
  543. raise unless e.path == "foo"
  544. end
  545. def a.to_path
  546. "#{tmp}"
  547. end
  548. p :ok if require "foo"
  549. end;
  550. }
  551. }
  552. end
  553. def test_require_to_str_redefined_in_load_path
  554. bug7158 = '[ruby-core:47970]'
  555. Dir.mktmpdir {|tmp|
  556. Dir.chdir(tmp) {
  557. open("foo.rb", "w") {}
  558. assert_in_out_err([{"RUBYOPT"=>nil}, '--disable-gems'], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7158)
  559. begin;
  560. $:.replace([IO::NULL])
  561. a = Object.new
  562. def a.to_str
  563. "foo"
  564. end
  565. $: << a
  566. begin
  567. require "foo"
  568. p [:ng, $LOAD_PATH, ENV['RUBYLIB']]
  569. rescue LoadError => e
  570. raise unless e.path == "foo"
  571. end
  572. def a.to_str
  573. "#{tmp}"
  574. end
  575. p :ok if require "foo"
  576. end;
  577. }
  578. }
  579. end
  580. def assert_require_with_shared_array_modified(add, del)
  581. bug7383 = '[ruby-core:49518]'
  582. Dir.mktmpdir {|tmp|
  583. Dir.chdir(tmp) {
  584. open("foo.rb", "w") {}
  585. Dir.mkdir("a")
  586. open(File.join("a", "bar.rb"), "w") {}
  587. assert_in_out_err(['--disable-gems'], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7383)
  588. begin;
  589. $:.replace([IO::NULL])
  590. $:.#{add} "#{tmp}"
  591. $:.#{add} "#{tmp}/a"
  592. require "foo"
  593. $:.#{del}
  594. # Expanded load path cache should be rebuilt.
  595. begin
  596. require "bar"
  597. rescue LoadError => e
  598. if e.path == "bar"
  599. p :ok
  600. else
  601. raise
  602. end
  603. end
  604. end;
  605. }
  606. }
  607. end
  608. def test_require_with_array_pop
  609. assert_require_with_shared_array_modified("push", "pop")
  610. end
  611. def test_require_with_array_shift
  612. assert_require_with_shared_array_modified("unshift", "shift")
  613. end
  614. def test_require_local_var_on_toplevel
  615. bug7536 = '[ruby-core:50701]'
  616. Dir.mktmpdir {|tmp|
  617. Dir.chdir(tmp) {
  618. open("bar.rb", "w") {|f| f.puts 'TOPLEVEL_BINDING.eval("lib = 2")' }
  619. assert_in_out_err(%w[-r./bar.rb], "#{<<~"begin;"}\n#{<<~"end;"}", %w([:lib] 2), [], bug7536)
  620. begin;
  621. puts TOPLEVEL_BINDING.eval("local_variables").inspect
  622. puts TOPLEVEL_BINDING.eval("lib").inspect
  623. end;
  624. }
  625. }
  626. end
  627. def test_require_with_loaded_features_pop
  628. bug7530 = '[ruby-core:50645]'
  629. Tempfile.create(%w'bug-7530- .rb') {|script|
  630. script.close
  631. assert_in_out_err([{"RUBYOPT" => nil}, "-", script.path], "#{<<~"begin;"}\n#{<<~"end;"}", %w(:ok), [], bug7530, timeout: 60)
  632. begin;
  633. PATH = ARGV.shift
  634. THREADS = 4
  635. ITERATIONS_PER_THREAD = 1000
  636. THREADS.times.map {
  637. Thread.new do
  638. ITERATIONS_PER_THREAD.times do
  639. require PATH
  640. $".delete_if {|p| Regexp.new(PATH) =~ p}
  641. end
  642. end
  643. }.each(&:join)
  644. p :ok
  645. end;
  646. }
  647. end
  648. def test_loading_fifo_threading_raise
  649. Tempfile.create(%w'fifo .rb') {|f|
  650. f.close
  651. File.unlink(f.path)
  652. File.mkfifo(f.path)
  653. assert_separately(["-", f.path], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 3)
  654. begin;
  655. th = Thread.current
  656. Thread.start {begin sleep(0.001) end until th.stop?; th.raise(IOError)}
  657. assert_raise(IOError) do
  658. load(ARGV[0])
  659. end
  660. end;
  661. }
  662. end if File.respond_to?(:mkfifo)
  663. def test_loading_fifo_threading_success
  664. Tempfile.create(%w'fifo .rb') {|f|
  665. f.close
  666. File.unlink(f.path)
  667. File.mkfifo(f.path)
  668. assert_separately(["-", f.path], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 3)
  669. begin;
  670. path = ARGV[0]
  671. th = Thread.current
  672. $ok = false
  673. Thread.start {
  674. begin
  675. sleep(0.001)
  676. end until th.stop?
  677. open(path, File::WRONLY | File::NONBLOCK) {|fifo_w|
  678. fifo_w.print "$ok = true\n__END__\n" # ensure finishing
  679. }
  680. }
  681. load(path)
  682. assert($ok)
  683. end;
  684. }
  685. end if File.respond_to?(:mkfifo)
  686. def test_loading_fifo_fd_leak
  687. skip if RUBY_PLATFORM =~ /android/ # https://rubyci.org/logs/rubyci.s3.amazonaws.com/android29-x86_64/ruby-master/log/20200419T124100Z.fail.html.gz
  688. Tempfile.create(%w'fifo .rb') {|f|
  689. f.close
  690. File.unlink(f.path)
  691. File.mkfifo(f.path)
  692. assert_separately(["-", f.path], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 3)
  693. begin;
  694. Process.setrlimit(Process::RLIMIT_NOFILE, 50)
  695. th = Thread.current
  696. 100.times do |i|
  697. Thread.start {begin sleep(0.001) end until th.stop?; th.raise(IOError)}
  698. assert_raise(IOError, "\#{i} time") do
  699. begin
  700. tap {tap {tap {load(ARGV[0])}}}
  701. rescue LoadError
  702. GC.start
  703. retry
  704. end
  705. end
  706. end
  707. end;
  708. }
  709. end if File.respond_to?(:mkfifo) and defined?(Process::RLIMIT_NOFILE)
  710. def test_throw_while_loading
  711. Tempfile.create(%w'bug-11404 .rb') do |f|
  712. f.puts 'sleep'
  713. f.close
  714. assert_separately(["-", f.path], "#{<<~"begin;"}\n#{<<~'end;'}")
  715. begin;
  716. path = ARGV[0]
  717. class Error < RuntimeError
  718. def exception(*)
  719. begin
  720. throw :blah
  721. rescue UncaughtThrowError
  722. end
  723. self
  724. end
  725. end
  726. assert_throw(:blah) do
  727. x = Thread.current
  728. Thread.start {
  729. sleep 0.00001
  730. x.raise Error.new
  731. }
  732. load path
  733. end
  734. end;
  735. end
  736. end
  737. def test_symlink_load_path
  738. Dir.mktmpdir {|tmp|
  739. Dir.mkdir(File.join(tmp, "real"))
  740. begin
  741. File.symlink "real", File.join(tmp, "symlink")
  742. rescue NotImplementedError, Errno::EACCES
  743. skip "File.symlink is not implemented"
  744. end
  745. File.write(File.join(tmp, "real/test_symlink_load_path.rb"), "print __FILE__")
  746. result = IO.popen([EnvUtil.rubybin, "-I#{tmp}/symlink", "-e", "require 'test_symlink_load_path.rb'"], &:read)
  747. assert_operator(result, :end_with?, "/real/test_symlink_load_path.rb")
  748. }
  749. end
  750. if defined?($LOAD_PATH.resolve_feature_path)
  751. def test_resolve_feature_path
  752. paths, loaded = $:.dup, $".dup
  753. Dir.mktmpdir do |tmp|
  754. Tempfile.create(%w[feature .rb], tmp) do |file|
  755. file.close
  756. path = File.realpath(file.path)
  757. dir, base = File.split(path)
  758. $:.unshift(dir)
  759. assert_equal([:rb, path], $LOAD_PATH.resolve_feature_path(base))
  760. $".push(path)
  761. assert_equal([:rb, path], $LOAD_PATH.resolve_feature_path(base))
  762. end
  763. end
  764. ensure
  765. $:.replace(paths)
  766. $".replace(loaded)
  767. end
  768. end
  769. end