class ShowPatchActionTest(unittest.TestCase): def setUp(self): self.uut = ShowPatchAction() self.file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} diff_dict = {"a": Diff(self.file_dict['a']), "b": Diff(self.file_dict['b'])} diff_dict["a"].add_lines(1, ["test\n"]) diff_dict["a"].delete_line(3) diff_dict["b"].add_lines(0, ["first\n"]) self.test_result = Result("origin", "message", diffs=diff_dict) self.section = Section("name") self.section.append(Setting("colored", "false")) def test_is_applicable(self): self.assertFalse(self.uut.is_applicable(1, None, None)) self.assertFalse(self.uut.is_applicable(Result("o", "m"), None, None)) self.assertTrue(self.uut.is_applicable(self.test_result, None, None)) def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b\n" "| 3| |-c\n" "|----| | b\n" "| |++++| b\n" "| | 1|+first\n" "| 1| 2| old_first\n") def test_apply_with_previous_patches(self): with retrieve_stdout() as stdout: previous_diffs = {"a": Diff(self.file_dict['a'])} previous_diffs["a"].change_line(2, "b\n", "b_changed\n") self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b_changed\n" "| 3| |-c\n" "|----| | b\n" "| |++++| b\n" "| | 1|+first\n" "| 1| 2| old_first\n")
def print_result(console_printer, section, file_diff_dict, result, file_dict, interactive=True): """ Prints the result to console. :param console_printer: Object to print messages on the console. :param section: Name of section to which the result belongs. :param file_diff_dict: Dictionary containing filenames as keys and Diff objects as values. :param result: A derivative of Result. :param file_dict: A dictionary containing all files with filename as key. :param interactive: Variable to check whether or not to offer the user actions interactively. """ no_color = not console_printer.print_colored if not isinstance(result, Result): logging.warning('One of the results can not be printed since it is ' 'not a valid derivative of the coala result ' 'class.') return if hasattr(section, 'name'): console_printer.print( '\n**** {bear} [Section: {section}] ****\n'.format( bear=result.origin, section=section.name), color=RESULT_SEVERITY_COLORS[result.severity]) else: console_printer.print( '\n**** {bear} [Section: {section}] ****\n'.format( bear=result.origin, section='<empty>'), color=RESULT_SEVERITY_COLORS[result.severity]) console_printer.print(format_lines( '[Severity: {sev}]'.format( sev=RESULT_SEVERITY.__str__(result.severity)), '!'), color=RESULT_SEVERITY_COLORS[result.severity]) lexer = TextLexer() result.message = highlight_text(no_color, result.message, lexer, BackgroundMessageStyle) console_printer.print(format_lines(result.message, symbol='!')) if interactive: cli_actions = CLI_ACTIONS show_patch_action = ShowPatchAction() if show_patch_action.is_applicable(result, file_dict, file_diff_dict) is True: diff_size = sum(len(diff) for diff in result.diffs.values()) if diff_size <= DIFF_EXCERPT_MAX_SIZE: show_patch_action.apply_from_section(result, file_dict, file_diff_dict, section) cli_actions = tuple(action for action in cli_actions if not isinstance(action, ShowPatchAction)) else: print_diffs_info(result.diffs, console_printer) acquire_actions_and_apply(console_printer, section, file_diff_dict, result, file_dict, cli_actions)
def print_result(console_printer, log_printer, section, file_diff_dict, result, file_dict, interactive=True): """ Prints the result to console. :param console_printer: Object to print messages on the console. :param log_printer: Printer responsible for logging the messages. :param section: Name of section to which the result belongs. :param file_diff_dict: Dictionary containing filenames as keys and Diff objects as values. :param result: A derivative of Result. :param file_dict: A dictionary containing all files with filename as key. :interactive: Variable to check wether or not to offer the user actions interactively. """ if not isinstance(result, Result): log_printer.warn("One of the results can not be printed since it is " "not a valid derivative of the coala result " "class.") return console_printer.print(format_lines("[{sev}] {bear}:".format( sev=RESULT_SEVERITY.__str__(result.severity), bear=result.origin)), color=RESULT_SEVERITY_COLORS[result.severity]) lexer = TextLexer() result.message = highlight_text(result.message, lexer, BackgroundMessageStyle) console_printer.print(format_lines(result.message)) if interactive: cli_actions = CLI_ACTIONS show_patch_action = ShowPatchAction() if show_patch_action.is_applicable(result, file_dict, file_diff_dict): diff_size = sum(len(diff) for diff in result.diffs.values()) if diff_size <= DIFF_EXCERPT_MAX_SIZE: show_patch_action.apply_from_section(result, file_dict, file_diff_dict, section) cli_actions = tuple(action for action in cli_actions if not isinstance(action, ShowPatchAction)) else: print_diffs_info(result.diffs, console_printer) acquire_actions_and_apply(console_printer, log_printer, section, file_diff_dict, result, file_dict, cli_actions)
class ShowPatchActionTest(unittest.TestCase): def setUp(self): self.uut = ShowPatchAction() self.file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} self.diff_dict = { 'a': Diff(self.file_dict['a']), 'b': Diff(self.file_dict['b']) } self.diff_dict['a'].add_lines(1, ['test\n']) self.diff_dict['a'].delete_line(3) self.diff_dict['b'].add_lines(0, ['first\n']) self.test_result = Result('origin', 'message', diffs=self.diff_dict) self.section = Section('name') self.section.append(Setting('no_color', 'True')) def test_is_applicable(self): diff = Diff([], rename='new_name') result = Result('', '', diffs={'f': diff}) # Two renames donot result in any change self.assertEqual(self.uut.is_applicable(result, {}, {'f': diff}), 'The given patches do not change anything anymore.') with self.assertRaises(TypeError): self.uut.is_applicable(1, None, None) self.assertEqual(self.uut.is_applicable(Result('o', 'm'), None, None), 'This result has no patch attached.') self.assertTrue(self.uut.is_applicable(self.test_result, {}, {})) self.assertIn( 'Two or more patches conflict with each other: ', self.uut.is_applicable(self.test_result, {}, self.diff_dict)) def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual( self.uut.apply_from_section(self.test_result, self.file_dict, {}, self.section), {}) self.assertEqual( stdout.getvalue(), '[----] a\n' '[++++] a\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] b\n' '[ 1] first\n') def test_apply_renaming_only(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([], rename='b')}) file_dict = {'a': []} self.assertEqual( self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual( stdout.getvalue(), '[----] ' + join('a', 'a') + '\n' '[++++] ' + join('b', 'b') + '\n') def test_apply_empty(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([])}) file_dict = {'a': []} self.assertEqual( self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '') def test_apply_with_previous_patches(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].modify_line(2, 'b_changed\n') self.assertEqual( self.uut.apply_from_section(self.test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual( stdout.getvalue(), '[----] a\n' '[++++] a\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] b\n' '[ 1] first\n') def test_apply_with_rename(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].modify_line(2, 'b_changed\n') diff_dict = { 'a': Diff(self.file_dict['a'], rename='a.rename'), 'b': Diff(self.file_dict['b'], delete=True) } diff_dict['a'].add_lines(1, ['test\n']) diff_dict['a'].delete_line(3) diff_dict['b'].add_lines(0, ['first\n']) test_result = Result('origin', 'message', diffs=diff_dict) self.assertEqual( self.uut.apply_from_section(test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual( stdout.getvalue(), '[----] a\n' '[++++] a.rename\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] /dev/null\n' '[ 1] old_first\n')
class ShowPatchActionTest(unittest.TestCase): def setUp(self): self.uut = ShowPatchAction() self.file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} self.diff_dict = {'a': Diff(self.file_dict['a']), 'b': Diff(self.file_dict['b'])} self.diff_dict['a'].add_lines(1, ['test\n']) self.diff_dict['a'].delete_line(3) self.diff_dict['b'].add_lines(0, ['first\n']) self.test_result = Result('origin', 'message', diffs=self.diff_dict) self.section = Section('name') self.section.append(Setting('no_color', 'True')) def test_is_applicable(self): diff = Diff([], rename='new_name') result = Result('', '', diffs={'f': diff}) # Two renames donot result in any change self.assertEqual( self.uut.is_applicable(result, {}, {'f': diff}), 'The given patches do not change anything anymore.' ) with self.assertRaises(TypeError): self.uut.is_applicable(1, None, None) self.assertEqual( self.uut.is_applicable(Result('o', 'm'), None, None), 'This result has no patch attached.') self.assertTrue(self.uut.is_applicable(self.test_result, {}, {})) self.assertIn( 'Two or more patches conflict with each other: ', self.uut.is_applicable(self.test_result, {}, self.diff_dict)) def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '[----] a\n' '[++++] a\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] b\n' '[ 1] first\n') def test_apply_renaming_only(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([], rename='b')}) file_dict = {'a': []} self.assertEqual(self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '[----] ' + join('a', 'a') + '\n' '[++++] ' + join('b', 'b') + '\n') def test_apply_empty(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([])}) file_dict = {'a': []} self.assertEqual(self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '') def test_apply_with_previous_patches(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].modify_line(2, 'b_changed\n') self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), '[----] a\n' '[++++] a\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] b\n' '[ 1] first\n') def test_apply_with_rename(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].modify_line(2, 'b_changed\n') diff_dict = {'a': Diff(self.file_dict['a'], rename='a.rename'), 'b': Diff(self.file_dict['b'], delete=True)} diff_dict['a'].add_lines(1, ['test\n']) diff_dict['a'].delete_line(3) diff_dict['b'].add_lines(0, ['first\n']) test_result = Result('origin', 'message', diffs=diff_dict) self.assertEqual(self.uut.apply_from_section(test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), '[----] a\n' '[++++] a.rename\n' '[ 2] test\n' '[ 3] c\n' '[----] b\n' '[++++] /dev/null\n' '[ 1] old_first\n')
def print_result(console_printer, section, file_diff_dict, result, file_dict, interactive=True, apply_single=False): """ Prints the result to console. :param console_printer: Object to print messages on the console. :param section: Name of section to which the result belongs. :param file_diff_dict: Dictionary containing filenames as keys and Diff objects as values. :param result: A derivative of Result. :param file_dict: A dictionary containing all files with filename as key. :param apply_single: The action that should be applied for all results. If it's not selected, has a value of False. :param interactive: Variable to check whether or not to offer the user actions interactively. """ no_color = not console_printer.print_colored if not isinstance(result, Result): logging.warning('One of the results can not be printed since it is ' 'not a valid derivative of the coala result ' 'class.') return if hasattr(section, 'name'): console_printer.print('**** {bear} [Section: {section} | Severity: ' '{severity}] ****' .format(bear=result.origin, section=section.name, severity=RESULT_SEVERITY.__str__( result.severity)), color=RESULT_SEVERITY_COLORS[result.severity]) else: console_printer.print('**** {bear} [Section {section} | Severity ' '{severity}] ****' .format(bear=result.origin, section='<empty>', severity=RESULT_SEVERITY.__str__( result.severity)), color=RESULT_SEVERITY_COLORS[result.severity]) lexer = TextLexer() result.message = highlight_text(no_color, result.message, BackgroundMessageStyle, lexer) console_printer.print(format_lines(result.message, symbol='!')) if interactive: cli_actions = CLI_ACTIONS show_patch_action = ShowPatchAction() if show_patch_action.is_applicable( result, file_dict, file_diff_dict) is True: diff_size = sum(len(diff) for diff in result.diffs.values()) if diff_size <= DIFF_EXCERPT_MAX_SIZE: show_patch_action.apply_from_section(result, file_dict, file_diff_dict, section) cli_actions = tuple(action for action in cli_actions if not isinstance(action, ShowPatchAction)) else: print_diffs_info(result.diffs, console_printer) acquire_actions_and_apply(console_printer, section, file_diff_dict, result, file_dict, cli_actions, apply_single=apply_single)
class ShowPatchActionTest(unittest.TestCase): def setUp(self): self.uut = ShowPatchAction() self.file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} self.diff_dict = { 'a': Diff(self.file_dict['a']), 'b': Diff(self.file_dict['b']) } self.diff_dict['a'].add_lines(1, ['test\n']) self.diff_dict['a'].delete_line(3) self.diff_dict['b'].add_lines(0, ['first\n']) self.test_result = Result('origin', 'message', diffs=self.diff_dict) self.section = Section('name') self.section.append(Setting('colored', 'false')) def test_is_applicable(self): self.assertFalse(self.uut.is_applicable(1, None, None)) self.assertFalse(self.uut.is_applicable(Result('o', 'm'), None, None)) self.assertTrue(self.uut.is_applicable(self.test_result, {}, {})) self.assertFalse( self.uut.is_applicable(self.test_result, {}, self.diff_dict)) def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual( self.uut.apply_from_section(self.test_result, self.file_dict, {}, self.section), {}) self.assertEqual( stdout.getvalue(), '|----| | a\n' '| |++++| a\n' '| 1| 1| a\n' '| | 2|+test\n' '| 2| 3| b\n' '| 3| |-c\n' '|----| | b\n' '| |++++| b\n' '| | 1|+first\n' '| 1| 2| old_first\n') def test_apply_renaming_only(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([], rename='b')}) file_dict = {'a': []} self.assertEqual( self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual( stdout.getvalue(), '|----| | ' + join('a', 'a') + '\n' '| |++++| ' + join('b', 'b') + '\n') def test_apply_empty(self): with retrieve_stdout() as stdout: test_result = Result('origin', 'message', diffs={'a': Diff([])}) file_dict = {'a': []} self.assertEqual( self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '') def test_apply_with_previous_patches(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].change_line(2, 'b\n', 'b_changed\n') self.assertEqual( self.uut.apply_from_section(self.test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual( stdout.getvalue(), '|----| | a\n' '| |++++| a\n' '| 1| 1| a\n' '| | 2|+test\n' '| 2| 3| b_changed\n' '| 3| |-c\n' '|----| | b\n' '| |++++| b\n' '| | 1|+first\n' '| 1| 2| old_first\n') def test_apply_with_rename(self): with retrieve_stdout() as stdout: previous_diffs = {'a': Diff(self.file_dict['a'])} previous_diffs['a'].change_line(2, 'b\n', 'b_changed\n') diff_dict = { 'a': Diff(self.file_dict['a'], rename='a.rename'), 'b': Diff(self.file_dict['b'], delete=True) } diff_dict['a'].add_lines(1, ['test\n']) diff_dict['a'].delete_line(3) diff_dict['b'].add_lines(0, ['first\n']) test_result = Result('origin', 'message', diffs=diff_dict) self.assertEqual( self.uut.apply_from_section(test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual( stdout.getvalue(), '|----| | a\n' '| |++++| a.rename\n' '| 1| 1| a\n' '| | 2|+test\n' '| 2| 3| b_changed\n' '| 3| |-c\n' '|----| | b\n' '| |++++| /dev/null\n' '| 1| |-old_first\n')
class ShowPatchActionTest(unittest.TestCase): def setUp(self): self.uut = ShowPatchAction() self.file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} self.diff_dict = {"a": Diff(self.file_dict['a']), "b": Diff(self.file_dict['b'])} self.diff_dict["a"].add_lines(1, ["test\n"]) self.diff_dict["a"].delete_line(3) self.diff_dict["b"].add_lines(0, ["first\n"]) self.test_result = Result("origin", "message", diffs=self.diff_dict) self.section = Section("name") self.section.append(Setting("colored", "false")) def test_is_applicable(self): self.assertFalse(self.uut.is_applicable(1, None, None)) self.assertFalse(self.uut.is_applicable(Result("o", "m"), None, None)) self.assertTrue(self.uut.is_applicable(self.test_result, {}, {})) self.assertFalse(self.uut.is_applicable(self.test_result, {}, self.diff_dict)) def test_apply(self): with retrieve_stdout() as stdout: self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b\n" "| 3| |-c\n" "|----| | b\n" "| |++++| b\n" "| | 1|+first\n" "| 1| 2| old_first\n") def test_apply_renaming_only(self): with retrieve_stdout() as stdout: test_result = Result("origin", "message", diffs={'a': Diff([], rename='b')}) file_dict = {'a': []} self.assertEqual(self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '|----| | ' + join('a', 'a') + '\n' '| |++++| ' + join('b', 'b') + '\n') def test_apply_empty(self): with retrieve_stdout() as stdout: test_result = Result("origin", "message", diffs={'a': Diff([])}) file_dict = {'a': []} self.assertEqual(self.uut.apply_from_section(test_result, file_dict, {}, self.section), {}) self.assertEqual(stdout.getvalue(), '') def test_apply_with_previous_patches(self): with retrieve_stdout() as stdout: previous_diffs = {"a": Diff(self.file_dict['a'])} previous_diffs["a"].change_line(2, "b\n", "b_changed\n") self.assertEqual(self.uut.apply_from_section(self.test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b_changed\n" "| 3| |-c\n" "|----| | b\n" "| |++++| b\n" "| | 1|+first\n" "| 1| 2| old_first\n") def test_apply_with_rename(self): with retrieve_stdout() as stdout: previous_diffs = {"a": Diff(self.file_dict['a'])} previous_diffs["a"].change_line(2, "b\n", "b_changed\n") diff_dict = {"a": Diff(self.file_dict['a'], rename="a.rename"), "b": Diff(self.file_dict['b'], delete=True)} diff_dict["a"].add_lines(1, ["test\n"]) diff_dict["a"].delete_line(3) diff_dict["b"].add_lines(0, ["first\n"]) test_result = Result("origin", "message", diffs=diff_dict) self.assertEqual(self.uut.apply_from_section(test_result, self.file_dict, previous_diffs, self.section), previous_diffs) self.assertEqual(stdout.getvalue(), "|----| | a\n" "| |++++| a.rename\n" "| 1| 1| a\n" "| | 2|+test\n" "| 2| 3| b_changed\n" "| 3| |-c\n" "|----| | b\n" "| |++++| /dev/null\n" "| 1| |-old_first\n")