Skip to content

Commit 6d2658e

Browse files
committed
Add python-dotenv
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 53d00f7 commit 6d2658e

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

compose/config/environment.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4-
import codecs
5-
import contextlib
64
import logging
75
import os
86
import re
97

8+
import dotenv
109
import six
1110

1211
from ..const import IS_WINDOWS_PLATFORM
@@ -39,17 +38,8 @@ def env_vars_from_file(filename):
3938
raise EnvFileNotFound("Couldn't find env file: {}".format(filename))
4039
elif not os.path.isfile(filename):
4140
raise EnvFileNotFound("{} is not a file.".format(filename))
42-
env = {}
43-
with contextlib.closing(codecs.open(filename, 'r', 'utf-8-sig')) as fileobj:
44-
for line in fileobj:
45-
line = line.strip()
46-
if line and not line.startswith('#'):
47-
try:
48-
k, v = split_env(line)
49-
env[k] = v
50-
except ConfigurationError as e:
51-
raise ConfigurationError('In file {}: {}'.format(filename, e.msg))
52-
return env
41+
42+
return dotenv.dotenv_values(dotenv_path=filename, encoding='utf-8-sig')
5343

5444

5545
class Environment(dict):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ paramiko==2.7.1
1717
pypiwin32==219; sys_platform == 'win32' and python_version < '3.6'
1818
pypiwin32==223; sys_platform == 'win32' and python_version >= '3.6'
1919
PySocks==1.7.1
20+
python-dotenv==0.10.5
2021
PyYAML==5.3
2122
requests==2.22.0
2223
six==1.12.0

tests/unit/config/environment_test.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import shutil
99
import tempfile
1010

11-
import pytest
11+
from ddt import data
12+
from ddt import ddt
13+
from ddt import unpack
1214

1315
from compose.config.environment import env_vars_from_file
1416
from compose.config.environment import Environment
15-
from compose.config.errors import ConfigurationError
1617
from tests import unittest
1718

1819

20+
@ddt
1921
class EnvironmentTest(unittest.TestCase):
22+
@classmethod
2023
def test_get_simple(self):
2124
env = Environment({
2225
'FOO': 'bar',
@@ -28,12 +31,14 @@ def test_get_simple(self):
2831
assert env.get('BAR') == '1'
2932
assert env.get('BAZ') == ''
3033

34+
@classmethod
3135
def test_get_undefined(self):
3236
env = Environment({
3337
'FOO': 'bar'
3438
})
3539
assert env.get('FOOBAR') is None
3640

41+
@classmethod
3742
def test_get_boolean(self):
3843
env = Environment({
3944
'FOO': '',
@@ -48,20 +53,18 @@ def test_get_boolean(self):
4853
assert env.get_boolean('FOOBAR') is True
4954
assert env.get_boolean('UNDEFINED') is False
5055

51-
def test_env_vars_from_file_bom(self):
56+
@data(
57+
('unicode exclude test', '\ufeffPARK_BOM=박봄\n', {'PARK_BOM': '박봄'}),
58+
('export prefixed test', 'export PREFIXED_VARS=yes\n', {"PREFIXED_VARS": "yes"}),
59+
('quoted vars test', "QUOTED_VARS='yes'\n", {"QUOTED_VARS": "yes"}),
60+
('double quoted vars test', 'DOUBLE_QUOTED_VARS="yes"\n', {"DOUBLE_QUOTED_VARS": "yes"}),
61+
('extra spaces test', 'SPACES_VARS = "yes"\n', {"SPACES_VARS": "yes"}),
62+
)
63+
@unpack
64+
def test_env_vars(self, test_name, content, expected):
5265
tmpdir = tempfile.mkdtemp('env_file')
5366
self.addCleanup(shutil.rmtree, tmpdir)
54-
with codecs.open('{}/bom.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f:
55-
f.write('\ufeffPARK_BOM=박봄\n')
56-
assert env_vars_from_file(str(os.path.join(tmpdir, 'bom.env'))) == {
57-
'PARK_BOM': '박봄'
58-
}
59-
60-
def test_env_vars_from_file_whitespace(self):
61-
tmpdir = tempfile.mkdtemp('env_file')
62-
self.addCleanup(shutil.rmtree, tmpdir)
63-
with codecs.open('{}/whitespace.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f:
64-
f.write('WHITESPACE =yes\n')
65-
with pytest.raises(ConfigurationError) as exc:
66-
env_vars_from_file(str(os.path.join(tmpdir, 'whitespace.env')))
67-
assert 'environment variable' in exc.exconly()
67+
file_abs_path = str(os.path.join(tmpdir, ".env"))
68+
with codecs.open(file_abs_path, 'w', encoding='utf-8') as f:
69+
f.write(content)
70+
assert env_vars_from_file(file_abs_path) == expected, '"{}" Failed'.format(test_name)

0 commit comments

Comments
 (0)