Example #1
0
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')
Example #2
0
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')
Example #3
0
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'),
    ]
Example #4
0
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
Example #5
0
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 == '*****@*****.**'
Example #6
0
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 == '*****@*****.**'
Example #7
0
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)
Example #8
0
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')