diff options
author | Asutosh Palai <asupalai@gmail.com> | 2016-06-09 13:47:02 +0530 |
---|---|---|
committer | Asutosh Palai <asupalai@gmail.com> | 2016-06-09 13:47:16 +0530 |
commit | 8e6aab8395d0a6855886cbda8833a7f01f980298 (patch) | |
tree | dd88ee577f2b31484147008398f61d68edbe32f3 | |
parent | 3ddce71274da2ec529685635d90cc7a208b840c9 (diff) | |
download | bundler-8e6aab8395d0a6855886cbda8833a7f01f980298.tar.gz |
Made yaml_serializer compatible for use with settings
-rw-r--r-- | lib/bundler/yaml_serializer.rb | 25 | ||||
-rw-r--r-- | spec/bundler/yaml_serializer_spec.rb | 34 |
2 files changed, 45 insertions, 14 deletions
diff --git a/lib/bundler/yaml_serializer.rb b/lib/bundler/yaml_serializer.rb index e64fe623c8..327baa4ee7 100644 --- a/lib/bundler/yaml_serializer.rb +++ b/lib/bundler/yaml_serializer.rb @@ -23,10 +23,24 @@ module Bundler yaml end + SCAN_REGEX = / + ^ + ([ ]*) # indentations + (.*) # key + (?::(?=\s)) # : (without the lookahead the #key includes this when : is present in value) + [ ]? + (?: !\s)? # optional exclamation mark found with ruby 1.9.3 + (['"]?) # optional opening quote + (.*) # value + \3 # matching closing quote + $ + /xo + def load(str) res = {} stack = [res] - str.scan(/^( *)(.*):\s?(["']?)([^"'\n]*)\3\n/).each do |(indent, key, _, val)| + str.scan(SCAN_REGEX).each do |(indent, key, _, val)| + key = convert_to_backward_compatible_key(key) depth = indent.scan(/ /).length if val.empty? new_hash = {} @@ -39,8 +53,15 @@ module Bundler res end + # for settings' keys + def convert_to_backward_compatible_key(key) + key = "#{key}/" if key =~ /https?:/i && key !~ %r{/\Z} + key = key.gsub(".", "__") if key.include?(".") + key + end + class << self - private :dump_hash + private :dump_hash, :convert_to_backward_compatible_key end end end diff --git a/spec/bundler/yaml_serializer_spec.rb b/spec/bundler/yaml_serializer_spec.rb index c15c67552b..59906deb81 100644 --- a/spec/bundler/yaml_serializer_spec.rb +++ b/spec/bundler/yaml_serializer_spec.rb @@ -3,10 +3,12 @@ require "spec_helper" require "bundler/yaml_serializer" describe Bundler::YAMLSerializer do + subject(:serializer) { Bundler::YAMLSerializer } + describe "#dump" do it "works for simple hash" do - hash = {"Q" => "Where does Thursday come before Wednesday?", - "Ans" => "In the dictionary. :P"} + hash = { "Q" => "Where does Thursday come before Wednesday?", + "Ans" => "In the dictionary. :P" } expected = strip_whitespace <<-YAML --- @@ -14,16 +16,16 @@ describe Bundler::YAMLSerializer do Ans: "In the dictionary. :P" YAML - expect(subject.dump(hash)).to eq(expected) + expect(serializer.dump(hash)).to eq(expected) end it "handles nested hash" do hash = { "a_joke" => { "my-stand" => "I can totally keep secrets", - "my-explanation" => "It's the people I tell them to that can't" + "my-explanation" => "It's the people I tell them to that can't", }, - "read_ahead" => "All generalizations are false, including this one" + "read_ahead" => "All generalizations are false, including this one", } expected = strip_whitespace <<-YAML @@ -34,7 +36,7 @@ describe Bundler::YAMLSerializer do read_ahead: "All generalizations are false, including this one" YAML - expect(subject.dump(hash)).to eq(expected) + expect(serializer.dump(hash)).to eq(expected) end end @@ -48,10 +50,10 @@ describe Bundler::YAMLSerializer do hash = { "Jon" => "Air is free dude!", - "Jack" => "Yes.. until you buy a bag of chips!" + "Jack" => "Yes.. until you buy a bag of chips!", } - expect(subject.load(yaml)).to eq(hash) + expect(serializer.load(yaml)).to eq(hash) end it "works for nested hash" do @@ -66,13 +68,21 @@ describe Bundler::YAMLSerializer do hash = { "baa" => { "baa" => "black sheep", - "have"=>"you any wool?", - "yes"=>"merry have I" + "have" => "you any wool?", + "yes" => "merry have I", }, - "three"=>"bags full" + "three" => "bags full", } - expect(subject.load(yaml)).to eq(hash) + expect(serializer.load(yaml)).to eq(hash) + end + + it "handles colon in key/value" do + yaml = strip_whitespace <<-YAML + BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG/: http://rubygems-mirror.org + YAML + + expect(serializer.load(yaml)).to eq("BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG/" => "http://rubygems-mirror.org") end end end |