Esempio n. 1
0
def test_tags_on_repositories(in_tmpdir, tempdir_factory, store):
    tag = "v1.1"
    git_dir_1 = _create_repo_with_tags(tempdir_factory, "prints_cwd_repo", tag)
    git_dir_2 = _create_repo_with_tags(tempdir_factory, "script_hooks_repo", tag)

    repo_1 = Repository.create(make_config_from_repo(git_dir_1, sha=tag), store)
    ret = repo_1.run_hook(repo_1.hooks[0][1], ["-L"])
    assert ret[0] == 0
    assert ret[1].strip() == _norm_pwd(in_tmpdir)

    repo_2 = Repository.create(make_config_from_repo(git_dir_2, sha=tag), store)
    ret = repo_2.run_hook(repo_2.hooks[0][1], ["bar"])
    assert ret[0] == 0
    assert ret[1] == b"bar\nHello World\n"
Esempio n. 2
0
def test_autoupdate_old_revision_broken(tempdir_factory, in_tmpdir, store):
    """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)

    cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml', cwd=path)
    git_commit(cwd=path)
    # Assume this is the revision the user's old repository was at
    rev = git.head_rev(path)
    cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE, cwd=path)
    git_commit(cwd=path)
    update_rev = git.head_rev(path)

    config['rev'] = rev
    write_config('.', config)
    with open(C.CONFIG_FILE) as f:
        before = f.read()
    ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
    with open(C.CONFIG_FILE) as f:
        after = f.read()
    assert ret == 0
    assert before != after
    assert update_rev in after
Esempio n. 3
0
def test_control_c_control_c_on_install(tmpdir_factory, store):
    """Regression test for #186."""
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    repo = Repository.create(config, store)
    hook = repo.hooks[0][1]

    class MyKeyboardInterrupt(KeyboardInterrupt):
        pass

    # To simulate a killed install, we'll make PythonEnv.run raise ^C
    # and then to simulate a second ^C during cleanup, we'll make shutil.rmtree
    # raise as well.
    with pytest.raises(MyKeyboardInterrupt):
        with mock.patch.object(
            PythonEnv, 'run', side_effect=MyKeyboardInterrupt,
        ):
            with mock.patch.object(
                shutil, 'rmtree', side_effect=MyKeyboardInterrupt,
            ):
                repo.run_hook(hook, [])

    # Should have made an environment, however this environment is broken!
    assert os.path.exists(repo.cmd_runner.path('py_env'))

    # However, it should be perfectly runnable (reinstall after botched
    # install)
    retv, stdout, stderr = repo.run_hook(hook, [])
    assert retv == 0
Esempio n. 4
0
def test_out_of_date_repo(out_of_date_repo, store):
    config = make_config_from_repo(
        out_of_date_repo.path, rev=out_of_date_repo.original_rev,
    )
    ret = _update_repo(config, store, tags_only=False)
    assert ret['rev'] != out_of_date_repo.original_rev
    assert ret['rev'] == out_of_date_repo.head_rev
Esempio n. 5
0
def test_versions_ok(tempdir_factory, store, version):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    with modify_manifest(path) as manifest:
        manifest[0]['minimum_pre_commit_version'] = version
    config = make_config_from_repo(path)
    # Should succeed
    Repository.create(config, store).require_installed()
Esempio n. 6
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
Esempio n. 7
0
def test_additional_dependencies(tempdir_factory, store):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['pep8']
    repo = Repository.create(config, store)
    venv, = repo._venvs
    assert venv == (mock.ANY, 'python', 'default', ['pep8'])
Esempio n. 8
0
def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
    config = make_config_from_repo(
        out_of_date_repo.path, sha=out_of_date_repo.original_sha,
    )
    ret = _update_repository(config, runner_with_mocked_store)
    assert ret['sha'] != out_of_date_repo.original_sha
    assert ret['sha'] == out_of_date_repo.head_sha
