grim/envconfig2
Clone
Summary
Browse
Changes
Graph
Update the docs for the change to url's
2017-08-30, Gary Kramlich
5ed88b4cd1bf
Update the docs for the change to url's
# vi:et:ts=4 sw=4 sts=4
#
# envconfig2: easily read your config from the environment
# Copyright (C) 2016 Gary Kramlich <grim@reaperworld.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import
os
import
urlparse
import
pytest
import
envconfig2
def
test_integer_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
integer
(
'INT'
,
required
=
True
)
def
test_integer_default_not_set
():
os
.
environ
=
{}
assert
envconfig2
.
integer
(
'INT'
,
default
=
2
)
==
2
def
test_integer_default_set
():
os
.
environ
=
{
'INT'
:
'1'
,
}
assert
envconfig2
.
integer
(
'INT'
,
default
=
2
)
==
1
def
test_integer
():
os
.
environ
=
{
'INT'
:
'1'
,
}
assert
envconfig2
.
integer
(
'INT'
)
==
1
def
test_boolean_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
integer
(
'BOOL'
,
required
=
True
)
def
test_boolean_default_not_set
():
os
.
environ
=
{}
assert
envconfig2
.
boolean
(
'BOOL'
,
default
=
True
)
is
True
def
test_boolean_default_set
():
os
.
environ
=
{
'BOOL'
:
'TRUE'
}
assert
envconfig2
.
boolean
(
'BOOL'
,
default
=
False
)
is
True
def
test_boolean
():
for
item
in
[
'1'
,
't'
,
'true'
,
'y'
,
'yes'
]:
os
.
environ
=
{
'BOOL'
:
item
,
}
assert
envconfig2
.
boolean
(
'BOOL'
)
is
True
for
item
in
[
'0'
,
'f'
,
'false'
,
'n'
,
'no'
,
'asdf'
,
''
]:
os
.
environ
=
{
'BOOL'
:
item
,
}
assert
envconfig2
.
boolean
(
'BOOL'
)
is
False
def
test_string_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
string
(
'STR'
,
required
=
True
)
def
test_string_default_not_set
():
os
.
environ
=
{}
assert
envconfig2
.
string
(
'STR'
,
default
=
'foo'
)
==
'foo'
def
test_string_default_set
():
os
.
environ
=
{
'STR'
:
'bar'
,
}
assert
envconfig2
.
string
(
'STR'
,
default
=
'foo'
)
==
'bar'
def
test_string
():
os
.
environ
=
{
'STR'
:
'bar'
,
}
assert
envconfig2
.
string
(
'STR'
)
==
'bar'
def
test_list_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
list
(
'LIST'
,
required
=
True
)
def
test_list_required_set
():
os
.
environ
=
{
'LIST'
:
'1,2'
,
}
assert
envconfig2
.
list
(
'LIST'
,
required
=
True
)
==
[
'1'
,
'2'
]
def
test_list_default_not_set
():
os
.
environ
=
{}
assert
envconfig2
.
list
(
'LIST'
,
default
=
[
'1'
,
'2'
])
==
[
'1'
,
'2'
]
def
test_list_different_separator
():
os
.
environ
=
{
'LIST'
:
'1-2'
,
}
assert
envconfig2
.
list
(
'LIST'
,
separator
=
'-'
)
def
test_list
():
os
.
environ
=
{
'LIST'
:
'1,2'
,
}
assert
envconfig2
.
list
(
'LIST'
)
==
[
'1'
,
'2'
]
def
test_json_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
json
(
'JSON'
,
required
=
True
)
def
test_json_required_set
():
os
.
environ
=
{
'JSON'
:
'{"foo": "bar"}'
,
}
assert
envconfig2
.
json
(
'JSON'
,
required
=
True
)
==
{
'foo'
:
'bar'
}
def
test_json_default_not_set
():
os
.
environ
=
{}
assert
envconfig2
.
json
(
'JSON'
,
default
=
{
'foo'
:
'bar'
})
==
{
'foo'
:
'bar'
}
def
test_json
():
os
.
environ
=
{
'JSON'
:
'{"foo": "bar"}'
,
}
assert
envconfig2
.
json
(
'JSON'
)
==
{
'foo'
:
'bar'
}
def
test_url_required_not_set
():
os
.
environ
=
{}
with
pytest
.
raises
(
ValueError
):
envconfig2
.
url
(
'URL'
,
required
=
True
)
def
test_url_required_set
():
os
.
environ
=
{
'URL'
:
'https://example.com'
,
}
assert
envconfig2
.
url
(
'URL'
,
required
=
True
)
==
'https://example.com'
def
test_url_default_not_set
():
os
.
environ
=
{}
exp
=
'https://example.com'
act
=
envconfig2
.
url
(
'URL'
,
default
=
'https://example.com'
)
assert
act
==
exp
def
test_url_default_set_expand_true
():
os
.
environ
=
{}
exp
=
urlparse
.
urlparse
(
'https://example.com'
)
act
=
envconfig2
.
url
(
'URL'
,
default
=
'https://example.com'
,
expand
=
True
)
assert
act
==
exp
def
test_url
():
os
.
environ
=
{
'URL'
:
'https://example.com'
,
}
assert
envconfig2
.
url
(
'URL'
)
==
'https://example.com'
def
test_url_expand_true
():
os
.
environ
=
{
'URL'
:
'https://example.com'
,
}
exp
=
urlparse
.
urlparse
(
'https://example.com'
)
act
=
envconfig2
.
url
(
'URL'
,
expand
=
True
)
assert
act
==
exp
def
test_url_expand_false
():
os
.
environ
=
{
'URL'
:
'https://example.com'
,
}
assert
envconfig2
.
url
(
'URL'
,
expand
=
False
)
==
'https://example.com'
def
test_prefix
():
try
:
os
.
environ
=
{
'TEST_FOO'
:
'ABC'
,
}
envconfig2
.
prefix
=
'TEST'
assert
envconfig2
.
string
(
'FOO'
)
==
'ABC'
finally
:
envconfig2
.
prefix
=
None
def
test_prefix_required_not_set
():
try
:
os
.
environ
=
{
'BAR'
:
'123'
,
}
envconfig2
.
prefix
=
'TEST'
envconfig2
.
prefix_required
=
True
assert
envconfig2
.
string
(
'BAR'
)
is
None
assert
envconfig2
.
string
(
'FOO'
)
is
None
finally
:
envconfig2
.
prefix
=
None
envconfig2
.
prefix_required
=
False
def
test_prefix_required_set
():
try
:
os
.
environ
=
{
'TEST_FOO'
:
'ABC'
,
'TEST_BAR'
:
'123'
,
}
envconfig2
.
prefix
=
'TEST'
envconfig2
.
prefix_required
=
True
assert
envconfig2
.
string
(
'FOO'
)
==
'ABC'
assert
envconfig2
.
string
(
'BAR'
)
==
'123'
finally
:
envconfig2
.
prefix
=
None
envconfig2
.
prefix_required
=
False
def
test_prefix_prefered
():
try
:
os
.
environ
=
{
'FOO'
:
'ABC'
,
'TEST_FOO'
:
'123'
,
}
envconfig2
.
prefix
=
'TEST'
assert
envconfig2
.
string
(
'FOO'
)
==
'123'
finally
:
envconfig2
.
prefix
=
None