Remove expected_returncode from CalledProcessError

This commit is contained in:
marsha
2022-10-30 15:18:13 -05:00
parent 84b38f7b89
commit 42102a1bfd
5 changed files with 8 additions and 14 deletions

View File

@@ -83,14 +83,12 @@ class CalledProcessError(RuntimeError):
self,
returncode: int,
cmd: tuple[str, ...],
expected_returncode: int,
stdout: bytes,
stderr: bytes | None,
) -> None:
super().__init__(returncode, cmd, expected_returncode, stdout, stderr)
super().__init__(returncode, cmd, stdout, stderr)
self.returncode = returncode
self.cmd = cmd
self.expected_returncode = expected_returncode
self.stdout = stdout
self.stderr = stderr
@@ -104,7 +102,6 @@ class CalledProcessError(RuntimeError):
return b''.join((
f'command: {self.cmd!r}\n'.encode(),
f'return code: {self.returncode}\n'.encode(),
f'expected return code: {self.expected_returncode}\n'.encode(),
b'stdout:', _indent_or_none(self.stdout), b'\n',
b'stderr:', _indent_or_none(self.stderr),
))
@@ -142,9 +139,8 @@ def cmd_output_b(
stdout_b, stderr_b = proc.communicate()
returncode = proc.returncode
SUCCESS = 0
if check and returncode != SUCCESS:
raise CalledProcessError(returncode, cmd, SUCCESS, stdout_b, stderr_b)
if check and returncode:
raise CalledProcessError(returncode, cmd, stdout_b, stderr_b)
return returncode, stdout_b, stderr_b

View File

@@ -162,7 +162,7 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
def test_error_handler_non_utf8_exception(mock_store_dir):
with pytest.raises(SystemExit):
with error_handler.error_handler():
raise CalledProcessError(1, ('exe',), 0, b'error: \xa0\xe1', b'')
raise CalledProcessError(1, ('exe',), b'error: \xa0\xe1', b'')
def test_error_handler_non_stringable_exception(mock_store_dir):

View File

@@ -178,6 +178,6 @@ def test_get_docker_path_in_docker_windows(in_docker):
def test_get_docker_path_in_docker_docker_in_docker(in_docker):
# won't be able to discover "self" container in true docker-in-docker
err = CalledProcessError(1, (), 0, b'', b'')
err = CalledProcessError(1, (), b'', b'')
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
assert docker._get_docker_path('/project') == '/project'

View File

@@ -127,7 +127,7 @@ def test_clone_shallow_failure_fallback_to_complete(
# Force shallow clone failure
def fake_shallow_clone(self, *args, **kwargs):
raise CalledProcessError(1, (), 0, b'', None)
raise CalledProcessError(1, (), b'', None)
store._shallow_clone = fake_shallow_clone
ret = store.clone(path, rev)

View File

@@ -18,11 +18,10 @@ from pre_commit.util import tmpdir
def test_CalledProcessError_str():
error = CalledProcessError(1, ('exe',), 0, b'output', b'errors')
error = CalledProcessError(1, ('exe',), b'output', b'errors')
assert str(error) == (
"command: ('exe',)\n"
'return code: 1\n'
'expected return code: 0\n'
'stdout:\n'
' output\n'
'stderr:\n'
@@ -31,11 +30,10 @@ def test_CalledProcessError_str():
def test_CalledProcessError_str_nooutput():
error = CalledProcessError(1, ('exe',), 0, b'', b'')
error = CalledProcessError(1, ('exe',), b'', b'')
assert str(error) == (
"command: ('exe',)\n"
'return code: 1\n'
'expected return code: 0\n'
'stdout: (none)\n'
'stderr: (none)'
)