Skip to content

Commit

Permalink
Use the Python attrs library + improve output on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Takishima committed Oct 15, 2023
1 parent c0d7df1 commit 0c69242
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
19 changes: 13 additions & 6 deletions cmake_pc_hooks/_call_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@
import subprocess as sp
import sys

import attrs


@attrs.define(slots=True)
class History: # pylint: disable=too-few-public-methods
"""Process execution data."""

__slots__ = ('stdout', 'stderr', 'returncode')
stdout: str
stderr: str
returncode: int
_print_output: bool = True

def __init__(self, stdout, stderr, returncode):
self.stdout = stdout
self.stderr = stderr
self.returncode = returncode
def disable_print_output(self) -> None:
"""Disable output printing."""
self._print_output = False

def to_stdout_and_stderr(self):
def to_stdout_and_stderr(self) -> None:
"""Copy the relevant content to the standard output and error streams."""
if not self._print_output:
return
sys.stdout.write(self.stdout)
sys.stdout.flush()
sys.stderr.write(self.stderr)
Expand Down
2 changes: 1 addition & 1 deletion cmake_pc_hooks/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def run(self):

has_errors = False
for res in self.history:
res.to_stdout_and_stderr()
has_errors |= self._parse_output(res)
res.to_stdout_and_stderr()

if has_errors:
sys.exit(1)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install_requires =
CLinters >= 1.3.0
fasteners
filelock
attrs >= 23

[options.entry_points]
console_scripts =
Expand Down
16 changes: 11 additions & 5 deletions tests/python/_call_process_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import subprocess as sp

from cmake_pc_hooks._call_process import History, call_process

# ==============================================================================


def test_history(mocker):
@pytest.parametrize('disable_print', [False, True])
def test_history(mocker, disable_print):
stdout = 'out'
stderr = 'err'
returncode = 1
history = History(stdout=stdout, stderr=stderr, returncode=returncode)
if disable_print:
history.disable_print_output()

assert history.stdout == stdout
assert history.stderr == stderr
assert history.returncode == returncode

sys_stdout = mocker.patch('sys.stdout')
sys_stderr = mocker.patch('sys.stderr')

history.to_stdout_and_stderr()

sys_stdout.write.assert_called_once()
sys_stderr.write.assert_called_once()
if disable_print:
sys_stdout.write.assert_not_called()
sys_stderr.write.assert_not_called()
else:
sys_stdout.write.assert_called_once()
sys_stderr.write.assert_called_once()


# ==============================================================================
Expand Down

0 comments on commit 0c69242

Please sign in to comment.