Esempio n. 9
0
 def run_on_version(version, expected_output):
     config = make_config_from_repo(path, hooks=[{"id": "python3-hook", "language_version": version}])
     repo = Repository.create(config, store)
     hook_dict, = [hook for repo_hook_id, hook in repo.hooks if repo_hook_id == "python3-hook"]
     ret = repo.run_hook(hook_dict, [])
     assert ret[0] == 0
     assert ret[1].replace(b"\r\n", b"\n") == expected_output
Esempio n. 10
0
def test_hook_disppearing_repo_raises(hook_disappearing_repo, store):
    config = make_config_from_repo(
        hook_disappearing_repo.path,
        rev=hook_disappearing_repo.original_rev,
        hooks=[{'id': 'foo'}],
    )
    with pytest.raises(RepositoryCannotBeUpdatedError):
        _update_repo(config, store, tags_only=False)
Esempio n. 11
0
def test_additional_python_dependencies_installed(tempdir_factory, store):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['mccabe']
    repo = Repository.create(config, store)
    repo.run_hook(repo.hooks[0][1], [])
    with python.in_env(repo.cmd_runner, 'default') as env:
        output = env.run('pip freeze -l')[1]
        assert 'mccabe' in output
Esempio n. 12
0
def test_additional_python_dependencies_installed(tempdir_factory, store):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['mccabe']
    repo = Repository.create(config, store)
    repo.require_installed()
    with python.in_env(repo._cmd_runner, 'default'):
        output = cmd_output('pip', 'freeze', '-l')[1]
        assert 'mccabe' in output
Esempio n. 13
0
def test_hook_disppearing_repo_raises(
        hook_disappearing_repo, runner_with_mocked_store
):
    config = make_config_from_repo(
        hook_disappearing_repo.path,
        sha=hook_disappearing_repo.original_sha,
        hooks=[OrderedDict((('id', 'foo'),))],
    )
    with pytest.raises(RepositoryCannotBeUpdatedError):
        _update_repository(config, runner_with_mocked_store)
Esempio n. 14
0
def test_additional_dependencies_duplicated(
        tempdir_factory, store, log_warning_mock,
):
    path = make_repo(tempdir_factory, 'ruby_hooks_repo')
    config = make_config_from_repo(path)
    deps = ['thread_safe', 'tins', 'thread_safe']
    config['hooks'][0]['additional_dependencies'] = deps
    repo = Repository.create(config, store)
    venv, = repo._venvs
    assert venv == (mock.ANY, 'ruby', 'default', ['thread_safe', 'tins'])
Esempio n. 15
0
def test_autoupdate_tagged_repo(tagged_repo, in_tmpdir, store):
    config = make_config_from_repo(
        tagged_repo.path, rev=tagged_repo.original_rev,
    )
    write_config('.', config)

    ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
    assert ret == 0
    with open(C.CONFIG_FILE) as f:
        assert 'v1.2.3' in f.read()
Esempio n. 16
0
def test_config_overrides_repo_specifics(tempdir_factory, store):
    path = make_repo(tempdir_factory, "script_hooks_repo")
    config = make_config_from_repo(path)

    repo = Repository.create(config, store)
    assert repo.hooks[0][1]["files"] == ""
    # Set the file regex to something else
    config["hooks"][0]["files"] = "\\.sh$"
    repo = Repository.create(config, store)
    assert repo.hooks[0][1]["files"] == "\\.sh$"
Esempio n. 17
0
def _test_hook_repo(
    tempdir_factory, store, repo_path, hook_id, args, expected, expected_return_code=0, config_kwargs=None
):
    path = make_repo(tempdir_factory, repo_path)
    config = make_config_from_repo(path, **(config_kwargs or {}))
    repo = Repository.create(config, store)
    hook_dict = [hook for repo_hook_id, hook in repo.hooks if repo_hook_id == hook_id][0]
    ret = repo.run_hook(hook_dict, args)
    assert ret[0] == expected_return_code
    assert ret[1].replace(b"\r\n", b"\n") == expected
