def get_parsed_patch(self, file_id, invert=False): """Return a parsed version of a file's patch. :param file_id: The id of the file to generate a patch for. :param invert: If True, provide an inverted patch (insertions displayed as removals, removals displayed as insertions). :return: A patches.Patch. """ diff_file = StringIO() if invert: old_tree = self.work_tree new_tree = self.target_tree else: old_tree = self.target_tree new_tree = self.work_tree old_path = old_tree.id2path(file_id) new_path = new_tree.id2path(file_id) text_differ = diff.DiffText( old_tree, new_tree, diff_file, path_encoding=osutils.get_terminal_encoding()) patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file') diff_file.seek(0) return patches.parse_patch(diff_file)
def compare_parsed(self, patchtext): lines = patchtext.splitlines(True) patch = parse_patch(lines.__iter__()) pstr = str(patch) i = difference_index(patchtext, pstr) if i is not None: print "%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i]) self.assertEqual(patchtext, str(patch))
def compare_parsed(self, patchtext): lines = patchtext.splitlines(True) patch = parse_patch(lines.__iter__()) pstr = str(patch) i = difference_index(patchtext, pstr) if i is not None: print "%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i]) self.assertEqual (patchtext, str(patch))
def test_iter_patched_from_hunks(self): """Test a few patch files, and make sure they work.""" files = [ ('diff-2', 'orig-2', 'mod-2'), ('diff-3', 'orig-3', 'mod-3'), ('diff-4', 'orig-4', 'mod-4'), ('diff-5', 'orig-5', 'mod-5'), ('diff-6', 'orig-6', 'mod-6'), ('diff-7', 'orig-7', 'mod-7'), ] for diff, orig, mod in files: parsed = parse_patch(self.datafile(diff)) orig_lines = list(self.datafile(orig)) mod_lines = list(self.datafile(mod)) iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks) patched_file = IterableFile(iter_patched) count = 0 for patch_line in patched_file: self.assertEqual(patch_line, mod_lines[count]) count += 1 self.assertEqual(count, len(mod_lines))
def get_parsed_patch(self, file_id, invert=False): """Return a parsed version of a file's patch. :param file_id: The id of the file to generate a patch for. :param invert: If True, provide an inverted patch (insertions displayed as removals, removals displayed as insertions). :return: A patches.Patch. """ diff_file = StringIO() if invert: old_tree = self.work_tree new_tree = self.target_tree else: old_tree = self.target_tree new_tree = self.work_tree old_path = old_tree.id2path(file_id) new_path = new_tree.id2path(file_id) text_differ = diff.DiffText(old_tree, new_tree, diff_file, path_encoding=osutils.get_terminal_encoding()) patch = text_differ.diff(file_id, old_path, new_path, 'file', 'file') diff_file.seek(0) return patches.parse_patch(diff_file)
def testLineLookup(self): import sys """Make sure we can accurately look up mod line from orig""" patch = parse_patch(self.datafile("diff")) orig = list(self.datafile("orig")) mod = list(self.datafile("mod")) removals = [] for i in range(len(orig)): mod_pos = patch.pos_in_mod(i) if mod_pos is None: removals.append(orig[i]) continue self.assertEqual(mod[mod_pos], orig[i]) rem_iter = removals.__iter__() for hunk in patch.hunks: for line in hunk.lines: if isinstance(line, RemoveLine): next = rem_iter.next() if line.contents != next: sys.stdout.write(" orig:%spatch:%s" % (next, line.contents)) self.assertEqual(line.contents, next) self.assertRaises(StopIteration, rem_iter.next)
def testStatsValues(self): """Test the added, removed and hunks values for stats_values.""" patch = parse_patch(self.datafile("diff")) self.assertEqual((299, 407, 48), patch.stats_values())
def testFirstLineRenumber(self): """Make sure we handle lines at the beginning of the hunk""" patch = parse_patch(self.datafile("insert_top.patch")) self.assertEqual(patch.pos_in_mod(0), 1)