Skip to content

Commit

Permalink
Fix mocking of errors
Browse files Browse the repository at this point in the history
Moved from print -> logger.
  • Loading branch information
Chaitanya Tata authored and Chaitanya Tata committed Jan 10, 2024
1 parent 0937a22 commit c579d2f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Copyright (c) Blu Wireless Technology Ltd. 2023, All Rights Reserved


import logging
import re
import unittest
from io import StringIO
from pathlib import Path
from unittest.mock import mock_open, patch

Expand All @@ -25,11 +25,17 @@ def mock_file_read(self, data):

def _error_tests(self, config, err_msg):
patcher = self.mock_file_read(config)
with patch("sys.stderr", new=StringIO()) as fake_stderr:
with self.assertRaisesRegex(SystemExit, r"1"):
dut_logger = logging.getLogger("checkpatch_hook")
with patch.object(dut_logger, "error") as mock_logger:
with self.assertRaisesRegex(SystemExit, r"1") as cm:
self.config_file.load_config()
error_message = fake_stderr.getvalue()
self.assertTrue(re.search(err_msg, error_message))
print(f"cm.exception: {cm.exception}")
error_message_found = any(
re.search(err_msg, call.args[0]) for call in mock_logger.mock_calls
)
self.assertTrue(
error_message_found, f"Expected error message not found in logger calls: {err_msg}"
)
patcher.stop()

def test_load_config_with_valid_file(self):
Expand Down

0 comments on commit c579d2f

Please sign in to comment.