core: put shell quoting into its own util class, it is useful

This commit is contained in:
Mitchell Hashimoto 2013-12-13 21:17:15 -08:00
parent 902b769e6b
commit 72398faeaf
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,15 @@
module Vagrant
module Util
module ShellQuote
# This will auto-escape the text with the given quote mark type.
#
# @param [String] text Text to escape
# @param [String] quote The quote character, such as "
def self.escape(text, quote)
text.gsub(/#{quote}/) do |m|
"#{m}\\#{m}#{m}"
end
end
end
end
end

View File

@ -0,0 +1,12 @@
require File.expand_path("../../../base", __FILE__)
require "vagrant/util/shell_quote"
describe Vagrant::Util::ShellQuote do
subject { described_class }
it "quotes properly" do
expected = "foo '\\''bar'\\''"
expect(subject.escape("foo 'bar'", "'")).to eql(expected)
end
end