Exemplo n.º 1
0
    def test_filter_interdiff_opcodes(self):
        """Testing filter_interdiff_opcodes"""
        opcodes = [
            ('insert', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 6),
            ('delete', 5, 10, 6, 6),
            ('equal', 10, 25, 6, 21),
            ('replace', 25, 26, 21, 22),
            ('equal', 26, 40, 22, 36),
            ('insert', 40, 40, 36, 46),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(22, 10, 22, 10)
        new_diff = b''.join([
            self._build_dummy_diff_data(2, 14, 2, 9),
            self._build_dummy_diff_data(22, 10, 22, 10),
        ])

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 0, 0, 1),
            ('filtered-equal', 0, 5, 1, 6),
            ('filtered-equal', 5, 10, 6, 6),
            ('equal', 10, 25, 6, 21),
            ('replace', 25, 26, 21, 22),
            ('equal', 26, 28, 22, 24),
            ('filtered-equal', 28, 40, 24, 36),
            ('filtered-equal', 40, 40, 36, 46),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 2
0
    def test_filter_interdiff_opcodes(self):
        """Testing filter_interdiff_opcodes"""
        opcodes = [
            ('insert', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 5),
            ('delete', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 26),
            ('equal', 26, 40, 26, 40),
            ('insert', 40, 40, 40, 45),
        ]

        # NOTE: Only the "@@" lines in the diff matter below for this
        #       processor, so the rest can be left out.
        orig_diff = '@@ -22,7 +22,7 @@\n'
        new_diff = (
            '@@ -2,11 +2,6 @@\n'
            '@@ -22,7 +22,7 @@\n'
        )

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('equal', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 5),
            ('delete', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 26),
            ('equal', 26, 40, 26, 40),
            ('equal', 40, 40, 40, 45),
        ])
