From 6c45b6ccdb57b6a9e56aa96bb192e115f3ce067b Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Wed, 28 Jan 2015 15:58:49 -0500 Subject: [PATCH 1/3] Make sure we're testing blank lines and comments in env files Signed-off-by: Aanand Prasad --- tests/fixtures/env/one.env | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/fixtures/env/one.env b/tests/fixtures/env/one.env index 75a4f62ff..45b59fe63 100644 --- a/tests/fixtures/env/one.env +++ b/tests/fixtures/env/one.env @@ -1,4 +1,11 @@ +# Keep the blank lines and comments in this file, please + ONE=2 TWO=1 + + # (thanks) + THREE=3 + FOO=bar +# FOO=somethingelse From dfc6206d0d3e8f31a1dcbb5c707c7b36688a9450 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Wed, 28 Jan 2015 16:13:34 -0500 Subject: [PATCH 2/3] Extract get_env_files() Signed-off-by: Aanand Prasad --- compose/service.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/compose/service.py b/compose/service.py index 709584059..49bccd31d 100644 --- a/compose/service.py +++ b/compose/service.py @@ -617,15 +617,18 @@ def split_port(port): return internal_port, (external_ip, external_port or None) +def get_env_files(options): + env_files = options.get('env_file', []) + if not isinstance(env_files, list): + env_files = [env_files] + return env_files + + def merge_environment(options): env = {} - if 'env_file' in options: - if isinstance(options['env_file'], list): - for f in options['env_file']: - env.update(env_vars_from_file(f)) - else: - env.update(env_vars_from_file(options['env_file'])) + for f in get_env_files(options): + env.update(env_vars_from_file(f)) if 'environment' in options: if isinstance(options['environment'], list): From de07e0471ef02d85c248144959fd6932d8adaaf7 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Wed, 28 Jan 2015 16:01:48 -0500 Subject: [PATCH 3/3] Show a nicer error when the env file doesn't exist Closes #865 Signed-off-by: Aanand Prasad --- compose/service.py | 4 ++++ tests/unit/service_test.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/compose/service.py b/compose/service.py index 49bccd31d..0c9bd3570 100644 --- a/compose/service.py +++ b/compose/service.py @@ -95,6 +95,10 @@ class Service(object): if 'image' in options and 'build' in options: raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name) + for filename in get_env_files(options): + if not os.path.exists(filename): + raise ConfigError("Couldn't find env file for service %s: %s" % (name, filename)) + supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose', 'external_links'] diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 0a7239b0d..c7b122fc2 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -378,6 +378,10 @@ class ServiceEnvironmentTest(unittest.TestCase): {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'} ) + def test_env_nonexistent_file(self): + self.assertRaises(ConfigError, lambda: Service('foo', env_file='tests/fixtures/env/nonexistent.env')) + + @mock.patch.dict(os.environ) def test_resolve_environment_from_file(self): os.environ['FILE_DEF'] = 'E1'