From d1e78f791d4f2d5c3b011f38f0f8d68cb482f05f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Jan 2012 19:24:32 -0800 Subject: [PATCH] Remove test warnings, add ANSI escape code remover --- lib/vagrant/util/ansi_escape_code_remover.rb | 34 +++++++++++++++++++ test/unit/vagrant/config/base_test.rb | 7 ++-- .../util/ansi_escape_code_remover_test.rb | 17 ++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 lib/vagrant/util/ansi_escape_code_remover.rb create mode 100644 test/unit/vagrant/util/ansi_escape_code_remover_test.rb diff --git a/lib/vagrant/util/ansi_escape_code_remover.rb b/lib/vagrant/util/ansi_escape_code_remover.rb new file mode 100644 index 000000000..c714b98b5 --- /dev/null +++ b/lib/vagrant/util/ansi_escape_code_remover.rb @@ -0,0 +1,34 @@ +module Vagrant + module Util + module ANSIEscapeCodeRemover + # Removes ANSI escape code sequences from the text and returns + # it. + # + # This removes all the ANSI escape codes listed here along with + # the escape codes for VT100 terminals: + # + # http://ascii-table.com/ansi-escape-sequences.php + def remove_ansi_escape_codes(text) + # An array of regular expressions which match various kinds + # of escape sequences. I can't think of a better single regular + # expression or any faster way to do this. + matchers = [/\e\[\d*[ABCD]/, # Matches things like \e[4D + /\e\[(\d*;)?\d*[HF]/, # Matches \e[1;2H or \e[H + /\e\[(s|u|2J|K)/, # Matches \e[s, \e[2J, etc. + /\e\[(\d*;){0,2}\d*m/, # Matches color escapes: \e[32m + /\e\[=\d*[hl]/, # Matches \e[=24h + /\e\[\?[1-9][hl]/, # Matches \e[?2h + /\e\[20[hl]/, # Matches \e[20l] + /\e[DME78H]/, # Matches \eD, \eH, etc. + /\e\[[0-2]?[JK]/, # Matches \e[0J, \e[K, etc. + ] + # Take each matcher and replace it with emptiness. + matchers.each do |matcher| + text.gsub!(matcher, "") + end + + text + end + end + end +end diff --git a/test/unit/vagrant/config/base_test.rb b/test/unit/vagrant/config/base_test.rb index f8a3d0a9e..2a0a5266a 100644 --- a/test/unit/vagrant/config/base_test.rb +++ b/test/unit/vagrant/config/base_test.rb @@ -25,10 +25,11 @@ describe Vagrant::Config::Base do it "doesn't merge values that start with a double underscore" do bar_class = Class.new(foo_class) do - @@counter = 0 + class_variable_set(:@@counter, 0) + def initialize - @__test = @@counter - @@counter += 1 + @__test = self.class.send(:class_variable_get, :@@counter) + self.class.send(:class_variable_set, :@@counter, @__test + 1) end end diff --git a/test/unit/vagrant/util/ansi_escape_code_remover_test.rb b/test/unit/vagrant/util/ansi_escape_code_remover_test.rb new file mode 100644 index 000000000..bd8c102d3 --- /dev/null +++ b/test/unit/vagrant/util/ansi_escape_code_remover_test.rb @@ -0,0 +1,17 @@ +require File.expand_path("../../../base", __FILE__) + +require "vagrant/util/ansi_escape_code_remover" + +describe Vagrant::Util::ANSIEscapeCodeRemover do + let(:klass) do + Class.new do + extend Vagrant::Util::ANSIEscapeCodeRemover + end + end + + it "should remove ANSI escape codes" do + klass.remove_ansi_escape_codes("\e[Hyo").should == "yo" + klass.remove_ansi_escape_codes("\e[38myo").should == "yo" + end +end +