-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: async utils: aio_run_command(): check returncode (#1181)
Raise subprocess.CalledProcessError if subprocess fails with a non-zero return code. Fixes: #1177 Signed-off-by: John Andersen <johnandersenpdx@gmail.com> Co-authored-by: Terri Oda <terri.oda@intel.com>
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright (C) 2021 Intel Corporation | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
""" | ||
CVE-bin-tool async util tests | ||
""" | ||
import dataclasses | ||
import subprocess | ||
import unittest.mock | ||
from typing import Callable, Coroutine, Tuple | ||
|
||
import pytest | ||
|
||
from cve_bin_tool.async_utils import aio_run_command | ||
|
||
|
||
@dataclasses.dataclass | ||
class FakeProcess: | ||
returncode: int | ||
|
||
async def communicate(self) -> Tuple[bytes, bytes]: | ||
return b"", b"" | ||
|
||
|
||
def mkexec(returncode: int) -> Callable[..., Coroutine[None, None, FakeProcess]]: | ||
async def return_fake_process(*args, **kwargs) -> FakeProcess: | ||
return FakeProcess(returncode=returncode) | ||
|
||
return return_fake_process | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_aio_run_command_success(): | ||
with unittest.mock.patch("asyncio.create_subprocess_exec", new=mkexec(0)): | ||
await aio_run_command(("echo", "hello")) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_aio_run_command_returncode_non_zero(): | ||
with unittest.mock.patch("asyncio.create_subprocess_exec", new=mkexec(1)): | ||
with pytest.raises(subprocess.CalledProcessError): | ||
await aio_run_command(("echo", "hello")) |