Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b904127

Browse files
committed
A few cleanups for CalledProcessError to hopefully make it more readable
1 parent 83f0802 commit b904127

4 files changed

Lines changed: 36 additions & 45 deletions

File tree

pre_commit/util.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,31 @@ def make_executable(filename):
7474

7575

7676
class CalledProcessError(RuntimeError):
77-
def __init__(self, returncode, cmd, expected_returncode, output=None):
77+
def __init__(self, returncode, cmd, expected_returncode, stdout, stderr):
7878
super(CalledProcessError, self).__init__(
79-
returncode, cmd, expected_returncode, output,
79+
returncode, cmd, expected_returncode, stdout, stderr,
8080
)
8181
self.returncode = returncode
8282
self.cmd = cmd
8383
self.expected_returncode = expected_returncode
84-
self.output = output
84+
self.stdout = stdout
85+
self.stderr = stderr
8586

8687
def to_bytes(self):
87-
output = []
88-
for maybe_text in self.output:
89-
if maybe_text:
90-
output.append(
91-
b'\n ' +
92-
five.to_bytes(maybe_text).replace(b'\n', b'\n '),
93-
)
88+
def _indent_or_none(part):
89+
if part:
90+
return b'\n ' + part.replace(b'\n', b'\n ')
9491
else:
95-
output.append(b'(none)')
92+
return b' (none)'
9693

9794
return b''.join((
98-
five.to_bytes(
99-
'Command: {!r}\n'
100-
'Return code: {}\n'
101-
'Expected return code: {}\n'.format(
102-
self.cmd, self.returncode, self.expected_returncode,
103-
),
104-
),
105-
b'Output: ', output[0], b'\n',
106-
b'Errors: ', output[1],
95+
'command: {!r}\n'
96+
'return code: {}\n'
97+
'expected return code: {}\n'.format(
98+
self.cmd, self.returncode, self.expected_returncode,
99+
).encode('UTF-8'),
100+
b'stdout:', _indent_or_none(self.stdout), b'\n',
101+
b'stderr:', _indent_or_none(self.stderr),
107102
))
108103

109104
def to_text(self):
@@ -143,9 +138,7 @@ def cmd_output_b(*cmd, **kwargs):
143138
returncode = proc.returncode
144139

145140
if retcode is not None and retcode != returncode:
146-
raise CalledProcessError(
147-
returncode, cmd, retcode, output=(stdout_b, stderr_b),
148-
)
141+
raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
149142

150143
return returncode, stdout_b, stderr_b
151144

tests/languages/docker_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def test_docker_is_running_process_error():
1111
with mock.patch(
1212
'pre_commit.languages.docker.cmd_output_b',
13-
side_effect=CalledProcessError(*(None,) * 4),
13+
side_effect=CalledProcessError(None, None, None, None, None),
1414
):
1515
assert docker.docker_is_running() is False
1616

tests/store_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_clone_shallow_failure_fallback_to_complete(
125125

126126
# Force shallow clone failure
127127
def fake_shallow_clone(self, *args, **kwargs):
128-
raise CalledProcessError(None, None, None)
128+
raise CalledProcessError(None, None, None, None, None)
129129
store._shallow_clone = fake_shallow_clone
130130

131131
ret = store.clone(path, rev)

tests/util_test.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,34 @@
99
from pre_commit.util import CalledProcessError
1010
from pre_commit.util import clean_path_on_failure
1111
from pre_commit.util import cmd_output
12+
from pre_commit.util import cmd_output_b
1213
from pre_commit.util import cmd_output_p
1314
from pre_commit.util import parse_version
1415
from pre_commit.util import rmtree
1516
from pre_commit.util import tmpdir
1617

1718

1819
def test_CalledProcessError_str():
19-
error = CalledProcessError(
20-
1, [str('git'), str('status')], 0, (str('stdout'), str('stderr')),
21-
)
20+
error = CalledProcessError(1, [str('exe')], 0, b'output', b'errors')
2221
assert str(error) == (
23-
"Command: ['git', 'status']\n"
24-
'Return code: 1\n'
25-
'Expected return code: 0\n'
26-
'Output: \n'
27-
' stdout\n'
28-
'Errors: \n'
29-
' stderr'
22+
"command: ['exe']\n"
23+
'return code: 1\n'
24+
'expected return code: 0\n'
25+
'stdout:\n'
26+
' output\n'
27+
'stderr:\n'
28+
' errors'
3029
)
3130

3231

3332
def test_CalledProcessError_str_nooutput():
34-
error = CalledProcessError(
35-
1, [str('git'), str('status')], 0, (str(''), str('')),
36-
)
33+
error = CalledProcessError(1, [str('exe')], 0, b'', b'')
3734
assert str(error) == (
38-
"Command: ['git', 'status']\n"
39-
'Return code: 1\n'
40-
'Expected return code: 0\n'
41-
'Output: (none)\n'
42-
'Errors: (none)'
35+
"command: ['exe']\n"
36+
'return code: 1\n'
37+
'expected return code: 0\n'
38+
'stdout: (none)\n'
39+
'stderr: (none)'
4340
)
4441

4542

@@ -90,8 +87,9 @@ def test_cmd_output_exe_not_found():
9087
assert out == 'Executable `dne` not found'
9188

9289

93-
def test_cmd_output_p_exe_not_found():
94-
ret, out, _ = cmd_output_p('dne', retcode=None, stderr=subprocess.STDOUT)
90+
@pytest.mark.parametrize('fn', (cmd_output_b, cmd_output_p))
91+
def test_cmd_output_exe_not_found_bytes(fn):
92+
ret, out, _ = fn('dne', retcode=None, stderr=subprocess.STDOUT)
9593
assert ret == 1
9694
assert out == b'Executable `dne` not found'
9795

0 commit comments

Comments
 (0)