compose/tests/unit/config/environment_test.py
alexrecuenco 4d3d9f64b9 Removed Python2 support
Closes: #6890

Some remarks,

- `# coding ... utf-8` statements are not needed
- isdigit on strings instead of a try-catch.
- Default opening mode is read, so we can do `open()` without the `'r'` everywhere
- Removed inheritinng from `object` class, it isn't necessary in python3.
- `super(ClassName, self)` can now be replaced with `super()`
- Use of itertools and `chain` on a couple places dealing with sets.
- Used the operator module instead of lambdas when warranted
    `itemgetter(0)` instead of `lambda x: x[0]`
    `attrgetter('name')` instead of `lambda x: x.name`
- `sorted` returns a list, so no need to use `list(sorted(...))`
- Removed `dict()` using dictionary comprehensions whenever possible
- Attempted to remove python3.2 support

Signed-off-by: alexrecuenco <alejandrogonzalezrecuenco@gmail.com>
2020-08-11 17:45:13 +07:00

66 lines
2.0 KiB
Python

import codecs
import os
import shutil
import tempfile
from ddt import data
from ddt import ddt
from ddt import unpack
from compose.config.environment import env_vars_from_file
from compose.config.environment import Environment
from tests import unittest
@ddt
class EnvironmentTest(unittest.TestCase):
@classmethod
def test_get_simple(self):
env = Environment({
'FOO': 'bar',
'BAR': '1',
'BAZ': ''
})
assert env.get('FOO') == 'bar'
assert env.get('BAR') == '1'
assert env.get('BAZ') == ''
@classmethod
def test_get_undefined(self):
env = Environment({
'FOO': 'bar'
})
assert env.get('FOOBAR') is None
@classmethod
def test_get_boolean(self):
env = Environment({
'FOO': '',
'BAR': '0',
'BAZ': 'FALSE',
'FOOBAR': 'true',
})
assert env.get_boolean('FOO') is False
assert env.get_boolean('BAR') is False
assert env.get_boolean('BAZ') is False
assert env.get_boolean('FOOBAR') is True
assert env.get_boolean('UNDEFINED') is False
@data(
('unicode exclude test', '\ufeffPARK_BOM=박봄\n', {'PARK_BOM': '박봄'}),
('export prefixed test', 'export PREFIXED_VARS=yes\n', {"PREFIXED_VARS": "yes"}),
('quoted vars test', "QUOTED_VARS='yes'\n", {"QUOTED_VARS": "yes"}),
('double quoted vars test', 'DOUBLE_QUOTED_VARS="yes"\n', {"DOUBLE_QUOTED_VARS": "yes"}),
('extra spaces test', 'SPACES_VARS = "yes"\n', {"SPACES_VARS": "yes"}),
)
@unpack
def test_env_vars(self, test_name, content, expected):
tmpdir = tempfile.mkdtemp('env_file')
self.addCleanup(shutil.rmtree, tmpdir)
file_abs_path = str(os.path.join(tmpdir, ".env"))
with codecs.open(file_abs_path, 'w', encoding='utf-8') as f:
f.write(content)
assert env_vars_from_file(file_abs_path) == expected, '"{}" Failed'.format(test_name)