diff --git a/compose/config/environment.py b/compose/config/environment.py index 696356f3246..15454d68eef 100644 --- a/compose/config/environment.py +++ b/compose/config/environment.py @@ -22,6 +22,12 @@ def split_env(env): key = value = None if '=' in env: key, value = env.split('=', 1) + # If the value is quoted, remove the quotes + if len(value) >= 2 and value[0] == value[-1] and value[0] in ['"', "'"]: + value = value[1:-1] + # unescape quoted values + quote = value[0] + value.replace("\\"+quote, quote) else: key = env if re.search(r'\s', key): diff --git a/tests/unit/config/environment_test.py b/tests/unit/config/environment_test.py index 88eb0d6e11a..69f78b330a6 100644 --- a/tests/unit/config/environment_test.py +++ b/tests/unit/config/environment_test.py @@ -62,3 +62,14 @@ def test_env_vars_from_file_whitespace(self): with pytest.raises(ConfigurationError) as exc: env_vars_from_file(str(tmpdir.join('whitespace.env'))) assert 'environment variable' in exc.exconly() + + def test_env_vars_from_file_quoted(self): + tmpdir = pytest.ensuretemp('env_file') + self.addCleanup(tmpdir.remove) + with codecs.open('{}/quoted.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: + f.write('DOUBLE_QUOTES="testing \"double\" quotes"\n') + f.write("SINGLE_QUOTES='testing \'single\' quotes'\n") + assert env_vars_from_file(str(tmpdir.join('quoted.env'))) == { + 'DOUBLE_QUOTES': 'testing "double" quotes', + 'SINGLE_QUOTES': "testing 'single' quotes", + }