def test_empty_commit(mocker): """Tests core.commit() with empty commit and no commit message""" mocker.patch.object(formaldict.Schema, 'prompt', autospec=True, return_value={}) # Git does not allow empty commit messages assert core.commit(allow_empty=True).returncode == 1
def test_squash(mocker): """Tests core.squash""" mocker.patch.object( formaldict.Schema, 'prompt', autospec=True, side_effect=[ { # The first commit 'type': 'bug', 'summary': 'summary', 'description': 'description', 'jira': 'WEB-9999', }, { # The second commit 'type': 'trivial', 'summary': 'Fixing up something', }, { # The third commit 'type': 'trivial', 'summary': 'Fixing up something else', }, { # The commit when squashing all commits 'type': 'bug', 'summary': 'final summary', 'description': 'final description', 'jira': 'WEB-9999', }, ], ) for f_name in ['file_to_commit1', 'file_to_commit2', 'file_to_commit3']: with open(f_name, 'w+') as f: f.write('Hello World') utils.shell('git add .') assert core.commit().returncode == 0 assert core.squash('HEAD~2').returncode == 0 commit = utils.shell_stdout('git show --summary') assert (' final summary\n' ' \n' ' final description\n' ' \n' ' Type: bug\n' ' Jira: WEB-9999') in commit
def test_commit(mocker, input_data): """Tests core.commit and verifies the resulting commit object.""" mocker.patch.object(formaldict.Schema, 'prompt', autospec=True, return_value=input_data) with open('file_to_commit', 'w+') as f: f.write('Hello World') utils.shell('git add .') assert core.commit().returncode == 0 commit = core.CommitRange('HEAD~1..')[0] assert commit.is_parsed for key, value in input_data.items(): assert getattr(commit, key) == value
def test_commit_w_pre_commit_hook(pre_commit_return, mocker): """Tests core.commit() with a pre commit hook""" with open('.git/hooks/pre-commit', 'w+') as f: f.write(f'#!/bin/bash\nexit {pre_commit_return}') os.chmod('.git/hooks/pre-commit', 0o777) mocker.patch.object( formaldict.Schema, 'prompt', autospec=True, return_value={ 'type': 'bug!', 'summary': 'summary!', 'description': 'description!', 'jira': 'WEB-9999', }, ) assert core.commit(allow_empty=True).returncode == pre_commit_return
def test_squash_error_rollback(mocker): """Tests core.squash rolls back resets on commit error""" mocker.patch.object( formaldict.Schema, 'prompt', autospec=True, side_effect=[ { # The first commit 'type': 'bug', 'summary': 'summary', 'description': 'description', 'jira': 'WEB-9999', }, # Throw an exception when trying to do the squash commit Exception, ], ) with open('file_to_commit', 'w+') as f: f.write('Hello World') utils.shell('git add .') assert core.commit().returncode == 0 # The first squash call throws an unexpected error and rolls back the reset with pytest.raises(Exception): core.squash('HEAD~1') assert utils.shell_stdout('git diff --cached') == '' # Make commit return a non-zero exit code on next squash commit mocker.patch( 'tidy.core.commit', autospec=True, return_value=mocker.Mock(returncode=1), ) # The next squash call has a commit error and rolls back the reset assert core.squash('HEAD~1').returncode == 1 assert utils.shell_stdout('git diff --cached') == ''
def commit(ctx, no_verify, allow_empty): """ Perform a tidy commit. """ result = core.commit(no_verify=no_verify, allow_empty=allow_empty) ctx.exit(result.returncode)
def test_squash_diverging_branches(mocker): """Tests core.squash against a base branch that has diverged""" mocker.patch.object( formaldict.Schema, 'prompt', autospec=True, side_effect=[ { # The first commit on the base branch 'type': 'bug', 'summary': 'summary', 'description': 'first master commit', 'jira': 'WEB-9999', }, { # The second commit on base after making squash branch 'type': 'trivial', 'summary': 'wont be seen in history', }, { # The first commit on the branch to squash 'type': 'trivial', 'summary': 'Fixing up something', }, { # The second commit on the branch to squash 'type': 'trivial', 'summary': 'Fixing up something else', }, { # The commit when squashing all commits 'type': 'bug', 'summary': 'final summary', 'description': 'final description', 'jira': 'WEB-9999', }, ], ) # Make a commit that all branches will shared core.commit(allow_empty=True) # Make a branch that we will squash utils.shell('git branch test-squash') # Now commit against the base branch (i.e. make it diverge) core.commit(allow_empty=True) # Change branches and do a few more commits that will be squashed utils.shell('git checkout test-squash') core.commit(allow_empty=True) core.commit(allow_empty=True) assert core.squash('master', allow_empty=True).returncode == 0 commits = utils.shell_stdout('git --no-pager log') # These commits disappeared when squashing assert 'Fixing up something' not in commits # First commit against master should be in log assert 'first master commit' in commits # Squashed commit should be in log assert 'final description' in commits # The divergent commit should not appear in history assert 'wont be seen' not in commits
def test_empty_commit_not_allowed(): """Tests core.commit() with an empty commit when its not allowed""" assert core.commit().returncode == 1