Exemplo n.º 3
0
    def test_filter_interdiff_opcodes_with_inserts_right(self):
        """Testing filter_interdiff_opcodes with inserts on the right"""
        # These opcodes were taken from the r1-r2 interdiff at
        # http://reviews.reviewboard.org/r/4221/
        opcodes = [
            ('equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ]

        # NOTE: Only the "@@" lines in the diff matter below for this
        #       processor, so the rest can be left out.
        orig_diff = '@@ -0,0 +1,232 @@\n'
        new_diff = '@@ -0,0 +1,239 @@\n'

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ])
Exemplo n.º 4
0
    def test_filter_interdiff_opcodes_with_inserts_right(self):
        """Testing filter_interdiff_opcodes with inserts on the right"""
        # These opcodes were taken from the r1-r2 interdiff at
        # http://reviews.reviewboard.org/r/4221/
        opcodes = [
            ('equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(0, 3, 1, 235)
        new_diff = self._build_dummy_diff_data(0, 3, 1, 242)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 5
0
    def _apply_processors(self, opcodes):
        if self.interfilediff:
            # Filter out any lines unrelated to these changes from the
            # interdiff. This will get rid of any merge information.
            opcodes = filter_interdiff_opcodes(opcodes, self.filediff.diff,
                                               self.interfilediff.diff)

        for opcode in opcodes:
            yield opcode
Exemplo n.º 6
0
    def _apply_processors(self, opcodes):
        if self.interfilediff:
            # Filter out any lines unrelated to these changes from the
            # interdiff. This will get rid of any merge information.
            opcodes = filter_interdiff_opcodes(opcodes, self.filediff.diff,
                                               self.interfilediff.diff)

            # From the filtered content, we may have ended up with consecutive
            # "equal" chunks, so merge them.
            opcodes = merge_adjacent_chunks(opcodes)

        for opcode in opcodes:
            yield opcode
Exemplo n.º 7
0
    def test_filter_interdiff_opcodes_with_many_ignorable_ranges(self):
        """Testing filter_interdiff_opcodes with many ignorable ranges"""
        # These opcodes were taken from the r1-r2 interdiff at
        # http://reviews.reviewboard.org/r/4257/
        opcodes = [
            ('equal', 0, 631, 0, 631),
            ('replace', 631, 632, 631, 632),
            ('insert', 632, 632, 632, 633),
            ('equal', 632, 882, 633, 883),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = b''.join([
            self._build_dummy_diff_data(*values)
            for values in (
                (413, 9, 413, 11),
                (422, 12, 424, 16),
                (433, 9, 439, 11),
                (442, 9, 450, 12),
                (595, 9, 605, 208),
                (636, 9, 845, 39),
            )
        ])
        new_diff = b''.join([
            self._build_dummy_diff_data(*values)
            for values in (
                (413, 9, 413, 11),
                (422, 12, 424, 16),
                (433, 9, 439, 11),
                (442, 9, 450, 11),
                (595, 6, 605, 209),
                (636, 9, 846, 39),
            )
        ])

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 631, 0, 631),
            ('replace', 631, 632, 631, 632),
            ('insert', 632, 632, 632, 633),
            ('equal', 632, 809, 633, 810),
            ('filtered-equal', 809, 882, 810, 883),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 8
0
    def test_filter_interdiff_opcodes_early_change(self):
        """Testing filter_interdiff_opcodes with a change early in the file"""
        opcodes = [
            ('replace', 2, 3, 2, 3),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(1, 5, 1, 6,
                                                pre_lines_of_context=2)
        new_diff = self._build_dummy_diff_data(1, 5, 1, 6,
                                               pre_lines_of_context=2)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('replace', 2, 3, 2, 3),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 9
0
    def test_filter_interdiff_opcodes_1_line(self):
        """Testing filter_interdiff_opcodes with a 1 line file"""
        opcodes = [
            ('replace', 0, 1, 0, 1),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(1, 1, 1, 2,
                                                pre_lines_of_context=0)
        new_diff = self._build_dummy_diff_data(1, 1, 1, 2,
                                               pre_lines_of_context=0)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('replace', 0, 1, 0, 1),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 10
0
    def test_filter_interdiff_opcodes_replace_after_valid_ranges(self):
        """Testing filter_interdiff_opcodes with replace after valid range"""
        # While developing the fix for replace lines in
        # https://reviews.reviewboard.org/r/6030/, an iteration of the fix
        # broke replace lines when one side exceeded its last range found in
        # the diff.
        opcodes = [
            ('replace', 12, 13, 5, 6),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(2, 10, 2, 10)
        new_diff = self._build_dummy_diff_data(2, 10, 2, 10)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('replace', 12, 13, 5, 6),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 11
0
    def test_filter_interdiff_opcodes_replace_after_valid_ranges(self):
        """Testing filter_interdiff_opcodes with replace after valid range"""
        # While developing the fix for replace lines in
        # https://reviews.reviewboard.org/r/6030/, an iteration of the fix
        # broke replace lines when one side exceeded its last range found in
        # the diff.
        opcodes = [
            ('replace', 12, 13, 5, 6),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(2, 10, 2, 10)
        new_diff = self._build_dummy_diff_data(2, 10, 2, 10)

        new_opcodes = list(
            filter_interdiff_opcodes(opcodes, orig_diff, new_diff))

        self.assertEqual(new_opcodes, [
            ('replace', 12, 13, 5, 6),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 12
0
    def test_filter_interdiff_opcodes_with_many_ignorable_ranges(self):
        """Testing filter_interdiff_opcodes with many ignorable ranges"""
        # These opcodes were taken from the r1-r2 interdiff at
        # http://reviews.reviewboard.org/r/4257/
        opcodes = [
            ('equal', 0, 631, 0, 631),
            ('replace', 631, 632, 631, 632),
            ('insert', 632, 632, 632, 633),
            ('equal', 632, 882, 633, 883),
        ]

        # NOTE: Only the "@@" lines in the diff matter below for this
        #       processor, so the rest can be left out.
        orig_diff = (
            '@@ -413,6 +413,8 @@\n'
            '@@ -422,9 +424,13 @@\n'
            '@@ -433,6 +439,8 @@\n'
            '@@ -442,6 +450,9 @@\n'
            '@@ -595,6 +605,205 @@\n'
            '@@ -636,6 +845,36 @@\n'
        )
        new_diff = (
            '@@ -413,6 +413,8 @@\n'
            '@@ -422,9 +424,13 @@\n'
            '@@ -433,6 +439,8 @@\n'
            '@@ -442,6 +450,8 @@\n'
            '@@ -595,6 +605,206 @@\n'
            '@@ -636,6 +846,36 @@\n'
        )

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('equal', 0, 631, 0, 631),
            ('replace', 631, 632, 631, 632),
            ('insert', 632, 632, 632, 633),
            ('equal', 632, 882, 633, 883),
        ])
Exemplo n.º 13
0
    def test_filter_interdiff_opcodes_with_replace_overflowing_range(self):
        """Testing filter_interdiff_opcodes with replace overflowing range"""
        # In the case where there's a replace chunk with i2 or j2 larger than
        # the end position of the current range, the chunk would get chopped,
        # and the two replace ranges could be unequal. This broke an assertion
        # check when generating opcode metadata, and would result in a
        # corrupt-looking diff.
        #
        # This is bug #3440
        #
        # Before the fix, the below opcodes and diff ranges would result
        # in the replace turning into (2, 6, 2, 15), instead of staying at
        # (2, 15, 2, 15).
        #
        # This only really tends to happen in early ranges (since the range
        # numbers are small), but could also happen further into the diff
        # if a replace range is huge on one side.
        opcodes = [
            ('equal', 0, 2, 0, 2),
            ('replace', 2, 100, 2, 100),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = b''.join([
            self._build_dummy_diff_data(1, 4, 1, 3, pre_lines_of_context=0),
            self._build_dummy_diff_data(8, 21, 9, 22),
        ])
        new_diff = self._build_dummy_diff_data(1, 13, 1, 17,
                                               pre_lines_of_context=0)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('equal', 0, 2, 0, 2),
            ('replace', 2, 15, 2, 15),
            ('filtered-equal', 15, 100, 15, 100),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 14
0
    def test_filter_interdiff_opcodes_with_trailing_context(self):
        """Testing filter_interdiff_opcodes with trailing context"""
        opcodes = [
            ('replace', 0, 13, 0, 13),
            ('insert', 13, 13, 13, 14),
            ('replace', 13, 20, 14, 21),
        ]
        self._sanity_check_opcodes(opcodes)

        orig_diff = self._build_dummy_diff_data(10, 5, 10, 6)
        new_diff = self._build_dummy_diff_data(10, 6, 10, 7,
                                               pre_lines_of_context=4)

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 13, 0, 13),
            ('insert', 13, 13, 13, 14),
            ('filtered-equal', 13, 20, 14, 21),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 15
0
    def test_filter_interdiff_opcodes_with_inserts_right(self):
        """Testing filter_interdiff_opcodes with inserts on the right"""
        # These opcodes were taken from the r1-r2 interdiff at
        # http://reviews.reviewboard.org/r/4221/
        opcodes = [
            ('equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ]
        self._sanity_check_opcodes(opcodes)

        # NOTE: Only the "@@" lines and the lines leading up to the first
        #       change in a chunk matter to the processor for this test,
        #       so the rest can be left out.
        orig_diff = ('@@ -0,0 +1,232 @@\n' ' #\n #\n #\n+#\n')
        new_diff = ('@@ -0,0 +1,239 @@\n' ' #\n #\n #\n+#\n')

        new_opcodes = list(
            filter_interdiff_opcodes(opcodes, orig_diff, new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 141, 0, 141),
            ('replace', 141, 142, 141, 142),
            ('insert', 142, 142, 142, 144),
            ('equal', 142, 165, 144, 167),
            ('replace', 165, 166, 167, 168),
            ('insert', 166, 166, 168, 170),
            ('equal', 166, 190, 170, 194),
            ('insert', 190, 190, 194, 197),
            ('equal', 190, 232, 197, 239),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 16
0
    def test_filter_interdiff_opcodes_replace_after_valid_ranges(self):
        """Testing filter_interdiff_opcodes with replace after valid range"""
        # While developing the fix for replace lines in
        # https://reviews.reviewboard.org/r/6030/, an iteration of the fix
        # broke replace lines when one side exceeded its last range found in
        # the diff.
        opcodes = [
            ('replace', 12, 13, 5, 6),
        ]
        self._sanity_check_opcodes(opcodes)

        # NOTE: Only the "@@" lines and the lines leading up to the first
        #       change in a chunk matter to the processor for this test,
        #       so the rest can be left out.
        orig_diff = ('@@ -2,7 +2,7 @@\n' ' #\n #\n #\n-#\n')
        new_diff = ('@@ -2,7 +2,7 @@\n' ' #\n #\n #\n-#\n')

        new_opcodes = list(
            filter_interdiff_opcodes(opcodes, orig_diff, new_diff))

        self.assertEqual(new_opcodes, [
            ('replace', 12, 13, 5, 6),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 17
0
    def test_filter_interdiff_opcodes(self):
        """Testing filter_interdiff_opcodes"""
        opcodes = [
            ('insert', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 5),
            ('delete', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 21),
            ('equal', 26, 40, 21, 35),
            ('insert', 40, 40, 35, 45),
        ]
        self._sanity_check_opcodes(opcodes)

        # NOTE: Only the "@@" lines and the lines leading up to the first
        #       change in a chunk matter to the processor for this test,
        #       so the rest can be left out.
        orig_diff = ('@@ -22,7 +22,7 @@\n' ' #\n #\n #\n-#\n')
        new_diff = ('@@ -2,11 +2,6 @@\n'
                    ' #\n #\n #\n-#\n'
                    '@@ -22,7 +22,7 @@\n'
                    ' #\n #\n #\n-#\n')

        new_opcodes = list(
            filter_interdiff_opcodes(opcodes, orig_diff, new_diff))

        self.assertEqual(new_opcodes, [
            ('filtered-equal', 0, 0, 0, 1),
            ('filtered-equal', 0, 5, 1, 5),
            ('filtered-equal', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 21),
            ('equal', 26, 28, 21, 23),
            ('filtered-equal', 28, 40, 23, 35),
            ('filtered-equal', 40, 40, 35, 45),
        ])
        self._sanity_check_opcodes(new_opcodes)
Exemplo n.º 18
0
    def test_filter_interdiff_opcodes(self):
        """Testing filter_interdiff_opcodes"""
        opcodes = [
            ('insert', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 5),
            ('delete', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 26),
            ('equal', 26, 40, 26, 40),
            ('insert', 40, 40, 40, 45),
        ]

        orig_diff = (
            '--- README\n'
            '+++ README\n'
            '@@ -22,7 +22,7 @@\n'
            ' # line 22\n'
            ' # line 23\n'
            ' # line 24\n'
            '-# line 25\n'
            '+# line 25!\n'
            ' # line 26\n'
            ' # line 27\n'
            ' # line 28\n'
        )

        new_diff = (
            '--- README\n'
            '+++ README\n'
            '@@ -2,11 +2,6 @@\n'
            ' # line 2\n'
            ' # line 3\n'
            ' # line 4\n'
            '-# line 5\n'
            '-# line 6\n'
            '-# line 7\n'
            '-# line 8\n'
            '-# line 9\n'
            ' # line 10\n'
            ' # line 11\n'
            ' # line 12\n'
            '@@ -22,7 +22,7 @@\n'
            ' # line 22\n'
            ' # line 23\n'
            ' # line 24\n'
            '-# line 25\n'
            '+# line 25!\n'
            ' # line 26\n'
            ' # line 27\n'
            ' # line 28\n'
        )

        new_opcodes = list(filter_interdiff_opcodes(opcodes, orig_diff,
                                                    new_diff))

        self.assertEqual(new_opcodes, [
            ('equal', 0, 0, 0, 1),
            ('equal', 0, 5, 1, 5),
            ('delete', 5, 10, 5, 5),
            ('equal', 10, 25, 5, 20),
            ('replace', 25, 26, 20, 26),
            ('equal', 26, 40, 26, 40),
            ('equal', 40, 40, 40, 45),
        ])