From 7bd1dd977d771e800a82180cf9f22fd5905ab8d3 Mon Sep 17 00:00:00 2001 From: Buck Golemon Date: Tue, 6 Jan 2015 16:25:46 -0800 Subject: [PATCH] improve output in error case --- pre_commit/util.py | 12 ++++++++++-- tests/prefixed_command_runner_test.py | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pre_commit/util.py b/pre_commit/util.py index 5efe75fb..1891c067 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -108,15 +108,23 @@ class CalledProcessError(RuntimeError): self.output = output def __str__(self): + output = [] + for text in self.output: + if text: + output.append('\n ' + text.replace('\n', '\n ')) + else: + output.append('(none)') + return ( 'Command: {0!r}\n' 'Return code: {1}\n' 'Expected return code: {2}\n' - 'Output: {3!r}\n'.format( + 'Output: {3}\n' + 'Errors: {4}\n'.format( self.cmd, self.returncode, self.expected_returncode, - self.output, + *output ) ) diff --git a/tests/prefixed_command_runner_test.py b/tests/prefixed_command_runner_test.py index f7cf1c6f..a477100b 100644 --- a/tests/prefixed_command_runner_test.py +++ b/tests/prefixed_command_runner_test.py @@ -19,7 +19,23 @@ def test_CalledProcessError_str(): "Command: ['git', 'status']\n" "Return code: 1\n" "Expected return code: 0\n" - "Output: ('stdout', 'stderr')\n" + "Output: \n" + " stdout\n" + "Errors: \n" + " stderr\n" + ) + + +def test_CalledProcessError_str_nooutput(): + error = CalledProcessError( + 1, [str('git'), str('status')], 0, (str(''), str('')) + ) + assert str(error) == ( + "Command: ['git', 'status']\n" + "Return code: 1\n" + "Expected return code: 0\n" + "Output: (none)\n" + "Errors: (none)\n" )