From d110ec70fdbb8358584eb57f7b8421b27ea30a1d Mon Sep 17 00:00:00 2001 From: Antonis Kalipetis Date: Fri, 16 Sep 2016 09:37:22 +0300 Subject: [PATCH 1/2] Make `env_file` optional This makes sure: * the actual file is not necessary, meaning that you don't have to create it each time you clone (given that usually `.env` files are ignored) * you still get a warning if the file doesn't exist, so you can be aware of times where a file should have been loaded but eventually didn't, for example due to a typo Signed-off-by: Antonis Kalipetis --- compose/config/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compose/config/config.py b/compose/config/config.py index f1195c8ec67..a8f00ac9a69 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -598,7 +598,10 @@ def resolve_environment(service_dict, environment=None): """ env = {} for env_file in service_dict.get('env_file', []): - env.update(env_vars_from_file(env_file)) + try: + env.update(env_vars_from_file(env_file)) + except ConfigurationError as exc: + log.warn('{} - ignoring'.format(exc.msg)) env.update(parse_environment(service_dict.get('environment'))) return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(env)) From 8f3403f7a71f6e02a91abcecd5fe76cedbb3ed9c Mon Sep 17 00:00:00 2001 From: Antonis Kalipetis Date: Wed, 28 Dec 2016 17:51:44 +0200 Subject: [PATCH 2/2] Add test for the non-existent env-file Signed-off-by: Antonis Kalipetis --- tests/unit/config/config_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index 6bf4986ff56..508b195d01f 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -2936,13 +2936,13 @@ def test_resolve_environment_with_multiple_env_files(self): ) def test_resolve_environment_nonexistent_file(self): - with pytest.raises(ConfigurationError) as exc: + with mock.patch('compose.config.config.log') as mock_logging: config.load(build_config_details( {'foo': {'image': 'example', 'env_file': 'nonexistent.env'}}, working_dir='tests/fixtures/env')) - assert 'Couldn\'t find env file' in exc.exconly() - assert 'nonexistent.env' in exc.exconly() + assert 'Couldn\'t find env file' in mock_logging.warn.call_args[0][0] + assert 'nonexistent.env' in mock_logging.warn.call_args[0][0] @mock.patch.dict(os.environ) def test_resolve_environment_from_env_file_with_empty_values(self):