test_cdiff.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """Unit test for cdiff"""
  4. import sys
  5. if sys.hexversion < 0x02050000:
  6. raise SystemExit("*** Requires python >= 2.5.0")
  7. import unittest
  8. import tempfile
  9. import subprocess
  10. import os
  11. sys.path.insert(0, '')
  12. import cdiff
  13. class Sequential(object):
  14. """A non-seekable iterator, mock of file object"""
  15. def __init__(self, items):
  16. self._items = items
  17. self._index = 0
  18. def __iter__(self):
  19. while True:
  20. try:
  21. item = self._items[self._index]
  22. except IndexError:
  23. raise StopIteration
  24. yield item
  25. self._index += 1
  26. def readline(self):
  27. try:
  28. item = self._items[self._index]
  29. except IndexError:
  30. return ''
  31. self._index += 1
  32. return item
  33. class TestPatchStream(unittest.TestCase):
  34. def test_is_empty(self):
  35. stream = cdiff.PatchStream(Sequential([]))
  36. self.assertTrue(stream.is_empty())
  37. stream = cdiff.PatchStream(Sequential(['hello', 'world']))
  38. self.assertFalse(stream.is_empty())
  39. def test_read_stream_header(self):
  40. stream = cdiff.PatchStream(Sequential([]))
  41. self.assertEqual(stream.read_stream_header(1), [])
  42. items = ['hello', 'world', 'again']
  43. stream = cdiff.PatchStream(Sequential(items))
  44. self.assertEqual(stream.read_stream_header(2), items[:2])
  45. stream = cdiff.PatchStream(Sequential(items))
  46. self.assertEqual(stream.read_stream_header(4), items[:])
  47. def test_iter_after_read_stream_header(self):
  48. items = ['hello', 'world', 'again', 'and', 'again']
  49. stream = cdiff.PatchStream(Sequential(items))
  50. _ = stream.read_stream_header(2)
  51. out = list(stream)
  52. self.assertEqual(out, items)
  53. class TestHunk(unittest.TestCase):
  54. def test_get_old_text(self):
  55. hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@', (1,2), (1,2))
  56. hunk.append(('-', 'foo\n'))
  57. hunk.append(('+', 'bar\n'))
  58. hunk.append((' ', 'common\n'))
  59. self.assertEqual(hunk._get_old_text(), ['foo\n', 'common\n'])
  60. def test_get_new_text(self):
  61. hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@', (1,2), (1,2))
  62. hunk.append(('-', 'foo\n'))
  63. hunk.append(('+', 'bar\n'))
  64. hunk.append((' ', 'common\n'))
  65. self.assertEqual(hunk._get_new_text(), ['bar\n', 'common\n'])
  66. class TestDiff(unittest.TestCase):
  67. def _init_diff(self):
  68. hunk = cdiff.Hunk(['hunk header\n'], '@@ -1,2 +1,2 @@\n', (1,2), (1,2))
  69. hunk.append(('-', 'hella\n'))
  70. hunk.append(('+', 'hello\n'))
  71. hunk.append((' ', 'world\n'))
  72. diff = cdiff.Diff(['header\n'], '--- old\n', '+++ new\n', [hunk])
  73. return diff
  74. def test_markup_mix(self):
  75. line = 'foo \x00-del\x01 \x00+add\x01 \x00^chg\x01 bar'
  76. base_color = 'red'
  77. diff = cdiff.Diff(None, None, None, None)
  78. self.assertEqual(diff._markup_mix(line, base_color),
  79. '\x1b[31mfoo \x1b[7m\x1b[31mdel\x1b[0m\x1b[31m '
  80. '\x1b[7m\x1b[31madd\x1b[0m\x1b[31m '
  81. '\x1b[4m\x1b[31mchg\x1b[0m\x1b[31m bar\x1b[0m')
  82. def test_markup_traditional(self):
  83. diff = self._init_diff()
  84. out = list(diff.markup_traditional())
  85. self.assertEqual(len(out), 8)
  86. sys.stdout.write('\n')
  87. for markup in out:
  88. sys.stdout.write(markup)
  89. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  90. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  91. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  92. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  93. self.assertEqual(out[4], '\x1b[1;34m@@ -1,2 +1,2 @@\n\x1b[0m')
  94. self.assertEqual(out[5],
  95. '\x1b[1;31m-\x1b[0m\x1b[31mhell\x1b[4m'
  96. '\x1b[31ma\x1b[0m\x1b[31m\n\x1b[0m')
  97. self.assertEqual(out[6],
  98. '\x1b[1;32m+\x1b[0m\x1b[32mhell\x1b[4m'
  99. '\x1b[32mo\x1b[0m\x1b[32m\n\x1b[0m')
  100. self.assertEqual(out[7], '\x1b[0m world\n\x1b[0m')
  101. def test_markup_side_by_side_padded(self):
  102. diff = self._init_diff()
  103. out = list(diff.markup_side_by_side(6))
  104. self.assertEqual(len(out), 7)
  105. sys.stdout.write('\n')
  106. for markup in out:
  107. sys.stdout.write(markup)
  108. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  109. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  110. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  111. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  112. self.assertEqual(out[4], '\x1b[1;34m@@ -1,2 +1,2 @@\n\x1b[0m')
  113. self.assertEqual(out[5],
  114. '\x1b[33m1\x1b[0m '
  115. '\x1b[31mhell\x1b[4m\x1b[31ma\x1b[0m\x1b[31m\x1b[0m '
  116. '\x1b[0m\x1b[33m1\x1b[0m '
  117. '\x1b[32mhell\x1b[4m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
  118. self.assertEqual(out[6],
  119. '\x1b[33m2\x1b[0m '
  120. '\x1b[0mworld\x1b[0m '
  121. '\x1b[0m\x1b[33m2\x1b[0m '
  122. '\x1b[0mworld\x1b[0m\n')
  123. def test_markup_side_by_side_off_by_one(self):
  124. diff = self._init_diff()
  125. out = list(diff.markup_side_by_side(5))
  126. self.assertEqual(len(out), 7)
  127. sys.stdout.write('\n')
  128. for markup in out:
  129. sys.stdout.write(markup)
  130. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  131. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  132. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  133. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  134. self.assertEqual(out[4], '\x1b[1;34m@@ -1,2 +1,2 @@\n\x1b[0m')
  135. self.assertEqual(out[5],
  136. '\x1b[33m1\x1b[0m '
  137. '\x1b[31mhell\x1b[4m\x1b[31ma\x1b[0m '
  138. '\x1b[0m\x1b[33m1\x1b[0m '
  139. '\x1b[32mhell\x1b[4m\x1b[32mo\x1b[0m\n')
  140. self.assertEqual(out[6],
  141. '\x1b[33m2\x1b[0m '
  142. '\x1b[0mworld\x1b[0m '
  143. '\x1b[0m\x1b[33m2\x1b[0m '
  144. '\x1b[0mworld\x1b[0m\n')
  145. def test_markup_side_by_side_wrapped(self):
  146. diff = self._init_diff()
  147. out = list(diff.markup_side_by_side(4))
  148. self.assertEqual(len(out), 7)
  149. sys.stdout.write('\n')
  150. for markup in out:
  151. sys.stdout.write(markup)
  152. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  153. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  154. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  155. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  156. self.assertEqual(out[4], '\x1b[1;34m@@ -1,2 +1,2 @@\n\x1b[0m')
  157. self.assertEqual(out[5],
  158. '\x1b[33m1\x1b[0m '
  159. '\x1b[31mhel\x1b[0m\x1b[1;35m>\x1b[0m '
  160. '\x1b[0m\x1b[33m1\x1b[0m '
  161. '\x1b[32mhel\x1b[0m\x1b[1;35m>\x1b[0m\n')
  162. self.assertEqual(out[6],
  163. '\x1b[33m2\x1b[0m '
  164. '\x1b[0mwor\x1b[0m\x1b[1;35m>\x1b[0m '
  165. '\x1b[0m\x1b[33m2\x1b[0m '
  166. '\x1b[0mwor\x1b[0m\x1b[1;35m>\x1b[0m\n')
  167. class TestUdiff(unittest.TestCase):
  168. diff = cdiff.Udiff(None, None, None, None)
  169. def test_is_hunk_meta_normal(self):
  170. self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
  171. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@'))
  172. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo'))
  173. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\n'))
  174. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\r\n'))
  175. def test_is_hunk_meta_svn_prop(self):
  176. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##'))
  177. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\n'))
  178. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\r\n'))
  179. def test_is_hunk_meta_neg(self):
  180. self.assertFalse(self.diff.is_hunk_meta('@@ -1 + @@'))
  181. self.assertFalse(self.diff.is_hunk_meta('@@ -this is not a hunk meta'))
  182. self.assertFalse(self.diff.is_hunk_meta('## -this is not either'))
  183. def test_parse_hunk_meta_normal(self):
  184. self.assertEqual(
  185. self.diff.parse_hunk_meta('@@ -3,7 +3,6 @@'),
  186. ((3, 7), (3, 6)))
  187. def test_parse_hunk_meta_missing(self):
  188. self.assertEqual(
  189. self.diff.parse_hunk_meta('@@ -3 +3,6 @@'),
  190. ((3, 0), (3, 6)))
  191. self.assertEqual(
  192. self.diff.parse_hunk_meta('@@ -3,7 +3 @@'),
  193. ((3, 7), (3, 0)))
  194. self.assertEqual(
  195. self.diff.parse_hunk_meta('@@ -3 +3 @@'),
  196. ((3, 0), (3, 0)))
  197. def test_parse_hunk_meta_svn_prop(self):
  198. self.assertEqual(
  199. self.diff.parse_hunk_meta('## -0,0 +1 ##'),
  200. ((0, 0), (1, 0)))
  201. def test_is_old(self):
  202. self.assertTrue(self.diff.is_old('-hello world'))
  203. self.assertTrue(self.diff.is_old('----')) # yaml
  204. def test_is_old_neg(self):
  205. self.assertFalse(self.diff.is_old('--- considered as old path'))
  206. self.assertFalse(self.diff.is_old('-------------')) # svn log --diff
  207. def test_is_new(self):
  208. self.assertTrue(self.diff.is_new('+hello world'))
  209. self.assertTrue(self.diff.is_new('++++hello world'))
  210. def test_is_new_neg(self):
  211. self.assertFalse(self.diff.is_new('+++ considered as new path'))
  212. class TestDiffParser(unittest.TestCase):
  213. def test_type_detect(self):
  214. patch = r"""\
  215. spam
  216. --- a
  217. +++ b
  218. @@ -1,2 +1,2 @@
  219. """
  220. items = patch.splitlines(True)
  221. stream = cdiff.PatchStream(Sequential(items))
  222. parser = cdiff.DiffParser(stream)
  223. self.assertEqual(parser._type, 'udiff')
  224. def test_type_detect_neg(self):
  225. patch = r"""\
  226. spam
  227. --- a
  228. +++ b
  229. spam
  230. """
  231. items = patch.splitlines(True)
  232. stream = cdiff.PatchStream(Sequential(items))
  233. self.assertRaises(RuntimeError, cdiff.DiffParser, stream)
  234. def test_parse_dangling_header(self):
  235. patch = r"""\
  236. --- a
  237. +++ b
  238. @@ -1,2 +1,2 @@
  239. -foo
  240. +bar
  241. common
  242. spam
  243. """
  244. items = patch.splitlines(True)
  245. stream = cdiff.PatchStream(Sequential(items))
  246. parser = cdiff.DiffParser(stream)
  247. self.assertRaises(RuntimeError, list, parser._parse())
  248. def test_parse_missing_new_path(self):
  249. patch = r"""\
  250. --- a
  251. +++ b
  252. @@ -1,2 +1,2 @@
  253. -foo
  254. +bar
  255. common
  256. --- c
  257. """
  258. items = patch.splitlines(True)
  259. stream = cdiff.PatchStream(Sequential(items))
  260. parser = cdiff.DiffParser(stream)
  261. self.assertRaises(AssertionError, list, parser._parse())
  262. def test_parse_missing_hunk(self):
  263. patch = r"""\
  264. --- a
  265. +++ b
  266. @@ -1,2 +1,2 @@
  267. -foo
  268. +bar
  269. common
  270. --- c
  271. +++ d
  272. """
  273. items = patch.splitlines(True)
  274. stream = cdiff.PatchStream(Sequential(items))
  275. parser = cdiff.DiffParser(stream)
  276. self.assertRaises(AssertionError, list, parser._parse())
  277. def test_parse_svn_prop(self):
  278. patch = r"""\
  279. --- a
  280. +++ b
  281. Added: svn:executable
  282. ## -0,0 +1 ##
  283. +*
  284. \ No newline at end of property
  285. Added: svn:keywords
  286. ## -0,0 +1 ##
  287. +Id
  288. """
  289. items = patch.splitlines(True)
  290. stream = cdiff.PatchStream(Sequential(items))
  291. parser = cdiff.DiffParser(stream)
  292. out = list(parser._parse())
  293. self.assertEqual(len(out), 1)
  294. self.assertEqual(len(out[0]._hunks), 2)
  295. hunk = out[0]._hunks[1]
  296. self.assertEqual(hunk._hunk_headers, ['Added: svn:keywords\n'])
  297. self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
  298. class TestMain(unittest.TestCase):
  299. def setUp(self):
  300. self._cwd = os.getcwd()
  301. self._ws = tempfile.mkdtemp(prefix='test_cdiff')
  302. self._non_ws = tempfile.mkdtemp(prefix='test_cdiff')
  303. cmd = ('cd %s; git init; git config user.name me; '
  304. 'git config user.email me@example.org') % self._ws
  305. subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
  306. self._change_file('init')
  307. def tearDown(self):
  308. os.chdir(self._cwd)
  309. cmd = ['/bin/rm', '-rf', self._ws, self._non_ws]
  310. subprocess.call(cmd)
  311. def _change_file(self, text):
  312. cmd = ['/bin/sh', '-c',
  313. 'cd %s; echo "%s" > foo' % (self._ws, text)]
  314. subprocess.call(cmd)
  315. def _commit_file(self):
  316. cmd = ['/bin/sh', '-c',
  317. 'cd %s; git add foo; git commit foo -m update' % self._ws]
  318. subprocess.call(cmd, stdout=subprocess.PIPE)
  319. def test_too_many_args(self):
  320. sys.argv = [sys.argv[0], 'a', 'b', 'c']
  321. self.assertNotEqual(cdiff.main(), 0)
  322. def test_read_diff(self):
  323. sys.argv = sys.argv[:1]
  324. self._change_file('read_diff')
  325. os.chdir(self._ws)
  326. #ret = cdiff.main()
  327. ret = 0
  328. os.chdir(self._cwd)
  329. self.assertEqual(ret, 0)
  330. def test_read_diff_neg(self):
  331. sys.argv = sys.argv[:1]
  332. os.chdir(self._non_ws)
  333. ret = cdiff.main()
  334. os.chdir(self._cwd)
  335. self.assertNotEqual(ret, 0)
  336. def test_read_log(self):
  337. sys.argv = [sys.argv[0], '--log']
  338. self._change_file('read_log')
  339. self._commit_file()
  340. os.chdir(self._ws)
  341. ret = cdiff.main()
  342. os.chdir(self._cwd)
  343. self.assertEqual(ret, 0)
  344. def test_read_log_neg(self):
  345. sys.argv = [sys.argv[0], '--log']
  346. os.chdir(self._non_ws)
  347. ret = cdiff.main()
  348. os.chdir(self._cwd)
  349. self.assertNotEqual(ret, 0)
  350. if __name__ == '__main__':
  351. unittest.main()
  352. # vim:set et sts=4 sw=4 tw=80: