Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/prepush_ci/init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _plan(options: Options, cwd: Path, root: Path, stdin: TextIO) -> _Plan:
hooks = _ask_hooks(root, cwd, stdin)
if session_hook:
session_hook = parse_yes_no(_ask(stdin, SESSION_QUESTION))
except Refusal as refusal:
except (Refusal, install_command.Unreadable) as refusal:
raise _Stop(f"prepush-ci init: {refusal}") from refusal
text = None
if config is None:
Expand Down
5 changes: 3 additions & 2 deletions src/prepush_ci/install_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ def found_files(root: Path | None) -> dict[str, str]:


def previous_pre_push(cwd: Path) -> Path | None:
"""The `pre-push` hook `install` would chain to, if one is there to run."""
"""The `pre-push` hook `install` would chain to, if one is there to run; unreadable is an
error."""
common = _resolve(cwd, git(cwd, "rev-parse", "--git-common-dir").stdout.strip())
directory = common.joinpath(*HOOKS_DIRECTORY)
hook = _previous(cwd, common, directory) / "pre-push"
return hook if hook.is_file() else None
return hook if hook.name in _listing(hook.parent) else None


def _marker(root: Path, name: str) -> bool:
Expand Down
64 changes: 64 additions & 0 deletions tests/integration/init_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,3 +1299,67 @@ def test_init_sets_up_a_repository_whose_path_has_a_non_ascii_character_under_an
assert "Traceback" not in result.stderr
assert result.returncode == 0, result.stderr
assert (where / ".prepush-ci.toml").is_file()


def a_repository_with_a_recorded_previous_hooks_directory(
repo: Path, git: Git, tmp_path: Path
) -> Path:
"""Hooks installed natively over a `core.hooksPath` directory holding a `pre-push` hook."""
(repo / ".prepush-ci.toml").write_text('check = "true"\n')
previous = tmp_path / "previous-hooks"
previous.mkdir()
(previous / "pre-push").write_text("#!/bin/sh\nexit 0\n")
git(repo, "config", "--local", "core.hooksPath", previous.as_posix())
installed = subprocess.run(
[sys.executable, "-m", "prepush_ci", "install", "--hooks", "native"],
cwd=repo,
capture_output=True,
text=True,
check=False,
)
assert installed.returncode == 0, installed.stderr
return previous


def refused_by_init_with_one_line(result: "subprocess.CompletedProcess[str]") -> None:
assert result.returncode == 1
assert "Traceback" not in result.stderr
assert len(result.stderr.strip().splitlines()) == 1
assert result.stderr.startswith("prepush-ci init: cannot read ")


@pytest.mark.skipif(
sys.platform == "win32", reason="a directory cannot be made unreadable with chmod"
)
def test_interactive_init_refuses_in_one_line_when_the_previous_hooks_directory_cannot_be_read(
repo: Path, git: Git, tmp_path: Path
) -> None:
previous = a_repository_with_a_recorded_previous_hooks_directory(
repo, git, tmp_path
)
previous.chmod(0o000)
try:
result = init(repo, stdin=answers("", ""))
finally:
previous.chmod(0o755) # so that the temporary directory can be removed

refused_by_init_with_one_line(result)
assert previous.as_posix() in result.stderr


@pytest.mark.parametrize("record", ["bytes", "directory"])
def test_interactive_init_refuses_in_one_line_when_the_record_of_the_previous_directory_is_unreadable(
repo: Path, git: Git, tmp_path: Path, record: str
) -> None:
a_repository_with_a_recorded_previous_hooks_directory(repo, git, tmp_path)
path = repo / ".git" / "prepush-ci" / "hooks" / "previous"
path.unlink()
if record == "bytes":
path.write_bytes(b"\xff\xfe")
else:
path.mkdir()

result = init(repo, stdin=answers("", ""))

refused_by_init_with_one_line(result)
assert path.as_posix() in result.stderr
Loading