forked from douban/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pygit2_diff.py
More file actions
86 lines (77 loc) · 2.74 KB
/
test_pygit2_diff.py
File metadata and controls
86 lines (77 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
from vilya.libs import gyt
import shutil
import tempfile
from os.path import join as opj
from tests.base import TestCase
TEST_REPO = '/tmp/test_gyt/repo/.git'
ENV_FOR_GIT = {
'GIT_AUTHOR_NAME': 'default_test',
'GIT_AUTHOR_EMAIL': '[email protected]',
'GIT_COMMITTER_NAME': 'default_test',
'GIT_COMMITTER_EMAIL': '[email protected]',
}
class TestGyt(TestCase):
def _repo(self, bare=False):
if bare:
rep = gyt.repo(self._gd, bare=True, init=True)
else:
rep = gyt.repo(self._gd, self._wt, bare=False, init=True)
return rep
def _add_file(self, rep, filename, content='', commit=True,
commit_msg=None):
f = open(opj(rep.work_tree, filename), 'w')
f.write(content)
f.close()
rep.call(['add', filename])
if commit:
if not commit_msg:
commit_msg = 'unit test commit msg for %s' % filename
rep.call(['commit', '-m', commit_msg], _env=ENV_FOR_GIT)
def setUp(self):
self._gd = tempfile.mkdtemp(prefix='gyt_gd_') # Git dir
self._wt = tempfile.mkdtemp(prefix='gyt_wt_') # Work tree
self._gd2 = tempfile.mkdtemp(prefix='gyt_gd2_') # Git dir
self._wt2 = tempfile.mkdtemp(prefix='gyt_wt2_') # Work tree
def tearDown(self):
shutil.rmtree(self._gd)
shutil.rmtree(self._wt)
shutil.rmtree(self._gd2)
shutil.rmtree(self._wt2)
def test_one_diff(self):
rep = self._repo()
fn1 = 'test1'
content1a = 'a\nb\nc\nd'
content1b = 'a\nb\ncxxx\nd'
self._add_file(rep, fn1, commit_msg='msg1a', content=content1a)
sha = rep.sha()
self._add_file(rep, fn1, commit_msg='msg1b', content=content1b)
d = rep.diff(sha)
assert len(d) == 1
d1 = d[0]
assert d1['filename'] == fn1
assert d1['amode'] == d1['bmode'] == '100644'
assert len(d1['patch']) == 1
assert d1['patch'][0][0] == '@@ -1,4 +1,4 @@'
chunk = d1['patch'][0][1]
assert chunk == [
('idem', u'a'),
('idem', u'b'),
('rem', u'c'),
('add', u'cxxx'),
('idem', u'd'),
('other', u' No newline at end of file')
]
repo = rep.repo
ref_cmt1 = repo.revparse_single(sha)
ref_cmt2 = repo.revparse_single('HEAD')
diff = ref_cmt1.tree.diff(ref_cmt2.tree)
patches, filenames = gyt.parse_raw_diff_patches(diff.patch, True)
assert patches[0][0][1] == [
('idem', u'a'),
('idem', u'b'),
('rem', u'c'),
('add', u'cxxx'),
('idem', u'd'),
('other', u' No newline at end of file')
]