Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mocking of errors #2

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading