Skip to content

Commit c6e9285

Browse files
eregonheadius
andcommitted
Split YAML load specs into safe and unsafe
* Based on #837 * Run both with YAML.load on Psych < 4, only safe on Psych >= 4 * Run both with YAML.unsafe_load (available in Psych >= 4) Co-authored-by: Charles Oliver Nutter <headius@headius.com>
1 parent 62eddf1 commit c6e9285

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

library/yaml/load_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
require_relative '../../spec_helper'
22
require_relative 'shared/load'
33

4-
ruby_version_is ""..."3.1" do
5-
describe "YAML.load" do
6-
it_behaves_like :yaml_load, :load
4+
describe "YAML.load" do
5+
it_behaves_like :yaml_load_safe, :load
6+
7+
guard -> { Psych::VERSION < "4.0.0" } do
8+
it_behaves_like :yaml_load_unsafe, :load
79
end
810
end

library/yaml/shared/load.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
require_relative '../fixtures/common'
22
require_relative '../fixtures/strings'
33

4-
describe :yaml_load, shared: true do
5-
after :each do
6-
rm_r $test_file
7-
end
8-
4+
describe :yaml_load_safe, shared: true do
95
it "returns a document from current io stream when io provided" do
106
File.open($test_file, 'w') do |io|
117
YAML.dump( ['badger', 'elephant', 'tiger'], io )
128
end
139
File.open($test_file) { |yf| YAML.send(@method, yf ) }.should == ['badger', 'elephant', 'tiger']
10+
ensure
11+
rm_r $test_file
1412
end
1513

1614
it "loads strings" do
@@ -90,6 +88,14 @@
9088
YAML.send(@method, block_seq).should == [[["one", "two", "three"]]]
9189
end
9290

91+
it "loads a symbol key that contains spaces" do
92+
string = ":user name: This is the user name."
93+
expected = { :"user name" => "This is the user name."}
94+
YAML.send(@method, string).should == expected
95+
end
96+
end
97+
98+
describe :yaml_load_unsafe, shared: true do
9399
it "works on complex keys" do
94100
require 'date'
95101
expected = {
@@ -101,12 +107,6 @@
101107
YAML.send(@method, $complex_key_1).should == expected
102108
end
103109

104-
it "loads a symbol key that contains spaces" do
105-
string = ":user name: This is the user name."
106-
expected = { :"user name" => "This is the user name."}
107-
YAML.send(@method, string).should == expected
108-
end
109-
110110
describe "with iso8601 timestamp" do
111111
it "computes the microseconds" do
112112
[ [YAML.send(@method, "2011-03-22t23:32:11.2233+01:00"), 223300],

library/yaml/unsafe_load_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
require_relative '../../spec_helper'
22
require_relative 'shared/load'
33

4-
ruby_version_is "3.1" do
4+
guard -> { Psych::VERSION >= "4.0.0" } do
55
describe "YAML.unsafe_load" do
6-
it_behaves_like :yaml_load, :unsafe_load
6+
it_behaves_like :yaml_load_safe, :unsafe_load
7+
it_behaves_like :yaml_load_unsafe, :unsafe_load
78
end
89
end

0 commit comments

Comments
 (0)