Exemplo n.º 1
0
class ConsolePrinterTestCase(unittest.TestCase):
    def test_printing(self):
        self.uut = ConsolePrinter()
        self.uut.print("\ntest", "message", color="green")
        self.uut.print("\ntest", "message", color="greeeeen")
        self.uut.print("\ntest", "message")

    def test_pickling(self):
        outputfile = os.path.join(tempfile.gettempdir(), "ConsolePrinterPickleTestFile")
        with open(outputfile, "wb") as f:
            pickle.dump(ConsolePrinter(), f)

        with open(outputfile, "rb") as f:
            obj = pickle.load(f)

        self.assertIsInstance(obj, ConsolePrinter)
        obj.print("test")  # print will use the output param, this will ensure that the printer is valid

        os.remove(outputfile)
Exemplo n.º 2
0
class ConsolePrinterTest(unittest.TestCase):
    def test_printing(self):
        self.uut = ConsolePrinter()
        self.uut.print("\ntest", "message", color="green")
        self.uut.print("\ntest", "message", color="greeeeen")
        self.uut.print("\ntest", "message")

    def test_pickling(self):
        outputfile = os.path.join(tempfile.gettempdir(),
            "ConsolePrinterPickleTestFile")
        with open(outputfile, "wb") as f:
            pickle.dump(ConsolePrinter(), f)

        with open(outputfile, "rb") as f:
            obj = pickle.load(f)

        self.assertIsInstance(obj, ConsolePrinter)
        obj.print("test")  # print will use the output param, this will ensure
                           # that the printer is valid

        os.remove(outputfile)
Exemplo n.º 3
0
def print_results(log_printer,
                  section,
                  result_list,
                  file_dict,
                  file_diff_dict,
                  color=True,
                  pre_padding=3):
    """
    Print all the results in a section.

    :param log_printer:    Printer responsible for logging the messages.
    :param section:        The section to which the results belong to.
    :param result_list:    List containing the results
    :param file_dict:      A dictionary containing all files with filename as
                           key.
    :param file_diff_dict: A dictionary that contains filenames as keys and
                           diff objects as values.
    :param color:          Boolean variable to print the results in color or
                           not.
    :param pre_padding:    No of lines of file to print before the result line.
                           Default value is 3.
    """
    if not isinstance(result_list, list):
        raise TypeError("result_list should be of type list")
    if not isinstance(file_dict, dict):
        raise TypeError("file_dict should be of type dict")
    if not isinstance(file_diff_dict, dict):
        raise TypeError("file_diff_dict should be of type dict")

    # We can't use None since we need line 109 be executed if file of first
    # result is None
    console_printer = ConsolePrinter(print_colored=color)
    current_file = False
    current_line = 0

    for result in sorted(result_list):
        if result.file != current_file:
            if result.file in file_dict or result.file is None:
                current_file = result.file
                current_line = 0
                console_printer.print("\n\n{}".format(current_file
                                                      if current_file is not
                                                      None
                                                      else STR_PROJECT_WIDE),
                                      color=FILE_NAME_COLOR)
            else:
                log_printer.warn(_("A result ({}) cannot be printed "
                                   "because it refers to a file that "
                                   "doesn't seem to "
                                   "exist.").format(str(result)))
                continue

        if result.line_nr is not None:
            if current_file is None:
                raise AssertionError("A result with a line_nr should also "
                                     "have a file.")
            if result.line_nr < current_line:  # pragma: no cover
                raise AssertionError("The sorting of the results doesn't "
                                     "work correctly.")
            if len(file_dict[result.file]) < result.line_nr - 1:
                console_printer.print(format_line(line=STR_LINE_DOESNT_EXIST))
            else:
                print_lines(console_printer,
                            pre_padding,
                            file_dict,
                            current_line,
                            result.line_nr,
                            result.file)
                current_line = result.line_nr

        print_result(console_printer,
                     log_printer,
                     section,
                     file_diff_dict,
                     result,
                     file_dict)