def test_uninstall_doesnt_remove_not_our_hooks(tmpdir_factory): path = git_dir(tmpdir_factory) with cwd(path): runner = Runner(path) with io.open(runner.pre_commit_path, 'w') as pre_commit_file: pre_commit_file.write('#!/usr/bin/env bash\necho 1\n') make_executable(runner.pre_commit_path) assert uninstall(runner) == 0 assert os.path.exists(runner.pre_commit_path)
def test_install_overwrite(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Write out the "old" hook with io.open(runner.pre_commit_path, 'w') as hook_file: hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n') make_executable(runner.pre_commit_path) assert install(runner, overwrite=True) == 0 ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_failing_existing_hook_returns_1(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Write out a failing "old" hook with io.open(runner.pre_commit_path, 'w') as hook_file: hook_file.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n') make_executable(runner.pre_commit_path) assert install(runner) == 0 # We should get a failure from the legacy hook ret, output = _get_commit_output(tmpdir_factory) assert ret == 1 assert FAIL_OLD_HOOK.match(output)
def test_uninstall_restores_legacy_hooks(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Write out an "old" hook with io.open(runner.pre_commit_path, 'w') as hook_file: hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n') make_executable(runner.pre_commit_path) # Now install and uninstall pre-commit assert install(runner) == 0 assert uninstall(runner) == 0 # Make sure we installed the "old" hook correctly ret, output = _get_commit_output(tmpdir_factory, touch_file='baz') assert ret == 0 assert EXISTING_COMMIT_RUN.match(output)
def test_install_existing_hook_no_overwrite_idempotent(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Write out an "old" hook with io.open(runner.pre_commit_path, 'w') as hook_file: hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n') make_executable(runner.pre_commit_path) # Install twice assert install(runner) == 0 assert install(runner) == 0 # We should run both the legacy and pre-commit hooks ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert output.startswith('legacy hook\n') assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
def test_replace_old_commit_script(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, "script_hooks_repo") with cwd(path): runner = Runner(path) # Install a script that looks like our old script pre_commit_contents = io.open(resource_filename("hook-tmpl")).read() new_contents = pre_commit_contents.replace(IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1]) with io.open(runner.pre_commit_path, "w") as pre_commit_file: pre_commit_file.write(new_contents) make_executable(runner.pre_commit_path) # Install normally assert install(runner) == 0 ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_replace_old_commit_script(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Install a script that looks like our old script pre_commit_contents = io.open(resource_filename('hook-tmpl'), ).read() new_contents = pre_commit_contents.replace( IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1], ) with io.open(runner.pre_commit_path, 'w') as pre_commit_file: pre_commit_file.write(new_contents) make_executable(runner.pre_commit_path) # Install normally assert install(runner) == 0 ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert NORMAL_PRE_COMMIT_RUN.match(output)
def test_install_existing_hooks_no_overwrite(tmpdir_factory): path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo') with cwd(path): runner = Runner(path) # Write out an "old" hook with io.open(runner.pre_commit_path, 'w') as hook_file: hook_file.write('#!/usr/bin/env bash\necho "legacy hook"\n') make_executable(runner.pre_commit_path) # Make sure we installed the "old" hook correctly ret, output = _get_commit_output(tmpdir_factory, touch_file='baz') assert ret == 0 assert EXISTING_COMMIT_RUN.match(output) # Now install pre-commit (no-overwrite) assert install(runner) == 0 # We should run both the legacy and pre-commit hooks ret, output = _get_commit_output(tmpdir_factory) assert ret == 0 assert output.startswith('legacy hook\n') assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])