From 2a49eadc57285e6f7d7577c729e41ac987df3128 Mon Sep 17 00:00:00 2001 From: Nico von Geyso <Nico.Geyso@FU-Berlin.de> Date: Thu, 24 May 2012 19:10:00 +0200 Subject: [PATCH] added some tests for diff --- test/__init__.py | 2 +- test/test_diff.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/test_diff.py diff --git a/test/__init__.py b/test/__init__.py index 62823ae..fcb82c5 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -36,7 +36,7 @@ import unittest names = ['blob', 'commit', 'index', 'refs', 'repository', 'revwalk', 'tag', - 'tree', 'signature', 'status', 'treebuilder'] + 'tree', 'signature', 'status', 'treebuilder', 'diff'] def test_suite(): modules = ['test.test_%s' % n for n in names] return unittest.defaultTestLoader.loadTestsFromNames(modules) diff --git a/test/test_diff.py b/test/test_diff.py new file mode 100644 index 0000000..cc7e0c7 --- /dev/null +++ b/test/test_diff.py @@ -0,0 +1,94 @@ +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Nico von Geyso +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License, version 2, +# as published by the Free Software Foundation. +# +# In addition to the permissions in the GNU General Public License, +# the authors give you unlimited permission to link the compiled +# version of this file into combinations with other programs, +# and to distribute those combinations without any restriction +# coming from the use of this file. (The General Public License +# restrictions do apply in other respects; for example, they cover +# modification of the file, and distribution when not linked into +# a combined executable.) +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + +"""Tests for Diff objects.""" + +from __future__ import absolute_import +from __future__ import unicode_literals +import unittest + +import pygit2 +from . import utils + +__author__ = 'Nico.Geyso@FU-Berlin.de (Nico von Geyso)' + + +COMMIT_SHA1_1 = '5fe808e8953c12735680c257f56600cb0de44b10' +COMMIT_SHA1_2 = 'c2792cfa289ae6321ecf2cd5806c2194b0fd070c' +PATCH = b"""diff --git a/a b/a +index 7f129fd..af431f2 100644 +--- a/a ++++ b/a +@@ -1 +1 @@ +-a contents 2 ++a contents +diff --git a/c/d b/c/d +deleted file mode 100644 +index 297efb8..0000000 +--- a/c/d ++++ /dev/null +@@ -1 +0,0 @@ +-c/d contents +""" + + +class DiffTest(utils.BareRepoTestCase): + + def test_diff_invalid(self): + commit_a = self.repo[COMMIT_SHA1_1] + commit_b = self.repo[COMMIT_SHA1_2] + self.assertRaises(TypeError, commit_a.tree.diff, commit_b) + + def test_diff_tree(self): + commit_a = self.repo[COMMIT_SHA1_1] + commit_b = self.repo[COMMIT_SHA1_2] + + diff = commit_a.tree.diff(commit_b.tree) + + self.assertIsNotNone(diff) + self.assertIn(('a','a', 3), diff.changes['files']) + self.assertEqual(2, len(diff.changes['hunks'])) + + hunk = diff.changes['hunks'][0] + self.assertEqual(hunk.old_start, 1) + self.assertEqual(hunk.old_lines, 0) + self.assertEqual(hunk.new_start, 1) + self.assertEqual(hunk.new_lines, 0) + + self.assertEqual(hunk.old_data, b'a contents 2\n') + self.assertEqual(hunk.new_data, b'a contents\n') + + def test_diff_patch(self): + commit_a = self.repo[COMMIT_SHA1_1] + commit_b = self.repo[COMMIT_SHA1_2] + + diff = commit_a.tree.diff(commit_b.tree) + self.assertEqual(diff.patch, PATCH) + + +if __name__ == '__main__': + unittest.main()