Skip to content

Commit

Permalink
Merge branch 'release/1.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
cdown committed Nov 3, 2018
2 parents 6745fc1 + 0caa1c5 commit 7185821
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 131 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -18,7 +18,9 @@ matrix:
# One-off runs
- python: '3.6'
env: TOXENV=coverage
- python: '3.5' # 3.6 after https://github.com/PyCQA/pylint/issues/1113
- python: '3.6'
env: TOXENV=black
- python: '3.6'
env: TOXENV=pylint

# This is required to enable container based Travis CI infrastructure.
Expand Down
31 changes: 12 additions & 19 deletions setup.py
Expand Up @@ -3,32 +3,25 @@
from setuptools import setup


with open('README.rst') as readme_f:
with open("README.rst") as readme_f:
README = readme_f.read()

with open('requirements.txt') as requirements_f:
with open("requirements.txt") as requirements_f:
REQUIREMENTS = requirements_f.readlines()

setup(
name='tzupdate',
version='1.3.0',
description='Set the system timezone based on IP geolocation',
name="tzupdate",
version="1.3.1",
description="Set the system timezone based on IP geolocation",
long_description=README,
url='https://github.com/cdown/tzupdate',
license='Public Domain',

author='Chris Down',
author_email='chris@chrisdown.name',

py_modules=['tzupdate'],
url="https://github.com/cdown/tzupdate",
license="Public Domain",
author="Chris Down",
author_email="chris@chrisdown.name",
py_modules=["tzupdate"],
install_requires=REQUIREMENTS,

entry_points={
'console_scripts': ['tzupdate=tzupdate:main'],
},

keywords='timezone localtime tz',

entry_points={"console_scripts": ["tzupdate=tzupdate:main"]},
keywords="timezone localtime tz",
classifiers=[
"Development Status :: 4 - Beta",
"License :: Public Domain",
Expand Down
36 changes: 16 additions & 20 deletions tests/_test_utils.py
Expand Up @@ -9,39 +9,33 @@


IP_ADDRESSES = builds(
ipaddress.IPv4Address,
integers(min_value=0, max_value=(2**32 - 1))
ipaddress.IPv4Address, integers(min_value=0, max_value=(2 ** 32 - 1))
).map(str)

FAKE_TIMEZONE = 'Fake/Timezone'
FAKE_ERROR = 'Virus = very yes'
FAKE_TIMEZONE = "Fake/Timezone"
FAKE_ERROR = "Virus = very yes"

FAKE_SERVICES = set([
FAKE_SERVICES = [
tzupdate.GeoIPService("http://example.com/json/{ip}", ("timezone",), ("message",)),
tzupdate.GeoIPService("https://doesnotexistreally.com/{ip}", ("time_zone",), None),
tzupdate.GeoIPService(
'http://example.com/json/{ip}', ('timezone',), ('message',),
"http://tzupdate.com/foo/bar/{ip}", ("location", "time_zone"), ("msg",)
),
tzupdate.GeoIPService(
'https://doesnotexistreally.com/{ip}', ('time_zone',), None,
),
tzupdate.GeoIPService(
'http://tzupdate.com/foo/bar/{ip}', ('location', 'time_zone'),
('msg',),
),
])
]


def setup_basic_api_response(services=None, empty_resp=False, empty_val=False):
'''
"""
If `empty_resp', we return a totally empty API response, except for any
error message.
If `empty_val', the tz_key is an empty string.
'''
"""
if services is None:
services = FAKE_SERVICES

for service in services:
url_regex = re.compile(service.url.format(ip=r'.*'))
url_regex = re.compile(service.url.format(ip=r".*"))
api_body = {}

if not empty_resp:
Expand All @@ -50,7 +44,7 @@ def setup_basic_api_response(services=None, empty_resp=False, empty_val=False):
for i, key in enumerate(service.tz_keys, start=1):
if i == len(service.tz_keys):
if empty_val:
cur_level[key] = ''
cur_level[key] = ""
else:
cur_level[key] = FAKE_TIMEZONE
else:
Expand All @@ -66,6 +60,8 @@ def setup_basic_api_response(services=None, empty_resp=False, empty_val=False):
tmp[key] = tmp.copy()

httpretty.register_uri(
httpretty.GET, url_regex,
body=json.dumps(api_body), content_type='application/json',
httpretty.GET,
url_regex,
body=json.dumps(api_body),
content_type="application/json",
)
59 changes: 27 additions & 32 deletions tests/e2e_tests.py
Expand Up @@ -4,60 +4,58 @@
import tzupdate
import mock
from nose.tools import assert_false, assert_raises
from tests._test_utils import (FAKE_SERVICES, FAKE_TIMEZONE,
setup_basic_api_response)
from tests._test_utils import FAKE_SERVICES, FAKE_TIMEZONE, setup_basic_api_response


@httpretty.activate
@mock.patch('tzupdate.write_debian_timezone')
@mock.patch('tzupdate.link_localtime')
@mock.patch("tzupdate.write_debian_timezone")
@mock.patch("tzupdate.link_localtime")
def test_end_to_end_no_args(link_localtime_mock, deb_tz_mock):
setup_basic_api_response()
args = []
tzupdate.main(args, services=FAKE_SERVICES)
link_localtime_mock.assert_called_once_with(
FAKE_TIMEZONE, tzupdate.DEFAULT_ZONEINFO_PATH,
tzupdate.DEFAULT_LOCALTIME_PATH,
FAKE_TIMEZONE, tzupdate.DEFAULT_ZONEINFO_PATH, tzupdate.DEFAULT_LOCALTIME_PATH
)
deb_tz_mock.assert_called_once_with(
FAKE_TIMEZONE, tzupdate.DEFAULT_DEBIAN_TIMEZONE_PATH,
FAKE_TIMEZONE, tzupdate.DEFAULT_DEBIAN_TIMEZONE_PATH
)


@httpretty.activate
@mock.patch('tzupdate.write_debian_timezone')
@mock.patch('tzupdate.link_localtime')
@mock.patch("tzupdate.write_debian_timezone")
@mock.patch("tzupdate.link_localtime")
def test_print_only_no_link(link_localtime_mock, deb_tz_mock):
setup_basic_api_response()
args = ['-p']
args = ["-p"]
tzupdate.main(args, services=FAKE_SERVICES)
assert_false(link_localtime_mock.called)
assert_false(deb_tz_mock.called)


@httpretty.activate
@mock.patch('tzupdate.write_debian_timezone')
@mock.patch('tzupdate.link_localtime')
@mock.patch("tzupdate.write_debian_timezone")
@mock.patch("tzupdate.link_localtime")
def test_explicit_paths(link_localtime_mock, deb_tz_mock):
setup_basic_api_response()
localtime_path = '/l'
zoneinfo_path = '/z'
deb_path = '/d'
args = ['-l', localtime_path, '-z', zoneinfo_path, '-d', deb_path]
localtime_path = "/l"
zoneinfo_path = "/z"
deb_path = "/d"
args = ["-l", localtime_path, "-z", zoneinfo_path, "-d", deb_path]
tzupdate.main(args, services=FAKE_SERVICES)
link_localtime_mock.assert_called_once_with(
FAKE_TIMEZONE, zoneinfo_path, localtime_path,
FAKE_TIMEZONE, zoneinfo_path, localtime_path
)
deb_tz_mock.assert_called_once_with(FAKE_TIMEZONE, deb_path)


@httpretty.activate
@mock.patch('tzupdate.write_debian_timezone')
@mock.patch('tzupdate.link_localtime')
@mock.patch("tzupdate.write_debian_timezone")
@mock.patch("tzupdate.link_localtime")
def test_explicit_ip(_unused_ll, _unused_deb):
setup_basic_api_response()
ip_addr = '1.2.3.4'
args = ['-a', ip_addr]
ip_addr = "1.2.3.4"
args = ["-a", ip_addr]
tzupdate.main(args, services=FAKE_SERVICES)

# TODO (#16): httpretty.last_request() and
Expand All @@ -66,27 +64,24 @@ def test_explicit_ip(_unused_ll, _unused_deb):
# this in


@mock.patch('tzupdate.write_debian_timezone')
@mock.patch('tzupdate.link_localtime')
@mock.patch("tzupdate.write_debian_timezone")
@mock.patch("tzupdate.link_localtime")
def test_explicit_timezone(link_localtime_mock, deb_tz_mock):
timezone = 'Foo/Bar'
args = ['-t', timezone]
timezone = "Foo/Bar"
args = ["-t", timezone]
tzupdate.main(args)
link_localtime_mock.assert_called_once_with(
timezone,
tzupdate.DEFAULT_ZONEINFO_PATH, tzupdate.DEFAULT_LOCALTIME_PATH,
)
deb_tz_mock.assert_called_once_with(
timezone, tzupdate.DEFAULT_DEBIAN_TIMEZONE_PATH
timezone, tzupdate.DEFAULT_ZONEINFO_PATH, tzupdate.DEFAULT_LOCALTIME_PATH
)
deb_tz_mock.assert_called_once_with(timezone, tzupdate.DEFAULT_DEBIAN_TIMEZONE_PATH)


@httpretty.activate
@mock.patch('tzupdate.Process')
@mock.patch("tzupdate.Process")
def test_timeout_results_in_exception(process_mock):
# The process mock causes us to never run get_timezone_from_ip, so we
# should time out
setup_basic_api_response()
args = ['-s', '0.01']
args = ["-s", "0.01"]
with assert_raises(tzupdate.TimezoneAcquisitionError):
tzupdate.main(args, services=FAKE_SERVICES)
2 changes: 1 addition & 1 deletion tests/requirements.txt
@@ -1,6 +1,6 @@
nose
hypothesis
httpretty
nose-parameterized
parameterized
mock
ipaddress

0 comments on commit 7185821

Please sign in to comment.