Esempio n. 18
0
def test_config_overrides_repo_specifics(tmpdir_factory, store):
    path = make_repo(tmpdir_factory, 'script_hooks_repo')
    config = make_config_from_repo(path)

    repo = Repository.create(config, store)
    assert repo.hooks[0][1]['files'] == ''
    # Set the file regex to something else
    config['hooks'][0]['files'] = '\\.sh$'
    repo = Repository.create(config, store)
    assert repo.hooks[0][1]['files'] == '\\.sh$'
Esempio n. 19
0
def test_really_long_file_paths(tmpdir_factory, store):
    base_path = tmpdir_factory.get()
    really_long_path = os.path.join(base_path, 'really_long' * 10)
    cmd_output('git', 'init', really_long_path)

    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)

    with cwd(really_long_path):
        repo = Repository.create(config, store)
        repo.require_installed()
Esempio n. 20
0
def test_gc_repo_not_cloned(tempdir_factory, store, in_git_dir, cap_out):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    write_config('.', make_config_from_repo(path))
    store.mark_config_used(C.CONFIG_FILE)

    assert _config_count(store) == 1
    assert _repo_count(store) == 0
    assert not gc(store)
    assert _config_count(store) == 1
    assert _repo_count(store) == 0
    assert cap_out.get().splitlines()[-1] == '0 repo(s) removed.'
Esempio n. 21
0
def test_really_long_file_paths(tempdir_factory, store):
    base_path = tempdir_factory.get()
    really_long_path = os.path.join(base_path, "really_long" * 10)
    cmd_output("git", "init", really_long_path)

    path = make_repo(tempdir_factory, "python_hooks_repo")
    config = make_config_from_repo(path)

    with cwd(really_long_path):
        repo = Repository.create(config, store)
        repo.require_installed()
Esempio n. 22
0
def test_tags_on_repositories(in_tmpdir, tmpdir_factory, store):
    tag = 'v1.1'
    git_dir_1 = _create_repo_with_tags(tmpdir_factory, 'prints_cwd_repo', tag)
    git_dir_2 = _create_repo_with_tags(
        tmpdir_factory, 'script_hooks_repo', tag,
    )

    repo_1 = Repository.create(
        make_config_from_repo(git_dir_1, sha=tag), store,
    )
    ret = repo_1.run_hook(repo_1.hooks[0][1], ['-L'])
    assert ret[0] == 0
    assert ret[1].strip() == _norm_pwd(in_tmpdir)

    repo_2 = Repository.create(
        make_config_from_repo(git_dir_2, sha=tag), store,
    )
    ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar'])
    assert ret[0] == 0
    assert ret[1] == 'bar\nHello World\n'
Esempio n. 23
0
def test_additional_ruby_dependencies_installed(
        tempdir_factory, store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'ruby_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['thread_safe']
    repo = Repository.create(config, store)
    repo.run_hook(repo.hooks[0][1], [])
    with ruby.in_env(repo.cmd_runner, 'default') as env:
        output = env.run('gem list --local')[1]
        assert 'thread_safe' in output
Esempio n. 24
0
def test_autoupdate_tagged_repo(
        tagged_repo, in_tmpdir, mock_out_store_directory,
):
    config = make_config_from_repo(
        tagged_repo.path, sha=tagged_repo.original_sha,
    )
    write_config('.', config)

    ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
    assert ret == 0
    assert 'v1.2.3' in open(C.CONFIG_FILE).read()
Esempio n. 25
0
def test_additional_ruby_dependencies_installed(
        tempdir_factory, store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'ruby_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins']
    repo = Repository.create(config, store)
    repo.require_installed()
    with ruby.in_env(repo._cmd_runner, 'default'):
        output = cmd_output('gem', 'list', '--local')[1]
        assert 'thread_safe' in output
        assert 'tins' in output
Esempio n. 26
0
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['id'] = 'i-dont-exist'
    repo = Repository.create(config, store)
    with pytest.raises(SystemExit):
        repo.require_installed()
    assert fake_log_handler.handle.call_args[0][0].msg == (
        '`i-dont-exist` is not present in repository {}.  '
        'Typo? Perhaps it is introduced in a newer version?  '
        'Often `pre-commit autoupdate` fixes this.'.format(path)
    )
Esempio n. 27
0
def test_reinstall(tmpdir_factory, store):
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    repo = Repository.create(config, store)
    repo.require_installed()
    # Reinstall with same repo should not trigger another install
    # TODO: how to assert this?
    repo.require_installed()
    # Reinstall on another run should not trigger another install
    # TODO: how to assert this?
    repo = Repository.create(config, store)
    repo.require_installed()
Esempio n. 28
0
 def run_on_version(version, expected_output):
     config = make_config_from_repo(
         path, hooks=[{'id': 'python3-hook', 'language_version': version}],
     )
     repo = Repository.create(config, store)
     hook_dict, = [
         hook
         for repo_hook_id, hook in repo.hooks
         if repo_hook_id == 'python3-hook'
     ]
     ret = repo.run_hook(hook_dict, [])
     assert ret[0] == 0
     assert ret[1].replace(b'\r\n', b'\n') == expected_output
Esempio n. 29
0
def test_additional_node_dependencies_installed(
        tempdir_factory, store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'node_hooks_repo')
    config = make_config_from_repo(path)
    # Careful to choose a small package that's not depped by npm
    config['hooks'][0]['additional_dependencies'] = ['lodash']
    repo = Repository.create(config, store)
    repo.require_installed()
    with node.in_env(repo._cmd_runner, 'default'):
        cmd_output('npm', 'config', 'set', 'global', 'true')
        output = cmd_output('npm', 'ls')[1]
        assert 'lodash' in output
Esempio n. 30
0
def test_additional_node_dependencies_installed(
        tempdir_factory, store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'node_hooks_repo')
    config = make_config_from_repo(path)
    # Careful to choose a small package that's not depped by npm
    config['hooks'][0]['additional_dependencies'] = ['lodash']
    repo = Repository.create(config, store)
    repo.run_hook(repo.hooks[0][1], [])
    with node.in_env(repo.cmd_runner, 'default') as env:
        env.run('npm config set global true')
        output = env.run(('npm ls'))[1]
        assert 'lodash' in output
Esempio n. 31
0
def test_additional_dependencies_roll_forward(tempdir_factory, store):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    # Run the repo once without additional_dependencies
    repo = Repository.create(config, store)
    repo.require_installed()
    # Now run it with additional_dependencies
    config['hooks'][0]['additional_dependencies'] = ['mccabe']
    repo = Repository.create(config, store)
    repo.require_installed()
    # We should see our additional dependency installed
    with python.in_env(repo.cmd_runner, 'default'):
        output = cmd_output('pip', 'freeze', '-l')[1]
        assert 'mccabe' in output
Esempio n. 32
0
def test_additional_ruby_dependencies_installed(
    tempdir_factory,
    store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'ruby_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['additional_dependencies'] = ['thread_safe', 'tins']
    repo = Repository.create(config, store)
    repo.require_installed()
    (prefix, _, version, _), = repo._venvs()
    with ruby.in_env(prefix, version):
        output = cmd_output('gem', 'list', '--local')[1]
        assert 'thread_safe' in output
        assert 'tins' in output
Esempio n. 33
0
def test_additional_node_dependencies_installed(
    tempdir_factory,
    store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'node_hooks_repo')
    config = make_config_from_repo(path)
    # Careful to choose a small package that's not depped by npm
    config['hooks'][0]['additional_dependencies'] = ['lodash']
    repo = Repository.create(config, store)
    repo.require_installed()
    (prefix, _, version, _), = repo._venvs()
    with node.in_env(prefix, version):
        output = cmd_output('npm', 'ls', '-g')[1]
        assert 'lodash' in output
Esempio n. 34
0
def test_autoupdate_local_hooks_with_out_of_date_repo(
        out_of_date_repo, in_tmpdir, mock_out_store_directory,
):
    stale_config = make_config_from_repo(
        out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
    )
    local_config = config_with_local_hooks()
    config = {'repos': [local_config, stale_config]}
    write_config('.', config)
    runner = Runner('.', C.CONFIG_FILE)
    assert autoupdate(runner, tags_only=False) == 0
    new_config_writen = load_config(runner.config_file_path)
    assert len(new_config_writen['repos']) == 2
    assert new_config_writen['repos'][0] == local_config
Esempio n. 35
0
def test_autoupdate_latest_no_config(out_of_date_repo, in_tmpdir, store):
    config = make_config_from_repo(
        out_of_date_repo.path,
        rev=out_of_date_repo.original_rev,
    )
    write_config('.', config)

    cmd_output('git', 'rm', '-r', ':/', cwd=out_of_date_repo.path)
    git_commit(cwd=out_of_date_repo.path)

    ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
    assert ret == 1
    with open(C.CONFIG_FILE) as f:
        assert out_of_date_repo.original_rev in f.read()
def test_too_new_version(tempdir_factory, store, caplog):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    with modify_manifest(path) as manifest:
        manifest[0]['minimum_pre_commit_version'] = '999.0.0'
    config = make_config_from_repo(path)
    with pytest.raises(SystemExit):
        _get_hook(config, store, 'bash_hook')
    _, msg = caplog.messages
    pattern = re_assert.Matches(
        r'^The hook `bash_hook` requires pre-commit version 999\.0\.0 but '
        r'version \d+\.\d+\.\d+ is installed.  '
        r'Perhaps run `pip install --upgrade pre-commit`\.$',
    )
    pattern.assert_matches(msg)
Esempio n. 37
0
def test_autoupdate_tags_only(
    tagged_repo_with_more_commits,
    in_tmpdir,
    mock_out_store_directory,
):
    config = make_config_from_repo(
        tagged_repo_with_more_commits.path,
        sha=tagged_repo_with_more_commits.original_sha,
    )
    write_config('.', config)

    ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=True)
    assert ret == 0
    assert 'v1.2.3' in open(C.CONFIG_FILE).read()
def test_useless_excludes_broken_symlink(capsys, in_git_dir, tempdir_factory):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['exclude'] = 'broken-symlink'
    add_config_to_repo(in_git_dir.strpath, config)

    in_git_dir.join('broken-symlink').mksymlinkto('DNE')
    cmd_output('git', 'add', 'broken-symlink')
    git.commit()

    assert check_useless_excludes.main(('.pre-commit-config.yaml', )) == 0

    out, _ = capsys.readouterr()
    assert out == ''
Esempio n. 39
0
def test_too_new_version(tempdir_factory, store, fake_log_handler):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    with modify_manifest(path) as manifest:
        manifest[0]['minimum_pre_commit_version'] = '999.0.0'
    config = make_config_from_repo(path)
    with pytest.raises(SystemExit):
        _get_hook(config, store, 'bash_hook')
    msg = fake_log_handler.handle.call_args[0][0].msg
    assert re.match(
        r'^The hook `bash_hook` requires pre-commit version 999\.0\.0 but '
        r'version \d+\.\d+\.\d+ is installed.  '
        r'Perhaps run `pip install --upgrade pre-commit`\.$',
        msg,
    )
Esempio n. 40
0
def test_autoupdate_up_to_date_repo(
        up_to_date_repo, in_tmpdir, mock_out_store_directory,
):
    # Write out the config
    config = make_config_from_repo(up_to_date_repo, check=False)
    write_config('.', config)

    before = open(C.CONFIG_FILE).read()
    assert '^$' not in before
    runner = Runner('.')
    ret = autoupdate(runner)
    after = open(C.CONFIG_FILE).read()
    assert ret == 0
    assert before == after
Esempio n. 41
0
def test_autoupdate_tagged_repo(
    tagged_repo,
    in_tmpdir,
    mock_out_store_directory,
):
    config = make_config_from_repo(
        tagged_repo.path,
        sha=tagged_repo.original_sha,
    )
    write_config('.', config)

    ret = autoupdate(Runner('.'))
    assert ret == 0
    assert 'v1.2.3' in open(C.CONFIG_FILE).read()
Esempio n. 42
0
def test_additional_node_dependencies_installed(
    tempdir_factory,
    store,
):  # pragma: no cover (non-windows)
    path = make_repo(tempdir_factory, 'node_hooks_repo')
    config = make_config_from_repo(path)
    # Careful to choose a small package that's not depped by npm
    config['hooks'][0]['additional_dependencies'] = ['lodash']
    repo = Repository.create(config, store)
    repo.require_installed()
    with node.in_env(repo._cmd_runner, 'default'):
        cmd_output('npm', 'config', 'set', 'global', 'true')
        output = cmd_output('npm', 'ls')[1]
        assert 'lodash' in output
Esempio n. 43
0
def test_hook_id_not_present(tempdir_factory, store, fake_log_handler):
    path = make_repo(tempdir_factory, 'script_hooks_repo')
    config = make_config_from_repo(path)
    config['hooks'][0]['id'] = 'i-dont-exist'
    repo = Repository.create(config, store)
    with pytest.raises(SystemExit):
        repo.install()
    assert fake_log_handler.handle.call_args[0][0].msg == (
        '`i-dont-exist` is not present in repository {}.  '
        'Typo? Perhaps it is introduced in a newer version?  '
        'Often you can fix this by removing the hook, '
        'running `pre-commit autoupdate`, '
        'and then adding the hook.'.format(path)
    )
Esempio n. 44
0
def test_autoupdate_local_hooks_with_out_of_date_repo(
        out_of_date_repo, in_tmpdir, mock_out_store_directory):
    stale_config = make_config_from_repo(
        out_of_date_repo.path,
        sha=out_of_date_repo.original_sha,
        check=False,
    )
    local_config = config_with_local_hooks()
    config = [local_config, stale_config]
    write_config('.', config)
    runner = Runner('.')
    assert autoupdate(runner) == 0
    new_config_writen = load_config(runner.config_file_path)
    assert len(new_config_writen) == 2
    assert new_config_writen[0] == local_config
Esempio n. 45
0
def test_autoupdate_hook_disappearing_repo(hook_disappearing_repo, in_tmpdir,
                                           mock_out_store_directory):
    config = make_config_from_repo(
        hook_disappearing_repo.path,
        sha=hook_disappearing_repo.original_sha,
        hooks=[OrderedDict((('id', 'foo'), ))],
        check=False,
    )
    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 == 1
    assert before == after
Esempio n. 46
0
def test_tags_on_repositories(in_tmpdir, tempdir_factory, store):
    tag = 'v1.1'
    git_dir_1 = _create_repo_with_tags(tempdir_factory, 'prints_cwd_repo', tag)
    git_dir_2 = _create_repo_with_tags(
        tempdir_factory,
        'script_hooks_repo',
        tag,
    )

    repo_1 = Repository.create(
        make_config_from_repo(git_dir_1, rev=tag),
        store,
    )
    ret = repo_1.run_hook(repo_1.hooks[0][1], ['-L'])
    assert ret[0] == 0
    assert ret[1].strip() == _norm_pwd(in_tmpdir)

    repo_2 = Repository.create(
        make_config_from_repo(git_dir_2, rev=tag),
        store,
    )
    ret = repo_2.run_hook(repo_2.hooks[0][1], ['bar'])
    assert ret[0] == 0
    assert ret[1] == b'bar\nHello World\n'
Esempio n. 47
0
def _test_hook_repo(
        tempdir_factory,
        store,
        repo_path,
        hook_id,
        args,
        expected,
        expected_return_code=0,
        config_kwargs=None,
):
    path = make_repo(tempdir_factory, repo_path)
    config = make_config_from_repo(path, **(config_kwargs or {}))
    ret = _get_hook(config, store, hook_id).run(args)
    assert ret[0] == expected_return_code
    assert _norm_out(ret[1]) == expected
Esempio n. 48
0
def test_reinstall(tempdir_factory, store, log_info_mock):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    config = make_config_from_repo(path)
    repo = Repository.create(config, store)
    repo.require_installed()
    # We print some logging during clone (1) + install (3)
    assert log_info_mock.call_count == 4
    log_info_mock.reset_mock()
    # Reinstall with same repo should not trigger another install
    repo.require_installed()
    assert log_info_mock.call_count == 0
    # Reinstall on another run should not trigger another install
    repo = Repository.create(config, store)
    repo.require_installed()
    assert log_info_mock.call_count == 0
Esempio n. 49
0
def test_additional_golang_dependencies_installed(
        tempdir_factory, store,
):
    path = make_repo(tempdir_factory, 'golang_hooks_repo')
    config = make_config_from_repo(path)
    # A small go package
    deps = ['github.com/golang/example/hello']
    config['hooks'][0]['additional_dependencies'] = deps
    repo = Repository.create(config, store)
    repo.require_installed()
    binaries = os.listdir(repo._cmd_runner.path(
        helpers.environment_dir(golang.ENVIRONMENT_DIR, 'default'), 'bin',
    ))
    # normalize for windows
    binaries = [os.path.splitext(binary)[0] for binary in binaries]
    assert 'hello' in binaries
Esempio n. 50
0
def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
        out_of_date_repo, in_tmpdir, mock_out_store_directory,
):
    # Write out the config
    config = make_config_from_repo(
        out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
    )
    write_config('.', config)

    runner = Runner('.', C.CONFIG_FILE)
    before = open(C.CONFIG_FILE).read()
    # It will not update it, because the name doesn't match
    ret = autoupdate(runner, tags_only=False, repos=('wrong_repo_name',))
    after = open(C.CONFIG_FILE).read()
    assert ret == 0
    assert before == after
