Skip to content

Commit

Permalink
Fix #2 porcelein no value; test for git errors (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
robnagler authored Mar 15, 2024
1 parent 8998861 commit 71d8a01
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions rschronver/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _is_edited():
"git",
"status",
"--ignore-submodules",
"--porcelain=v1",
"--porcelain",
"--untracked-files=no",
),
),
Expand All @@ -73,7 +73,7 @@ def _sh(cmd):
return None
except subprocess.CalledProcessError as e:
if hasattr(e, "output") and len(e.output):
sys.stderr.write(e.output)
sys.stderr.write(e.output.decode(locale.getpreferredencoding()))
raise

if not _is_repo():
Expand Down
33 changes: 30 additions & 3 deletions tests/git_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""

import contextlib


def test_basic():
from pykern import pkunit
Expand All @@ -12,9 +14,7 @@ def test_basic():
import os
import time

with pkunit.save_chdir_work():
a = git.version()
pkunit.pkeq(None, a)
with _setup():
e = datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S")
pkunit.pkeq(
0,
Expand All @@ -28,3 +28,30 @@ def test_basic():
f.write("change")
b = git.version()
pkunit.pkok(a < b, "expect={} should be older than={}", a, b)


def test_git_status_error(capsys):
from pykern import pkunit, pkio, pkdebug
from rschronver import git
import os
import subprocess

with _setup() as d:
pkio.mkdir_parent(d)
with pkunit.pkexcept(subprocess.CalledProcessError):
git.version()
_, e = capsys.readouterr()
pkunit.pkre("not a git repository", e)


@contextlib.contextmanager
def _setup():
from pykern import pkunit, pkio, pkdebug
from rschronver import git
import os

with pkunit.save_chdir_work() as d:
rv = d.join(".git")
os.environ["GIT_DIR"] = str(rv)
pkunit.pkeq(None, git.version())
yield rv

0 comments on commit 71d8a01

Please sign in to comment.