/
yaml_test.rb
119 lines (103 loc) · 3.47 KB
/
yaml_test.rb
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
# 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
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_yaml_tags
error = assert_raises Bootsnap::CompileCache::Uncompilable do
::Bootsnap::CompileCache::YAML.strict_load('!many Boolean')
end
assert_equal "YAML tags are not supported: !many", error.message
error = assert_raises Bootsnap::CompileCache::Uncompilable do
::Bootsnap::CompileCache::YAML.strict_load('!ruby/object {}')
end
assert_equal "YAML tags are not supported: !ruby/object", error.message
end
if YAML::VERSION >= '4'
def test_load_psych_4
# Until we figure out a proper strategy, only `YAML.unsafe_load_file`
# is cached with Psych >= 4
Help.set_file('a.yml', "foo: &foo\n bar: 42\nplop:\n <<: *foo", 100)
assert_raises FakeYaml::Fallback do
FakeYaml.load_file('a.yml')
end
end
else
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
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
end
end