hookmeup

A Git hook to automate your Pipenv and Django workflow
git clone git://git.danielmoch.com/hookmeup.git
Log | Files | Refs | README | LICENSE

commit 2b9e3c50e20c01396ea32a0f4da14e89928d6ea5
parent c9d8ec8da0306f5c9b98fdb1dd2a3a7ab4cd3e0a
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Tue,  4 Sep 2018 21:03:00 -0400

Refactor messages and exception

- Abstract print() commands into print_msg(), which ensures proper
  formatting of messages
- Actually use EXIT_CODE when HookMeUpError encountered

Diffstat:
Mhookmeup/__init__.py | 3++-
Mhookmeup/__main__.py | 2+-
Mhookmeup/hookmeup.py | 20+++++++++++++-------
Mtests/test_main.py | 3++-
4 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/hookmeup/__init__.py b/hookmeup/__init__.py @@ -8,7 +8,7 @@ from .hookmeup import HookMeUpError __author__ = 'Daniel Moch' -__version__ = '0.2.0' +__version__ = '0.2.0.dev0' def main(): """Main hookmeup entrypoint""" @@ -59,3 +59,4 @@ def main(): func(arg_dict) except HookMeUpError as ex: print(str(ex)) + exit(ex.EXIT_CODE) diff --git a/hookmeup/__main__.py b/hookmeup/__main__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- """hookmeup main module""" import hookmeup diff --git a/hookmeup/hookmeup.py b/hookmeup/hookmeup.py @@ -4,12 +4,18 @@ import subprocess from subprocess import CalledProcessError +FORMAT_STRING = 'hookmeup: {}' + +def print_msg(msg): + """Print a formatted message to stdout""" + print(FORMAT_STRING.format(msg)) + class HookMeUpError(Exception): """Errors raised by hookmeup""" EXIT_CODE = 1 def __str__(self): - return "hookmeup: {}".format(self.args[0]) + return FORMAT_STRING.format(self.args[0]) class DjangoMigrator(): """ @@ -83,7 +89,7 @@ def call_checked_subprocess(arg_list, msg="fatal error"): def adjust_pipenv(): """Adjust pipenv to match Pipfile""" - print('Adjusting virtualenv to match Pipfile') + print_msg('Adjusting virtualenv to match Pipfile') call_checked_subprocess( ['pipenv', 'clean'], 'Attempt to clean pipenv failed' @@ -142,13 +148,13 @@ def install(args): already_installed = 'hookmeup' in hook_file.read() if already_installed: - print('hookmeup: already installed') + print_msg('already installed') else: - print('hookmeup: installing to existing hook') + print_msg('installing to existing hook') with open(hook_path, 'a') as hook_file: hook_file.write('hookmeup post-checkout "$@"\n') else: - print('hookmeup: creating hook') + print_msg('creating hook') with open(hook_path, 'w') as hook_file: hook_file.write('#!/bin/sh\nhookmeup post-checkout "$@"\n') @@ -184,7 +190,7 @@ def remove(args): with open(hook_path, 'w') as hook_file: hook_file.writelines(hook_lines) else: - print('hookmeup: hookmeup not installed. nothing to do.') + print_msg('hookmeup not installed. nothing to do.') else: - print('hookmeup: no hook to remove') + print_msg('no hook to remove') diff --git a/tests/test_main.py b/tests/test_main.py @@ -90,6 +90,7 @@ def test_error(mocker): ) mocker.patch.object(sys, 'argv', ['hookmeup', 'install']) mocker.patch('hookmeup.print') - hookmeup.main() + with pytest.raises(SystemExit): + hookmeup.main() hookmeup.hookmeup.install.assert_called_once() hookmeup.print.called_with('hookmeup: test error')