Cope with diffstat not being present in test_patch.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_patch.py
1 # test_patch.py -- tests for patch.py
2 # Copryight (C) 2010 Jelmer Vernooij <jelmer@samba.org>
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) a later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # MA  02110-1301, USA.
18
19 """Tests for patch.py."""
20
21 from cStringIO import StringIO
22 from unittest import TestCase
23
24 from dulwich.objects import (
25     Commit,
26     Tree,
27     )
28 from dulwich.patch import (
29     write_commit_patch,
30     )
31
32
33 class WriteCommitPatchTests(TestCase):
34
35     def test_simple(self):
36         f = StringIO()
37         c = Commit()
38         c.committer = c.author = "Jelmer <jelmer@samba.org>"
39         c.commit_time = c.author_time = 1271350201
40         c.commit_timezone = c.author_timezone = 0
41         c.message = "This is the first line\nAnd this is the second line.\n"
42         c.tree = Tree().id
43         write_commit_patch(f, c, "CONTENTS", (1, 1), version="custom")
44         f.seek(0)
45         lines = f.readlines()
46         self.assertTrue(lines[0].startswith("From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
47         self.assertEquals(lines[1], "From: Jelmer <jelmer@samba.org>\n")
48         self.assertTrue(lines[2].startswith("Date: "))
49         self.assertEquals([
50             "Subject: [PATCH 1/1] This is the first line\n",
51             "And this is the second line.\n",
52             "\n",
53             "\n",
54             "---\n"], lines[3:8])
55         self.assertEquals([
56             "\n",
57             "CONTENTS-- \n",
58             "custom\n"], lines[-3:])
59         if len(lines) >= 12:
60             # diffstat may not be present
61             self.assertEquals(lines[8], " 0 files changed\n")