Esempio n. 51
0
def test_additional_rust_cli_dependencies_installed(
        tempdir_factory, store, dep,
):
    path = make_repo(tempdir_factory, 'rust_hooks_repo')
    config = make_config_from_repo(path)
    # A small rust package with no dependencies.
    config['hooks'][0]['additional_dependencies'] = [dep]
    repo = Repository.create(config, store)
    repo.require_installed()
    (prefix, _, _, _), = repo._venvs()
    binaries = os.listdir(prefix.path(
        helpers.environment_dir(rust.ENVIRONMENT_DIR, 'default'), 'bin',
    ))
    # normalize for windows
    binaries = [os.path.splitext(binary)[0] for binary in binaries]
    assert 'shellharden' in binaries
Esempio n. 52
0
 def run_on_version(version, expected_output):
     config = make_config_from_repo(
         path,
         hooks=[{
             'id': 'python3-hook',
             'language_version': version
         }],
     )
     repo = Repository.create(config, store)
     hook_dict, = [
         hook for repo_hook_id, hook in repo.hooks
         if repo_hook_id == 'python3-hook'
     ]
     ret = repo.run_hook(hook_dict, [])
     assert ret[0] == 0
     assert _norm_out(ret[1]) == expected_output
Esempio n. 53
0
def test_additional_rust_cli_dependencies_installed(
        tempdir_factory, store, dep,
):
    path = make_repo(tempdir_factory, 'rust_hooks_repo')
    config = make_config_from_repo(path)
    # A small rust package with no dependencies.
    config['hooks'][0]['additional_dependencies'] = [dep]
    hook = _get_hook(config, store, 'rust-hook')
    binaries = os.listdir(
        hook.prefix.path(
            helpers.environment_dir(rust.ENVIRONMENT_DIR, C.DEFAULT), 'bin',
        ),
    )
    # normalize for windows
    binaries = [os.path.splitext(binary)[0] for binary in binaries]
    assert 'shellharden' in binaries
