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_milieu_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 os.environ.get('yo-dawg').should.equal('I heard you like variables')
def test_milieu_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 env.items().should.contain(('myvar', 'myvalue'))
def test_milieu_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 env.get_uri('blah').should.be.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 env.get_uri('blah', 'http://yipit.com').host.should.equal('yipit.com')
def test_merge_regular_environment(): "Environment.update() Should update a regular environment with data from another one" # Given that I load variables to my env from a folder environment = Environment() # When I merge another environment into the current one that *does # not* have the variable CITY environment.update(Environment(storage={'CITY': 'Porto Alegre'})) # Then I see that the old value was overwritten environment.get('CITY').should.equal('Porto Alegre')
def test_milieu_environment_get_uri(): # Given that I have an environment with a variable containing a uri env = Environment() env.set('githubpage', 'https://*****:*****@github.com/yipit/milieu') # When I try to get the value as a Uri uri = env.get_uri('githubpage') # Then I see things working uri.scheme.should.equal('https') uri.host.should.equal('github.com') uri.port.should.equal(None) uri.user.should.equal('clarete') uri.password.should.equal('passwd!!') uri.path.should.equal('/yipit/milieu') uri.relative_path.should.equal('yipit/milieu')
def test_milieu_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 env.get('FAVORITE_SUPER_HERO').should.equal('Batman NANANANANA')
def test_milieu_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 env.get('FAVORITE_SUPER_HERO').should.equal('Batman!')
def test_milieu_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.write.assert_called_once_with('NEW-YORK')
def test_milieu_environment_from_file_object(): "Environment.from_file_object() Should load variables form file-like objects" # Given a file-like object with a variable inside file_like = io.StringIO('FAVORITE_SUPER_HERO: Batman!') # When I create a new environment from that object env = Environment.from_file_object(file_like) # then I see that it contains the variable env.get('FAVORITE_SUPER_HERO').should.equal('Batman!')
def test_milieu_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_merge_folder_environment(): "Environment.update() Should update a folder based environment with data from another one" # Given that I load variables to my env from a folder path = os.path.join(os.path.dirname(__file__), './fixtures/env') environment = Environment.from_folder(path) # When I merge another environment into the current one that *does # not* have the variable CITY environment.update(Environment(storage={'CITY': 'Porto Alegre'})) # Then I see that the old value was overwritten environment.get('CITY').should.equal('Porto Alegre') # And then I just cleanup the file I created above del environment['CITY']
def test_merge_regular_environment_with_non_string_values(): "Environment.update() Should convert all the values before setting them back to the original environment" # Given that I load variables to my env from a folder environment = Environment() # When I merge another environment into the current one that *does # not* have the variable `age`, I set it as an integer environment.update(Environment(storage={'age': 27})) # Then I see that the value was converted to a string environment.get('age').should.equal('27') # And then I see it's not a unicode string, but a bytes sequence environment.get('age').should.be.a(bytes)
def test_milieu_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') os.path.exists(target).should.be.true # And then I see that the value is also right open(target).read().should.equal('NEW-YORK') # And then I see that after removing the item, the file will also go away del env['CITY'] os.path.exists(target).should.be.false
def test_milieu_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 sorted(env.items(), key=lambda x: x[0]).should.equal([ ('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 env.get_bool('ENABLE_SOMETHING').should.be.false env.get_bool('ENABLE_SOMETHING_ELSE', True).should.be.true env.get_float('PI').should.equal(3.14) env.get_uri('SERVER_URI').host.should.equal('mserver.com') env.get_uri('SERVER_URI').user.should.equal('*****@*****.**')
def test_milieu_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.read.side_effect = [ '', '3.14', 'smtp://[email protected]:[email protected]:25', ] # When I try to list all the variables inside of that folder sorted(env.items(), key=lambda x: x[0]).should.equal([ ('ENABLE_SOMETHING', u''), ('PI', u'3.14'), ('SERVER_URI', u'smtp://[email protected]:[email protected]:25'), ])
def test_milieu_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.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 env.get_bool('ENABLE_SOMETHING').should.be.false env.get_bool('ENABLE_SOMETHING_ELSE', True).should.be.true env.get_float('PI').should.equal(3.14) env.get_uri('SERVER_URI').host.should.equal('mserver.com') env.get_uri('SERVER_URI').user.should.equal('*****@*****.**')
def test_milieu_environment_get(): # Given that I have an environment env = Environment({'val1': 'yo'}) # When I set something env.get('val1').should.equal('yo')
def test_milieu_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', } env = Environment(storage=data) # Let's retrieve things with their correct types env.get_int('int').should.equal(42) env.get_float('float').should.equal(3.14) env.get_bool('bool0').should.be.true env.get_bool('bool1').should.be.true env.get_bool('bool2').should.be.true env.get_bool('bool3').should.be.true env.get_bool('bool4').should.be.false env.get_bool('bool5').should.be.false env.get_bool('bool6').should.be.false # Sanity checks env.get_int.when.called_with('str').should.throw(ValueError) env.get_float.when.called_with('str').should.throw(ValueError) env.get_bool('str').should.be.false # Testing default values env.get('i-dont-exist', 'blah').should.equal('blah') env.get_int('i-dont-exist', 2).should.equal(2) env.get_float('i-dont-exist', 2.5).should.equal(2.5) env.get_bool('i-dont-exist', True).should.be.true