def test_perform_rebase(mock_repo):
    """
    GIVEN Rebaser initialized correctly including branch and commit
    WHEN perform_rebase is called
    THEN branch.rebase_to_hash is called
    WITH the same branch and commit
    """
    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)
    rebaser.perform_rebase()

    mock_repo.branch.rebase_to_hash.assert_called()
    mock_repo.branch.rebase_to_hash.assert_called_with("my_branch",
                                                       "my_commit")
def test_rebase_exception_gitreview(mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN perform_rebase asserts with RebaseException
    THEN Rebaser calls the try_automated_rebase_fix method
    """
    mock_repo.branch.rebase_to_hash.side_effect = [
        exceptions.RebaseException('.gitreview failed to rebase'), True
    ]
    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    rebaser.try_automated_rebase_fix = Mock()
    rebaser.try_automated_rebase_fix.side_effect = [True]
    rebaser.perform_rebase()
    rebaser.try_automated_rebase_fix.assert_called()
def test_perform_rebase_aborts_on_failure(mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN rebase_to_hash fails with RebaseException
    THEN perform_rebase also raises RebaseException
    AND abort_rebase is called
    """
    mock_repo.branch.rebase_to_hash.side_effect = exceptions.RebaseException

    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    with pytest.raises(exceptions.RebaseException):
        rebaser.perform_rebase()

    mock_repo.branch.abort_rebase.assert_called()
def test_rebase_exception_not_gitreview(mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN perform_rebase asserts with RebaseException
    AND the exception message does not contain .gitreview
    THEN Rebaser calls the repo.branch.abort_rebase method
    """
    mock_repo.branch.rebase_to_hash.side_effect = [
        exceptions.RebaseException('Whatever other exception'), True
    ]
    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    rebaser.repo.branch.abort_rebase = Mock()
    with pytest.raises(exceptions.RebaseException):
        rebaser.perform_rebase()
    rebaser.repo.branch.abort_rebase.assert_called()