Skip to content

Commit

Permalink
Merge pull request #125 from LeastAuthority/report-git-failure-output
Browse files Browse the repository at this point in the history
Report git's output when git fails
  • Loading branch information
hawkowl authored Sep 7, 2018
2 parents 7889f54 + 2b81b5d commit 47754a6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
21 changes: 15 additions & 6 deletions src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import click

from subprocess import check_output, STDOUT
from subprocess import (
CalledProcessError,
check_output,
STDOUT,
)

from ._settings import load_config, load_config_from_file
from ._builder import find_fragments
Expand All @@ -34,11 +38,16 @@ def __main(comparewith, directory, pyproject):
else:
config = load_config_from_file(pyproject)

files_changed = (
_run(["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory)
.decode(getattr(sys.stdout, "encoding", "utf8"))
.strip()
)
try:
files_changed = (
_run(["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory)
.decode(getattr(sys.stdout, "encoding", "utf8"))
.strip()
)
except CalledProcessError as e:
click.echo("git produced output while failing:")
click.echo(e.output)
raise

if not files_changed:
click.echo("On trunk, or no diffs, so no newsfragment required.")
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/124.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
towncrier.check now reports git output when it encounters a git failure.
89 changes: 41 additions & 48 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,47 @@
from towncrier.check import _main


def create_project(pyproject_path):
with open(pyproject_path, "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")
fragment_path = "foo/newsfragments/123.feature"
with open(fragment_path, "w") as f:
f.write("Adds levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "[email protected]"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "checkout", "-b", "otherbranch"])


class TestChecker(TestCase):
maxDiff = None

def test_git_fails(self):
"""
If git fails to report a comparison, git's output is reported to aid in
debugging the situation.
"""
runner = CliRunner()
with runner.isolated_filesystem():
create_project("pyproject.toml")

result = runner.invoke(_main, ["--compare-with", "hblaugh"])
self.assertIn(
"git produced output while failing",
result.output,
)
self.assertIn(
"hblaugh",
result.output,
)

def test_no_changes_made(self):
self._test_no_changes_made(
"pyproject.toml",
Expand All @@ -33,22 +71,7 @@ def _test_no_changes_made(self, pyproject_path, invoke):
runner = CliRunner()

with runner.isolated_filesystem():
with open(pyproject_path, "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")
fragment_path = "foo/newsfragments/123.feature"
with open(fragment_path, "w") as f:
f.write("Adds levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "[email protected]"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "checkout", "-b", "otherbranch"])
create_project(pyproject_path)

result = invoke(runner, _main, ["--compare-with", "master"])

Expand All @@ -61,22 +84,7 @@ def test_fragment_exists(self):
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")
fragment_path = "foo/newsfragments/123.feature"
with open(fragment_path, "w") as f:
f.write("Adds levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "[email protected]"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "checkout", "-b", "otherbranch"])
create_project("pyproject.toml")

file_path = "foo/somefile.py"
with open(file_path, "w") as f:
Expand Down Expand Up @@ -110,22 +118,7 @@ def test_fragment_missing(self):
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")
fragment_path = "foo/newsfragments/123.feature"
with open(fragment_path, "w") as f:
f.write("Adds levitation")

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "[email protected]"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "checkout", "-b", "otherbranch"])
create_project("pyproject.toml")

file_path = "foo/somefile.py"
with open(file_path, "w") as f:
Expand Down

0 comments on commit 47754a6

Please sign in to comment.