88import shutil
99import tempfile
1010
11- import pytest
11+ from ddt import data
12+ from ddt import ddt
13+ from ddt import unpack
1214
1315from compose .config .environment import env_vars_from_file
1416from compose .config .environment import Environment
15- from compose .config .errors import ConfigurationError
1617from tests import unittest
1718
1819
20+ @ddt
1921class 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' , '\ufeff PARK_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 ('\ufeff PARK_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