core: put shell quoting into its own util class, it is useful
This commit is contained in:
parent
902b769e6b
commit
72398faeaf
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue