-
Notifications
You must be signed in to change notification settings - Fork 579
[MODULES-2462] Improve parseyaml function #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,64 @@ | ||
require 'spec_helper' | ||
|
||
describe 'parsejson' do | ||
it { is_expected.not_to eq(nil) } | ||
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
it { is_expected.to run.with_params('["one"').and_raise_error(PSON::ParserError) } | ||
it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) } | ||
it 'should exist' do | ||
is_expected.not_to eq(nil) | ||
end | ||
|
||
it 'should raise an error if called without any arguments' do | ||
is_expected.to run.with_params(). | ||
and_raise_error(/wrong number of arguments/i) | ||
end | ||
|
||
context 'with correct JSON data' do | ||
|
||
it 'should be able to parse a JSON data with a Hash' do | ||
is_expected.to run.with_params('{"a":"1","b":"2"}'). | ||
and_return({'a'=>'1', 'b'=>'2'}) | ||
end | ||
|
||
it 'should be able to parse a JSON data with an Array' do | ||
is_expected.to run.with_params('["a","b","c"]'). | ||
and_return(['a', 'b', 'c']) | ||
end | ||
|
||
it 'should be able to parse empty JSON values' do | ||
is_expected.to run.with_params('[]'). | ||
and_return([]) | ||
is_expected.to run.with_params('{}'). | ||
and_return({}) | ||
end | ||
|
||
it 'should be able to parse a JSON data with a mixed structure' do | ||
is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}'). | ||
and_return({'a' =>'1', 'b' => 2, 'c' => { 'd' => [true, false] } }) | ||
end | ||
|
||
it 'should not return the default value if the data was parsed correctly' do | ||
is_expected.to run.with_params('{"a":"1"}', 'default_value'). | ||
and_return({'a' => '1'}) | ||
end | ||
|
||
end | ||
|
||
context 'with incorrect YAML data' do | ||
it 'should return "nil" if a default value should be returned but is not provided' do | ||
is_expected.to run.with_params(''). | ||
and_return(nil) | ||
end | ||
|
||
it 'should support a structure for a default value' do | ||
is_expected.to run.with_params('', {'a' => '1'}). | ||
and_return({'a' => '1'}) | ||
end | ||
|
||
['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value| | ||
it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do | ||
is_expected.to run.with_params(value, 'default_value'). | ||
and_return('default_value') | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
describe 'parseyaml' do | ||
it { is_expected.not_to eq(nil) } | ||
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) } | ||
context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do | ||
it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) } | ||
it 'should exist' do | ||
is_expected.not_to eq(nil) | ||
end | ||
context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do | ||
it { is_expected.to run.with_params('["one"').and_raise_error(ArgumentError) } | ||
|
||
it 'should raise an error if called without any arguments' do | ||
is_expected.to run.with_params(). | ||
and_raise_error(/wrong number of arguments/i) | ||
end | ||
|
||
context 'with correct YAML data' do | ||
it 'should be able to parse a YAML data with a String' do | ||
is_expected.to run.with_params('--- just a string'). | ||
and_return('just a string') | ||
is_expected.to run.with_params('just a string'). | ||
and_return('just a string') | ||
end | ||
|
||
it 'should be able to parse a YAML data with a Hash' do | ||
is_expected.to run.with_params("---\na: '1'\nb: '2'\n"). | ||
and_return({'a' => '1', 'b' => '2'}) | ||
end | ||
|
||
it 'should be able to parse a YAML data with an Array' do | ||
is_expected.to run.with_params("---\n- a\n- b\n- c\n"). | ||
and_return(['a', 'b', 'c']) | ||
end | ||
|
||
it 'should be able to parse a YAML data with a mixed structure' do | ||
is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n"). | ||
and_return({'a' => '1', 'b' => 2, 'c' => {'d' => [:a, true, false]}}) | ||
end | ||
|
||
it 'should not return the default value if the data was parsed correctly' do | ||
is_expected.to run.with_params("---\na: '1'\n", 'default_value'). | ||
and_return({'a' => '1'}) | ||
end | ||
|
||
end | ||
|
||
context 'with incorrect YAML data' do | ||
it 'should return "nil" if a default value should be returned but is not provided' do | ||
is_expected.to run.with_params(''). | ||
and_return(nil) | ||
end | ||
|
||
it 'should support a structure for a default value' do | ||
is_expected.to run.with_params('', {'a' => '1'}). | ||
and_return({'a' => '1'}) | ||
end | ||
|
||
[1, 1.2, nil, true, false, [], {}, :yaml].each do |value| | ||
it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do | ||
is_expected.to run.with_params(value, 'default_value'). | ||
and_return('default_value') | ||
end | ||
end | ||
|
||
context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do | ||
['---', '...', '*8', ''].each do |value| | ||
it "should return the default value for an incorrect #{value.inspect} string parameter" do | ||
is_expected.to run.with_params(value, 'default_value'). | ||
and_return('default_value') | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, er, I realize this is merged by now. But this is a problem.
Don't ever rescue Exception.
Thoughts? This seems to be a recurring issue in stdlib, yes? Should I open a ticket for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setup rubocop, profit :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both, please. weeps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mumbles something about modulesync