import pytest

git = pytest.importorskip('skilleter_modules.git', reason='pygit2 not installed')

def test_git():
    """Test suite for the module"""

    print('Creating local git repo')

    init('test-repo')
    os.chdir('test-repo')

    try:
        print('Initial status:')
        print('  User name:            %s' % config_get('user', 'name'))
        print('  Project:              %s' % project())
        print('  Remotes:              %s' % remotes())
        print('  Current branch:       %s' % branch())
        print('  Current working tree: %s' % working_tree())
        print('  Rebasing?             %s' % rebasing())
        print('  Status:               %s' % status())
        print('  Default branch?:      %s' % is_default_branch())
        print()

        print('Adding a removing a configuration value')

        config_set('user', 'size', 'medium')

        print('  User size config:     %s' % config_get('user', 'size'))

        config_rm('user', 'size')

        value = config_get('user', 'size')
        if value is None:
            print('  Successfully failed to read the newly-deleted config data')
        else:
            raise GitError('Unexpected lack of error reading configuration data', 1)

        config_set('user', 'email', 'user@localhost')
        config_set('user', 'name', 'User Name')

        print('')

        with open('newfile.txt', 'w', encoding='utf8') as newfile:
            newfile.write('THIS IS A TEST')

        print('Adding and committing "newfile.txt"')

        add(['newfile.txt'])

        commit(['newfile.txt'], 'Test the add and commit functionality')

        print('Removing and committing "newfile.txt"')

        rm(['newfile.txt'])

        commit(None, 'Removed "newfile.txt"')

        print('Removing the last commit')

        reset('HEAD~1')

        print('Commit info for HEAD  %s' % commit_info('HEAD'))

    except GitError as exc:
        sys.stderr.write(f'ERROR: {exc.msg}')
        sys.exit(1)

    finally:
        # If anything fails, then clean up afterwards

        os.chdir('..')
        shutil.rmtree('test-repo')
