Skip to content

Commit

Permalink
Do not separate run/main methods
Browse files Browse the repository at this point in the history
This complication is unnecessary.
  • Loading branch information
cdown committed Nov 15, 2015
1 parent 374ec43 commit 14fc8b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
21 changes: 11 additions & 10 deletions tests/e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
@mock.patch('tzupdate.link_localtime')
def test_end_to_end_no_args(link_localtime_mock, get_timezone_for_ip_mock):
get_timezone_for_ip_mock.return_value = FAKE_TIMEZONE
args = tzupdate.parse_args([])
tzupdate.run(args)
args = []
tzupdate.main(args)
assert_true(link_localtime_mock.called)


@mock.patch('tzupdate.get_timezone_for_ip')
@mock.patch('tzupdate.link_localtime')
def test_print_only_no_link(link_localtime_mock, get_timezone_for_ip_mock):
get_timezone_for_ip_mock.return_value = FAKE_TIMEZONE
args = tzupdate.parse_args(['-p'])
tzupdate.run(args)
args = ['-p']
tzupdate.main(args)
assert_false(link_localtime_mock.called)


Expand All @@ -32,8 +32,8 @@ def test_explicit_paths(link_localtime_mock, get_timezone_for_ip_mock):
localtime_path = '/l'
zoneinfo_path = '/z'
get_timezone_for_ip_mock.return_value = FAKE_TIMEZONE
args = tzupdate.parse_args(['-l', localtime_path, '-z', zoneinfo_path])
tzupdate.run(args)
args = ['-l', localtime_path, '-z', zoneinfo_path]
tzupdate.main(args)
assert_true(
link_localtime_mock.called_once_with(
FAKE_TIMEZONE, zoneinfo_path, localtime_path,
Expand All @@ -46,15 +46,16 @@ def test_explicit_paths(link_localtime_mock, get_timezone_for_ip_mock):
def test_explicit_ip(_, get_timezone_for_ip_mock):
ip_addr = '1.2.3.4'
get_timezone_for_ip_mock.return_value = FAKE_TIMEZONE
args = tzupdate.parse_args(['-a', ip_addr])
tzupdate.run(args)
args = ['-a', ip_addr]
tzupdate.main(args)
assert_true(get_timezone_for_ip_mock.called_once_with(ip_addr))


@mock.patch('tzupdate.link_localtime')
def test_explicit_timezone(link_localtime_mock):
timezone = 'Foo/Bar'
args = tzupdate.parse_args(['-t', timezone])
tzupdate.run(args)
args = ['-t', timezone]
tzupdate.main(args)
assert_true(
link_localtime_mock.called_once_with(
timezone,
Expand Down
26 changes: 7 additions & 19 deletions tzupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ def parse_args(argv):
return args


def run(args):
def main(argv=None):
if argv is None:
argv = sys.argv[1:]

args = parse_args(argv)
logging.basicConfig(level=args.log_level)

if args.timezone:
timezone = args.timezone
print('Using explicitly passed timezone: %s' % timezone)
Expand All @@ -158,23 +164,5 @@ def run(args):
print('Linked %s to %s.' % (args.localtime_path, zoneinfo_tz_path))


def main(argv=None):
if argv is None:
argv = sys.argv[1:]

args = parse_args(argv)
logging.basicConfig(level=args.log_level)

try:
run(args)
except TimezoneUpdateException as thrown_exc:
if args.log_level == logging.DEBUG:
# Give the full traceback if we are in debug mode
raise
else:
print('fatal: {0!s}'.format(thrown_exc), file=sys.stderr)
sys.exit(thrown_exc.exit_code)


if __name__ == '__main__':
main()

0 comments on commit 14fc8b6

Please sign in to comment.