Esempio n. 54
0
def _test_hook_repo(tmpdir_factory,
                    store,
                    repo_path,
                    hook_id,
                    args,
                    expected,
                    expected_return_code=0,
                    config_kwargs=None):
    path = make_repo(tmpdir_factory, repo_path)
    config = make_config_from_repo(path, **(config_kwargs or {}))
    repo = Repository.create(config, store)
    hook_dict = [
        hook for repo_hook_id, hook in repo.hooks if repo_hook_id == hook_id
    ][0]
    ret = repo.run_hook(hook_dict, args)
    assert ret[0] == expected_return_code
    assert ret[1].replace('\r\n', '\n') == expected
Esempio n. 55
0
def test_autoupdate_out_of_date_repo(
        out_of_date_repo, in_tmpdir, mock_out_store_directory,
):
    # Write out the config
    config = make_config_from_repo(
        out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
    )
    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
    # Make sure we don't add defaults
    assert 'exclude' not in after
    assert out_of_date_repo.head_sha in after
Esempio n. 56
0
def test_autoupdate_out_of_date_repo(out_of_date_repo, in_tmpdir, store):
    # Write out the config
    config = make_config_from_repo(
        out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
    )
    write_config('.', config)

    with open(C.CONFIG_FILE) as f:
        before = f.read()
    ret = autoupdate(C.CONFIG_FILE, store, tags_only=False)
    with open(C.CONFIG_FILE) as f:
        after = f.read()
    assert ret == 0
    assert before != after
    # Make sure we don't add defaults
    assert 'exclude' not in after
    assert out_of_date_repo.head_rev in after
