def test_envelop_with_a_real_environment(): # Given that I have an environment env = Environment() # When I set a variable in that environment env.set('yo-dawg', 'I heard you like variables') # Then I see that it was set in the actual environment assert os.environ.get('yo-dawg') == 'I heard you like variables'
def test_envelop_environment_set(): # Given that I have an empty environment env = Environment() # When I set something env.set('myvar', 'myvalue') # I'll be able to get it properly assert ('myvar', 'myvalue') in env.items()
def test_envelop_environment_get_uri_returning_none(): # Given that I have an empty environment env = Environment() # When I try to get a uri variable that doesn't exist, then I get None assert env.get_uri('blah') is None # And When I try to get a variable that doesn't exist but I provide a # default value, it will be returned instead of none assert env.get_uri('blah', 'http://yipit.com').host == 'yipit.com'
def test_envelop_environment_get_uri(): # Given that I have an environment with a variable containing a uri env = Environment() env.set('githubpage', 'https://*****:*****@github.com/yipit/envelop') # When I try to get the value as a Uri uri = env.get_uri('githubpage') # Then I see things working assert uri.scheme == 'https' assert uri.host == 'github.com' assert uri.port is None assert uri.user == 'clarete' assert uri.password == 'passwd!!' assert uri.path == '/yipit/envelop' assert uri.relative_path == 'yipit/envelop'
def test_envelop_environment_from_file(_io): # Given that I load variables to my environment from a file _io.open.return_value = io.StringIO('FAVORITE_SUPER_HERO: Batman!') env = Environment.from_file('myfile.cfg') # When I try to find a variable defined in that file, then I see that it # works assert env.get('FAVORITE_SUPER_HERO') == 'Batman!'
def test_envelop_environment_from_file(): # Given that I load variables to my environment from a file env = Environment.from_file( os.path.join(os.path.dirname(__file__), './fixtures/env.cfg')) # When I try to find a variable defined in that file, then I see that it # works assert env.get('FAVORITE_SUPER_HERO') == 'Batman NANANANANA'
def test_envelop_environment_from_directory_set(_io): # Given that I load variables to my env from a folder env = Environment.from_folder( os.path.join(os.path.dirname(__file__), './fixtures/env')) # When I set some stuff env.set('CITY', 'NEW-YORK') # Then I see that we always try to write the file _io.open.return_value.__enter__.return_value.write.assert_called_once_with( 'NEW-YORK')
def main(): parser = argparse.ArgumentParser(description='Manage your environment.') parser.add_argument('-f', '--file', metavar='<FILE>', type=str, action='store', help='File to load variables from') parser.add_argument( '-d', '--directory', metavar='<DIR>', type=str, action='store', help='Directory to load variables from, works just like `envparse\'') subparsers = parser.add_subparsers(title='subcommands', metavar='COMMAND') get_parser = subparsers.add_parser( 'get', help='Get and print out the value of the variable <VAR>') get_parser.add_argument('cmd_get') get_uri_parser = subparsers.add_parser('get-uri', help='Exposes the URI parser API') get_uri_parser.add_argument('cmd_get_uri', nargs=2) args = parser.parse_args() if args.file: env = Environment.from_file(args.file) if args.directory: env = Environment.from_folder(args.directory) if hasattr(args, 'cmd_get'): return env.get(args.cmd_get) if hasattr(args, 'cmd_get_uri'): part, variable = args.cmd_get_uri return getattr(env.get_uri(variable), part)
def test_envelop_environment_from_directory_del(_os, _io): # Given that I have a folder environment with an item `CITY` env = Environment.from_folder('./path') env.set('CITY', 'NEW-YORK') # We need the path.join function over there, so we need to restore it # manually _os.path.join.side_effect = os.path.join # When I remove that item del env['CITY'] # Then I can see that the unlink function was called properly _os.unlink.assert_called_once_with('./path/CITY')
def test_envelop_environment_from_directory_set(): # Given that I load variables to my env from a folder path = os.path.join(os.path.dirname(__file__), './fixtures/env') env = Environment.from_folder(path) # When I set a new variable to my folder env env.set('CITY', 'NEW-YORK') # Then I see the file was created with the right content target = os.path.join(path, 'CITY') assert os.path.exists(target) # And then I see that the value is also right assert open(target).read() == 'NEW-YORK' # And then I see that after removing the item, the file will also go away del env['CITY'] assert os.path.exists(target) is False
def test_envelop_environment_from_directory_items(_os, _io): # Given that I load variables to my env from a folder env = Environment.from_folder( os.path.join(os.path.dirname(__file__), './fixtures/env')) _os.listdir.return_value = ['ENABLE_SOMETHING', 'PI', 'SERVER_URI'] _io.open.return_value.__enter__.return_value.read.side_effect = [ '', '3.14', 'smtp://[email protected]:[email protected]:25', ] # When I try to list all the variables inside of that folder assert sorted(env.items(), key=lambda x: x[0]) == [ ('ENABLE_SOMETHING', u''), ('PI', u'3.14'), ('SERVER_URI', u'smtp://[email protected]:[email protected]:25'), ]
def test_envelop_environment_from_directory(): # Given that I load variables to my env from a folder env = Environment.from_folder( os.path.join(os.path.dirname(__file__), './fixtures/env')) # When I try to list all the variables inside of that folder assert sorted(env.items(), key=lambda x: x[0]) == [ ('ALLOWED_IPS', '10.0.0.1,10.0.0.2'), ('ENABLE_SOMETHING', u''), ('PI', u'3.14'), ('SERVER_URI', u'smtp://[email protected]:[email protected]:25'), ] # When I try to find the variables, then I see they're there correctly assert env.get_bool('ENABLE_SOMETHING') is False assert env.get_bool('ENABLE_SOMETHING_ELSE', True) is True assert env.get_float('PI') == 3.14 assert env.get_uri('SERVER_URI').host == 'mserver.com' assert env.get_uri('SERVER_URI').user == '*****@*****.**'
def test_envelop_environment_from_directory_get(_os, _io): # Given that I load variables to my env from a folder env = Environment.from_folder( os.path.join(os.path.dirname(__file__), './fixtures/env')) _os.listdir.return_value = ['ENABLE_SOMETHING', 'PI', 'SERVER_URI'] _io.open.return_value.__enter__.return_value.read.side_effect = [ '', IOError, '3.14', 'smtp://[email protected]:[email protected]:25', 'smtp://[email protected]:[email protected]:25', ] # When I try to find the variables, then I see they're there correctly assert env.get_bool('ENABLE_SOMETHING') is False assert env.get_bool('ENABLE_SOMETHING_ELSE', True) is True assert env.get_float('PI') == 3.14 assert env.get_uri('SERVER_URI').host == 'mserver.com' assert env.get_uri('SERVER_URI').user == '*****@*****.**'
# -*- coding: utf-8 -*- from __future__ import unicode_literals from os.path import dirname, abspath, join try: from envelop import Environment except ImportError: raise ImportError('could not import milieu, please make sure it is installed (pip install milieu)') env = Environment() SECRET_KEY = '8y2v7mq%foapmsuftqu#)_muync$+x7$$n7$3!66kwblvh40%w' DEBUG = True PRODUCTION = False LOCAL_FILE = lambda *parts: join(abspath(dirname(__file__)), *parts) TEMPLATE_DEBUG = DEBUG SITE_DOMAIN = env.get('SITE_DOMAIN', '127.0.0.1:8000') db_auth = env.get_uri('DATABASE_DEFAULT', "mysql://root@localhost/tckt") db_options = { 'init_command': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'compress': True } DATABASES = { 'default': {
import multiprocessing from os.path import join, abspath, dirname from envelop import Environment # enforce utf-8 ENCODING = 'utf-8' reload(sys) sys.setdefaultencoding(ENCODING) local_file = lambda *path: join(abspath(dirname(__file__)), *path) module_file = lambda *path: abspath(local_file('..', *path)) project_file = lambda *path: abspath(module_file('..', *path)) env = Environment() SELF = sys.modules[__name__] CONNECTION_POOL_SIZE = multiprocessing.cpu_count() LOCAL_PORT = 19842 PORT = env.get_int('PORT', LOCAL_PORT) TEST_MODE = env.get('TEST_MODE', 'false') OLDSPEAK_WORKDIR = env.get('OLDSPEAK_WORKDIR', '/srv/oldspeak/sandbox') OLDSPEAK_DATADIR = env.get('OLDSPEAK_DATADIR', '/srv/oldspeak/private-data') OLDSPEAK_PUBLICDIR = env.get('OLDSPEAK_PUBLICDIR', '/srv/oldspeak/public-data') STATIC_FOLDER_PATH = env.get('OLDSPEAK_STATIC_FOLDER_PATH', project_file('static', 'dist')) HTML_TEMPLATE_PATH = env.get('OLDSPEAK_HTML_TEMPLATE_PATH', project_file('static', 'templates')) STATIC_URL_PREFIX = '/s'
# -*- coding: utf-8 -*- from __future__ import unicode_literals from os.path import dirname, abspath, join try: from envelop import Environment except ImportError: raise ImportError( 'could not import milieu, please make sure it is installed (pip install milieu)' ) env = Environment() SECRET_KEY = '8y2v7mq%foapmsuftqu#)_muync$+x7$$n7$3!66kwblvh40%w' DEBUG = True PRODUCTION = False LOCAL_FILE = lambda *parts: join(abspath(dirname(__file__)), *parts) TEMPLATE_DEBUG = DEBUG SITE_DOMAIN = env.get('SITE_DOMAIN', '127.0.0.1:8000') db_auth = env.get_uri('DATABASE_DEFAULT', "mysql://root@localhost/tckt") db_options = { 'init_command': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'compress': True }
def test_envelop_helper_methods(): # Given that I have an environment with some variables set data = { 'str': 'I heard you like variables', 'int': '42', 'float': '3.14', 'bool0': 'True', 'bool1': 'true', 'bool2': '1', 'bool3': '2', 'bool4': 'False', 'bool5': 'false', 'bool6': '0', 'list': 'foo,bar,baz' } env = Environment(storage=data) # Let's retrieve things with their correct types assert env.get_int('int') == 42 assert env.get_float('float') == 3.14 assert env.get_bool('bool0') is True assert env.get_bool('bool1') is True assert env.get_bool('bool2') is True assert env.get_bool('bool3') is True assert env.get_bool('bool4') is False assert env.get_bool('bool5') is False assert env.get_bool('bool6') is False assert env.get_list('list') == ['foo', 'bar', 'baz'] # Sanity checks with pytest.raises(ValueError): env.get_int('str') with pytest.raises(ValueError): env.get_float('str') assert env.get_bool('str') is False # Testing default values assert env.get('i-dont-exist', 'blah') == 'blah' assert env.get_int('i-dont-exist', 2) == 2 assert env.get_float('i-dont-exist', 2.5) == 2.5 assert env.get_bool('i-dont-exist', True) is True
def test_envelop_environment_from_directory_that_does_not_exist(): # When I try to load the environment from a folder that does not exist, # Then I see that I receive an OSError with pytest.raises(OSError): Environment.from_folder('something-that-does-not-exist')
def test_envelop_environment_get(): # Given that I have an environment env = Environment({'val1': 'yo'}) # When I set something assert env.get('val1') == 'yo'