def compare_parsed(self, patchtext): lines = patchtext.splitlines(True) patch = parse_patch(lines.__iter__()) pstr = patch.as_bytes() i = difference_index(patchtext, pstr) if i is not None: print("%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i])) self.assertEqual(patchtext, patch.as_bytes())
def test_apply_add(self): tree = self.make_branch_and_tree('.') self.build_tree_contents([('a', 'a\n')]) tree.add('a') tree.commit('Add a') patch = parse_patch(b"""\ --- /dev/null +++ a/b @@ -0,0 +1 @@ +b """.splitlines(True)) with AppliedPatches(tree, [patch]) as newtree: self.assertEqual(b'b\n', newtree.get_file_text('b'))
def test_apply_delete(self): tree = self.make_branch_and_tree('.') self.build_tree_contents([('a', 'a\n')]) tree.add('a') tree.commit('Add a') patch = parse_patch(b"""\ --- a/a +++ /dev/null @@ -1 +0,0 @@ -a """.splitlines(True)) with AppliedPatches(tree, [patch]) as newtree: self.assertFalse(newtree.has_filename('a'))
def test_apply_add(self): tree = self.make_branch_and_tree(".") self.build_tree_contents([("a", "a\n")]) tree.add("a") tree.commit("Add a") patch = parse_patch( b"""\ --- /dev/null +++ b/b @@ -0,0 +1 @@ +b """.splitlines( True ) ) with AppliedPatches(tree, [patch]) as newtree: self.assertEqual(b"b\n", newtree.get_file_text("b"))
def testLineLookup(self): """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): self.assertEqual(line.contents, next(rem_iter)) self.assertRaises(StopIteration, next, rem_iter)
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 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)