|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require("test_helper") |
| 4 | + |
| 5 | +module Bootsnap |
| 6 | + class KernelTest < Minitest::Test |
| 7 | + include TmpdirHelper |
| 8 | + |
| 9 | + def test_require_symlinked_file_twice |
| 10 | + setup_symlinked_files |
| 11 | + if RUBY_VERSION >= "3.1" |
| 12 | + # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1) |
| 13 | + assert_both_pass(<<~RUBY) |
| 14 | + require "b/test.rb" |
| 15 | + require "a/test.rb" |
| 16 | + RUBY |
| 17 | + else |
| 18 | + assert_both_pass(<<~RUBY) |
| 19 | + require "b/test.rb" |
| 20 | + begin |
| 21 | + require "a/test.rb" |
| 22 | + rescue RuntimeError |
| 23 | + exit 0 |
| 24 | + else |
| 25 | + exit 1 |
| 26 | + end |
| 27 | + RUBY |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def test_require_symlinked_file_twice_aliased |
| 32 | + setup_symlinked_files |
| 33 | + assert_both_pass(<<~RUBY) |
| 34 | + $LOAD_PATH.unshift(File.expand_path("b")) |
| 35 | + require "test.rb" |
| 36 | +
|
| 37 | + $LOAD_PATH.unshift(File.expand_path("a")) |
| 38 | + require "test.rb" |
| 39 | + RUBY |
| 40 | + end |
| 41 | + |
| 42 | + def test_require_relative_symlinked_file_twice |
| 43 | + setup_symlinked_files |
| 44 | + if RUBY_VERSION >= "3.1" |
| 45 | + # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1) |
| 46 | + assert_both_pass(<<~RUBY) |
| 47 | + require_relative "b/test.rb" |
| 48 | + require_relative "a/test.rb" |
| 49 | + RUBY |
| 50 | + else |
| 51 | + assert_both_pass(<<~RUBY) |
| 52 | + require_relative "b/test.rb" |
| 53 | + begin |
| 54 | + require_relative "a/test.rb" |
| 55 | + rescue RuntimeError |
| 56 | + exit 0 |
| 57 | + else |
| 58 | + exit 1 |
| 59 | + end |
| 60 | + RUBY |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def test_require_and_then_require_relative_symlinked_file |
| 65 | + setup_symlinked_files |
| 66 | + assert_both_pass(<<~RUBY) |
| 67 | + $LOAD_PATH.unshift(File.expand_path("b")) |
| 68 | + require "test" |
| 69 | +
|
| 70 | + require_relative "a/test.rb" |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + def test_require_relative_and_then_require_symlinked_file |
| 75 | + setup_symlinked_files |
| 76 | + assert_both_pass(<<~RUBY) |
| 77 | + require_relative "a/test.rb" |
| 78 | +
|
| 79 | + $LOAD_PATH.unshift(File.expand_path("b")) |
| 80 | + require "test" |
| 81 | + RUBY |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def assert_both_pass(source) |
| 87 | + Help.set_file("without_bootsnap.rb", source) |
| 88 | + unless execute("without_bootsnap.rb", "debug.txt") |
| 89 | + flunk "expected snippet to pass WITHOUT bootsnap enabled:\n#{debug_output}" |
| 90 | + end |
| 91 | + |
| 92 | + Help.set_file("with_bootsnap.rb", %{require "bootsnap/setup"\n#{source}}) |
| 93 | + unless execute("with_bootsnap.rb", "debug.txt") |
| 94 | + flunk "expected snippet to pass WITH bootsnap enabled:\n#{debug_output}" |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + def debug_output |
| 99 | + File.read("debug.txt") |
| 100 | + rescue Errno::ENOENT |
| 101 | + end |
| 102 | + |
| 103 | + def execute(script_path, output_path) |
| 104 | + system( |
| 105 | + {"BOOTSNAP_CACHE_DIR" => "tmp/cache"}, |
| 106 | + RbConfig.ruby, "-I.", script_path, |
| 107 | + out: output_path, err: output_path |
| 108 | + ) |
| 109 | + end |
| 110 | + |
| 111 | + def assert_successful(source) |
| 112 | + Help.set_file("test_case.rb", source) |
| 113 | + |
| 114 | + Help.set_file("test_case.rb", %{require "bootsnap/setup"\n#{source}}) |
| 115 | + assert system({"BOOTSNAP_CACHE_DIR" => "tmp/cache"}, RbConfig.ruby, "-I.", "test_case.rb") |
| 116 | + end |
| 117 | + |
| 118 | + def setup_symlinked_files |
| 119 | + skip("Platform doesn't support symlinks") unless File.respond_to?(:symlink) |
| 120 | + |
| 121 | + Help.set_file("a/test.rb", <<-RUBY) |
| 122 | + if $already_required |
| 123 | + raise "required more than once" |
| 124 | + else |
| 125 | + $already_required = true |
| 126 | + end |
| 127 | + RUBY |
| 128 | + File.symlink("a", "b") |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments