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 fada8bfffada5f8081e6184b0c358b7e3049fe58
parent 55fed00742265a8b4d1291ac008a4d5b16369db7
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Thu, 13 Sep 2018 20:31:08 -0400

Properly name private functions

Closes #1

Diffstat:
Mhookmeup/hookmeup.py | 46+++++++++++++++++++++++-----------------------
Mtests/test_hookmeup.py | 36++++++++++++++++++------------------
2 files changed, 41 insertions(+), 41 deletions(-)

diff --git a/hookmeup/hookmeup.py b/hookmeup/hookmeup.py @@ -6,7 +6,7 @@ FORMAT_STRING = 'hookmeup: {}' -def print_msg(msg): +def _print_msg(msg): """Print a formatted message to stdout""" print(FORMAT_STRING.format(msg)) @@ -17,7 +17,7 @@ class HookMeUpError(Exception): def __str__(self): return FORMAT_STRING.format(self.args[0]) -class DjangoMigrator(): +class _DjangoMigrator(): """ Class responsible for parsing, applying, and unapplying Django migrations @@ -31,7 +31,7 @@ def __init__(self, args): 'manage.py', 'migrate'] deleted_migrations = {} - stdout = call_checked_subprocess( + stdout = _call_checked_subprocess( ['git', 'diff', '--name-status', args['old'], args['new']], 'not in a Git repository' ) @@ -69,40 +69,40 @@ def migrate(self): target_migration = format(oldest - 1, '04d') if target_migration == '0000': target_migration = 'zero' - call_checked_subprocess( + _call_checked_subprocess( self._migrate_command + [app, target_migration], 'rollback migration for {} failed'.format(app) ) if self.added_migration_apps != []: - call_checked_subprocess( + _call_checked_subprocess( self._migrate_command + self.added_migration_apps, 'migration failed' ) -def call_checked_subprocess(arg_list, msg="fatal error"): +def _call_checked_subprocess(arg_list, msg="fatal error"): """Handle return data from a call to a subprocess""" try: return subprocess.check_output(arg_list, text=True) except CalledProcessError: raise HookMeUpError(msg) -def adjust_pipenv(): +def _adjust_pipenv(): """Adjust pipenv to match Pipfile""" - print_msg('Adjusting virtualenv to match Pipfile') - call_checked_subprocess( + _print_msg('Adjusting virtualenv to match Pipfile') + _call_checked_subprocess( ['pipenv', 'clean'], 'Attempt to clean pipenv failed' ) - call_checked_subprocess( + _call_checked_subprocess( ['pipenv', 'sync', '--dev'], 'Attempt to sync pipenv failed' ) -def pipfile_changed(args): +def _pipfile_changed(args): """Test if the Pipfile has changed""" - stdout = call_checked_subprocess( + stdout = _call_checked_subprocess( ['git', 'diff', '--name-only', @@ -114,16 +114,16 @@ def pipfile_changed(args): 'Not in a Git repository' ) - return 'Pipfile' in stdout + return 'Pipfile' in str(stdout) def post_checkout(args): """Run post-checkout hook""" if args['branch_checkout'] == 1: - migrator = DjangoMigrator(args) + migrator = _DjangoMigrator(args) if migrator.migrations_changed(): migrator.migrate() - if pipfile_changed(args): - adjust_pipenv() + if _pipfile_changed(args): + _adjust_pipenv() def install(args): """Install hook into repository""" @@ -132,7 +132,7 @@ def install(args): "Argument passed to 'install', but expected none" ) - stdout = call_checked_subprocess( + stdout = _call_checked_subprocess( ['git', 'rev-parse', '--git-dir'], 'Not in a Git repository' ) @@ -148,13 +148,13 @@ def install(args): already_installed = 'hookmeup' in hook_file.read() if already_installed: - print_msg('already installed') + _print_msg('already installed') else: - print_msg('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_msg('creating hook') + _print_msg('creating hook') with open(hook_path, 'w') as hook_file: hook_file.write('#!/bin/sh\nhookmeup post-checkout "$@"\n') @@ -165,7 +165,7 @@ def remove(args): "Argument passed to 'remove', but expected none" ) - stdout = call_checked_subprocess( + stdout = _call_checked_subprocess( ['git', 'rev-parse', '--git-dir'], 'Not in a Git repository' ) @@ -190,7 +190,7 @@ def remove(args): with open(hook_path, 'w') as hook_file: hook_file.writelines(hook_lines) else: - print_msg('hookmeup not installed. nothing to do.') + _print_msg('hookmeup not installed. nothing to do.') else: - print_msg('no hook to remove') + _print_msg('no hook to remove') diff --git a/tests/test_hookmeup.py b/tests/test_hookmeup.py @@ -7,7 +7,7 @@ import pytest import hookmeup -from hookmeup.hookmeup import HookMeUpError, DjangoMigrator +from hookmeup.hookmeup import HookMeUpError, _DjangoMigrator # pylint: disable=protected-access @pytest.fixture @@ -92,10 +92,10 @@ def test_error(): def test_post_checkout_non_branch(mocker): """Test post_checkout call for non-branch checkout""" mocker.patch( - 'hookmeup.hookmeup.adjust_pipenv' + 'hookmeup.hookmeup._adjust_pipenv' ) mocker.patch.object( - DjangoMigrator, + _DjangoMigrator, 'migrations_changed' ) hookmeup.hookmeup.post_checkout( @@ -103,8 +103,8 @@ def test_post_checkout_non_branch(mocker): 'new': 'new', 'branch_checkout': 0} ) - hookmeup.hookmeup.adjust_pipenv.assert_not_called() - DjangoMigrator.migrations_changed.assert_not_called() + hookmeup.hookmeup._adjust_pipenv.assert_not_called() + _DjangoMigrator.migrations_changed.assert_not_called() def test_post_checkout(mocker): """Test nominal post_checkout""" @@ -115,14 +115,14 @@ def test_post_checkout(mocker): Pipfile\nA ' + migration) ) - mocker.patch('hookmeup.hookmeup.adjust_pipenv') + mocker.patch('hookmeup.hookmeup._adjust_pipenv') hookmeup.hookmeup.post_checkout({ 'branch_checkout': 1, 'old': 'HEAD^', 'new': 'HEAD' }) assert subprocess.check_output.call_count == 3 - hookmeup.hookmeup.adjust_pipenv.assert_called_once() + hookmeup.hookmeup._adjust_pipenv.assert_called_once() def test_post_checkout_no_changes(mocker): """Test nominal post_checkout""" @@ -130,26 +130,26 @@ def test_post_checkout_no_changes(mocker): 'subprocess.check_output', new=mocker.MagicMock(return_value='') ) - mocker.patch('hookmeup.hookmeup.adjust_pipenv') + mocker.patch('hookmeup.hookmeup._adjust_pipenv') hookmeup.hookmeup.post_checkout({ 'branch_checkout': 1, 'old': 'HEAD^', 'new': 'HEAD' }) assert subprocess.check_output.call_count == 2 - assert hookmeup.hookmeup.adjust_pipenv.call_count == 0 + assert hookmeup.hookmeup._adjust_pipenv.call_count == 0 -def test_adjust_pipenv(mocker): - """Test call to adjust_pipenv""" +def test__adjust_pipenv(mocker): + """Test call to _adjust_pipenv""" mocker.patch( 'subprocess.check_output', new=mocker.MagicMock(return_value='.git\n') ) - hookmeup.hookmeup.adjust_pipenv() + hookmeup.hookmeup._adjust_pipenv() assert subprocess.check_output.call_count == 2 -def test_adjust_pipenv_failure(mocker): - """Test adjust_pipenv with failed subprocess call""" +def test__adjust_pipenv_failure(mocker): + """Test _adjust_pipenv with failed subprocess call""" mocker.patch( 'subprocess.check_output', new=mocker.Mock( @@ -157,7 +157,7 @@ def test_adjust_pipenv_failure(mocker): ) ) with pytest.raises(HookMeUpError): - hookmeup.hookmeup.adjust_pipenv() + hookmeup.hookmeup._adjust_pipenv() def build_diff_output(file_list): lines = [] @@ -177,7 +177,7 @@ def test_migrate_up(mocker): 'subprocess.check_output', new=mocker.MagicMock(return_value=build_diff_output(file_list)) ) - migrator = DjangoMigrator({'old': 'test', 'new': 'test2'}) + migrator = _DjangoMigrator({'old': 'test', 'new': 'test2'}) assert migrator.migrations_changed() is True subprocess.check_output.assert_called_once() mocker.resetall() @@ -196,7 +196,7 @@ def test_migrate_down(mocker): 'subprocess.check_output', new=mocker.MagicMock(return_value=build_diff_output(file_list)) ) - migrator = DjangoMigrator({'old': 'test', 'new': 'test2'}) + migrator = _DjangoMigrator({'old': 'test', 'new': 'test2'}) assert migrator.migrations_changed() is True subprocess.check_output.assert_called_once() mocker.resetall() @@ -222,7 +222,7 @@ def test_migrate_to_zero(mocker): 'subprocess.check_output', new=mocker.MagicMock(return_value=build_diff_output(file_list)) ) - migrator = DjangoMigrator({'old': 'test', 'new': 'test2'}) + migrator = _DjangoMigrator({'old': 'test', 'new': 'test2'}) assert migrator.migrations_changed() is True subprocess.check_output.assert_called_once() mocker.resetall()