From f1f131741b55b5c4a3d4e1ddd9003ea0037458b9 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 17 Oct 2013 15:36:29 -0700 Subject: force encode all strings to UTF-8 - this is a bit of a hack for ohai-6 while we're still using popen4 instead of mixlib-shellout - this may mean that we poorly support ISO-8851 / Shift_JIS or other non-UTF-8 encodings for characters where there is no translation between the external encoding and UTF-8 - this essentially standardizes on using UTF-8 internally --- lib/ohai/mixin/command.rb | 7 ++++--- spec/unit/mixin/command_spec.rb | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index a719e274..76cebb63 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -128,12 +128,12 @@ module Ohai # # Thanks Ara! def popen4(cmd, args={}, &b) - + ## Disable garbage collection to work around possible bug in MRI # Ruby 1.8 suffers from intermittent segfaults believed to be due to GC while IO.select # See OHAI-330 / CHEF-2916 / CHEF-1305 GC.disable - + # Waitlast - this is magic. # # Do we wait for the child process to die before we yield @@ -329,9 +329,10 @@ module Ohai # have encoding methods. if "".respond_to?(:force_encoding) && defined?(Encoding) o.string.force_encoding(Encoding.default_external) + o.string.encode!('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') e.string.force_encoding(Encoding.default_external) + e.string.encode!('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') end - b[cid, pi[0], o, e] results.last end diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/command_spec.rb index 9fd30402..26189dfd 100644 --- a/spec/unit/mixin/command_spec.rb +++ b/spec/unit/mixin/command_spec.rb @@ -50,17 +50,17 @@ describe Ohai::Mixin::Command, "popen4" do Encoding.default_internal = @saved_default_internal end - it "should force encode the string to US_ASCII" do + it "should force encode the string to UTF-8" do extend Ohai::Mixin::Command snowy = run_command(:command => ("echo '" + ('☃' * 8096) + "'"))[1] - snowy.encoding.should == Encoding::US_ASCII + snowy.encoding.should == Encoding::UTF_8 end end - it "[OHAI-275] should mark strings as in the default external encoding" do + it "should force encode the string to UTF-8" do extend Ohai::Mixin::Command snowy = run_command(:command => ("echo '" + ('☃' * 8096) + "'"))[1] - snowy.encoding.should == Encoding.default_external + snowy.encoding.should == Encoding::UTF_8 end end -- cgit v1.2.1