From 72398faeaf717894e2f35c41eed4fd22f4a7f279 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 13 Dec 2013 21:17:15 -0800 Subject: [PATCH] core: put shell quoting into its own util class, it is useful --- lib/vagrant/util/shell_quote.rb | 15 +++++++++++++++ test/unit/vagrant/util/shell_quote_test.rb | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 lib/vagrant/util/shell_quote.rb create mode 100644 test/unit/vagrant/util/shell_quote_test.rb 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