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 cf927651d4a93ab1e1e3beb98b67c975ac5a702f
parent 9cc83c4812e63a18ed25bd93e7fde10ea2364db3
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Wed, 31 Oct 2018 21:09:09 -0400

Use argv to dynamically determine exec name

Closes #3

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

diff --git a/hookmeup/hookmeup.py b/hookmeup/hookmeup.py @@ -4,6 +4,7 @@ import os import subprocess from subprocess import CalledProcessError +import sys FORMAT_STRING = 'hookmeup: {}' @@ -133,6 +134,8 @@ def install(args): "Argument passed to 'install', but expected none" ) + exec_name = sys.argv[0] + stdout = _call_checked_subprocess( ['git', 'rev-parse', '--git-dir'], 'Not in a Git repository' @@ -146,18 +149,22 @@ def install(args): if os.path.exists(hook_path): with open(hook_path, 'r') as hook_file: - already_installed = 'hookmeup' in hook_file.read() + already_installed = exec_name in hook_file.read() if already_installed: _print_msg('already installed') else: _print_msg('installing to existing hook') with open(hook_path, 'a') as hook_file: - hook_file.write('hookmeup post-checkout "$@"\n') + hook_file.write( + '{} post-checkout "$@"\n'.format(exec_name) + ) else: _print_msg('creating hook') with open(hook_path, 'w') as hook_file: - hook_file.write('#!/bin/sh\nhookmeup post-checkout "$@"\n') + hook_file.write( + '#!/bin/sh\n{} post-checkout "$@"\n'.format(exec_name) + ) def remove(args): """Remove the hook from the repository""" @@ -166,6 +173,8 @@ def remove(args): "Argument passed to 'remove', but expected none" ) + exec_name = sys.argv[0] + stdout = _call_checked_subprocess( ['git', 'rev-parse', '--git-dir'], 'Not in a Git repository' @@ -180,14 +189,14 @@ def remove(args): if os.path.exists(hook_path): with open(hook_path, 'r') as hook_file: hook_lines = hook_file.read() - installed = 'hookmeup' in hook_lines + installed = exec_name in hook_lines hook_lines = hook_lines.splitlines() if installed: hook_lines = \ ['{}\n'.format(line) for line in hook_lines - if line.find('hookmeup') == -1] + if line.find(exec_name) == -1] with open(hook_path, 'w') as hook_file: hook_file.writelines(hook_lines) else: diff --git a/tests/test_hookmeup.py b/tests/test_hookmeup.py @@ -5,6 +5,7 @@ import os import subprocess from subprocess import CalledProcessError +import sys import pytest import hookmeup @@ -27,6 +28,7 @@ def test_install(mock_install, mocker): 'os.path.exists', new=mocker.MagicMock(return_value=False) ) + mocker.patch.object(sys, 'argv', ['hookmeup', 'install']) hookmeup.hookmeup.install({}) mock_file.assert_called_once_with( os.path.sep.join(['.git', 'hooks', 'post-checkout']), @@ -39,6 +41,31 @@ def test_install(mock_install, mocker): os.path.sep.join(['.git', 'hooks', 'post-checkout']) ) +def test_install_exotic_argv0(mock_install, mocker): + """Test install with an exotic sys.argv[0] value""" + mock_file = mocker.mock_open() + mocker.patch('hookmeup.hookmeup.open', mock_file) + mocker.patch( + 'os.path.exists', + new=mocker.MagicMock(return_value=False) + ) + mocker.patch.object( + hookmeup.hookmeup.sys, + 'argv', + ['hookmeup3', 'install'] + ) + hookmeup.hookmeup.install({}) + mock_file.assert_called_once_with( + os.path.sep.join(['.git', 'hooks', 'post-checkout']), + 'w' + ) + mock_file().write.assert_called_once_with( + '#!/bin/sh\nhookmeup3 post-checkout "$@"\n' + ) + os.path.exists.assert_called_once_with( + os.path.sep.join(['.git', 'hooks', 'post-checkout']) + ) + def test_install_existing_hook(mock_install, mocker): """Test install function when post-checkout already exists""" mock_file = mocker.mock_open() @@ -75,13 +102,16 @@ def test_install_already_installed(mock_install, mocker): read_data='#!/bin/sh\nhookmeup post-checkout\n' ) mocker.patch('hookmeup.hookmeup.open', mock_file) + mocker.patch.object(sys, 'argv', ['hookmeup', 'install']) mocker.patch( 'os.path.exists', new=mocker.MagicMock(return_value=True) ) mocker.patch('hookmeup.hookmeup.print') hookmeup.hookmeup.install({}) - hookmeup.hookmeup.print.assert_called_once() + hookmeup.hookmeup.print.assert_called_once_with( + 'hookmeup: already installed' + ) def test_error(): """Test accessing error members""" @@ -246,6 +276,7 @@ def test_remove(mocker): 'os.path.exists', new=mocker.MagicMock(return_value=True) ) + mocker.patch.object(sys, 'argv', ['hookmeup', 'remove']) mock_file = mocker.mock_open( read_data='#!/bin/sh\nfoo\nhookmeup post-checkout "$@"' )