diff options
author | lamont-granquist <lamont@scriptkiddie.org> | 2013-10-17 16:41:56 -0700 |
---|---|---|
committer | lamont-granquist <lamont@scriptkiddie.org> | 2013-10-17 16:41:56 -0700 |
commit | 9b43885bf3d9a3e1d0cca92abed7b447676696b4 (patch) | |
tree | 59921add92d11c711e1e382cc4c202ef63b4765f | |
parent | 923f9fed8b826db0db119c8582b1f035c0ed7742 (diff) | |
parent | f1f131741b55b5c4a3d4e1ddd9003ea0037458b9 (diff) | |
download | ohai-9b43885bf3d9a3e1d0cca92abed7b447676696b4.tar.gz |
Merge pull request #213 from opscode/lcg/force-encode
Test for LANG=C and input contains UTF-8
-rw-r--r-- | lib/ohai/mixin/command.rb | 7 | ||||
-rw-r--r-- | spec/unit/mixin/command_spec.rb | 24 |
2 files changed, 26 insertions, 5 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 9e454117..26189dfd 100644 --- a/spec/unit/mixin/command_spec.rb +++ b/spec/unit/mixin/command_spec.rb @@ -37,10 +37,30 @@ describe Ohai::Mixin::Command, "popen4" do end if defined?(::Encoding) && "".respond_to?(:force_encoding) #i.e., ruby 1.9 - it "[OHAI-275] should mark strings as in the default external encoding" do + context "when external commands return UTF-8 strings and we are running under LANG=C encoding" do + before do + @saved_default_external = Encoding.default_external + @saved_default_internal = Encoding.default_internal + Encoding.default_external = Encoding::US_ASCII + Encoding.default_internal = Encoding::US_ASCII + end + + after do + Encoding.default_external = @saved_default_external + Encoding.default_internal = @saved_default_internal + end + + 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::UTF_8 + end + end + + 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 |