def test_print_result_interactive_big_patch(self, diffs_info, _): file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} diff_dict = {'a': Diff(file_dict['a']), 'b': Diff(file_dict['b'])} diff_dict['a'].add_lines(1, ['test\n', 'test1\n', 'test2\n']) diff_dict['a'].delete_line(3) diff_dict['a'].add_lines(3, ['3test\n']) result = Result('origin', 'msg', diffs=diff_dict) section = Section('test') print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) diffs_info.assert_called_once_with(diff_dict, self.console_printer)
def test_print_result_interactive_big_patch(self, diffs_info, _): file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} diff_dict = {"a": Diff(file_dict['a']), "b": Diff(file_dict['b'])} diff_dict["a"].add_lines(1, ["test\n", "test1\n", "test2\n"]) diff_dict["a"].delete_line(3) diff_dict["a"].add_lines(3, ["3test\n"]) result = Result("origin", "msg", diffs=diff_dict) section = Section("test") print_result(self.console_printer, self.log_printer, section, self.file_diff_dict, result, file_dict, True) diffs_info.assert_called_once_with(diff_dict, self.console_printer)
def test_print_result_interactive_small_patch(self, apply_from_section, _): file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} diff_dict = {'a': Diff(file_dict['a']), 'b': Diff(file_dict['b'])} diff_dict['a'].add_lines(1, ['test\n']) diff_dict['a'].delete_line(3) result = Result('origin', 'msg', diffs=diff_dict) section = Section('test') print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) apply_from_section.assert_called_once_with(result, file_dict, self.file_diff_dict, section)
def test_print_result_interactive_small_patch(self, apply_from_section, _): file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} diff_dict = {"a": Diff(file_dict['a']), "b": Diff(file_dict['b'])} diff_dict["a"].add_lines(1, ["test\n"]) diff_dict["a"].delete_line(3) result = Result("origin", "msg", diffs=diff_dict) section = Section("test") print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) apply_from_section.assert_called_once_with(result, file_dict, self.file_diff_dict, section)
def test_print_result_interactive_small_patch(self, apply_from_section, _): file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']} diff_dict = {'a': Diff(file_dict['a']), 'b': Diff(file_dict['b'])} diff_dict['a'].add_lines(1, ['test\n']) diff_dict['a'].delete_line(3) result = Result('origin', 'msg', diffs=diff_dict) section = Section('test') print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) apply_from_section.assert_called_once_with( result, file_dict, self.file_diff_dict, section)
def test_print_result_interactive_big_patch(self, diffs_info, _): file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} diff_dict = {"a": Diff(file_dict['a']), "b": Diff(file_dict['b'])} diff_dict["a"].add_lines(1, ["test\n", "test1\n", "test2\n"]) diff_dict["a"].delete_line(3) diff_dict["a"].add_lines(3, ["3test\n"]) result = Result("origin", "msg", diffs=diff_dict) section = Section("test") print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) diffs_info.assert_called_once_with(diff_dict, self.console_printer)
def test_print_result_interactive_small_patch(self, apply_from_section, _): file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]} diff_dict = {"a": Diff(file_dict['a']), "b": Diff(file_dict['b'])} diff_dict["a"].add_lines(1, ["test\n"]) diff_dict["a"].delete_line(3) result = Result("origin", "msg", diffs=diff_dict) section = Section("test") print_result(self.console_printer, section, self.file_diff_dict, result, file_dict, True) apply_from_section.assert_called_once_with( result, file_dict, self.file_diff_dict, section)
def apply(self, result, original_file_dict, file_diff_dict, no_color: bool = False, show_result_on_top: bool = False): """ (S)how patch :param no_color: Whether or not to use colored output. :param show_result_on_top: Set this to True if you want to show the result info on top. (Useful for e.g. coala_ci.) """ printer = ConsolePrinter(not no_color) if show_result_on_top: from coalib.output.ConsoleInteraction import print_result # Most of the params are empty because they're unneeded in # noninteractive mode. Yes, this cries for a refactoring... print_result(printer, None, {}, result, {}, interactive=False) for filename, this_diff in sorted(result.diffs.items()): to_filename = this_diff.rename if this_diff.rename else filename to_filename = '/dev/null' if this_diff.delete else to_filename original_file = original_file_dict[filename] try: current_file = file_diff_dict[filename].modified new_file = (file_diff_dict[filename] + this_diff).modified except KeyError: current_file = original_file new_file = this_diff.modified if tuple(current_file) != tuple(new_file): print_beautified_diff(difflib.unified_diff(current_file, new_file, fromfile=filename, tofile=to_filename), printer) elif filename != to_filename: print_from_name(printer, join('a', relpath(filename))) print_to_name(printer, join('b', relpath(to_filename))) return file_diff_dict
def test_print_result(self): print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, "illegal value", {}) with simulate_console_inputs(0): print_result(self.console_printer, self.log_printer, self.section, self.file_diff_dict, Result("origin", "msg", diffs={}), {}) with make_temp() as testfile_path: file_dict = { testfile_path: ["1\n", "2\n", "3\n"], "f_b": ["1", "2", "3"] } diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, "3\n", "3_changed\n") ApplyPatchAction.is_applicable = staticmethod(lambda *args: True) # Interaction must be closed by the user with `0` if it's not a # param with simulate_console_inputs("INVALID", -1, 1, 0, 3) as input_generator: curr_section = Section("") print_section_beginning(self.console_printer, curr_section) print_result( self.console_printer, self.log_printer, curr_section, self.file_diff_dict, Result("origin", "msg", diffs={testfile_path: diff}), file_dict) self.assertEqual(input_generator.last_input, 3) self.file_diff_dict.clear() with open(testfile_path) as f: self.assertEqual(f.readlines(), ["1\n", "3_changed\n"]) os.remove(testfile_path + ".orig") name, section = get_action_info(curr_section, TestAction().get_metadata(), failed_actions=set()) self.assertEqual(input_generator.last_input, 4) self.assertEqual(str(section), " {param : '3'}") self.assertEqual(name, "TestAction") # Check if the user is asked for the parameter only the first time. # Use OpenEditorAction that needs this parameter (editor command). with simulate_console_inputs(1, "test_editor", 0, 1, 0) as generator: OpenEditorAction.is_applicable = staticmethod(lambda *args: True) patch_result = Result("origin", "msg", diffs={testfile_path: diff}) patch_result.file = "f_b" print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) # choose action, choose editor, choose no action (-1 -> 2) self.assertEqual(generator.last_input, 2) # It shoudn't ask for parameter again print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) self.assertEqual(generator.last_input, 4)
def test_print_result(self): print_result(self.console_printer, None, self.file_diff_dict, 'illegal value', {}) with simulate_console_inputs(0): print_result(self.console_printer, self.section, self.file_diff_dict, Result('origin', 'msg', diffs={}), {}) with make_temp() as testfile_path: file_dict = { testfile_path: ['1\n', '2\n', '3\n'], 'f_b': ['1', '2', '3'] } diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, '3\n', '3_changed\n') ApplyPatchAction.is_applicable = staticmethod( lambda *args: True) # Interaction must be closed by the user with `0` if it's not a # param with simulate_console_inputs('INVALID', -1, 1, 0, 3) as input_generator: curr_section = Section('') print_section_beginning(self.console_printer, curr_section) print_result(self.console_printer, curr_section, self.file_diff_dict, Result('origin', 'msg', diffs={ testfile_path: diff}), file_dict) self.assertEqual(input_generator.last_input, 3) self.file_diff_dict.clear() with open(testfile_path) as f: self.assertEqual(f.readlines(), ['1\n', '3_changed\n']) os.remove(testfile_path + '.orig') name, section = get_action_info(curr_section, TestAction().get_metadata(), failed_actions=set()) self.assertEqual(input_generator.last_input, 4) self.assertEqual(str(section), " {param : '3'}") self.assertEqual(name, 'TestAction') # Check if the user is asked for the parameter only the first time. # Use OpenEditorAction that needs this parameter (editor command). with simulate_console_inputs(1, 'test_editor', 0, 1, 0) as generator: OpenEditorAction.is_applicable = staticmethod(lambda *args: True) patch_result = Result('origin', 'msg', diffs={testfile_path: diff}) patch_result.file = 'f_b' print_result(self.console_printer, curr_section, self.file_diff_dict, patch_result, file_dict) # choose action, choose editor, choose no action (-1 -> 2) self.assertEqual(generator.last_input, 2) # It shoudn't ask for parameter again print_result(self.console_printer, curr_section, self.file_diff_dict, patch_result, file_dict) self.assertEqual(generator.last_input, 4)
def test_print_result(self): print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, "illegal value", {}) with simulate_console_inputs(0): print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, Result("origin", "msg", diffs={}), {}) with make_temp() as testfile_path: file_dict = { testfile_path: ["1\n", "2\n", "3\n"], "f_b": ["1", "2", "3"] } diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, "3\n", "3_changed\n") with simulate_console_inputs(1), self.assertRaises(ValueError): ApplyPatchAction.is_applicable = staticmethod( lambda *args: True) print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, Result("origin", "msg", diffs={ testfile_path: diff}), file_dict) # Interaction must be closed by the user with `0` if it's not a # param with simulate_console_inputs("INVALID", -1, 1, 0, 3) as input_generator: curr_section = Section("") print_section_beginning(self.console_printer, curr_section) print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, Result("origin", "msg", diffs={ testfile_path: diff}), file_dict) self.assertEqual(input_generator.last_input, 3) self.file_diff_dict.clear() with open(testfile_path) as f: self.assertEqual(f.readlines(), ["1\n", "3_changed\n"]) os.remove(testfile_path + ".orig") name, section = get_action_info(curr_section, TestAction().get_metadata()) self.assertEqual(input_generator.last_input, 4) self.assertEqual(str(section), " {param : '3'}") self.assertEqual(name, "TestAction") # Check if the user is asked for the parameter only the first time. # Use OpenEditorAction that needs this parameter (editor command). with simulate_console_inputs(1, "test_editor", 0, 1, 0) as generator: OpenEditorAction.is_applicable = staticmethod(lambda *args: True) patch_result = Result("origin", "msg", diffs={testfile_path: diff}) patch_result.file = "f_b" print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) # choose action, choose editor, choose no action (-1 -> 2) self.assertEqual(generator.last_input, 2) # It shoudn't ask for parameter again print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) self.assertEqual(generator.last_input, 4)
def test_print_result(self): print_result(self.console_printer, None, self.file_diff_dict, 'illegal value', {}) with simulate_console_inputs('n'): print_result(self.console_printer, self.section, self.file_diff_dict, Result('origin', 'msg', diffs={}), {}) with make_temp() as testfile_path: file_dict = { testfile_path: ['1\n', '2\n', '3\n'], 'f_b': ['1', '2', '3'] } diff = Diff(file_dict[testfile_path]) diff.delete_line(2) diff.change_line(3, '3\n', '3_changed\n') ApplyPatchAction.is_applicable = staticmethod(lambda *args: True) # Interaction must be closed by the user with `0` if it's not a # param with simulate_console_inputs('INVALID', -1, 'a', 'n', 'm') as input_generator: curr_section = Section('') print_section_beginning(self.console_printer, curr_section) print_result( self.console_printer, curr_section, self.file_diff_dict, Result('origin', 'msg', diffs={testfile_path: diff}), file_dict) self.assertEqual(input_generator.last_input, 3) self.file_diff_dict.clear() with open(testfile_path) as f: self.assertEqual(f.readlines(), ['1\n', '3_changed\n']) os.remove(testfile_path + '.orig') name, section = get_action_info(curr_section, TestAction().get_metadata(), failed_actions=set()) self.assertEqual(input_generator.last_input, 4) self.assertEqual(str(section), " {param : 'm'}") self.assertEqual(name, 'TestAction') # Check if the user is asked for the parameter only the first time. # Use OpenEditorAction that needs this parameter (editor command). with simulate_console_inputs('o', 'test_editor', 'n', 'o', 'n') as generator: OpenEditorAction.is_applicable = staticmethod(lambda *args: True) patch_result = Result('origin', 'msg', diffs={testfile_path: diff}) patch_result.file = 'f_b' print_result(self.console_printer, curr_section, self.file_diff_dict, patch_result, file_dict) # choose action, choose editor, choose no action (-1 -> 2) self.assertEqual(generator.last_input, 2) # It shoudn't ask for parameter again print_result(self.console_printer, curr_section, self.file_diff_dict, patch_result, file_dict) self.assertEqual(generator.last_input, 4)
def test_print_result(self): print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, "illegal value", {}) with simulate_console_inputs(0): print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, PatchResult("origin", "msg", {}), {}) (testfile, testfile_path) = tempfile.mkstemp() os.close(testfile) file_dict = { testfile_path: ["1\n", "2\n", "3\n"], "f_b": ["1", "2", "3"] } diff = Diff() diff.delete_line(2) diff.change_line(3, "3\n", "3_changed\n") with simulate_console_inputs(1), self.assertRaises(ValueError): ApplyPatchAction.is_applicable = staticmethod(lambda result: True) print_result(self.console_printer, self.log_printer, None, self.file_diff_dict, PatchResult("origin", "msg", {testfile_path: diff}), file_dict) # To assure user can rechose if he didn't chose wisely with simulate_console_inputs("INVALID", -1, 1, 3) as input_generator: curr_section = Section("") print_section_beginning(self.console_printer, curr_section) print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, PatchResult("origin", "msg", {testfile_path: diff}), file_dict) self.assertEqual(input_generator.last_input, 2) finalize(self.file_diff_dict, file_dict) # Check that the next section does not use the same file_diff_dict self.assertEqual(len(self.file_diff_dict), 0) with open(testfile_path) as f: self.assertEqual(f.readlines(), ["1\n", "3_changed\n"]) os.remove(testfile_path) os.remove(testfile_path + ".orig") name, section = get_action_info(curr_section, TestAction().get_metadata()) self.assertEqual(input_generator.last_input, 3) self.assertEqual(str(section), " {param : 3}") self.assertEqual(name, "TestAction") # Check if the user is asked for the parameter only the first time. # Use OpenEditorAction that needs this parameter (editor command). with simulate_console_inputs(1, "test_editor", 0, 1, 0) as generator: OpenEditorAction.is_applicable = staticmethod(lambda result: True) patch_result = PatchResult("origin", "msg", {testfile_path: diff}) patch_result.file = "f_b" print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) # choose action, choose editor, choose no action (-1 -> 2) self.assertEqual(generator.last_input, 2) # It shoudn't ask for parameter again print_result(self.console_printer, self.log_printer, curr_section, self.file_diff_dict, patch_result, file_dict) self.assertEqual(generator.last_input, 4)