def test_logging(self): uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value="conSole")) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, ConsolePrinter) self.assertIsInstance(uut.interactor, ConsoleInteractor) uut = Section("test", log_printer=ConsolePrinter()) uut.append(Setting(key="log_TYPE", value="NONE")) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, NullPrinter) uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value="./invalid path/@#$%^&*()_")) uut.retrieve_logging_objects() # This should throw a warning self.assertIsInstance(uut.log_printer, ConsolePrinter) self.assertEqual(uut.log_printer.log_level, LOG_LEVEL.WARNING) uut.append(Setting(key="LOG_LEVEL", value="DEBUG")) uut.retrieve_logging_objects() # This should throw a warning self.assertEqual(uut.log_printer.log_level, LOG_LEVEL.DEBUG) filename = tempfile.gettempdir( ) + os.path.sep + "testcoalasectiontestfile~" uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value=filename)) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, FilePrinter) del uut os.remove(filename)
def retrieve_logging_objects(self): """ Creates an appropriate log printer and interactor according to the settings. """ log_type = str(self.get("log_type", "console")).lower() log_level = LOG_LEVEL.from_str(str(self.get("log_level", "none"))) if log_type == "console": self.log_printer = ConsolePrinter(log_level=log_level) else: try: # ConsolePrinter is the only printer which may not throw an exception (if we have no bugs though) # so well fallback to him if some other printer fails if log_type == "none": self.log_printer = NullPrinter() else: self.log_printer = FilePrinter(filename=log_type, log_level=log_level) except: self.log_printer = ConsolePrinter(log_level=log_level) self.log_printer.log( LOG_LEVEL.WARNING, _("Failed to instantiate the logging method '{}'. Falling back " "to console output.").format(log_type)) # We currently only offer console interactor, so we'll ignore the output setting for now self.interactor = ConsoleInteractor.from_section( self, log_printer=self.log_printer)
def retrieve_logging_objects(self): """ Creates an appropriate log printer and interactor according to the settings. """ log_type = str(self.get("log_type", "console")).lower() output_type = str(self.get("output", "console")).lower() str_log_level = str(self.get("log_level", "")).upper() log_level = LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.WARNING) if log_type == "console": self.log_printer = ConsolePrinter(log_level=log_level) else: try: # ConsolePrinter is the only printer which may not throw an # exception (if we have no bugs though) so well fallback to him # if some other printer fails if log_type == "none": self.log_printer = NullPrinter() else: self.log_printer = FilePrinter(filename=log_type, log_level=log_level) except: self.log_printer = ConsolePrinter(log_level=log_level) self.log_printer.log( LOG_LEVEL.WARNING, _("Failed to instantiate the logging method '{}'. Falling " "back to console output.").format(log_type)) if output_type == "none": self.interactor = NullInteractor(log_printer=self.log_printer) else: self.interactor = ConsoleInteractor.from_section( self, log_printer=self.log_printer)
def test_outputting(self): uut = Section("test", log_printer=NullPrinter()) uut.retrieve_logging_objects() self.assertIsInstance(uut.interactor, ConsoleInteractor) uut.append(Setting(key="output", value="none")) uut.retrieve_logging_objects() self.assertIsInstance(uut.interactor, NullInteractor) uut.append(Setting(key="output", value="anything else")) uut.retrieve_logging_objects() self.assertIsInstance(uut.interactor, ConsoleInteractor)
def test_non_printing(self): self.uut = NullPrinter() self.assertEqual(self.uut.print("anything"), None) self.assertEqual(self.uut.print("anything", color="red"), None) self.assertEqual(LogPrinter(self.uut).debug("message"), None)
def test_print_results(self): q = queue.Queue() self.uut._print = lambda string: q.put(string) self.assertRaises(TypeError, self.uut.print_results, 5, {}) self.assertRaises(TypeError, self.uut.print_results, [], 5) self.uut.print_results([], {}) self.assertRaises(queue.Empty, q.get, timeout=0) self.uut.print_results([Result("origin", "message")], {}) self.assertEqual( "\n\n{}\n| | | [{}] origin:\n| | | " "message\n".format(self.uut.STR_PROJECT_WIDE, RESULT_SEVERITY.__str__( RESULT_SEVERITY.NORMAL)), self.get_str_from_queue(q)) self.uut.print_results([ Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=2) ], {"proj/white": ["test line\n", "line 2\n", "line 3\n"]}) self.assertEqual( """\n\nproj/white | 1| 1| test line\n| 2| 2| line 2 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found """.format(RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL)), self.get_str_from_queue(q)) self.uut.print_results( [ Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=5) ], { "proj/white": [ "test line\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n" ] }) self.assertEqual( """\n\nproj/white | ...| ...| \n| 2| 2| line 2 | 3| 3| line 3 | 4| 4| line 4 | 5| 5| line 5 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found """.format(RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL)), self.get_str_from_queue(q)) # Check sorting and multi result output self.uut.print_results( [ Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=5), Result("SpaceConsistencyBear", "Trailing whitespace found", "proj/white", line_nr=2) ], { "proj/white": [ "test line\n", "line 2\n", "line 3\n", "line 4\n", "line 5\n" ] }) self.assertEqual( """\n\nproj/white | 1| 1| test line | 2| 2| line 2 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found | 3| 3| line 3 | 4| 4| line 4 | 5| 5| line 5 | | | [{}] SpaceConsistencyBear: | | | Trailing whitespace found """.format(RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL), RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL)), self.get_str_from_queue(q)) # File isn't in dict, shouldn't print but also shouldn't throw. This # can occur if filter writers are doing nonsense. If this happens twice # the same should happen (whitebox testing: this is a potential bug.) self.uut.log_printer = NullPrinter() self.uut.print_results([ Result("t", "msg", "file", line_nr=5), Result("t", "msg", "file", line_nr=5) ], {}) self.assertEqual("", self.get_str_from_queue(q)) # Line isn't in dict[file], shouldn't print but also shouldn't throw. # This can occur if filter writers are doing nonsense. self.uut.print_results([Result("t", "msg", "file", line_nr=5)], {"file": []}) self.assertEqual( """\n\nfile\n| | | {}\n| | | [{}] t: | | | msg\n""".format(self.uut.STR_LINE_DOESNT_EXIST, RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL)), self.get_str_from_queue(q)) self.assertRaises(AssertionError, self.uut.print_results, [Result("t", "msg", None, line_nr=5)], {})
def test_non_printing(self): self.uut = NullPrinter() self.assertEqual(self.uut.print("anything"), None) self.assertEqual(self.uut.print("anything", color="red"), None) self.assertEqual(self.uut.debug("message"), None)
class NullPrinterTestCase(unittest.TestCase): def test_non_printing(self): self.uut = NullPrinter() self.assertEqual(self.uut.print("anything"), None) self.assertEqual(self.uut.print("anything", color="red"), None) self.assertEqual(self.uut.debug("message"), None)