Quellcode durchsuchen

Injecting os.environ in Environment instance happens outside of init method

Signed-off-by: Joffrey F <[email protected]>
Joffrey F vor 9 Jahren
Ursprung
Commit
12ad3ff301

+ 2 - 2
compose/config/config.py

@@ -449,12 +449,12 @@ def process_config_file(config_file, environment, service_name=None):
 
 
 class ServiceExtendsResolver(object):
-    def __init__(self, service_config, config_file, environment=None, already_seen=None):
+    def __init__(self, service_config, config_file, environment, already_seen=None):
         self.service_config = service_config
         self.working_dir = service_config.working_dir
         self.already_seen = already_seen or []
         self.config_file = config_file
-        self.environment = environment or Environment()
+        self.environment = environment
 
     @property
     def signature(self):

+ 12 - 9
compose/config/environment.py

@@ -41,19 +41,22 @@ class Environment(dict):
     def __init__(self, *args, **kwargs):
         super(Environment, self).__init__(*args, **kwargs)
         self.missing_keys = []
-        self.update(os.environ)
 
     @classmethod
     def from_env_file(cls, base_dir):
-        result = cls()
-        if base_dir is None:
+        def _initialize():
+            result = cls()
+            if base_dir is None:
+                return result
+            env_file_path = os.path.join(base_dir, '.env')
+            try:
+                return cls(env_vars_from_file(env_file_path))
+            except ConfigurationError:
+                pass
             return result
-        env_file_path = os.path.join(base_dir, '.env')
-        try:
-            return cls(env_vars_from_file(env_file_path))
-        except ConfigurationError:
-            pass
-        return result
+        instance = _initialize()
+        instance.update(os.environ)
+        return instance
 
     def __getitem__(self, key):
         try:

+ 3 - 1
tests/integration/testcases.py

@@ -90,7 +90,9 @@ class DockerClientTestCase(unittest.TestCase):
         if 'command' not in kwargs:
             kwargs['command'] = ["top"]
 
-        kwargs['environment'] = resolve_environment(kwargs, Environment())
+        kwargs['environment'] = resolve_environment(
+            kwargs, Environment.from_env_file(None)
+        )
         labels = dict(kwargs.setdefault('labels', {}))
         labels['com.docker.compose.test-name'] = self.id()
 

+ 8 - 3
tests/unit/config/config_test.py

@@ -37,7 +37,9 @@ def make_service_dict(name, service_dict, working_dir, filename=None):
             filename=filename,
             name=name,
             config=service_dict),
-        config.ConfigFile(filename=filename, config={}))
+        config.ConfigFile(filename=filename, config={}),
+        environment=Environment.from_env_file(working_dir)
+    )
     return config.process_service(resolver.run())
 
 
@@ -2061,7 +2063,9 @@ class EnvTest(unittest.TestCase):
             },
         }
         self.assertEqual(
-            resolve_environment(service_dict, Environment()),
+            resolve_environment(
+                service_dict, Environment.from_env_file(None)
+            ),
             {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None},
         )
 
@@ -2099,7 +2103,8 @@ class EnvTest(unittest.TestCase):
         os.environ['ENV_DEF'] = 'E3'
         self.assertEqual(
             resolve_environment(
-                {'env_file': ['tests/fixtures/env/resolve.env']}, Environment()
+                {'env_file': ['tests/fixtures/env/resolve.env']},
+                Environment.from_env_file(None)
             ),
             {
                 'FILE_DEF': u'bär',

+ 4 - 4
tests/unit/config/interpolation_test.py

@@ -20,7 +20,7 @@ def mock_env():
 
 def test_interpolate_environment_variables_in_services(mock_env):
     services = {
-        'servivea': {
+        'servicea': {
             'image': 'example:${USER}',
             'volumes': ['$FOO:/target'],
             'logging': {
@@ -32,7 +32,7 @@ def test_interpolate_environment_variables_in_services(mock_env):
         }
     }
     expected = {
-        'servivea': {
+        'servicea': {
             'image': 'example:jenny',
             'volumes': ['bar:/target'],
             'logging': {
@@ -44,7 +44,7 @@ def test_interpolate_environment_variables_in_services(mock_env):
         }
     }
     assert interpolate_environment_variables(
-        services, 'service', Environment()
+        services, 'service', Environment.from_env_file(None)
     ) == expected
 
 
@@ -70,5 +70,5 @@ def test_interpolate_environment_variables_in_volumes(mock_env):
         'other': {},
     }
     assert interpolate_environment_variables(
-        volumes, 'volume', Environment()
+        volumes, 'volume', Environment.from_env_file(None)
     ) == expected