diff --git a/src/towncrier/check.py b/src/towncrier/check.py index 156873b3..f10e2721 100644 --- a/src/towncrier/check.py +++ b/src/towncrier/check.py @@ -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 @@ -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.") diff --git a/src/towncrier/newsfragments/124.feature b/src/towncrier/newsfragments/124.feature new file mode 100644 index 00000000..579dc4b8 --- /dev/null +++ b/src/towncrier/newsfragments/124.feature @@ -0,0 +1 @@ +towncrier.check now reports git output when it encounters a git failure. diff --git a/src/towncrier/test/test_check.py b/src/towncrier/test/test_check.py index 19be03a6..d4d7c39d 100644 --- a/src/towncrier/test/test_check.py +++ b/src/towncrier/test/test_check.py @@ -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", "user@example.com"]) + 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", @@ -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", "user@example.com"]) - 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"]) @@ -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", "user@example.com"]) - 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: @@ -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", "user@example.com"]) - 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: