|
113 | 113 | safeSQLIdentificatorNaming, |
114 | 114 | saveConfig, |
115 | 115 | serializeObject, |
| 116 | + setColor, |
116 | 117 | setTechnique, |
117 | 118 | splitFields, |
118 | 119 | trimAlphaNum, |
@@ -1784,6 +1785,49 @@ def test_no_old_options_is_noop(self): |
1784 | 1785 | self.assertIsNone(checkOldOptions(["-u", "http://test.invalid/?id=1", "--banner"])) |
1785 | 1786 |
|
1786 | 1787 |
|
| 1788 | +class TestSetColorHandlerMismatch(unittest.TestCase): |
| 1789 | + """ |
| 1790 | + Regression test for issue #6122: setColor() decided whether to colorize purely from |
| 1791 | + conf.disableColoring/IS_TTY, independent of whether the installed LOGGER_HANDLER actually |
| 1792 | + supports colorize() (a plain logging.StreamHandler - installed for --disable-coloring, or as |
| 1793 | + the ansistrm-unavailable fallback - never defines it). A multiprocessing hash-cracking worker |
| 1794 | + hit exactly this mismatch (conf.disableColoring not carried over into the worker) and crashed |
| 1795 | + with an AttributeError that got misreported as "there was a problem while hashing entry". |
| 1796 | + """ |
| 1797 | + |
| 1798 | + def setUp(self): |
| 1799 | + import lib.core.common as common_mod |
| 1800 | + self._common_mod = common_mod |
| 1801 | + self._saved_handler = common_mod.LOGGER_HANDLER |
| 1802 | + self._saved_disableColoring = conf.get("disableColoring") |
| 1803 | + |
| 1804 | + def tearDown(self): |
| 1805 | + self._common_mod.LOGGER_HANDLER = self._saved_handler |
| 1806 | + conf.disableColoring = self._saved_disableColoring |
| 1807 | + |
| 1808 | + def test_plain_handler_without_colorize_does_not_raise(self): |
| 1809 | + import logging |
| 1810 | + self._common_mod.LOGGER_HANDLER = logging.StreamHandler() # no .colorize(), like the --disable-coloring handler |
| 1811 | + conf.disableColoring = False # the desync: coloring "should" apply, but handler can't |
| 1812 | + result = setColor("[INFO] current status: abcde", istty=True) # must not raise |
| 1813 | + self.assertIsInstance(result, str) |
| 1814 | + |
| 1815 | + def test_colorizing_handler_still_used(self): |
| 1816 | + # sanity check: a handler that DOES define colorize() is unaffected by the guard |
| 1817 | + calls = [] |
| 1818 | + |
| 1819 | + class _FakeColorizingHandler(object): |
| 1820 | + def colorize(self, message, levelno, force=False): |
| 1821 | + calls.append((message, levelno, force)) |
| 1822 | + return "COLORIZED" |
| 1823 | + |
| 1824 | + self._common_mod.LOGGER_HANDLER = _FakeColorizingHandler() |
| 1825 | + conf.disableColoring = False |
| 1826 | + result = setColor("[INFO] current status: abcde", istty=True) |
| 1827 | + self.assertEqual(result, "COLORIZED") |
| 1828 | + self.assertEqual(len(calls), 1) |
| 1829 | + |
| 1830 | + |
1787 | 1831 | if __name__ == "__main__": |
1788 | 1832 | unittest.main(verbosity=2) |
1789 | 1833 |
|
|
0 commit comments