diff --git a/lib/vagrant/util/shell_quote.rb b/lib/vagrant/util/shell_quote.rb new file mode 100644 index 000000000..f3dd39c46 --- /dev/null +++ b/lib/vagrant/util/shell_quote.rb @@ -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 diff --git a/test/unit/vagrant/util/shell_quote_test.rb b/test/unit/vagrant/util/shell_quote_test.rb new file mode 100644 index 000000000..eaea27765 --- /dev/null +++ b/test/unit/vagrant/util/shell_quote_test.rb @@ -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