Revision: 3851
Author: pekka.klarck
Date: Thu Aug 19 04:41:42 2010
Log: verify the encoding gor from environ (issue 616)
http://code.google.com/p/robotframework/source/detail?r=3851

Added:
 /trunk/atest/robot/cli/monitor/encoding_from_env_vars.txt
Modified:
 /trunk/src/robot/utils/encoding.py

=======================================
--- /dev/null
+++ /trunk/atest/robot/cli/monitor/encoding_from_env_vars.txt Thu Aug 19 04:41:42 2010
@@ -0,0 +1,35 @@
+*** Settings ***
+Force Tags   regression   pybot  jybot  non windows
+Resource     monitor_resource.txt
+
+*** Variables ***
+${TES TFILE}    ${CURDIR}/../../../testdata/misc/pass_and_fail.html
+${STDOUT FILE}  ${TEMPDIR}/redirect_stdout.txt
+${STDERR FILE}  ${TEMPDIR}/redirect_stderr.txt
+
+*** Test Cases ***
+
+Invalid Encoding In Environment Variables
+  Set Invalid Environment Variables
+  ${stdout}  ${stderr} =  Run Some Tests With Std Streams Redirected
+  Should Contain   ${stdout}  Pass And Fail :: Some tests here
+  Should Be Empty  ${stderr}
+
+
+*** Keywords ***
+
+Set Invalid Environment Variables
+  :FOR  ${var}  IN  LANG  LC_TYPE  LANGUAGE  LC_ALL
+  \  Set Environment Variable  ${var}  invalid
+
+Run Some Tests With Std Streams Redirected
+  Set Runners
+  ${cmd} =  Catenate
+  ...  echo "redirect stdin" |
+  ...  ${ROBOT} --monitorcolors off ${TESTFILE}
+  ...  > ${STDOUT FILE} 2> ${STDERR FILE}
+  Run  ${cmd}
+  ${stdout} =  Log File  ${STDOUT FILE}
+  ${stderr} =  Log File  ${STDERR FILE}
+  [Return]  ${stdout}  ${stderr}
+
=======================================
--- /trunk/src/robot/utils/encoding.py  Wed Aug 18 03:29:04 2010
+++ /trunk/src/robot/utils/encoding.py  Thu Aug 19 04:41:42 2010
@@ -30,19 +30,26 @@


 def _get_output_encoding():
+    # Jython is buggy on Windows: http://bugs.jython.org/issue1568
+    if os.sep == '\\' and sys.platform.startswith('java'):
+        return 'cp437'  # Default DOS encoding
     encoding = sys.__stdout__.encoding or sys.__stdin__.encoding
+    if encoding:
+        return encoding
     if os.sep == '/':
-        return encoding or _read_encoding_from_env()
-    # Use default DOS encoding if no encoding found (guess)
-    # or on buggy Jython 2.5: http://bugs.jython.org/issue1568
-    if not encoding or sys.platform.startswith('java'):
-        return 'cp437'
-    return encoding
-
-def _read_encoding_from_env():
+        return _read_encoding_from_unix_env()
+    return 'cp437'  # Default DOS encoding
+
+def _read_encoding_from_unix_env():
     for name in 'LANG', 'LC_CTYPE', 'LANGUAGE', 'LC_ALL':
-        if name in os.environ:
-            return os.environ[name].split('.')[-1]
+        try:
+            # Encoding can be in format like `UTF-8` or `en_US.UTF-8`
+            encoding = os.environ[name].split('.')[-1]
+            'testing that encoding is valid'.encode(encoding)
+        except (KeyError, LookupError):
+            pass
+        else:
+            return encoding
     return 'ascii'

 _output_encoding = _get_output_encoding()

Reply via email to