From 43f3764e5b6285f8292ce92912a1dbac8d805c1e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 14 Mar 2013 12:40:19 -0700 Subject: [PATCH] Properly quote regular expression inputs to StringBlockEditor --- CHANGELOG.md | 3 +++ lib/vagrant/util/string_block_editor.rb | 2 ++ .../vagrant/util/string_block_editor_test.rb | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c30120c..7583301a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## 1.1.1 (unreleased) +BUG FIXES: + - Quote keys to StringBlockEditor so keys with spaces, parens, and + so on work properly. ## 1.1.0 (March 14, 2013) diff --git a/lib/vagrant/util/string_block_editor.rb b/lib/vagrant/util/string_block_editor.rb index 34c456d20..23400b44f 100644 --- a/lib/vagrant/util/string_block_editor.rb +++ b/lib/vagrant/util/string_block_editor.rb @@ -44,12 +44,14 @@ module Vagrant # This deletes the block with the given key if it exists. def delete(key) + key = Regexp.quote(key) regexp = /^#\s*VAGRANT-BEGIN:\s*#{key}$.*^#\s*VAGRANT-END:\s*#{key}$\r?\n?/m @value.gsub!(regexp, "") end # This gets the value of the block with the given key. def get(key) + key = Regexp.quote(key) regexp = /^#\s*VAGRANT-BEGIN:\s*#{key}$\r?\n?(.*?)\r?\n?^#\s*VAGRANT-END:\s*#{key}$\r?\n?/m match = regexp.match(@value) return nil if !match diff --git a/test/unit/vagrant/util/string_block_editor_test.rb b/test/unit/vagrant/util/string_block_editor_test.rb index 255631b58..f7b1f9221 100644 --- a/test/unit/vagrant/util/string_block_editor_test.rb +++ b/test/unit/vagrant/util/string_block_editor_test.rb @@ -61,17 +61,25 @@ DATA # VAGRANT-BEGIN: bar content # VAGRANT-END: bar +# VAGRANT-BEGIN: /Users/studio/Projects (studio)/tubes/.vagrant/machines/web/vmware_fusion/vm.vmwarevm +complex +# VAGRANT-END: /Users/studio/Projects (studio)/tubes/.vagrant/machines/web/vmware_fusion/vm.vmwarevm DATA end + subject { described_class.new(data) } + it "should get the value" do - instance = described_class.new(data) - instance.get("bar").should == "content" + subject.get("bar").should == "content" end it "should get nil for nonexistent values" do - instance = described_class.new(data) - instance.get("baz").should be_nil + subject.get("baz").should be_nil + end + + it "should get complicated keys" do + result = subject.get("/Users/studio/Projects (studio)/tubes/.vagrant/machines/web/vmware_fusion/vm.vmwarevm") + result.should == "complex" end end