File: yaml_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 (278 lines) | stat: -rw-r--r-- 7,879 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# frozen_string_literal: true

require "test_helper"

class CompileCacheYAMLTest < Minitest::Test
  include TmpdirHelper

  module FakeYaml
    Fallback = Class.new(StandardError)
    class << self
      def load_file(_path, symbolize_names: false, freeze: false, fallback: nil)
        raise Fallback
      end

      def unsafe_load_file(_path, symbolize_names: false, freeze: false, fallback: nil)
        raise Fallback
      end
    end
  end

  def setup
    skip("Unsupported platform") unless Bootsnap::CompileCache.supported?
    super
    Bootsnap::CompileCache::YAML.init!
    FakeYaml.singleton_class.prepend(Bootsnap::CompileCache::YAML.patch)
  end

  def test_yaml_strict_load
    document = ::Bootsnap::CompileCache::YAML.strict_load(<<~YAML)
      ---
      :foo: 42
      bar: [1]
    YAML
    expected = {
      foo: 42,
      "bar" => [1],
    }
    assert_equal expected, document
  end

  def test_strict_load_reject_dates
    error = assert_raises Psych::DisallowedClass do
      ::Bootsnap::CompileCache::YAML.strict_load(<<~YAML)
        ---
        :foo: 2023-01-20
      YAML
    end
    assert_includes error.message, "Date"
  end

  def test_strict_load_reject_times
    error = assert_raises Psych::DisallowedClass do
      ::Bootsnap::CompileCache::YAML.strict_load(<<~YAML)
        ---
        :foo: 2023-01-20 13:18:31.083375000 -05:00
      YAML
    end
    assert_includes error.message, "Time"
  end

  def test_strict_load_reject_aliases
    assert_raises Psych::BadAlias do
      ::Bootsnap::CompileCache::YAML.strict_load(<<~YAML)
        ---
        foo: &foo
          a: b
        bar:
          <<: *foo
      YAML
    end
  end

  def test_yaml_tags
    error = assert_raises Bootsnap::CompileCache::YAML::UnsupportedTags do
      ::Bootsnap::CompileCache::YAML.strict_load("!many Boolean")
    end
    assert_equal "YAML tags are not supported: !many", error.message

    error = assert_raises Bootsnap::CompileCache::YAML::UnsupportedTags do
      ::Bootsnap::CompileCache::YAML.strict_load("!ruby/object {}")
    end
    assert_equal "YAML tags are not supported: !ruby/object", error.message
  end

  def test_symbols_encoding
    symbols = [:ascii, :utf8_fée]
    Help.set_file("a.yml", YAML.dump(symbols), 100)

    loaded_symbols = FakeYaml.load_file("a.yml")
    assert_equal(symbols, loaded_symbols)
    assert_equal(symbols.map(&:encoding), loaded_symbols.map(&:encoding))
  end

  def test_custom_symbols_encoding
    sym = "壁に耳あり、障子に目あり".to_sym # rubocop:disable Lint/SymbolConversion
    Help.set_file("a.yml", YAML.dump(sym), 100)
    # YAML is limited to UTF-8 and UTF-16 by spec, but Psych does respect Encoding.default_internal
    # so strings and symbol can actually be of any encoding.
    assert_raises FakeYaml::Fallback do
      with_default_encoding_internal(Encoding::EUC_JP) do
        FakeYaml.load_file("a.yml")
      end
    end
  end

  if YAML::VERSION >= "4"
    def test_load_psych_4_with_alias
      Help.set_file("a.yml", "foo: &foo\n  bar: 42\nplop:\n  <<: *foo", 100)

      foo = {"bar" => 42}
      expected = {"foo" => foo, "plop" => foo}
      assert_equal(expected, FakeYaml.unsafe_load_file("a.yml"))

      assert_raises Psych::BadAlias do
        FakeYaml.load_file("a.yml")
      end
    end

    def test_load_psych_4_with_unsafe_class
      Help.set_file("a.yml", "---\nfoo: !ruby/regexp /bar/\n", 100)

      expected = {"foo" => /bar/}
      assert_equal(expected, FakeYaml.unsafe_load_file("a.yml"))

      assert_raises Psych::DisallowedClass do
        FakeYaml.load_file("a.yml")
      end
    end

    def test_yaml_input_to_output_safe
      document = ::Bootsnap::CompileCache::YAML::Psych4::SafeLoad.input_to_output(<<~YAML, {})
        ---
        :foo: 42
        bar: [1]
      YAML
      expected = {
        foo: 42,
        "bar" => [1],
      }
      assert_equal expected, document
    end

    def test_yaml_input_to_output_unsafe
      document = ::Bootsnap::CompileCache::YAML::Psych4::UnsafeLoad.input_to_output(<<~YAML, {})
        ---
        :foo: 42
        bar: [1]
      YAML
      expected = {
        foo: 42,
        "bar" => [1],
      }
      assert_equal expected, document
    end
  else
    def test_yaml_input_to_output
      document = ::Bootsnap::CompileCache::YAML::Psych3.input_to_output(<<~YAML, {})
        ---
        :foo: 42
        bar: [1]
      YAML
      expected = {
        foo: 42,
        "bar" => [1],
      }
      assert_equal expected, document
    end

    def test_load_file
      Help.set_file("a.yml", "---\nfoo: bar", 100)
      assert_equal({"foo" => "bar"}, FakeYaml.load_file("a.yml"))
    end

    def test_load_file_aliases
      Help.set_file("a.yml", "foo: &foo\n  bar: 42\nplop:\n  <<: *foo", 100)
      assert_equal({"foo" => {"bar" => 42}, "plop" => {"bar" => 42}}, FakeYaml.load_file("a.yml"))
    end

    def test_load_file_symbolize_names
      Help.set_file("a.yml", "---\nfoo: bar", 100)
      FakeYaml.load_file("a.yml")

      if ::Bootsnap::CompileCache::YAML.supported_options.include?(:symbolize_names)
        2.times do
          assert_equal({foo: "bar"}, FakeYaml.load_file("a.yml", symbolize_names: true))
        end
      else
        assert_raises(FakeYaml::Fallback) do # would call super
          FakeYaml.load_file("a.yml", symbolize_names: true)
        end
      end
    end

    def test_load_file_freeze
      Help.set_file("a.yml", "---\nfoo", 100)
      FakeYaml.load_file("a.yml")

      if ::Bootsnap::CompileCache::YAML.supported_options.include?(:freeze)
        2.times do
          string = FakeYaml.load_file("a.yml", freeze: true)
          assert_equal("foo", string)
          assert_predicate(string, :frozen?)
        end
      else
        assert_raises(FakeYaml::Fallback) do # would call super
          FakeYaml.load_file("a.yml", freeze: true)
        end
      end
    end

    def test_load_file_unknown_option
      Help.set_file("a.yml", "---\nfoo", 100)
      FakeYaml.load_file("a.yml")

      assert_raises(FakeYaml::Fallback) do # would call super
        FakeYaml.load_file("a.yml", fallback: true)
      end
    end
  end

  def test_precompile_regexp
    Help.set_file("a.yml", ::YAML.dump(foo: /bar/), 100)
    assert Bootsnap::CompileCache::YAML.precompile("a.yml")
  end

  def test_precompile_date
    Help.set_file("a.yml", ::YAML.dump(Date.today), 100)
    assert Bootsnap::CompileCache::YAML.precompile("a.yml")
  end

  def test_precompile_object
    Help.set_file("a.yml", ::YAML.dump(Object.new), 100)
    refute Bootsnap::CompileCache::YAML.precompile("a.yml")
  end

  if YAML.respond_to?(:unsafe_load_file)
    def test_unsafe_load_file
      Help.set_file("a.yml", "foo: &foo\n  bar: 42\nplop:\n  <<: *foo", 100)
      assert_equal({"foo" => {"bar" => 42}, "plop" => {"bar" => 42}}, FakeYaml.unsafe_load_file("a.yml"))
    end

    def test_unsafe_load_file_supports_regexp
      Help.set_file("a.yml", ::YAML.dump(foo: /bar/), 100)
      assert_equal({foo: /bar/}, FakeYaml.unsafe_load_file("a.yml"))
    end
  end

  def test_no_read_permission
    if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ || Process.uid.zero?
      # On windows removing read permission doesn't prevent reading.
      pass
    else
      file = "a.yml"
      Help.set_file(file, ::YAML.dump(Object.new), 100)
      FileUtils.chmod(0o000, file)
      exception = assert_raises(Errno::EACCES) do
        FakeYaml.load_file(file)
      end
      assert_match(file, exception.message)
    end
  end

  private

  def with_default_encoding_internal(encoding)
    original_internal = Encoding.default_internal
    $VERBOSE = false
    Encoding.default_internal = encoding
    $VERBOSE = true
    begin
      yield
    ensure
      $VERBOSE = false
      Encoding.default_internal = original_internal
      $VERBOSE = true
    end
  end
end