# SPDX-License-Identifier: GPL-3.0-or-later # Copyright (c) 2008-2026 Andrew Ziem. # # This work is licensed under the terms of the GNU GPL, version 3 or # later. See the COPYING file in the top-level directory. """ Test case for module GtkShim """ import ctypes import os import subprocess import sys import unittest from unittest import mock from tests.common import pytest from tests import common from bleachbit import IS_WINDOWS from bleachbit.General import get_executable from bleachbit.GtkShim import ( _build_error_html, _fix_arg, _handle_gtk_import_error, _patched_argv, _show_windows_error_dialog, path_has_lib_or_bin, ) class PathHasLibOrBinTestCase(unittest.TestCase): """Test path_has_lib_or_bin() detection logic.""" def test_path_has_lib_or_bin(self): """Test path_has_lib_or_bin() with various paths.""" tests = [ # (path, expected_result) # Standalone 'bin' directory triggers detection (r'C:\bin', 'bin'), (r'C:\bin\xlib', 'bin'), (r'C:\libx\bin', 'bin'), (r'C:\bin\bin', 'bin'), (r'C:\Users\username\Downloads\bin\BleachBit-Portable', 'bin'), # Standalone 'lib' directory triggers detection (r'C:\lib', 'lib'), (r'C:\lib\xbin', 'lib'), (r'C:\binx\lib', 'lib'), (r'C:\lib\lib', 'lib'), (r'C:\Users\username\Downloads\lib\BleachBit-Portable', 'lib'), (r'C:\ProgramData\chocolatey\lib\bleachbit.portable\BleachBit-Portable', 'lib'), # Detection is case-insensitive (r'C:\Users\BIN\app', 'bin'), (r'C:\Users\Lib\app', 'lib'), # 'binx' is not 'bin' (r'C:\Users\username\Downloads\binx\BleachBit-Portable', None), # 'xbin' is not 'bin' (r'C:\Users\username\Downloads\xbin\BleachBit-Portable', None), # 'libfoo' is not 'lib' (r'C:\ProgramData\libfoo\app', None), # Normal portable path without lib or bin (r'C:\Users\username\Downloads\BleachBit-5.0.0-portable\BleachBit-Portable', None), # Empty path returns None ('', None), # 'lib' or 'bin' embedded in longer names should not match (r'C:\calibre\library\app', None), (r'C:\cabinet\app', None), ] case_functions = ( lambda x: x.lower(), lambda x: x.upper(), lambda x: x.title(), lambda x: x.title().swapcase(), ) for path, expected in tests: with self.subTest(path=path, expected=expected): joined = os.path.join(path, 'bleachbit.exe') for func in case_functions: self.assertEqual(path_has_lib_or_bin( func(joined)), expected) class BuildErrorHtmlTestCase(unittest.TestCase): """Tests for _build_error_html().""" def test_unknown_error_includes_traceback_and_sysinfo(self): """Unknown-error HTML must include traceback and system information.""" html = _build_error_html( RuntimeError('something broke'), traceback_text='Traceback (most recent call last):...', ) self.assertIn('Error: something broke', html) self.assertIn('Traceback (most recent call last):...', html) self.assertIn('System information:\n', html) self.assertIn('textarea', html) self.assertIn('Copy to clipboard', html) self.assertIn("function copyBugReport()", html) self.assertIn('get-help', html) self.assertEqual(html.count(''), 0) def test_unknown_error_escapes_html_in_error_message(self): """Error message containing HTML special chars must be escaped.""" html = _build_error_html( RuntimeError(''), ) self.assertNotIn('