Example #1
0
def test_clone(store, tmpdir_factory, log_info_mock):
    path = git_dir(tmpdir_factory)
    with local.cwd(path):
        local['git']('commit', '--allow-empty', '-m', 'foo')
        sha = get_head_sha(path)
        local['git']('commit', '--allow-empty', '-m', 'bar')

    ret = store.clone(path, sha)
    # Should have printed some stuff
    log_info_mock.assert_called_with('This may take a few minutes...')

    # Should return a directory inside of the store
    assert os.path.exists(ret)
    assert ret.startswith(store.directory)
    # Directory should start with `repo`
    _, dirname = os.path.split(ret)
    assert dirname.startswith('repo')
    # Should be checked out to the sha we specified
    assert get_head_sha(ret) == sha

    # Assert that we made a symlink from the sha to the repo
    sha_path = os.path.join(store.directory, sha + '_' + hex_md5(path))
    assert os.path.exists(sha_path)
    assert os.path.islink(sha_path)
    assert os.readlink(sha_path) == ret
Example #2
0
def test_clone(store, tempdir_factory, log_info_mock):
    path = git_dir(tempdir_factory)
    with cwd(path):
        cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
        sha = get_head_sha(path)
        cmd_output('git', 'commit', '--allow-empty', '-m', 'bar')

    ret = store.clone(path, sha)
    # Should have printed some stuff
    assert log_info_mock.call_args_list[0][0][0].startswith(
        'Initializing environment for '
    )

    # Should return a directory inside of the store
    assert os.path.exists(ret)
    assert ret.startswith(store.directory)
    # Directory should start with `repo`
    _, dirname = os.path.split(ret)
    assert dirname.startswith('repo')
    # Should be checked out to the sha we specified
    assert get_head_sha(ret) == sha

    # Assert there's an entry in the sqlite db for this
    with sqlite3.connect(store.db_path) as db:
        path, = db.execute(
            'SELECT path from repos WHERE repo = ? and ref = ?',
            [path, sha],
        ).fetchone()
        assert path == ret
Example #3
0
def test_clone(store, tmpdir_factory, log_info_mock):
    path = git_dir(tmpdir_factory)
    with cwd(path):
        cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
        sha = get_head_sha(path)
        cmd_output('git', 'commit', '--allow-empty', '-m', 'bar')

    ret = store.clone(path, sha)
    # Should have printed some stuff
    log_info_mock.assert_called_with('This may take a few minutes...')

    # Should return a directory inside of the store
    assert os.path.exists(ret)
    assert ret.startswith(store.directory)
    # Directory should start with `repo`
    _, dirname = os.path.split(ret)
    assert dirname.startswith('repo')
    # Should be checked out to the sha we specified
    assert get_head_sha(ret) == sha

    # Assert that we made a symlink from the sha to the repo
    sha_path = os.path.join(store.directory, sha + '_' + hex_md5(path))
    assert os.path.exists(sha_path)
    assert os.path.islink(sha_path)
    assert os.readlink(sha_path) == ret
Example #4
0
def test_autoupdate_old_revision_broken(
    tempdir_factory, in_tmpdir, mock_out_store_directory,
):
    """In $FUTURE_VERSION, hooks.yaml will no longer be supported.  This
    asserts that when that day comes, pre-commit will be able to autoupdate
    despite not being able to read hooks.yaml in that repository.
    """
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path, check=False)

    with cwd(path):
        cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml')
        cmd_output('git', 'commit', '-m', 'simulate old repo')
        # Assume this is the revision the user's old repository was at
        rev = get_head_sha(path)
        cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE)
        cmd_output('git', 'commit', '-m', 'move hooks file')
        update_rev = get_head_sha(path)

    config['sha'] = rev
    write_config('.', config)
    before = open(C.CONFIG_FILE).read()
    ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
    after = open(C.CONFIG_FILE).read()
    assert ret == 0
    assert before != after
    assert update_rev in after
Example #5
0
def test_autoupdate_old_revision_broken(
    tempdir_factory,
    in_tmpdir,
    mock_out_store_directory,
):
    """In $FUTURE_VERSION, hooks.yaml will no longer be supported.  This
    asserts that when that day comes, pre-commit will be able to autoupdate
    despite not being able to read hooks.yaml in that repository.
    """
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path, check=False)

    with cwd(path):
        cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml')
        cmd_output('git', 'commit', '-m', 'simulate old repo')
        # Assume this is the revision the user's old repository was at
        rev = get_head_sha(path)
        cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE)
        cmd_output('git', 'commit', '-m', 'move hooks file')
        update_rev = get_head_sha(path)

    config['sha'] = rev
    write_config('.', config)
    before = open(C.CONFIG_FILE).read()
    ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
    after = open(C.CONFIG_FILE).read()
    assert ret == 0
    assert before != after
    assert update_rev in after
Example #6
0
def out_of_date_repo(tmpdir_factory):
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    original_sha = get_head_sha(path)

    # Make a commit
    with cwd(path):
        cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
    head_sha = get_head_sha(path)

    yield auto_namedtuple(
        path=path, original_sha=original_sha, head_sha=head_sha,
    )
Example #7
0
def out_of_date_repo(tmpdir_factory):
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    original_sha = get_head_sha(path)

    # Make a commit
    with local.cwd(path):
        local['git']['commit', '--allow-empty', '-m', 'foo']()
    head_sha = get_head_sha(path)

    yield auto_namedtuple(
        path=path, original_sha=original_sha, head_sha=head_sha,
    )
Example #8
0
def make_config_from_repo(
    repo_path,
    sha=None,
    hooks=None,
    check=True,
    legacy=False,
):
    filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE
    manifest = load_manifest(os.path.join(repo_path, filename))
    config = OrderedDict((
        ('repo', repo_path),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks
            or [OrderedDict((('id', hook['id']), )) for hook in manifest],
        ),
    ))

    if check:
        wrapped = validate([config], CONFIG_SCHEMA)
        config, = apply_defaults(wrapped, CONFIG_SCHEMA)
        return config
    else:
        return config
Example #9
0
def test_make_archive(tempdir_factory):
    output_dir = tempdir_factory.get()
    git_path = git_dir(tempdir_factory)
    # Add a files to the git directory
    with cwd(git_path):
        open('foo', 'a').close()
        cmd_output('git', 'add', '.')
        cmd_output('git', 'commit', '-m', 'foo')
        # We'll use this sha
        head_sha = get_head_sha('.')
        # And check that this file doesn't exist
        open('bar', 'a').close()
        cmd_output('git', 'add', '.')
        cmd_output('git', 'commit', '-m', 'bar')

    # Do the thing
    archive_path = make_archives.make_archive(
        'foo', git_path, head_sha, output_dir,
    )

    assert archive_path == os.path.join(output_dir, 'foo.tar.gz')
    assert os.path.exists(archive_path)

    extract_dir = tempdir_factory.get()

    # Extract the tar
    with tarfile.open(archive_path) as tf:
        tf.extractall(extract_dir)

    # Verify the contents of the tar
    assert os.path.exists(os.path.join(extract_dir, 'foo'))
    assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo'))
    assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git'))
    assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar'))
Example #10
0
def test_make_archive(tmpdir_factory):
    output_dir = tmpdir_factory.get()
    git_path = git_dir(tmpdir_factory)
    # Add a files to the git directory
    with local.cwd(git_path):
        local['touch']('foo')
        local['git']('add', '.')
        local['git']('commit', '-m', 'foo')
        # We'll use this sha
        head_sha = get_head_sha('.')
        # And check that this file doesn't exist
        local['touch']('bar')
        local['git']('add', '.')
        local['git']('commit', '-m', 'bar')

    # Do the thing
    archive_path = make_archives.make_archive(
        'foo', git_path, head_sha, output_dir,
    )

    assert archive_path == os.path.join(output_dir, 'foo.tar.gz')
    assert os.path.exists(archive_path)

    extract_dir = tmpdir_factory.get()

    # Extract the tar
    with tarfile_open(archive_path) as tf:
        tf.extractall(extract_dir)

    # Verify the contents of the tar
    assert os.path.exists(os.path.join(extract_dir, 'foo'))
    assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo'))
    assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git'))
    assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar'))
