test_cdiff.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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_meta(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_missing_hunk_list(self):
  278. patch = r"""\
  279. --- a
  280. +++ b
  281. @@ -1,2 +1,2 @@
  282. -foo
  283. +bar
  284. common
  285. --- c
  286. +++ d
  287. @@ -1,2 +1,2 @@
  288. """
  289. items = patch.splitlines(True)
  290. stream = cdiff.PatchStream(Sequential(items))
  291. parser = cdiff.DiffParser(stream)
  292. self.assertRaises(AssertionError, list, parser._parse())
  293. def test_parse_only_in_dir(self):
  294. patch = r"""\
  295. --- a
  296. +++ b
  297. @@ -1,2 +1,2 @@
  298. -foo
  299. +bar
  300. common
  301. Only in foo: foo
  302. --- c
  303. +++ d
  304. @@ -1,2 +1,2 @@
  305. -foo
  306. +bar
  307. common
  308. """
  309. items = patch.splitlines(True)
  310. stream = cdiff.PatchStream(Sequential(items))
  311. parser = cdiff.DiffParser(stream)
  312. out = list(parser._parse())
  313. self.assertEqual(len(out), 3)
  314. self.assertEqual(len(out[1]._hunks), 0)
  315. self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
  316. self.assertEqual(len(out[2]._hunks), 1)
  317. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  318. def test_parse_only_in_dir_at_last(self):
  319. patch = r"""\
  320. --- a
  321. +++ b
  322. @@ -1,2 +1,2 @@
  323. -foo
  324. +bar
  325. common
  326. Only in foo: foo
  327. """
  328. items = patch.splitlines(True)
  329. stream = cdiff.PatchStream(Sequential(items))
  330. parser = cdiff.DiffParser(stream)
  331. out = list(parser._parse())
  332. self.assertEqual(len(out), 2)
  333. self.assertEqual(len(out[1]._hunks), 0)
  334. self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
  335. def test_parse_binary_differ_diff_ru(self):
  336. patch = r"""\
  337. --- a
  338. +++ b
  339. @@ -1,2 +1,2 @@
  340. -foo
  341. +bar
  342. common
  343. Binary files a/1.pdf and b/1.pdf differ
  344. --- c
  345. +++ d
  346. @@ -1,2 +1,2 @@
  347. -foo
  348. +bar
  349. common
  350. """
  351. items = patch.splitlines(True)
  352. stream = cdiff.PatchStream(Sequential(items))
  353. parser = cdiff.DiffParser(stream)
  354. out = list(parser._parse())
  355. self.assertEqual(len(out), 3)
  356. self.assertEqual(len(out[1]._hunks), 0)
  357. self.assertEqual(out[1]._old_path, '')
  358. self.assertEqual(out[1]._new_path, '')
  359. self.assertEqual(len(out[1]._headers), 1)
  360. self.assertTrue(out[1]._headers[0].startswith('Binary files'))
  361. self.assertEqual(len(out[2]._hunks), 1)
  362. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  363. def test_parse_binary_differ_git(self):
  364. patch = r"""\
  365. diff --git a/foo b/foo
  366. index 529d8a3..ad71911 100755
  367. --- a/foo
  368. +++ b/foo
  369. @@ -1,2 +1,2 @@
  370. -foo
  371. +bar
  372. common
  373. diff --git a/example.pdf b/example.pdf
  374. index 1eacfd8..3696851 100644
  375. Binary files a/example.pdf and b/example.pdf differ
  376. diff --git a/bar b/bar
  377. index 529e8a3..ad71921 100755
  378. --- a/bar
  379. +++ b/bar
  380. @@ -1,2 +1,2 @@
  381. -foo
  382. +bar
  383. common
  384. """
  385. items = patch.splitlines(True)
  386. stream = cdiff.PatchStream(Sequential(items))
  387. parser = cdiff.DiffParser(stream)
  388. out = list(parser._parse())
  389. self.assertEqual(len(out), 3)
  390. self.assertEqual(len(out[1]._hunks), 0)
  391. self.assertEqual(out[1]._old_path, '')
  392. self.assertEqual(out[1]._new_path, '')
  393. self.assertEqual(len(out[1]._headers), 3)
  394. self.assertTrue(out[1]._headers[2].startswith('Binary files'))
  395. self.assertEqual(len(out[2]._hunks), 1)
  396. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  397. def test_parse_svn_prop(self):
  398. patch = r"""\
  399. --- a
  400. +++ b
  401. Added: svn:executable
  402. ## -0,0 +1 ##
  403. +*
  404. \ No newline at end of property
  405. Added: svn:keywords
  406. ## -0,0 +1 ##
  407. +Id
  408. """
  409. items = patch.splitlines(True)
  410. stream = cdiff.PatchStream(Sequential(items))
  411. parser = cdiff.DiffParser(stream)
  412. out = list(parser._parse())
  413. self.assertEqual(len(out), 1)
  414. self.assertEqual(len(out[0]._hunks), 2)
  415. hunk = out[0]._hunks[1]
  416. self.assertEqual(hunk._hunk_headers, ['Added: svn:keywords\n'])
  417. self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
  418. class TestMain(unittest.TestCase):
  419. def setUp(self):
  420. self._cwd = os.getcwd()
  421. self._ws = tempfile.mkdtemp(prefix='test_cdiff')
  422. self._non_ws = tempfile.mkdtemp(prefix='test_cdiff')
  423. cmd = ('cd %s; git init; git config user.name me; '
  424. 'git config user.email me@example.org') % self._ws
  425. subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
  426. self._change_file('init')
  427. def tearDown(self):
  428. os.chdir(self._cwd)
  429. cmd = ['/bin/rm', '-rf', self._ws, self._non_ws]
  430. subprocess.call(cmd)
  431. def _change_file(self, text):
  432. cmd = ['/bin/sh', '-c',
  433. 'cd %s; echo "%s" > foo' % (self._ws, text)]
  434. subprocess.call(cmd)
  435. def _commit_file(self):
  436. cmd = ['/bin/sh', '-c',
  437. 'cd %s; git add foo; git commit foo -m update' % self._ws]
  438. subprocess.call(cmd, stdout=subprocess.PIPE)
  439. def test_read_diff(self):
  440. sys.argv = sys.argv[:1]
  441. self._change_file('read_diff')
  442. os.chdir(self._ws)
  443. ret = cdiff.main()
  444. os.chdir(self._cwd)
  445. self.assertEqual(ret, 0)
  446. def test_read_diff_neg(self):
  447. sys.argv = sys.argv[:1]
  448. os.chdir(self._non_ws)
  449. ret = cdiff.main()
  450. os.chdir(self._cwd)
  451. self.assertNotEqual(ret, 0)
  452. def test_read_log(self):
  453. sys.argv = [sys.argv[0], '--log']
  454. self._change_file('read_log')
  455. self._commit_file()
  456. os.chdir(self._ws)
  457. ret = cdiff.main()
  458. os.chdir(self._cwd)
  459. self.assertEqual(ret, 0)
  460. def test_read_log_neg(self):
  461. sys.argv = [sys.argv[0], '--log']
  462. os.chdir(self._non_ws)
  463. ret = cdiff.main()
  464. os.chdir(self._cwd)
  465. self.assertNotEqual(ret, 0)
  466. if __name__ == '__main__':
  467. unittest.main()
  468. # vim:set et sts=4 sw=4 tw=80: