Example #1
0
    def test_apply(self):
        uut = ApplyPatchAction()
        file_dict = {
            "f_a": ["1", "2", "3"],
            "f_b": ["1", "2", "3"],
            "f_c": ["1", "2", "3"]
        }
        expected_file_dict = {
            "f_a": ["1", "3_changed"],
            "f_b": ["1", "2", "3_changed"],
            "f_c": ["1", "2", "3"]
        }

        file_diff_dict = {}

        diff = Diff()
        diff.delete_line(2)
        uut.apply_from_section(PatchResult("origin", "msg", {"f_a": diff}),
                               file_dict, file_diff_dict, Section("t"))

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut.apply_from_section(PatchResult("origin", "msg", {"f_a": diff}),
                               file_dict, file_diff_dict, Section("t"))

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut.apply(PatchResult("origin", "msg", {"f_b": diff}), file_dict,
                  file_diff_dict)

        for filename in file_diff_dict:
            file_dict[filename] = file_diff_dict[filename].apply(
                file_dict[filename])

        self.assertEqual(file_dict, expected_file_dict)
Example #2
0
    def test_add(self):
        file_dict = {
            "f_a": ["1", "2", "3"],
            "f_b": ["1", "2", "3"],
            "f_c": ["1", "2", "3"]
        }
        expected_file_dict = {
            "f_a": ["1", "3_changed"],
            "f_b": ["1", "2", "3_changed"],
            "f_c": ["1", "2", "3"]
        }

        diff = Diff()
        diff.delete_line(2)
        uut1 = PatchResult("origin", "msg", {"f_a": diff})

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut2 = PatchResult("origin", "msg", {"f_a": diff})

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut3 = PatchResult("origin", "msg", {"f_b": diff})

        uut1 += uut2 + uut3
        uut1.apply(file_dict)

        self.assertEqual(file_dict, expected_file_dict)
Example #3
0
    def test_print_result(self):
        self.uut.print = lambda x: x
        self.assertEqual("|    |    | [{normal}] {bear}:".format(normal=RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL),
                                                                 bear="origin") + "\n|    |    | message",
                         self.uut._print_result(Result("origin", "message")))

        builtins.__dict__["input"] = lambda x: 0
        self.uut.print_result(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")
        builtins.__dict__["input"] = self.generate_input  # To assure user can rechose if he didn't chose wisely
        self.uut.print_result(PatchResult("origin", "msg", {testfile_path: diff}), file_dict)
        self.assertEqual(self.curr, 1)
        self.uut.finalize(file_dict)
        with open(testfile_path) as f:
            self.assertEqual(f.readlines(), ["1\n", "3_changed\n"])

        os.remove(testfile_path)

        name, section = self.uut._get_action_info(TestAction().get_metadata())
        self.assertEqual(str(section), " {param : 3}")
        self.assertEqual(name, "TestAction")

        builtins.__dict__["input"] = lambda x: x
Example #4
0
 def test_raises(self):
     self.assertRaises(TypeError,
                       PatchResult,
                       origin="test",
                       message="test",
                       diffs=5)
     self.assertRaises(TypeError, PatchResult("t", "t", {}).apply, 5)
     self.assertRaises(TypeError, PatchResult("t", "t", {}).__add__, 5)
Example #5
0
    def test_add(self):
        file_dict = {
            "f_a": ["1", "2", "3"],
            "f_b": ["1", "2", "3"],
            "f_c": ["1", "2", "3"]
        }
        expected_file_dict = {
            "f_a": ["1", "3_changed"],
            "f_b": ["1", "2", "3_changed"],
            "f_c": ["1", "2", "3"]
        }

        diff = Diff()
        diff.delete_line(2)
        uut1 = PatchResult("origin", "msg", {"f_a": diff})

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut2 = PatchResult("origin", "msg", {"f_a": diff})

        diff = Diff()
        diff.change_line(3, "3", "3_changed")
        uut3 = PatchResult("origin", "msg", {"f_b": diff})

        uut1 += uut2 + uut3
        uut1.apply(file_dict)

        self.assertEqual(file_dict, expected_file_dict)
    def run(self,
            filename,
            file,
            use_spaces: bool,
            allow_trailing_whitespace: bool=False,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH):
        """
        Checks the space consistency for each line.

        :param use_spaces:                True if spaces are to be used
                                          instead of tabs.
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
                                          or not.
        :param tab_width:                 Number of spaces representing one
                                          tab.
        """
        results = []

        spacing_helper = SpacingHelper(tab_width)

        for line_number, line in enumerate(file):
            replacement = line

            if not allow_trailing_whitespace:
                replacement = replacement.rstrip(" \t\n") + "\n"

            if use_spaces:
                replacement = spacing_helper.replace_tabs_with_spaces(
                    replacement)
            else:
                replacement = spacing_helper.replace_spaces_with_tabs(
                    replacement)

            if replacement != line:
                diff = Diff()
                diff.change_line(line_number + 1, line, replacement)
                results.append(PatchResult(self,
                                           _("Line contains spacing "
                                             "inconsistencies."),
                                           {filename: diff},
                                           filename,
                                           line_nr=line_number+1))

        return results
Example #7
0
    def test_apply(self):
        file_dict = {"f_a": ["1", "2", "3"], "f_b": ["1", "2", "3"]}
        expected_file_dict = {
            "f_a": ["1", "3_changed"],
            "f_b": ["1", "2", "3"]
        }
        diff = Diff()
        diff.delete_line(2)
        diff.change_line(3, "3", "3_changed")

        uut = PatchResult("origin", "msg", {"f_a": diff})
        uut.apply(file_dict)

        self.assertEqual(file_dict, expected_file_dict)

        uut = PatchResult("origin", "msg", {})
        for action in uut.get_actions():
            action.apply(
                uut, {},
                {})  # All those actions should be able to apply this result

        self.assertEqual(len(uut.get_actions()), 1)
Example #8
0
    def test_apply(self):
        file_dict = {
            "f_a": ["1", "2", "3"],
            "f_b": ["1", "2", "3"]
        }
        expected_file_dict = {
            "f_a": ["1", "3_changed"],
            "f_b": ["1", "2", "3"]
        }
        diff = Diff()
        diff.delete_line(2)
        diff.change_line(3, "3", "3_changed")

        uut = PatchResult("origin", "msg", {"f_a": diff})
        uut.apply(file_dict)

        self.assertEqual(file_dict, expected_file_dict)

        uut = PatchResult("origin", "msg", {})
        for action in uut.get_actions():
            action.apply(uut, {}, {})  # All those actions should be able to apply this result

        self.assertEqual(len(uut.get_actions()), 1)
    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)
Example #10
0
    def test_print_result(self):
        self.uut.print = lambda x: x
        builtins.__dict__["input"] = lambda x: 0

        self.assertEqual(
            "|    |    | [{normal}] {bear}:".format(
                normal=RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL),
                bear="origin") + "\n|    |    | message",
            self.uut._print_result(Result("origin", "message")))

        self.uut.print_result(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")

        builtins.__dict__["input"] = lambda x: 1
        with self.assertRaises(ValueError):
            self.uut.print_result(
                PatchResult("origin", "msg", {testfile_path: diff}), file_dict)

        # To assure user can rechose if he didn't chose wisely
        input_generator = InputGenerator(["INVALID", -1, 1, 3])
        builtins.__dict__["input"] = input_generator.generate_input
        # To load current_section in ConsoleInteractor object
        self.uut.begin_section(Section(""))
        self.uut.print_result(
            PatchResult("origin", "msg", {testfile_path: diff}), file_dict)
        self.assertEqual(input_generator.current_input, 2)
        self.uut.finalize(file_dict)
        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 = self.uut._get_action_info(TestAction().get_metadata())
        self.assertEqual(str(section), " {param : 3}")
        self.assertEqual(name, "TestAction")

        # Check for asking the user for the paremeter just int the first time
        # Use OpenEditorAction thats need parameter (editor command)
        input_generator = InputGenerator([1, "test_editor", 1])
        builtins.__dict__[
            "input"] = input_generator.generate_input  # Choose open editor action
        PatchResult.get_actions = lambda self: [OpenEditorAction()]

        patch_result = PatchResult("origin", "msg", {testfile_path: diff})
        patch_result.file = "f_b"

        self.uut.print_result(patch_result, file_dict)
        self.assertEquals(input_generator.current_input,
                          1)  # Increase by 2 (-1 -> 1)

        # Increase by 1, It shoudn't ask for parameter again
        self.uut.print_result(patch_result, file_dict)
        self.assertEquals(input_generator.current_input, 2)
    def test_print_result(self):
        self.uut.print = lambda x: x
        builtins.__dict__["input"] = lambda x: 0

        self.assertEqual(
            "|    |    | [{normal}] {bear}:".format(
                normal=RESULT_SEVERITY.__str__(RESULT_SEVERITY.NORMAL),
                bear="origin") + "\n|    |    | message",
            self.uut._print_result(Result("origin", "message")))

        self.uut.print_result(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")

        builtins.__dict__["input"] = lambda x: 1
        with self.assertRaises(ValueError):
            self.uut.print_result(PatchResult("origin",
                                              "msg",
                                              {testfile_path: diff}), file_dict)

        # To assure user can rechose if he didn't chose wisely
        input_generator = InputGenerator(["INVALID", -1, 1, 3])
        builtins.__dict__["input"] = input_generator.generate_input
        # To load current_section in ConsoleInteractor object
        self.uut.begin_section(Section(""))
        self.uut.print_result(PatchResult("origin",
                                          "msg",
                                          {testfile_path: diff}), file_dict)
        self.assertEqual(input_generator.current_input, 2)
        self.uut.finalize(file_dict)
        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 = self.uut._get_action_info(TestAction().get_metadata())
        self.assertEqual(str(section), " {param : 3}")
        self.assertEqual(name, "TestAction")

        # Check for asking the user for the paremeter just int the first time
        # Use OpenEditorAction thats need parameter (editor command)
        input_generator = InputGenerator([1, "test_editor", 1])
        builtins.__dict__["input"] = input_generator.generate_input  # Choose open editor action
        PatchResult.get_actions = lambda self: [OpenEditorAction()]

        patch_result = PatchResult("origin", "msg", {testfile_path: diff})
        patch_result.file = "f_b"

        self.uut.print_result(patch_result, file_dict)
        self.assertEquals(input_generator.current_input, 1)  # Increase by 2 (-1 -> 1)

        # Increase by 1, It shoudn't ask for parameter again
        self.uut.print_result(patch_result, file_dict)
        self.assertEquals(input_generator.current_input, 2)