Example #11
0
def test_default_python_language_version(store, tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    repo_path = store.clone(path, get_head_sha(path))
    manifest = Manifest(repo_path, path)

    # This assertion is difficult as it is version dependent, just assert
    # that it is *something*
    assert manifest.hooks['foo']['language_version'] != 'default'
Example #12
0
def test_default_python_language_version(store, tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    repo_path = store.clone(path, get_head_sha(path))
    manifest = Manifest(repo_path, path)

    # This assertion is difficult as it is version dependent, just assert
    # that it is *something*
    assert manifest.hooks['foo']['language_version'] != 'default'
Example #13
0
def hook_disappearing_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_sha = get_head_sha(path)

    with cwd(path):
        shutil.copy(
            get_resource_path('manifest_without_foo.yaml'),
            C.MANIFEST_FILE,
        )
        cmd_output('git', 'add', '.')
        cmd_output('git', 'commit', '-m', 'Remove foo')

    yield auto_namedtuple(path=path, original_sha=original_sha)
Example #14
0
def hook_disappearing_repo(tmpdir_factory):
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    original_sha = get_head_sha(path)

    with local.cwd(path):
        shutil.copy(
            get_resource_path('manifest_without_foo.yaml'),
            C.MANIFEST_FILE,
        )
        local['git']('add', '.')
        local['git']('commit', '-m', 'Remove foo')

    yield auto_namedtuple(path=path, original_sha=original_sha)
Example #15
0
def test_legacy_manifest_warn(store, tempdir_factory, log_warning_mock):
    path = make_repo(tempdir_factory, 'legacy_hooks_yaml_repo')
    head_sha = get_head_sha(path)
    repo_path = store.clone(path, head_sha)
    Manifest(repo_path, path).manifest_contents

    # Should have printed a warning
    assert log_warning_mock.call_args_list[0][0][0] == (
        '{} uses legacy hooks.yaml to provide hooks.\n'
        'In newer versions, this file is called .pre-commit-hooks.yaml\n'
        'This will work in this version of pre-commit but will be removed at '
        'a later time.\n'
        'If `pre-commit autoupdate` does not silence this warning consider '
        'making an issue / pull request.'.format(path))
Example #16
0
def test_legacy_manifest_warn(store, tempdir_factory, log_warning_mock):
    path = make_repo(tempdir_factory, 'legacy_hooks_yaml_repo')
    head_sha = get_head_sha(path)
    repo_path = store.clone(path, head_sha)
    Manifest(repo_path, path).manifest_contents

    # Should have printed a warning
    assert log_warning_mock.call_args_list[0][0][0] == (
        '{} uses legacy hooks.yaml to provide hooks.\n'
        'In newer versions, this file is called .pre-commit-hooks.yaml\n'
        'This will work in this version of pre-commit but will be removed at '
        'a later time.\n'
        'If `pre-commit autoupdate` does not silence this warning consider '
        'making an issue / pull request.'.format(path)
    )
Example #17
0
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
    manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
    config = OrderedDict((
        ('repo', repo_path),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
        ),
    ))

    if check:
        wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
        validate_config_extra(wrapped_config)
        return wrapped_config[0]
    else:
        return config
Example #18
0
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
    manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
    config = OrderedDict((
        ('repo', repo_path),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
        ),
    ))

    if check:
        wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA)
        validate_config_extra(wrapped_config)
        return wrapped_config[0]
    else:
        return config
Example #19
0
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
    manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
    config = OrderedDict((
        ('repo', 'file://{}'.format(repo_path)),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
        ),
    ))

    if check:
        wrapped = validate({'repos': [config]}, CONFIG_SCHEMA)
        wrapped = apply_defaults(wrapped, CONFIG_SCHEMA)
        config, = wrapped['repos']
        return config
    else:
        return config
Example #20
0
def make_config_from_repo(
        repo_path, sha=None, hooks=None, check=True, legacy=False,
):
    filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE
    manifest = load_manifest(os.path.join(repo_path, filename))
    config = OrderedDict((
        ('repo', repo_path),
        ('sha', sha or get_head_sha(repo_path)),
        (
            'hooks',
            hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest],
        ),
    ))

    if check:
        wrapped = validate([config], CONFIG_SCHEMA)
        config, = apply_defaults(wrapped, CONFIG_SCHEMA)
        return config
    else:
        return config
Example #21
0
def manifest(store, tempdir_factory):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    head_sha = get_head_sha(path)
    repo_path = store.clone(path, head_sha)
    yield Manifest(repo_path, path)
Example #22
0
def manifest(store, tmpdir_factory):
    path = make_repo(tmpdir_factory, 'script_hooks_repo')
    head_sha = get_head_sha(path)
    repo_path_getter = store.get_repo_path_getter(path, head_sha)
    yield Manifest(repo_path_getter)
Example #23
0
def manifest(store, tempdir_factory):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    head_sha = get_head_sha(path)
    repo_path = store.clone(path, head_sha)
    yield Manifest(repo_path, path)