Skip to content

Commit

Permalink
Add: Add a new testing module to pontos #403
Browse files Browse the repository at this point in the history
Merge #403 into main
  • Loading branch information
bjoernricks authored Sep 1, 2022
2 parents 6ce552e + 629b417 commit 15d785e
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 158 deletions.
1 change: 0 additions & 1 deletion docs/README.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/pontos/pontos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Subpackages
pontos.github
pontos.release
pontos.terminal
pontos.testing
pontos.updateheader
pontos.version

Expand Down
9 changes: 9 additions & 0 deletions docs/pontos/pontos.testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pontos.testing package
======================

Module contents
---------------

.. automodule:: pontos.testing
:members:
:show-inheritance:
53 changes: 36 additions & 17 deletions pontos/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import subprocess
from os import PathLike, fspath
from pathlib import Path
from typing import List, Optional, Union

Expand All @@ -33,16 +34,34 @@ def __str__(self) -> str:
)


def _exec_git(
*args: str, ignore_errors: Optional[bool] = False, cwd: Optional[str] = None
def exec_git(
*args: str,
ignore_errors: Optional[bool] = False,
cwd: Optional[PathLike] = None,
) -> str:
"""
Internal module function to abstract calling git via subprocess
Internal module function to abstract calling git via subprocess. Most of the
cases the Git class should be used.
Args:
ignore_errors: Set to True if errors while running git should be
ignored. Default: False.
cwd: Set the current working directory
Raises:
GitError: Will be raised if ignore_errors is False and git returns with
an exit code != 0.
Returns:
stdout output of git command or empty string if ignore_errors is True
and git returns with an exit code != 0.
"""
try:
cmd_args = ["git"]
cmd_args.extend(args)
output = subprocess.check_output(cmd_args, cwd=cwd)
output = subprocess.check_output(
cmd_args, cwd=fspath(cwd) if cwd else None
)
return output.decode()
except subprocess.CalledProcessError as e:
if ignore_errors:
Expand Down Expand Up @@ -89,7 +108,7 @@ def init(self, *, bare: Optional[bool] = False):
args = ["init"]
if bare:
args.append("--bare")
_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def create_branch(self, branch: str, *, start_point: Optional[str] = None):
"""
Expand All @@ -104,7 +123,7 @@ def create_branch(self, branch: str, *, start_point: Optional[str] = None):
if start_point:
args.append(start_point)

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def rebase(
self,
Expand Down Expand Up @@ -132,7 +151,7 @@ def rebase(
if head:
args.append(head)

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def clone(
self,
Expand Down Expand Up @@ -161,7 +180,7 @@ def clone(
args.extend(["--depth", depth])
args.extend([repo_url, str(destination.absolute())])

_exec_git(
exec_git(
*args,
cwd=self._cwd,
)
Expand Down Expand Up @@ -191,13 +210,13 @@ def push(
if branch:
args.append(branch)

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def config(self, key: str, value: str):
"""
Set a (local) git config
"""
_exec_git("config", key, value, cwd=self._cwd)
exec_git("config", key, value, cwd=self._cwd)

def cherry_pick(self, commits: Union[str, List[str]]):
"""
Expand All @@ -213,28 +232,28 @@ def cherry_pick(self, commits: Union[str, List[str]]):
args = ["cherry-pick"]
args.extend(commits)

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def list_tags(self) -> List[str]:
"""
List all available tags
"""
return _exec_git("tag", "-l", cwd=self._cwd).splitlines()
return exec_git("tag", "-l", cwd=self._cwd).splitlines()

def add(self, files: Union[str, List[str]]):
def add(self, files: Union[PathLike, List[PathLike]]):
"""
Add files to the git staging area
Args:
files: A single file or a list of files to add to the staging area
"""
if isinstance(files, str):
if isinstance(files, (PathLike, str, bytes)):
files = [files]

args = ["add"]
args.extend(files)
args.extend([fspath(file) for file in files])

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)

def commit(
self,
Expand All @@ -259,4 +278,4 @@ def commit(

args.extend(["-m", message])

_exec_git(*args, cwd=self._cwd)
exec_git(*args, cwd=self._cwd)
Loading

0 comments on commit 15d785e

Please sign in to comment.