File: kernel_test.rb

package info (click to toggle)
ruby-bootsnap 1.18.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 524 kB
  • sloc: ruby: 3,718; ansic: 756; sh: 14; makefile: 9
file content (227 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# frozen_string_literal: true

require "test_helper"

module Bootsnap
  class KernelTest < Minitest::Test
    include LoadPathCacheHelper
    include TmpdirHelper

    def test_require_symlinked_file_twice
      skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

      setup_symlinked_files
      if RUBY_VERSION >= "3.1"
        # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
        assert_both_pass(<<~RUBY)
          require "symlink/test"
          require "real/test"
        RUBY
      else
        assert_both_pass(<<~RUBY)
          require "symlink/test"
          begin
            require "real/test"
          rescue RuntimeError
            exit 0
          else
            exit 1
          end
        RUBY
      end
    end

    def test_require_symlinked_file_twice_aliased
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "test"

        $LOAD_PATH.unshift(File.expand_path("a"))
        require "test"
      RUBY
    end

    def test_require_relative_symlinked_file_twice
      skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

      setup_symlinked_files
      if RUBY_VERSION >= "3.1"
        # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
        assert_both_pass(<<~RUBY)
          require_relative "symlink/test"
          require_relative "real/test"
        RUBY
      else
        assert_both_pass(<<~RUBY)
          require_relative "symlink/test"
          begin
            require_relative "real/test"
          rescue RuntimeError
            exit 0
          else
            exit 1
          end
        RUBY
      end
    end

    def test_require_and_then_require_relative_symlinked_file
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "test"

        require_relative "real/test"
      RUBY
    end

    def test_require_relative_and_then_require_symlinked_file
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        require_relative "real/test"

        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "test"
      RUBY
    end

    def test_require_deep_symlinked_file_twice
      skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

      setup_symlinked_files
      if RUBY_VERSION >= "3.1"
        # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
        assert_both_pass(<<~RUBY)
          require "symlink/dir/deep"
          require "real/dir/deep"
        RUBY
      else
        assert_both_pass(<<~RUBY)
          require "symlink/dir/deep"
          begin
            require "real/dir/deep"
          rescue RuntimeError
            exit 0
          else
            exit 1
          end
        RUBY
      end
    end

    def test_require_deep_symlinked_file_twice_aliased
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "dir/deep"

        $LOAD_PATH.unshift(File.expand_path("a"))
        require "dir/deep"
      RUBY
    end

    def test_require_relative_deep_symlinked_file_twice
      skip("https://github.com/oracle/truffleruby/issues/3138") if truffleruby?

      setup_symlinked_files
      if RUBY_VERSION >= "3.1"
        # Fixed in https://github.com/ruby/ruby/commit/79a4484a072e9769b603e7b4fbdb15b1d7eccb15 (Ruby 3.1)
        assert_both_pass(<<~RUBY)
          require_relative "symlink/dir/deep"
          require_relative "real/dir/deep"
        RUBY
      else
        assert_both_pass(<<~RUBY)
          require_relative "symlink/dir/deep"
          begin
            require_relative "real/dir/deep"
          rescue RuntimeError
            exit 0
          else
            exit 1
          end
        RUBY
      end
    end

    def test_require_and_then_require_relative_deep_symlinked_file
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "dir/deep"

        require_relative "real/dir/deep"
      RUBY
    end

    def test_require_relative_and_then_require_deep_symlinked_file
      setup_symlinked_files
      assert_both_pass(<<~RUBY)
        require_relative "real/dir/deep"

        $LOAD_PATH.unshift(File.expand_path("symlink"))
        require "dir/deep"
      RUBY
    end

    private

    def assert_both_pass(source)
      Help.set_file("without_bootsnap.rb", source)
      unless execute("without_bootsnap.rb", "debug.txt")
        flunk "expected snippet to pass WITHOUT bootsnap enabled:\n#{debug_output}"
      end

      Help.set_file("with_bootsnap.rb", %{require "bootsnap/setup"\n#{source}})
      unless execute("with_bootsnap.rb", "debug.txt")
        flunk "expected snippet to pass WITH bootsnap enabled:\n#{debug_output}"
      end
    end

    def debug_output
      File.read("debug.txt")
    rescue Errno::ENOENT
    end

    def execute(script_path, output_path)
      system(
        {"BOOTSNAP_CACHE_DIR" => "tmp/cache"},
        RbConfig.ruby, "-I.", script_path,
        out: output_path, err: output_path
      )
    end

    def assert_successful(source)
      Help.set_file("test_case.rb", source)

      Help.set_file("test_case.rb", %{require "bootsnap/setup"\n#{source}})
      assert system({"BOOTSNAP_CACHE_DIR" => "tmp/cache"}, RbConfig.ruby, "-Ilib:.", "test_case.rb")
    end

    def setup_symlinked_files
      skip("Platform doesn't support symlinks") unless File.respond_to?(:symlink)

      Help.set_file("real/test.rb", <<-RUBY)
        if $test_already_required
          raise "test.rb required more than once"
        else
          $test_already_required = true
        end
      RUBY

      Help.set_file("real/dir/deep.rb", <<-RUBY)
        if $deep_already_required
          raise "deep.rb required more than once"
        else
          $deep_already_required = true
        end
      RUBY
      File.symlink("real", "symlink")
    end

    def truffleruby?
      RUBY_ENGINE == "truffleruby"
    end
  end
end