Esempio n. 57
0
def test_additional_golang_dependencies_installed(
        tempdir_factory, store,
):
    path = make_repo(tempdir_factory, 'golang_hooks_repo')
    config = make_config_from_repo(path)
    # A small go package
    deps = ['github.com/golang/example/hello']
    config['hooks'][0]['additional_dependencies'] = deps
    hook = _get_hook(config, store, 'golang-hook')
    binaries = os.listdir(
        hook.prefix.path(
            helpers.environment_dir(golang.ENVIRONMENT_DIR, C.DEFAULT), 'bin',
        ),
    )
    # normalize for windows
    binaries = [os.path.splitext(binary)[0] for binary in binaries]
    assert 'hello' in binaries
Esempio n. 58
0
def test_autoupdate_out_of_date_repo_with_wrong_repo_name(
        out_of_date_repo, in_tmpdir, store,
):
    # Write out the config
    config = make_config_from_repo(
        out_of_date_repo.path, rev=out_of_date_repo.original_rev, check=False,
    )
    write_config('.', config)

    with open(C.CONFIG_FILE) as f:
        before = f.read()
    # It will not update it, because the name doesn't match
    ret = autoupdate(C.CONFIG_FILE, store, tags_only=False, repos=('dne',))
    with open(C.CONFIG_FILE) as f:
        after = f.read()
    assert ret == 0
    assert before == after
Esempio n. 59
0
def test_autoupdate_local_hooks_with_out_of_date_repo(
    out_of_date,
    in_tmpdir,
    store,
):
    stale_config = make_config_from_repo(
        out_of_date.path,
        rev=out_of_date.original_rev,
        check=False,
    )
    local_config = sample_local_config()
    config = {'repos': [local_config, stale_config]}
    write_config('.', config)
    assert autoupdate(C.CONFIG_FILE, store, freeze=False, tags_only=False) == 0
    new_config_writen = read_config('.')
    assert len(new_config_writen['repos']) == 2
    assert new_config_writen['repos'][0] == local_config
Esempio n. 60
0
def _test_hook_repo(
        tempdir_factory,
        store,
        repo_path,
        hook_id,
        args,
        expected,
        expected_return_code=0,
        config_kwargs=None,
        color=False,
):
    path = make_repo(tempdir_factory, repo_path)
    config = make_config_from_repo(path, **(config_kwargs or {}))
    hook = _get_hook(config, store, hook_id)
    ret, out = _hook_run(hook, args, color=color)
    assert ret == expected_return_code
    assert _norm_out(out) == expected