Skip to content

Commit fd30920

Browse files
committed
Merge pull request #897 from aanand/nicer-env-file-error
Nicer env file error
2 parents d79dc85 + de07e04 commit fd30920

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

compose/service.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def __init__(self, name, client=None, project='default', links=None, external_li
9595
if 'image' in options and 'build' in options:
9696
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)
9797

98+
for filename in get_env_files(options):
99+
if not os.path.exists(filename):
100+
raise ConfigError("Couldn't find env file for service %s: %s" % (name, filename))
101+
98102
supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose',
99103
'external_links']
100104

@@ -617,15 +621,18 @@ def split_port(port):
617621
return internal_port, (external_ip, external_port or None)
618622

619623

624+
def get_env_files(options):
625+
env_files = options.get('env_file', [])
626+
if not isinstance(env_files, list):
627+
env_files = [env_files]
628+
return env_files
629+
630+
620631
def merge_environment(options):
621632
env = {}
622633

623-
if 'env_file' in options:
624-
if isinstance(options['env_file'], list):
625-
for f in options['env_file']:
626-
env.update(env_vars_from_file(f))
627-
else:
628-
env.update(env_vars_from_file(options['env_file']))
634+
for f in get_env_files(options):
635+
env.update(env_vars_from_file(f))
629636

630637
if 'environment' in options:
631638
if isinstance(options['environment'], list):

tests/fixtures/env/one.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
# Keep the blank lines and comments in this file, please
2+
13
ONE=2
24
TWO=1
5+
6+
# (thanks)
7+
38
THREE=3
9+
410
FOO=bar
11+
# FOO=somethingelse

tests/unit/service_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ def test_env_from_multiple_files(self):
378378
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}
379379
)
380380

381+
def test_env_nonexistent_file(self):
382+
self.assertRaises(ConfigError, lambda: Service('foo', env_file='tests/fixtures/env/nonexistent.env'))
383+
384+
381385
@mock.patch.dict(os.environ)
382386
def test_resolve_environment_from_file(self):
383387
os.environ['FILE_DEF'] = 'E1'

0 commit comments

Comments
 (0)