From c59f9b162fece82c7868be7ee1789b53c7947075 Mon Sep 17 00:00:00 2001
From: david-sarah <david-sarah@jacaranda.org>
Date: Fri, 16 Jul 2010 21:58:16 -0700
Subject: [PATCH] stringutils: add test for when sys.stdout has no encoding
 attribute (fixes #1099).

---
 src/allmydata/test/test_encodingutil.py | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/allmydata/test/test_encodingutil.py b/src/allmydata/test/test_encodingutil.py
index a287bf41..10310ca6 100644
--- a/src/allmydata/test/test_encodingutil.py
+++ b/src/allmydata/test/test_encodingutil.py
@@ -35,7 +35,7 @@ if __name__ == "__main__":
 
 from twisted.trial import unittest
 from mock import patch
-import sys
+import sys, locale
 
 from allmydata.test.common_util import ReallyEqualMixin
 from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \
@@ -64,7 +64,28 @@ class StringUtilsErrors(ReallyEqualMixin, unittest.TestCase):
         mock_stdout.encoding = 'nonexistent_encoding'
         self.failUnlessRaises(AssertionError, _reload)
 
-        # TODO: mock_stdout.encoding = None
+    @patch('locale.getpreferredencoding')
+    def test_get_output_encoding_not_from_stdout(self, mock_locale_getpreferredencoding):
+        locale  # hush pyflakes
+        mock_locale_getpreferredencoding.return_value = 'koi8-r'
+
+        class DummyStdout:
+            pass
+        old_stdout = sys.stdout
+        sys.stdout = DummyStdout()
+        try:
+            _reload()
+            self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')
+
+            sys.stdout.encoding = None
+            _reload()
+            self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')
+
+            mock_locale_getpreferredencoding.return_value = None
+            _reload()
+            self.failUnlessReallyEqual(get_output_encoding(), 'utf-8')
+        finally:
+            sys.stdout = old_stdout
 
     @patch('sys.stdout')
     def test_argv_to_unicode(self, mock):
-- 
2.45.2