Skip to content

Commit

Permalink
refactor: Explicit checks for subprocess runs
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 19, 2023
1 parent c7bd4ce commit cc3ca2e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/griffe/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def _get_latest_tag(path: str | Path) -> str:
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
output = process.stdout.strip()
if process.returncode != 0 or not output:
Expand Down Expand Up @@ -99,16 +100,17 @@ def _tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]:
process = subprocess.run(
["git", "-C", repo, "worktree", "add", "-b", branch, location, ref],
capture_output=True,
check=False,
)
if process.returncode:
raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}")

try:
yield Path(location)
finally:
subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL, check=False)
subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL, check=False)
subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL, check=False)


def load_git(
Expand Down
12 changes: 6 additions & 6 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ def git_repo(tmp_path: Path) -> Path:
"""
repo_path = tmp_path / REPO_NAME
repo_path.mkdir()
run(["git", "-C", str(repo_path), "init"])
run(["git", "-C", str(repo_path), "config", "user.name", "Name"])
run(["git", "-C", str(repo_path), "config", "user.email", "my@email.com"])
run(["git", "-C", str(repo_path), "init"], check=True)
run(["git", "-C", str(repo_path), "config", "user.name", "Name"], check=True)
run(["git", "-C", str(repo_path), "config", "user.email", "my@email.com"], check=True)
for tagdir in REPO_SOURCE.iterdir():
ver = tagdir.name
_copy_contents(tagdir, repo_path)
run(["git", "-C", str(repo_path), "add", "."])
run(["git", "-C", str(repo_path), "commit", "-m", f"feat: {ver} stuff"])
run(["git", "-C", str(repo_path), "tag", ver])
run(["git", "-C", str(repo_path), "add", "."], check=True)
run(["git", "-C", str(repo_path), "commit", "-m", f"feat: {ver} stuff"], check=True)
run(["git", "-C", str(repo_path), "tag", ver], check=True)
return repo_path


Expand Down

0 comments on commit cc3ca2e

Please sign in to comment.