Browse Source

Fix unicode in environment variables for python2.

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 10 years ago
parent
commit
e168fd03ca

+ 4 - 1
compose/config/config.py

@@ -1,3 +1,4 @@
+import codecs
 import logging
 import os
 import sys
@@ -451,6 +452,8 @@ def parse_environment(environment):
 
 
 def split_env(env):
+    if isinstance(env, six.binary_type):
+        env = env.decode('utf-8')
     if '=' in env:
         return env.split('=', 1)
     else:
@@ -473,7 +476,7 @@ def env_vars_from_file(filename):
     if not os.path.exists(filename):
         raise ConfigurationError("Couldn't find env file: %s" % filename)
     env = {}
-    for line in open(filename, 'r'):
+    for line in codecs.open(filename, 'r', 'utf-8'):
         line = line.strip()
         if line and not line.startswith('#'):
             k, v = split_env(line)

+ 1 - 1
tests/fixtures/env/resolve.env

@@ -1,4 +1,4 @@
-FILE_DEF=F1
+FILE_DEF=bär
 FILE_DEF_EMPTY=
 ENV_DEF
 NO_DEF

+ 3 - 2
tests/unit/cli_test.py

@@ -1,3 +1,4 @@
+# encoding: utf-8
 from __future__ import absolute_import
 from __future__ import unicode_literals
 
@@ -98,7 +99,7 @@ class CLITestCase(unittest.TestCase):
         command.run(mock_project, {
             'SERVICE': 'service',
             'COMMAND': None,
-            '-e': ['BAR=NEW', 'OTHER=THREE'],
+            '-e': ['BAR=NEW', 'OTHER=bär'.encode('utf-8')],
             '--user': None,
             '--no-deps': None,
             '--allow-insecure-ssl': None,
@@ -114,7 +115,7 @@ class CLITestCase(unittest.TestCase):
         _, _, call_kwargs = mock_client.create_container.mock_calls[0]
         self.assertEqual(
             call_kwargs['environment'],
-            {'FOO': 'ONE', 'BAR': 'NEW', 'OTHER': 'THREE'})
+            {'FOO': 'ONE', 'BAR': 'NEW', 'OTHER': u'bär'})
 
     def test_run_service_with_restart_always(self):
         command = TopLevelCommand()

+ 7 - 1
tests/unit/config/config_test.py

@@ -1,3 +1,4 @@
+# encoding: utf-8
 from __future__ import print_function
 
 import os
@@ -894,7 +895,12 @@ class EnvTest(unittest.TestCase):
         )
         self.assertEqual(
             service_dict['environment'],
-            {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
+            {
+                'FILE_DEF': u'bär',
+                'FILE_DEF_EMPTY': '',
+                'ENV_DEF': 'E3',
+                'NO_DEF': ''
+            },
         )
 
     @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')