Properly quote regular expression inputs to StringBlockEditor

This commit is contained in:
Mitchell Hashimoto 2013-03-14 12:40:19 -07:00
parent 64d1b54d51
commit 43f3764e5b
3 changed files with 17 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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