How to use the unidiff.patch.Hunk function in unidiff

To help you get started, we’ve selected a few unidiff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github matiasb / python-unidiff / tests / test_hunks.py View on Github external
def test_default_is_valid(self):
        hunk = Hunk()
        self.assertTrue(hunk.is_valid())
github matiasb / python-unidiff / tests / test_hunks.py View on Github external
def test_append_added_line(self):
        hunk = Hunk(src_len=0, tgt_len=1)
        hunk.append(self.added_line)
        self.assertTrue(hunk.is_valid())
        self.assertEqual(len(hunk.target), 1)
        self.assertEqual(hunk.source, [])
        self.assertIn(str(self.added_line), hunk.target)
        target_lines = list(hunk.target_lines())
        self.assertEqual(target_lines, [self.added_line])
github matiasb / python-unidiff / tests / test_hunks.py View on Github external
def test_missing_length(self):
        hunk = Hunk(src_len=None, tgt_len=None)
        hunk.append(self.context_line)
        self.assertTrue(hunk.is_valid())
github matiasb / python-unidiff / tests / test_patchedfile.py View on Github external
def test_is_modified_file(self):
        hunk = Hunk(src_start=1, src_len=10, tgt_start=1, tgt_len=8)
        self.patched_file.append(hunk)
        self.assertTrue(self.patched_file.is_modified_file)
github matiasb / python-unidiff / tests / test_hunks.py View on Github external
def test_missing_data_is_not_valid(self):
        hunk = Hunk(src_len=1, tgt_len=1)
        self.assertFalse(hunk.is_valid())
github matiasb / python-unidiff / tests / test_hunks.py View on Github external
def test_append_deleted_line(self):
        hunk = Hunk(src_len=1, tgt_len=0)
        hunk.append(self.removed_line)
        self.assertTrue(hunk.is_valid())
        self.assertEqual(len(hunk.source), 1)
        self.assertEqual(hunk.target, [])
        self.assertIn(str(self.removed_line), hunk.source)
        source_lines = list(hunk.source_lines())
        self.assertEqual(source_lines, [self.removed_line])
github matiasb / python-unidiff / tests / test_patchedfile.py View on Github external
def test_is_added_file(self):
        hunk = Hunk(src_start=0, src_len=0, tgt_start=1, tgt_len=10)
        self.patched_file.append(hunk)
        self.assertTrue(self.patched_file.is_added_file)
github matiasb / python-unidiff / unidiff / patch.py View on Github external
def _parse_hunk(self, header, diff, encoding):
        """Parse hunk details."""
        header_info = RE_HUNK_HEADER.match(header)
        hunk_info = header_info.groups()
        hunk = Hunk(*hunk_info)

        source_line_no = hunk.source_start
        target_line_no = hunk.target_start
        expected_source_end = source_line_no + hunk.source_length
        expected_target_end = target_line_no + hunk.target_length

        for diff_line_no, line in diff:
            if encoding is not None:
                line = line.decode(encoding)

            valid_line = RE_HUNK_BODY_LINE.match(line)
            if not valid_line:
                valid_line = RE_HUNK_EMPTY_BODY_LINE.match(line)

            if not valid_line:
                raise UnidiffParseError('Hunk diff line expected: %s' % line)
github matiasb / python-unidiff / unidiff / parser.py View on Github external
def _parse_hunk(diff, source_start, source_len, target_start, target_len,
                section_header):
    """Parse a diff hunk details."""
    hunk = Hunk(source_start, source_len, target_start, target_len,
                section_header)
    source_line_no = hunk.source_start
    target_line_no = hunk.target_start

    for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if not valid_line:
            raise UnidiffParseError('Hunk diff line expected: %s' % line)

        line_type = valid_line.group('line_type')
        value = valid_line.group('value')
        original_line = Line(value, line_type=line_type)
        if line_type == LINE_TYPE_ADDED:
            original_line.target_line_no = target_line_no
            target_line_no += 1
        elif line_type == LINE_TYPE_REMOVED: