test_cdiff.py 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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") # pragma: no cover
  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 PatchStreamTest(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 HunkTest(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 DiffMarkupTest(unittest.TestCase):
  67. def _init_diff(self):
  68. """Return a minimal diff contains all required samples
  69. header
  70. --- old
  71. +++ new
  72. hunk header
  73. @@ -1,4 +1,4 @@
  74. -hhello
  75. +helloo
  76. +spammm
  77. world
  78. -garb
  79. -Again
  80. +again
  81. """
  82. hunk = cdiff.Hunk(['hunk header\n'], '@@ -1,4 +1,4 @@\n',
  83. (1, 4), (1, 4))
  84. hunk.append(('-', 'hhello\n'))
  85. hunk.append(('+', 'helloo\n'))
  86. hunk.append(('+', 'spammm\n'))
  87. hunk.append((' ', 'world\n'))
  88. hunk.append(('-', 'garb\n'))
  89. hunk.append(('-', 'Again\n'))
  90. hunk.append(('+', 'again\n'))
  91. diff = cdiff.UnifiedDiff(['header\n'], '--- old\n', '+++ new\n', [hunk])
  92. return diff
  93. def test_markup_mix(self):
  94. marker = cdiff.DiffMarker()
  95. line = 'foo \x00-del\x01 \x00+add\x01 \x00^chg\x01 bar'
  96. base_color = 'red'
  97. self.assertEqual(
  98. marker._markup_mix(line, base_color),
  99. '\x1b[31mfoo \x1b[7m\x1b[31mdel\x1b[0m\x1b[31m '
  100. '\x1b[7m\x1b[31madd\x1b[0m\x1b[31m '
  101. '\x1b[4m\x1b[31mchg\x1b[0m\x1b[31m bar\x1b[0m')
  102. def test_markup_traditional_hunk_header(self):
  103. hunk = cdiff.Hunk(['hunk header\n'], '@@ -0 +0 @@\n', (0, 0), (0, 0))
  104. diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
  105. marker = cdiff.DiffMarker()
  106. out = list(marker._markup_traditional(diff))
  107. self.assertEqual(len(out), 4)
  108. self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
  109. self.assertEqual(out[1], '\x1b[33m+++ new\n\x1b[0m')
  110. self.assertEqual(out[2], '\x1b[1;36mhunk header\n\x1b[0m')
  111. self.assertEqual(out[3], '\x1b[1;34m@@ -0 +0 @@\n\x1b[0m')
  112. def test_markup_traditional_old_changed(self):
  113. hunk = cdiff.Hunk([], '@@ -1 +0,0 @@\n', (1, 0), (0, 0))
  114. hunk.append(('-', 'spam\n'))
  115. diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
  116. marker = cdiff.DiffMarker()
  117. out = list(marker._markup_traditional(diff))
  118. self.assertEqual(len(out), 4)
  119. self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
  120. self.assertEqual(out[1], '\x1b[33m+++ new\n\x1b[0m')
  121. self.assertEqual(out[2], '\x1b[1;34m@@ -1 +0,0 @@\n\x1b[0m')
  122. self.assertEqual(out[3], '\x1b[1;31m-spam\n\x1b[0m')
  123. def test_markup_traditional_new_changed(self):
  124. hunk = cdiff.Hunk([], '@@ -0,0 +1 @@\n', (0, 0), (1, 0))
  125. hunk.append(('+', 'spam\n'))
  126. diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
  127. marker = cdiff.DiffMarker()
  128. out = list(marker._markup_traditional(diff))
  129. self.assertEqual(len(out), 4)
  130. self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
  131. self.assertEqual(out[1], '\x1b[33m+++ new\n\x1b[0m')
  132. self.assertEqual(out[2], '\x1b[1;34m@@ -0,0 +1 @@\n\x1b[0m')
  133. self.assertEqual(out[3], '\x1b[1;32m+spam\n\x1b[0m')
  134. def test_markup_traditional_both_changed(self):
  135. hunk = cdiff.Hunk([], '@@ -1,2 +1,2 @@\n', (1, 2), (1, 2))
  136. hunk.append(('-', 'hella\n'))
  137. hunk.append(('+', 'hello\n'))
  138. hunk.append((' ', 'common\n'))
  139. diff = cdiff.UnifiedDiff([], '--- old\n', '+++ new\n', [hunk])
  140. marker = cdiff.DiffMarker()
  141. out = list(marker._markup_traditional(diff))
  142. self.assertEqual(len(out), 6)
  143. self.assertEqual(out[0], '\x1b[33m--- old\n\x1b[0m')
  144. self.assertEqual(out[1], '\x1b[33m+++ new\n\x1b[0m')
  145. self.assertEqual(out[2], '\x1b[1;34m@@ -1,2 +1,2 @@\n\x1b[0m')
  146. self.assertEqual(
  147. out[3],
  148. '\x1b[1;31m-\x1b[0m\x1b[31mhell'
  149. '\x1b[4m\x1b[31ma\x1b[0m\x1b[31m\n\x1b[0m')
  150. self.assertEqual(
  151. out[4],
  152. '\x1b[1;32m+\x1b[0m\x1b[32mhell'
  153. '\x1b[4m\x1b[32mo\x1b[0m\x1b[32m\n\x1b[0m')
  154. self.assertEqual(out[5], '\x1b[0m common\n\x1b[0m')
  155. def test_markup_side_by_side_padded(self):
  156. diff = self._init_diff()
  157. marker = cdiff.DiffMarker()
  158. out = list(marker._markup_side_by_side(diff, 7))
  159. self.assertEqual(len(out), 10)
  160. sys.stdout.write('\n')
  161. for markup in out:
  162. sys.stdout.write(markup)
  163. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  164. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  165. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  166. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  167. self.assertEqual(out[4], '\x1b[1;34m@@ -1,4 +1,4 @@\n\x1b[0m')
  168. self.assertEqual(
  169. out[5],
  170. '\x1b[33m1\x1b[0m '
  171. '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m '
  172. '\x1b[0m\x1b[33m1\x1b[0m '
  173. '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
  174. self.assertEqual(
  175. out[6],
  176. '\x1b[33m '
  177. '\x1b[0m '
  178. '\x1b[0m\x1b[33m2\x1b[0m '
  179. '\x1b[1;32mspammm\x1b[0m\n')
  180. self.assertEqual(
  181. out[7],
  182. '\x1b[33m2\x1b[0m '
  183. '\x1b[0mworld\x1b[0m '
  184. '\x1b[0m\x1b[33m3\x1b[0m '
  185. '\x1b[0mworld\x1b[0m\n')
  186. self.assertEqual(
  187. out[8],
  188. '\x1b[33m3\x1b[0m '
  189. '\x1b[1;31mgarb\x1b[0m '
  190. '\x1b[0m\x1b[33m '
  191. '\x1b[0m \n')
  192. self.assertEqual(
  193. out[9],
  194. '\x1b[33m4\x1b[0m '
  195. '\x1b[31m\x1b[4m\x1b[31mA\x1b[0m\x1b[31mgain\x1b[0m '
  196. '\x1b[0m\x1b[33m4\x1b[0m '
  197. '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
  198. def test_markup_side_by_side_neg_width(self):
  199. diff = self._init_diff()
  200. marker = cdiff.DiffMarker()
  201. out = list(marker._markup_side_by_side(diff, -1))
  202. self.assertEqual(len(out), 10)
  203. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  204. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  205. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  206. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  207. self.assertEqual(out[4], '\x1b[1;34m@@ -1,4 +1,4 @@\n\x1b[0m')
  208. self.assertEqual(
  209. out[5],
  210. '\x1b[33m1\x1b[0m '
  211. '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m ' +
  212. (' ' * 74) +
  213. '\x1b[0m\x1b[33m1\x1b[0m '
  214. '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\x1b[32m\x1b[0m\n')
  215. self.assertEqual(
  216. out[6],
  217. '\x1b[33m '
  218. '\x1b[0m ' + (' ' * 80) +
  219. '\x1b[0m\x1b[33m2\x1b[0m '
  220. '\x1b[1;32mspammm\x1b[0m\n')
  221. self.assertEqual(
  222. out[7],
  223. '\x1b[33m2\x1b[0m '
  224. '\x1b[0mworld\x1b[0m ' + (' ' * 75) +
  225. '\x1b[0m\x1b[33m3\x1b[0m '
  226. '\x1b[0mworld\x1b[0m\n')
  227. self.assertEqual(
  228. out[8],
  229. '\x1b[33m3\x1b[0m '
  230. '\x1b[1;31mgarb\x1b[0m '
  231. '\x1b[0m\x1b[33m '
  232. '\x1b[0m \n')
  233. self.assertEqual(
  234. out[9],
  235. '\x1b[33m4\x1b[0m '
  236. '\x1b[31m\x1b[4m\x1b[31mA\x1b[0m\x1b[31mgain\x1b[0m ' +
  237. (' ' * 75) +
  238. '\x1b[0m\x1b[33m4\x1b[0m '
  239. '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
  240. def test_markup_side_by_side_off_by_one(self):
  241. diff = self._init_diff()
  242. marker = cdiff.DiffMarker()
  243. out = list(marker._markup_side_by_side(diff, 6))
  244. self.assertEqual(len(out), 10)
  245. sys.stdout.write('\n')
  246. for markup in out:
  247. sys.stdout.write(markup)
  248. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  249. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  250. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  251. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  252. self.assertEqual(out[4], '\x1b[1;34m@@ -1,4 +1,4 @@\n\x1b[0m')
  253. self.assertEqual(
  254. out[5],
  255. '\x1b[33m1\x1b[0m '
  256. '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhello\x1b[0m '
  257. '\x1b[0m\x1b[33m1\x1b[0m '
  258. '\x1b[32mhello\x1b[7m\x1b[32mo\x1b[0m\n')
  259. self.assertEqual(
  260. out[6],
  261. '\x1b[33m \x1b[0m '
  262. '\x1b[0m\x1b[33m2\x1b[0m '
  263. '\x1b[1;32mspammm\x1b[0m\n')
  264. self.assertEqual(
  265. out[7],
  266. '\x1b[33m2\x1b[0m '
  267. '\x1b[0mworld\x1b[0m '
  268. '\x1b[0m\x1b[33m3\x1b[0m '
  269. '\x1b[0mworld\x1b[0m\n')
  270. self.assertEqual(
  271. out[8],
  272. '\x1b[33m3\x1b[0m '
  273. '\x1b[1;31mgarb\x1b[0m '
  274. '\x1b[0m\x1b[33m '
  275. '\x1b[0m \n')
  276. self.assertEqual(
  277. out[9],
  278. '\x1b[33m4\x1b[0m '
  279. '\x1b[31m\x1b[4m\x1b[31mA\x1b[0m\x1b[31mgain\x1b[0m '
  280. '\x1b[0m\x1b[33m4\x1b[0m '
  281. '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
  282. def test_markup_side_by_side_wrapped(self):
  283. diff = self._init_diff()
  284. marker = cdiff.DiffMarker()
  285. out = list(marker._markup_side_by_side(diff, 5))
  286. self.assertEqual(len(out), 10)
  287. sys.stdout.write('\n')
  288. for markup in out:
  289. sys.stdout.write(markup)
  290. self.assertEqual(out[0], '\x1b[36mheader\n\x1b[0m')
  291. self.assertEqual(out[1], '\x1b[33m--- old\n\x1b[0m')
  292. self.assertEqual(out[2], '\x1b[33m+++ new\n\x1b[0m')
  293. self.assertEqual(out[3], '\x1b[1;36mhunk header\n\x1b[0m')
  294. self.assertEqual(out[4], '\x1b[1;34m@@ -1,4 +1,4 @@\n\x1b[0m')
  295. self.assertEqual(
  296. out[5],
  297. '\x1b[33m1\x1b[0m '
  298. '\x1b[31m\x1b[7m\x1b[31mh\x1b[0m\x1b[31mhel\x1b[0m\x1b[1;35m>\x1b[0m '
  299. '\x1b[0m\x1b[33m1\x1b[0m '
  300. '\x1b[32mhell\x1b[0m\x1b[1;35m>\x1b[0m\n')
  301. self.assertEqual(
  302. out[6],
  303. '\x1b[33m \x1b[0m '
  304. '\x1b[0m\x1b[33m2\x1b[0m '
  305. ''
  306. '\x1b[1;32mspam\x1b[0m\x1b[1;35m>\x1b[0m\n')
  307. self.assertEqual(
  308. out[7],
  309. '\x1b[33m2\x1b[0m '
  310. '\x1b[0mworld\x1b[0m '
  311. '\x1b[0m\x1b[33m3\x1b[0m '
  312. '\x1b[0mworld\x1b[0m\n')
  313. self.assertEqual(
  314. out[8],
  315. '\x1b[33m3\x1b[0m '
  316. '\x1b[1;31mgarb\x1b[0m '
  317. '\x1b[0m\x1b[33m '
  318. '\x1b[0m \n')
  319. self.assertEqual(
  320. out[9],
  321. '\x1b[33m4\x1b[0m '
  322. '\x1b[31m\x1b[4m\x1b[31mA\x1b[0m\x1b[31mgain\x1b[0m '
  323. '\x1b[0m\x1b[33m4\x1b[0m '
  324. '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
  325. class UnifiedDiffTest(unittest.TestCase):
  326. diff = cdiff.UnifiedDiff(None, None, None, None)
  327. def test_is_hunk_meta_normal(self):
  328. self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
  329. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@'))
  330. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo'))
  331. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\n'))
  332. self.assertTrue(
  333. self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\r\n'))
  334. def test_is_hunk_meta_svn_prop(self):
  335. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##'))
  336. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\n'))
  337. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\r\n'))
  338. def test_is_hunk_meta_neg(self):
  339. self.assertFalse(self.diff.is_hunk_meta('@@ -1 + @@'))
  340. self.assertFalse(self.diff.is_hunk_meta('@@ -this is not a hunk meta'))
  341. self.assertFalse(self.diff.is_hunk_meta('## -this is not either'))
  342. def test_parse_hunk_meta_normal(self):
  343. self.assertEqual(self.diff.parse_hunk_meta('@@ -3,7 +3,6 @@'),
  344. ((3, 7), (3, 6)))
  345. def test_parse_hunk_meta_missing(self):
  346. self.assertEqual(self.diff.parse_hunk_meta('@@ -3 +3,6 @@'),
  347. ((3, 0), (3, 6)))
  348. self.assertEqual(self.diff.parse_hunk_meta('@@ -3,7 +3 @@'),
  349. ((3, 7), (3, 0)))
  350. self.assertEqual(self.diff.parse_hunk_meta('@@ -3 +3 @@'),
  351. ((3, 0), (3, 0)))
  352. def test_parse_hunk_meta_svn_prop(self):
  353. self.assertEqual(self.diff.parse_hunk_meta('## -0,0 +1 ##'),
  354. ((0, 0), (1, 0)))
  355. def test_is_old(self):
  356. self.assertTrue(self.diff.is_old('-hello world'))
  357. self.assertTrue(self.diff.is_old('----')) # yaml
  358. def test_is_old_neg(self):
  359. self.assertFalse(self.diff.is_old('--- considered as old path'))
  360. self.assertFalse(self.diff.is_old('-' * 72)) # svn log --diff
  361. def test_is_new(self):
  362. self.assertTrue(self.diff.is_new('+hello world'))
  363. self.assertTrue(self.diff.is_new('++++hello world'))
  364. def test_is_new_neg(self):
  365. self.assertFalse(self.diff.is_new('+++ considered as new path'))
  366. class DiffParserTest(unittest.TestCase):
  367. def test_type_detect_unified(self):
  368. patch = """\
  369. spam
  370. --- a
  371. +++ b
  372. @@ -1,2 +1,2 @@
  373. """
  374. items = patch.splitlines(True)
  375. stream = cdiff.PatchStream(Sequential(items))
  376. parser = cdiff.DiffParser(stream)
  377. self.assertEqual(parser._type, 'unified')
  378. def test_type_detect_context(self):
  379. patch = """\
  380. *** /path/to/original timestamp
  381. --- /path/to/new timestamp
  382. ***************
  383. *** 1,1 ****
  384. --- 1,2 ----
  385. + This is an important
  386. This part of the
  387. """
  388. items = patch.splitlines(True)
  389. stream = cdiff.PatchStream(Sequential(items))
  390. parser = cdiff.DiffParser(stream)
  391. self.assertEqual(parser._type, 'context')
  392. def test_type_detect_neg(self):
  393. patch = """\
  394. spam
  395. --- a
  396. spam
  397. +++ b
  398. """
  399. items = patch.splitlines(True)
  400. stream = cdiff.PatchStream(Sequential(items))
  401. parser = cdiff.DiffParser(stream)
  402. self.assertEqual(parser._type, 'unified')
  403. def test_parse_invalid_hunk_meta(self):
  404. patch = """\
  405. spam
  406. --- a
  407. +++ b
  408. spam
  409. @@ -a,a +0 @@
  410. """
  411. items = patch.splitlines(True)
  412. stream = cdiff.PatchStream(Sequential(items))
  413. parser = cdiff.DiffParser(stream)
  414. self.assertRaises(RuntimeError, list, parser.get_diff_generator())
  415. def test_parse_dangling_header(self):
  416. patch = """\
  417. --- a
  418. +++ b
  419. @@ -1,2 +1,2 @@
  420. -foo
  421. +bar
  422. common
  423. spam
  424. """
  425. items = patch.splitlines(True)
  426. stream = cdiff.PatchStream(Sequential(items))
  427. parser = cdiff.DiffParser(stream)
  428. out = list(parser.get_diff_generator())
  429. self.assertEqual(len(out), 2)
  430. self.assertEqual(len(out[1]._headers), 1)
  431. self.assertEqual(out[1]._headers[0], 'spam\n')
  432. self.assertEqual(out[1]._old_path, '')
  433. self.assertEqual(out[1]._new_path, '')
  434. self.assertEqual(len(out[1]._hunks), 0)
  435. def test_parse_missing_new_path(self):
  436. patch = """\
  437. --- a
  438. +++ b
  439. @@ -1,2 +1,2 @@
  440. -foo
  441. +bar
  442. common
  443. --- c
  444. """
  445. items = patch.splitlines(True)
  446. stream = cdiff.PatchStream(Sequential(items))
  447. parser = cdiff.DiffParser(stream)
  448. self.assertRaises(AssertionError, list, parser.get_diff_generator())
  449. def test_parse_missing_hunk_meta(self):
  450. patch = """\
  451. --- a
  452. +++ b
  453. @@ -1,2 +1,2 @@
  454. -foo
  455. +bar
  456. common
  457. --- c
  458. +++ d
  459. """
  460. items = patch.splitlines(True)
  461. stream = cdiff.PatchStream(Sequential(items))
  462. parser = cdiff.DiffParser(stream)
  463. out = list(parser.get_diff_generator())
  464. self.assertEqual(len(out), 2)
  465. self.assertEqual(len(out[1]._headers), 0)
  466. self.assertEqual(out[1]._old_path, '--- c\n')
  467. self.assertEqual(out[1]._new_path, '+++ d\n')
  468. self.assertEqual(len(out[1]._hunks), 0)
  469. def test_parse_missing_hunk_list(self):
  470. patch = """\
  471. --- a
  472. +++ b
  473. @@ -1,2 +1,2 @@
  474. -foo
  475. +bar
  476. common
  477. --- c
  478. +++ d
  479. @@ -1,2 +1,2 @@
  480. """
  481. items = patch.splitlines(True)
  482. stream = cdiff.PatchStream(Sequential(items))
  483. parser = cdiff.DiffParser(stream)
  484. self.assertRaises(AssertionError, list, parser.get_diff_generator())
  485. def test_parse_only_in_dir(self):
  486. patch = """\
  487. --- a
  488. +++ b
  489. @@ -1,2 +1,2 @@
  490. -foo
  491. +bar
  492. common
  493. Only in foo: foo
  494. --- c
  495. +++ d
  496. @@ -1,2 +1,2 @@
  497. -foo
  498. +bar
  499. common
  500. """
  501. items = patch.splitlines(True)
  502. stream = cdiff.PatchStream(Sequential(items))
  503. parser = cdiff.DiffParser(stream)
  504. out = list(parser.get_diff_generator())
  505. self.assertEqual(len(out), 3)
  506. self.assertEqual(len(out[1]._hunks), 0)
  507. self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
  508. self.assertEqual(len(out[2]._hunks), 1)
  509. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  510. def test_parse_only_in_dir_at_last(self):
  511. patch = """\
  512. --- a
  513. +++ b
  514. @@ -1,2 +1,2 @@
  515. -foo
  516. +bar
  517. common
  518. Only in foo: foo
  519. """
  520. items = patch.splitlines(True)
  521. stream = cdiff.PatchStream(Sequential(items))
  522. parser = cdiff.DiffParser(stream)
  523. out = list(parser.get_diff_generator())
  524. self.assertEqual(len(out), 2)
  525. self.assertEqual(len(out[1]._hunks), 0)
  526. self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
  527. def test_parse_binary_differ_diff_ru(self):
  528. patch = """\
  529. --- a
  530. +++ b
  531. @@ -1,2 +1,2 @@
  532. -foo
  533. +bar
  534. common
  535. Binary files a/1.pdf and b/1.pdf differ
  536. --- c
  537. +++ d
  538. @@ -1,2 +1,2 @@
  539. -foo
  540. +bar
  541. common
  542. """
  543. items = patch.splitlines(True)
  544. stream = cdiff.PatchStream(Sequential(items))
  545. parser = cdiff.DiffParser(stream)
  546. out = list(parser.get_diff_generator())
  547. self.assertEqual(len(out), 3)
  548. self.assertEqual(len(out[1]._hunks), 0)
  549. self.assertEqual(out[1]._old_path, '')
  550. self.assertEqual(out[1]._new_path, '')
  551. self.assertEqual(len(out[1]._headers), 1)
  552. self.assertTrue(out[1]._headers[0].startswith('Binary files'))
  553. self.assertEqual(len(out[2]._hunks), 1)
  554. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  555. def test_parse_binary_differ_git(self):
  556. patch = """\
  557. diff --git a/foo b/foo
  558. index 529d8a3..ad71911 100755
  559. --- a/foo
  560. +++ b/foo
  561. @@ -1,2 +1,2 @@
  562. -foo
  563. +bar
  564. common
  565. diff --git a/example.pdf b/example.pdf
  566. index 1eacfd8..3696851 100644
  567. Binary files a/example.pdf and b/example.pdf differ
  568. diff --git a/bar b/bar
  569. index 529e8a3..ad71921 100755
  570. --- a/bar
  571. +++ b/bar
  572. @@ -1,2 +1,2 @@
  573. -foo
  574. +bar
  575. common
  576. """
  577. items = patch.splitlines(True)
  578. stream = cdiff.PatchStream(Sequential(items))
  579. parser = cdiff.DiffParser(stream)
  580. out = list(parser.get_diff_generator())
  581. self.assertEqual(len(out), 3)
  582. self.assertEqual(len(out[1]._hunks), 0)
  583. self.assertEqual(out[1]._old_path, '')
  584. self.assertEqual(out[1]._new_path, '')
  585. self.assertEqual(len(out[1]._headers), 3)
  586. self.assertTrue(out[1]._headers[2].startswith('Binary files'))
  587. self.assertEqual(len(out[2]._hunks), 1)
  588. self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
  589. def test_parse_svn_prop(self):
  590. patch = """\
  591. --- a
  592. +++ b
  593. Added: svn:executable
  594. ## -0,0 +1 ##
  595. +*
  596. \\ No newline at end of property
  597. Added: svn:keywords
  598. ## -0,0 +1 ##
  599. +Id
  600. """
  601. items = patch.splitlines(True)
  602. stream = cdiff.PatchStream(Sequential(items))
  603. parser = cdiff.DiffParser(stream)
  604. out = list(parser.get_diff_generator())
  605. self.assertEqual(len(out), 1)
  606. self.assertEqual(len(out[0]._hunks), 2)
  607. hunk = out[0]._hunks[1]
  608. self.assertEqual(hunk._hunk_headers, ['Added: svn:keywords\n'])
  609. self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
  610. class MainTest(unittest.TestCase):
  611. def setUp(self):
  612. self._cwd = os.getcwd()
  613. self._ws = tempfile.mkdtemp(prefix='test_cdiff')
  614. self._non_ws = tempfile.mkdtemp(prefix='test_cdiff')
  615. cmd = ('cd %s; git init; git config user.name me; '
  616. 'git config user.email me@example.org') % self._ws
  617. subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
  618. self._change_file('init')
  619. def tearDown(self):
  620. os.chdir(self._cwd)
  621. cmd = ['/bin/rm', '-rf', self._ws, self._non_ws]
  622. subprocess.call(cmd)
  623. def _change_file(self, text):
  624. cmd = ['/bin/sh', '-c',
  625. 'cd %s; echo "%s" > foo' % (self._ws, text)]
  626. subprocess.call(cmd)
  627. def _commit_file(self):
  628. cmd = ['/bin/sh', '-c',
  629. 'cd %s; git add foo; git commit foo -m update' % self._ws]
  630. subprocess.call(cmd, stdout=subprocess.PIPE)
  631. def test_read_diff(self):
  632. sys.argv = sys.argv[:1]
  633. self._change_file('read_diff')
  634. os.chdir(self._ws)
  635. ret = cdiff.main()
  636. os.chdir(self._cwd)
  637. self.assertEqual(ret, 0)
  638. def test_read_diff_neg(self):
  639. sys.argv = sys.argv[:1]
  640. os.chdir(self._non_ws)
  641. ret = cdiff.main()
  642. os.chdir(self._cwd)
  643. self.assertNotEqual(ret, 0)
  644. def test_read_log(self):
  645. sys.argv = [sys.argv[0], '--log']
  646. self._change_file('read_log')
  647. self._commit_file()
  648. os.chdir(self._ws)
  649. ret = cdiff.main()
  650. os.chdir(self._cwd)
  651. self.assertEqual(ret, 0)
  652. def test_read_log_neg(self):
  653. sys.argv = [sys.argv[0], '--log']
  654. os.chdir(self._non_ws)
  655. ret = cdiff.main()
  656. os.chdir(self._cwd)
  657. self.assertNotEqual(ret, 0)
  658. if __name__ == '__main__':
  659. unittest.main()
  660. # vim:set et sts=4 sw=4 tw=80: