func_test.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. # -*- coding: utf-8 -*-
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright 2017 Google, Inc
  5. #
  6. """Functional tests for checking that patman behaves correctly"""
  7. import os
  8. import re
  9. import shutil
  10. import sys
  11. import tempfile
  12. import unittest
  13. from patman.commit import Commit
  14. from patman import control
  15. from patman import gitutil
  16. from patman import patchstream
  17. from patman.patchstream import PatchStream
  18. from patman.series import Series
  19. from patman import settings
  20. from patman import terminal
  21. from patman import tools
  22. from patman.test_util import capture_sys_output
  23. import pygit2
  24. from patman import status
  25. class TestFunctional(unittest.TestCase):
  26. """Functional tests for checking that patman behaves correctly"""
  27. leb = (b'Lord Edmund Blackadd\xc3\xabr <weasel@blackadder.org>'.
  28. decode('utf-8'))
  29. fred = 'Fred Bloggs <f.bloggs@napier.net>'
  30. joe = 'Joe Bloggs <joe@napierwallies.co.nz>'
  31. mary = 'Mary Bloggs <mary@napierwallies.co.nz>'
  32. commits = None
  33. patches = None
  34. def setUp(self):
  35. self.tmpdir = tempfile.mkdtemp(prefix='patman.')
  36. self.gitdir = os.path.join(self.tmpdir, 'git')
  37. self.repo = None
  38. def tearDown(self):
  39. shutil.rmtree(self.tmpdir)
  40. terminal.SetPrintTestMode(False)
  41. @staticmethod
  42. def _get_path(fname):
  43. """Get the path to a test file
  44. Args:
  45. fname (str): Filename to obtain
  46. Returns:
  47. str: Full path to file in the test directory
  48. """
  49. return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  50. 'test', fname)
  51. @classmethod
  52. def _get_text(cls, fname):
  53. """Read a file as text
  54. Args:
  55. fname (str): Filename to read
  56. Returns:
  57. str: Contents of file
  58. """
  59. return open(cls._get_path(fname), encoding='utf-8').read()
  60. @classmethod
  61. def _get_patch_name(cls, subject):
  62. """Get the filename of a patch given its subject
  63. Args:
  64. subject (str): Patch subject
  65. Returns:
  66. str: Filename for that patch
  67. """
  68. fname = re.sub('[ :]', '-', subject)
  69. return fname.replace('--', '-')
  70. def _create_patches_for_test(self, series):
  71. """Create patch files for use by tests
  72. This copies patch files from the test directory as needed by the series
  73. Args:
  74. series (Series): Series containing commits to convert
  75. Returns:
  76. tuple:
  77. str: Cover-letter filename, or None if none
  78. fname_list: list of str, each a patch filename
  79. """
  80. cover_fname = None
  81. fname_list = []
  82. for i, commit in enumerate(series.commits):
  83. clean_subject = self._get_patch_name(commit.subject)
  84. src_fname = '%04d-%s.patch' % (i + 1, clean_subject[:52])
  85. fname = os.path.join(self.tmpdir, src_fname)
  86. shutil.copy(self._get_path(src_fname), fname)
  87. fname_list.append(fname)
  88. if series.get('cover'):
  89. src_fname = '0000-cover-letter.patch'
  90. cover_fname = os.path.join(self.tmpdir, src_fname)
  91. fname = os.path.join(self.tmpdir, src_fname)
  92. shutil.copy(self._get_path(src_fname), fname)
  93. return cover_fname, fname_list
  94. def testBasic(self):
  95. """Tests the basic flow of patman
  96. This creates a series from some hard-coded patches build from a simple
  97. tree with the following metadata in the top commit:
  98. Series-to: u-boot
  99. Series-prefix: RFC
  100. Series-cc: Stefan Brüns <stefan.bruens@rwth-aachen.de>
  101. Cover-letter-cc: Lord Mëlchett <clergy@palace.gov>
  102. Series-version: 3
  103. Patch-cc: fred
  104. Series-process-log: sort, uniq
  105. Series-changes: 4
  106. - Some changes
  107. - Multi
  108. line
  109. change
  110. Commit-changes: 2
  111. - Changes only for this commit
  112. ' Cover-changes: 4
  113. - Some notes for the cover letter
  114. Cover-letter:
  115. test: A test patch series
  116. This is a test of how the cover
  117. letter
  118. works
  119. END
  120. and this in the first commit:
  121. Commit-changes: 2
  122. - second revision change
  123. Series-notes:
  124. some notes
  125. about some things
  126. from the first commit
  127. END
  128. Commit-notes:
  129. Some notes about
  130. the first commit
  131. END
  132. with the following commands:
  133. git log -n2 --reverse >/path/to/tools/patman/test/test01.txt
  134. git format-patch --subject-prefix RFC --cover-letter HEAD~2
  135. mv 00* /path/to/tools/patman/test
  136. It checks these aspects:
  137. - git log can be processed by patchstream
  138. - emailing patches uses the correct command
  139. - CC file has information on each commit
  140. - cover letter has the expected text and subject
  141. - each patch has the correct subject
  142. - dry-run information prints out correctly
  143. - unicode is handled correctly
  144. - Series-to, Series-cc, Series-prefix, Cover-letter
  145. - Cover-letter-cc, Series-version, Series-changes, Series-notes
  146. - Commit-notes
  147. """
  148. process_tags = True
  149. ignore_bad_tags = False
  150. stefan = b'Stefan Br\xc3\xbcns <stefan.bruens@rwth-aachen.de>'.decode('utf-8')
  151. rick = 'Richard III <richard@palace.gov>'
  152. mel = b'Lord M\xc3\xablchett <clergy@palace.gov>'.decode('utf-8')
  153. add_maintainers = [stefan, rick]
  154. dry_run = True
  155. in_reply_to = mel
  156. count = 2
  157. settings.alias = {
  158. 'fdt': ['simon'],
  159. 'u-boot': ['u-boot@lists.denx.de'],
  160. 'simon': [self.leb],
  161. 'fred': [self.fred],
  162. }
  163. text = self._get_text('test01.txt')
  164. series = patchstream.get_metadata_for_test(text)
  165. cover_fname, args = self._create_patches_for_test(series)
  166. with capture_sys_output() as out:
  167. patchstream.fix_patches(series, args)
  168. if cover_fname and series.get('cover'):
  169. patchstream.insert_cover_letter(cover_fname, series, count)
  170. series.DoChecks()
  171. cc_file = series.MakeCcFile(process_tags, cover_fname,
  172. not ignore_bad_tags, add_maintainers,
  173. None)
  174. cmd = gitutil.EmailPatches(
  175. series, cover_fname, args, dry_run, not ignore_bad_tags,
  176. cc_file, in_reply_to=in_reply_to, thread=None)
  177. series.ShowActions(args, cmd, process_tags)
  178. cc_lines = open(cc_file, encoding='utf-8').read().splitlines()
  179. os.remove(cc_file)
  180. lines = iter(out[0].getvalue().splitlines())
  181. self.assertEqual('Cleaned %s patches' % len(series.commits),
  182. next(lines))
  183. self.assertEqual('Change log missing for v2', next(lines))
  184. self.assertEqual('Change log missing for v3', next(lines))
  185. self.assertEqual('Change log for unknown version v4', next(lines))
  186. self.assertEqual("Alias 'pci' not found", next(lines))
  187. self.assertIn('Dry run', next(lines))
  188. self.assertEqual('', next(lines))
  189. self.assertIn('Send a total of %d patches' % count, next(lines))
  190. prev = next(lines)
  191. for i, commit in enumerate(series.commits):
  192. self.assertEqual(' %s' % args[i], prev)
  193. while True:
  194. prev = next(lines)
  195. if 'Cc:' not in prev:
  196. break
  197. self.assertEqual('To: u-boot@lists.denx.de', prev)
  198. self.assertEqual('Cc: %s' % stefan, next(lines))
  199. self.assertEqual('Version: 3', next(lines))
  200. self.assertEqual('Prefix:\t RFC', next(lines))
  201. self.assertEqual('Cover: 4 lines', next(lines))
  202. self.assertEqual(' Cc: %s' % self.fred, next(lines))
  203. self.assertEqual(' Cc: %s' % self.leb,
  204. next(lines))
  205. self.assertEqual(' Cc: %s' % mel, next(lines))
  206. self.assertEqual(' Cc: %s' % rick, next(lines))
  207. expected = ('Git command: git send-email --annotate '
  208. '--in-reply-to="%s" --to "u-boot@lists.denx.de" '
  209. '--cc "%s" --cc-cmd "%s send --cc-cmd %s" %s %s'
  210. % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname,
  211. ' '.join(args)))
  212. self.assertEqual(expected, next(lines))
  213. self.assertEqual(('%s %s\0%s' % (args[0], rick, stefan)), cc_lines[0])
  214. self.assertEqual(
  215. '%s %s\0%s\0%s\0%s' % (args[1], self.fred, self.leb, rick, stefan),
  216. cc_lines[1])
  217. expected = '''
  218. This is a test of how the cover
  219. letter
  220. works
  221. some notes
  222. about some things
  223. from the first commit
  224. Changes in v4:
  225. - Multi
  226. line
  227. change
  228. - Some changes
  229. - Some notes for the cover letter
  230. Simon Glass (2):
  231. pci: Correct cast for sandbox
  232. fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base()
  233. cmd/pci.c | 3 ++-
  234. fs/fat/fat.c | 1 +
  235. lib/efi_loader/efi_memory.c | 1 +
  236. lib/fdtdec.c | 3 ++-
  237. 4 files changed, 6 insertions(+), 2 deletions(-)
  238. --\x20
  239. 2.7.4
  240. '''
  241. lines = open(cover_fname, encoding='utf-8').read().splitlines()
  242. self.assertEqual(
  243. 'Subject: [RFC PATCH v3 0/2] test: A test patch series',
  244. lines[3])
  245. self.assertEqual(expected.splitlines(), lines[7:])
  246. for i, fname in enumerate(args):
  247. lines = open(fname, encoding='utf-8').read().splitlines()
  248. subject = [line for line in lines if line.startswith('Subject')]
  249. self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count),
  250. subject[0][:18])
  251. # Check that we got our commit notes
  252. start = 0
  253. expected = ''
  254. if i == 0:
  255. start = 17
  256. expected = '''---
  257. Some notes about
  258. the first commit
  259. (no changes since v2)
  260. Changes in v2:
  261. - second revision change'''
  262. elif i == 1:
  263. start = 17
  264. expected = '''---
  265. Changes in v4:
  266. - Multi
  267. line
  268. change
  269. - Some changes
  270. Changes in v2:
  271. - Changes only for this commit'''
  272. if expected:
  273. expected = expected.splitlines()
  274. self.assertEqual(expected, lines[start:(start+len(expected))])
  275. def make_commit_with_file(self, subject, body, fname, text):
  276. """Create a file and add it to the git repo with a new commit
  277. Args:
  278. subject (str): Subject for the commit
  279. body (str): Body text of the commit
  280. fname (str): Filename of file to create
  281. text (str): Text to put into the file
  282. """
  283. path = os.path.join(self.gitdir, fname)
  284. tools.WriteFile(path, text, binary=False)
  285. index = self.repo.index
  286. index.add(fname)
  287. author = pygit2.Signature('Test user', 'test@email.com')
  288. committer = author
  289. tree = index.write_tree()
  290. message = subject + '\n' + body
  291. self.repo.create_commit('HEAD', author, committer, message, tree,
  292. [self.repo.head.target])
  293. def make_git_tree(self):
  294. """Make a simple git tree suitable for testing
  295. It has three branches:
  296. 'base' has two commits: PCI, main
  297. 'first' has base as upstream and two more commits: I2C, SPI
  298. 'second' has base as upstream and three more: video, serial, bootm
  299. Returns:
  300. pygit2.Repository: repository
  301. """
  302. repo = pygit2.init_repository(self.gitdir)
  303. self.repo = repo
  304. new_tree = repo.TreeBuilder().write()
  305. author = pygit2.Signature('Test user', 'test@email.com')
  306. committer = author
  307. _ = repo.create_commit('HEAD', author, committer, 'Created master',
  308. new_tree, [])
  309. self.make_commit_with_file('Initial commit', '''
  310. Add a README
  311. ''', 'README', '''This is the README file
  312. describing this project
  313. in very little detail''')
  314. self.make_commit_with_file('pci: PCI implementation', '''
  315. Here is a basic PCI implementation
  316. ''', 'pci.c', '''This is a file
  317. it has some contents
  318. and some more things''')
  319. self.make_commit_with_file('main: Main program', '''
  320. Hello here is the second commit.
  321. ''', 'main.c', '''This is the main file
  322. there is very little here
  323. but we can always add more later
  324. if we want to
  325. Series-to: u-boot
  326. Series-cc: Barry Crump <bcrump@whataroa.nz>
  327. ''')
  328. base_target = repo.revparse_single('HEAD')
  329. self.make_commit_with_file('i2c: I2C things', '''
  330. This has some stuff to do with I2C
  331. ''', 'i2c.c', '''And this is the file contents
  332. with some I2C-related things in it''')
  333. self.make_commit_with_file('spi: SPI fixes', '''
  334. SPI needs some fixes
  335. and here they are
  336. Signed-off-by: %s
  337. Series-to: u-boot
  338. Commit-notes:
  339. title of the series
  340. This is the cover letter for the series
  341. with various details
  342. END
  343. ''' % self.leb, 'spi.c', '''Some fixes for SPI in this
  344. file to make SPI work
  345. better than before''')
  346. first_target = repo.revparse_single('HEAD')
  347. target = repo.revparse_single('HEAD~2')
  348. repo.reset(target.oid, pygit2.GIT_CHECKOUT_FORCE)
  349. self.make_commit_with_file('video: Some video improvements', '''
  350. Fix up the video so that
  351. it looks more purple. Purple is
  352. a very nice colour.
  353. ''', 'video.c', '''More purple here
  354. Purple and purple
  355. Even more purple
  356. Could not be any more purple''')
  357. self.make_commit_with_file('serial: Add a serial driver', '''
  358. Here is the serial driver
  359. for my chip.
  360. Cover-letter:
  361. Series for my board
  362. This series implements support
  363. for my glorious board.
  364. END
  365. Series-links: 183237
  366. ''', 'serial.c', '''The code for the
  367. serial driver is here''')
  368. self.make_commit_with_file('bootm: Make it boot', '''
  369. This makes my board boot
  370. with a fix to the bootm
  371. command
  372. ''', 'bootm.c', '''Fix up the bootm
  373. command to make the code as
  374. complicated as possible''')
  375. second_target = repo.revparse_single('HEAD')
  376. repo.branches.local.create('first', first_target)
  377. repo.config.set_multivar('branch.first.remote', '', '.')
  378. repo.config.set_multivar('branch.first.merge', '', 'refs/heads/base')
  379. repo.branches.local.create('second', second_target)
  380. repo.config.set_multivar('branch.second.remote', '', '.')
  381. repo.config.set_multivar('branch.second.merge', '', 'refs/heads/base')
  382. repo.branches.local.create('base', base_target)
  383. return repo
  384. def testBranch(self):
  385. """Test creating patches from a branch"""
  386. repo = self.make_git_tree()
  387. target = repo.lookup_reference('refs/heads/first')
  388. self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
  389. control.setup()
  390. try:
  391. orig_dir = os.getcwd()
  392. os.chdir(self.gitdir)
  393. # Check that it can detect the current branch
  394. self.assertEqual(2, gitutil.CountCommitsToBranch(None))
  395. col = terminal.Color()
  396. with capture_sys_output() as _:
  397. _, cover_fname, patch_files = control.prepare_patches(
  398. col, branch=None, count=-1, start=0, end=0,
  399. ignore_binary=False, signoff=True)
  400. self.assertIsNone(cover_fname)
  401. self.assertEqual(2, len(patch_files))
  402. # Check that it can detect a different branch
  403. self.assertEqual(3, gitutil.CountCommitsToBranch('second'))
  404. with capture_sys_output() as _:
  405. _, cover_fname, patch_files = control.prepare_patches(
  406. col, branch='second', count=-1, start=0, end=0,
  407. ignore_binary=False, signoff=True)
  408. self.assertIsNotNone(cover_fname)
  409. self.assertEqual(3, len(patch_files))
  410. # Check that it can skip patches at the end
  411. with capture_sys_output() as _:
  412. _, cover_fname, patch_files = control.prepare_patches(
  413. col, branch='second', count=-1, start=0, end=1,
  414. ignore_binary=False, signoff=True)
  415. self.assertIsNotNone(cover_fname)
  416. self.assertEqual(2, len(patch_files))
  417. finally:
  418. os.chdir(orig_dir)
  419. def testTags(self):
  420. """Test collection of tags in a patchstream"""
  421. text = '''This is a patch
  422. Signed-off-by: Terminator
  423. Reviewed-by: %s
  424. Reviewed-by: %s
  425. Tested-by: %s
  426. ''' % (self.joe, self.mary, self.leb)
  427. pstrm = PatchStream.process_text(text)
  428. self.assertEqual(pstrm.commit.rtags, {
  429. 'Reviewed-by': {self.joe, self.mary},
  430. 'Tested-by': {self.leb}})
  431. def testInvalidTag(self):
  432. """Test invalid tag in a patchstream"""
  433. text = '''This is a patch
  434. Serie-version: 2
  435. '''
  436. with self.assertRaises(ValueError) as exc:
  437. pstrm = PatchStream.process_text(text)
  438. self.assertEqual("Line 3: Invalid tag = 'Serie-version: 2'",
  439. str(exc.exception))
  440. def testMissingEnd(self):
  441. """Test a missing END tag"""
  442. text = '''This is a patch
  443. Cover-letter:
  444. This is the title
  445. missing END after this line
  446. Signed-off-by: Fred
  447. '''
  448. pstrm = PatchStream.process_text(text)
  449. self.assertEqual(["Missing 'END' in section 'cover'"],
  450. pstrm.commit.warn)
  451. def testMissingBlankLine(self):
  452. """Test a missing blank line after a tag"""
  453. text = '''This is a patch
  454. Series-changes: 2
  455. - First line of changes
  456. - Missing blank line after this line
  457. Signed-off-by: Fred
  458. '''
  459. pstrm = PatchStream.process_text(text)
  460. self.assertEqual(["Missing 'blank line' in section 'Series-changes'"],
  461. pstrm.commit.warn)
  462. def testInvalidCommitTag(self):
  463. """Test an invalid Commit-xxx tag"""
  464. text = '''This is a patch
  465. Commit-fred: testing
  466. '''
  467. pstrm = PatchStream.process_text(text)
  468. self.assertEqual(["Line 3: Ignoring Commit-fred"], pstrm.commit.warn)
  469. def testSelfTest(self):
  470. """Test a tested by tag by this user"""
  471. test_line = 'Tested-by: %s@napier.com' % os.getenv('USER')
  472. text = '''This is a patch
  473. %s
  474. ''' % test_line
  475. pstrm = PatchStream.process_text(text)
  476. self.assertEqual(["Ignoring '%s'" % test_line], pstrm.commit.warn)
  477. def testSpaceBeforeTab(self):
  478. """Test a space before a tab"""
  479. text = '''This is a patch
  480. + \tSomething
  481. '''
  482. pstrm = PatchStream.process_text(text)
  483. self.assertEqual(["Line 3/0 has space before tab"], pstrm.commit.warn)
  484. def testLinesAfterTest(self):
  485. """Test detecting lines after TEST= line"""
  486. text = '''This is a patch
  487. TEST=sometest
  488. more lines
  489. here
  490. '''
  491. pstrm = PatchStream.process_text(text)
  492. self.assertEqual(["Found 2 lines after TEST="], pstrm.commit.warn)
  493. def testBlankLineAtEnd(self):
  494. """Test detecting a blank line at the end of a file"""
  495. text = '''This is a patch
  496. diff --git a/lib/fdtdec.c b/lib/fdtdec.c
  497. index c072e54..942244f 100644
  498. --- a/lib/fdtdec.c
  499. +++ b/lib/fdtdec.c
  500. @@ -1200,7 +1200,8 @@ int fdtdec_setup_mem_size_base(void)
  501. }
  502. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  503. - debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
  504. + debug("%s: Initial DRAM size %llx\n", __func__,
  505. + (unsigned long long)gd->ram_size);
  506. +
  507. diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
  508. --
  509. 2.7.4
  510. '''
  511. pstrm = PatchStream.process_text(text)
  512. self.assertEqual(
  513. ["Found possible blank line(s) at end of file 'lib/fdtdec.c'"],
  514. pstrm.commit.warn)
  515. def testNoUpstream(self):
  516. """Test CountCommitsToBranch when there is no upstream"""
  517. repo = self.make_git_tree()
  518. target = repo.lookup_reference('refs/heads/base')
  519. self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
  520. # Check that it can detect the current branch
  521. try:
  522. orig_dir = os.getcwd()
  523. os.chdir(self.gitdir)
  524. with self.assertRaises(ValueError) as exc:
  525. gitutil.CountCommitsToBranch(None)
  526. self.assertIn(
  527. "Failed to determine upstream: fatal: no upstream configured for branch 'base'",
  528. str(exc.exception))
  529. finally:
  530. os.chdir(orig_dir)
  531. @staticmethod
  532. def _fake_patchwork(url, subpath):
  533. """Fake Patchwork server for the function below
  534. This handles accessing a series, providing a list consisting of a
  535. single patch
  536. Args:
  537. url (str): URL of patchwork server
  538. subpath (str): URL subpath to use
  539. """
  540. re_series = re.match(r'series/(\d*)/$', subpath)
  541. if re_series:
  542. series_num = re_series.group(1)
  543. if series_num == '1234':
  544. return {'patches': [
  545. {'id': '1', 'name': 'Some patch'}]}
  546. raise ValueError('Fake Patchwork does not understand: %s' % subpath)
  547. def testStatusMismatch(self):
  548. """Test Patchwork patches not matching the series"""
  549. series = Series()
  550. with capture_sys_output() as (_, err):
  551. status.collect_patches(series, 1234, None, self._fake_patchwork)
  552. self.assertIn('Warning: Patchwork reports 1 patches, series has 0',
  553. err.getvalue())
  554. def testStatusReadPatch(self):
  555. """Test handling a single patch in Patchwork"""
  556. series = Series()
  557. series.commits = [Commit('abcd')]
  558. patches = status.collect_patches(series, 1234, None,
  559. self._fake_patchwork)
  560. self.assertEqual(1, len(patches))
  561. patch = patches[0]
  562. self.assertEqual('1', patch.id)
  563. self.assertEqual('Some patch', patch.raw_subject)
  564. def testParseSubject(self):
  565. """Test parsing of the patch subject"""
  566. patch = status.Patch('1')
  567. # Simple patch not in a series
  568. patch.parse_subject('Testing')
  569. self.assertEqual('Testing', patch.raw_subject)
  570. self.assertEqual('Testing', patch.subject)
  571. self.assertEqual(1, patch.seq)
  572. self.assertEqual(1, patch.count)
  573. self.assertEqual(None, patch.prefix)
  574. self.assertEqual(None, patch.version)
  575. # First patch in a series
  576. patch.parse_subject('[1/2] Testing')
  577. self.assertEqual('[1/2] Testing', patch.raw_subject)
  578. self.assertEqual('Testing', patch.subject)
  579. self.assertEqual(1, patch.seq)
  580. self.assertEqual(2, patch.count)
  581. self.assertEqual(None, patch.prefix)
  582. self.assertEqual(None, patch.version)
  583. # Second patch in a series
  584. patch.parse_subject('[2/2] Testing')
  585. self.assertEqual('Testing', patch.subject)
  586. self.assertEqual(2, patch.seq)
  587. self.assertEqual(2, patch.count)
  588. self.assertEqual(None, patch.prefix)
  589. self.assertEqual(None, patch.version)
  590. # RFC patch
  591. patch.parse_subject('[RFC,3/7] Testing')
  592. self.assertEqual('Testing', patch.subject)
  593. self.assertEqual(3, patch.seq)
  594. self.assertEqual(7, patch.count)
  595. self.assertEqual('RFC', patch.prefix)
  596. self.assertEqual(None, patch.version)
  597. # Version patch
  598. patch.parse_subject('[v2,3/7] Testing')
  599. self.assertEqual('Testing', patch.subject)
  600. self.assertEqual(3, patch.seq)
  601. self.assertEqual(7, patch.count)
  602. self.assertEqual(None, patch.prefix)
  603. self.assertEqual('v2', patch.version)
  604. # All fields
  605. patch.parse_subject('[RESEND,v2,3/7] Testing')
  606. self.assertEqual('Testing', patch.subject)
  607. self.assertEqual(3, patch.seq)
  608. self.assertEqual(7, patch.count)
  609. self.assertEqual('RESEND', patch.prefix)
  610. self.assertEqual('v2', patch.version)
  611. # RFC only
  612. patch.parse_subject('[RESEND] Testing')
  613. self.assertEqual('Testing', patch.subject)
  614. self.assertEqual(1, patch.seq)
  615. self.assertEqual(1, patch.count)
  616. self.assertEqual('RESEND', patch.prefix)
  617. self.assertEqual(None, patch.version)
  618. def testCompareSeries(self):
  619. """Test operation of compare_with_series()"""
  620. commit1 = Commit('abcd')
  621. commit1.subject = 'Subject 1'
  622. commit2 = Commit('ef12')
  623. commit2.subject = 'Subject 2'
  624. commit3 = Commit('3456')
  625. commit3.subject = 'Subject 2'
  626. patch1 = status.Patch('1')
  627. patch1.subject = 'Subject 1'
  628. patch2 = status.Patch('2')
  629. patch2.subject = 'Subject 2'
  630. patch3 = status.Patch('3')
  631. patch3.subject = 'Subject 2'
  632. series = Series()
  633. series.commits = [commit1]
  634. patches = [patch1]
  635. patch_for_commit, commit_for_patch, warnings = (
  636. status.compare_with_series(series, patches))
  637. self.assertEqual(1, len(patch_for_commit))
  638. self.assertEqual(patch1, patch_for_commit[0])
  639. self.assertEqual(1, len(commit_for_patch))
  640. self.assertEqual(commit1, commit_for_patch[0])
  641. series.commits = [commit1]
  642. patches = [patch1, patch2]
  643. patch_for_commit, commit_for_patch, warnings = (
  644. status.compare_with_series(series, patches))
  645. self.assertEqual(1, len(patch_for_commit))
  646. self.assertEqual(patch1, patch_for_commit[0])
  647. self.assertEqual(1, len(commit_for_patch))
  648. self.assertEqual(commit1, commit_for_patch[0])
  649. self.assertEqual(["Cannot find commit for patch 2 ('Subject 2')"],
  650. warnings)
  651. series.commits = [commit1, commit2]
  652. patches = [patch1]
  653. patch_for_commit, commit_for_patch, warnings = (
  654. status.compare_with_series(series, patches))
  655. self.assertEqual(1, len(patch_for_commit))
  656. self.assertEqual(patch1, patch_for_commit[0])
  657. self.assertEqual(1, len(commit_for_patch))
  658. self.assertEqual(commit1, commit_for_patch[0])
  659. self.assertEqual(["Cannot find patch for commit 2 ('Subject 2')"],
  660. warnings)
  661. series.commits = [commit1, commit2, commit3]
  662. patches = [patch1, patch2]
  663. patch_for_commit, commit_for_patch, warnings = (
  664. status.compare_with_series(series, patches))
  665. self.assertEqual(2, len(patch_for_commit))
  666. self.assertEqual(patch1, patch_for_commit[0])
  667. self.assertEqual(patch2, patch_for_commit[1])
  668. self.assertEqual(1, len(commit_for_patch))
  669. self.assertEqual(commit1, commit_for_patch[0])
  670. self.assertEqual(["Cannot find patch for commit 3 ('Subject 2')",
  671. "Multiple commits match patch 2 ('Subject 2'):\n"
  672. ' Subject 2\n Subject 2'],
  673. warnings)
  674. series.commits = [commit1, commit2]
  675. patches = [patch1, patch2, patch3]
  676. patch_for_commit, commit_for_patch, warnings = (
  677. status.compare_with_series(series, patches))
  678. self.assertEqual(1, len(patch_for_commit))
  679. self.assertEqual(patch1, patch_for_commit[0])
  680. self.assertEqual(2, len(commit_for_patch))
  681. self.assertEqual(commit1, commit_for_patch[0])
  682. self.assertEqual(["Multiple patches match commit 2 ('Subject 2'):\n"
  683. ' Subject 2\n Subject 2',
  684. "Cannot find commit for patch 3 ('Subject 2')"],
  685. warnings)
  686. def _fake_patchwork2(self, url, subpath):
  687. """Fake Patchwork server for the function below
  688. This handles accessing series, patches and comments, providing the data
  689. in self.patches to the caller
  690. Args:
  691. url (str): URL of patchwork server
  692. subpath (str): URL subpath to use
  693. """
  694. re_series = re.match(r'series/(\d*)/$', subpath)
  695. re_patch = re.match(r'patches/(\d*)/$', subpath)
  696. re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
  697. if re_series:
  698. series_num = re_series.group(1)
  699. if series_num == '1234':
  700. return {'patches': self.patches}
  701. elif re_patch:
  702. patch_num = int(re_patch.group(1))
  703. patch = self.patches[patch_num - 1]
  704. return patch
  705. elif re_comments:
  706. patch_num = int(re_comments.group(1))
  707. patch = self.patches[patch_num - 1]
  708. return patch.comments
  709. raise ValueError('Fake Patchwork does not understand: %s' % subpath)
  710. def testFindNewResponses(self):
  711. """Test operation of find_new_responses()"""
  712. commit1 = Commit('abcd')
  713. commit1.subject = 'Subject 1'
  714. commit2 = Commit('ef12')
  715. commit2.subject = 'Subject 2'
  716. patch1 = status.Patch('1')
  717. patch1.parse_subject('[1/2] Subject 1')
  718. patch1.name = patch1.raw_subject
  719. patch1.content = 'This is my patch content'
  720. comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
  721. patch1.comments = [comment1a]
  722. patch2 = status.Patch('2')
  723. patch2.parse_subject('[2/2] Subject 2')
  724. patch2.name = patch2.raw_subject
  725. patch2.content = 'Some other patch content'
  726. comment2a = {
  727. 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
  728. (self.mary, self.leb)}
  729. comment2b = {'content': 'Reviewed-by: %s' % self.fred}
  730. patch2.comments = [comment2a, comment2b]
  731. # This test works by setting up commits and patch for use by the fake
  732. # Rest API function _fake_patchwork2(). It calls various functions in
  733. # the status module after setting up tags in the commits, checking that
  734. # things behaves as expected
  735. self.commits = [commit1, commit2]
  736. self.patches = [patch1, patch2]
  737. count = 2
  738. new_rtag_list = [None] * count
  739. review_list = [None, None]
  740. # Check that the tags are picked up on the first patch
  741. status.find_new_responses(new_rtag_list, review_list, 0, commit1,
  742. patch1, None, self._fake_patchwork2)
  743. self.assertEqual(new_rtag_list[0], {'Reviewed-by': {self.joe}})
  744. # Now the second patch
  745. status.find_new_responses(new_rtag_list, review_list, 1, commit2,
  746. patch2, None, self._fake_patchwork2)
  747. self.assertEqual(new_rtag_list[1], {
  748. 'Reviewed-by': {self.mary, self.fred},
  749. 'Tested-by': {self.leb}})
  750. # Now add some tags to the commit, which means they should not appear as
  751. # 'new' tags when scanning comments
  752. new_rtag_list = [None] * count
  753. commit1.rtags = {'Reviewed-by': {self.joe}}
  754. status.find_new_responses(new_rtag_list, review_list, 0, commit1,
  755. patch1, None, self._fake_patchwork2)
  756. self.assertEqual(new_rtag_list[0], {})
  757. # For the second commit, add Ed and Fred, so only Mary should be left
  758. commit2.rtags = {
  759. 'Tested-by': {self.leb},
  760. 'Reviewed-by': {self.fred}}
  761. status.find_new_responses(new_rtag_list, review_list, 1, commit2,
  762. patch2, None, self._fake_patchwork2)
  763. self.assertEqual(new_rtag_list[1], {'Reviewed-by': {self.mary}})
  764. # Check that the output patches expectations:
  765. # 1 Subject 1
  766. # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
  767. # 2 Subject 2
  768. # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
  769. # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
  770. # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
  771. # 1 new response available in patchwork
  772. series = Series()
  773. series.commits = [commit1, commit2]
  774. terminal.SetPrintTestMode()
  775. status.check_patchwork_status(series, '1234', None, None, False, False,
  776. None, self._fake_patchwork2)
  777. lines = iter(terminal.GetPrintTestLines())
  778. col = terminal.Color()
  779. self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.BLUE),
  780. next(lines))
  781. self.assertEqual(
  782. terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
  783. bright=False),
  784. next(lines))
  785. self.assertEqual(terminal.PrintLine(self.joe, col.WHITE, bright=False),
  786. next(lines))
  787. self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.BLUE),
  788. next(lines))
  789. self.assertEqual(
  790. terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
  791. bright=False),
  792. next(lines))
  793. self.assertEqual(terminal.PrintLine(self.fred, col.WHITE, bright=False),
  794. next(lines))
  795. self.assertEqual(
  796. terminal.PrintLine(' Tested-by: ', col.GREEN, newline=False,
  797. bright=False),
  798. next(lines))
  799. self.assertEqual(terminal.PrintLine(self.leb, col.WHITE, bright=False),
  800. next(lines))
  801. self.assertEqual(
  802. terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
  803. next(lines))
  804. self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
  805. next(lines))
  806. self.assertEqual(terminal.PrintLine(
  807. '1 new response available in patchwork (use -d to write them to a new branch)',
  808. None), next(lines))
  809. def _fake_patchwork3(self, url, subpath):
  810. """Fake Patchwork server for the function below
  811. This handles accessing series, patches and comments, providing the data
  812. in self.patches to the caller
  813. Args:
  814. url (str): URL of patchwork server
  815. subpath (str): URL subpath to use
  816. """
  817. re_series = re.match(r'series/(\d*)/$', subpath)
  818. re_patch = re.match(r'patches/(\d*)/$', subpath)
  819. re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
  820. if re_series:
  821. series_num = re_series.group(1)
  822. if series_num == '1234':
  823. return {'patches': self.patches}
  824. elif re_patch:
  825. patch_num = int(re_patch.group(1))
  826. patch = self.patches[patch_num - 1]
  827. return patch
  828. elif re_comments:
  829. patch_num = int(re_comments.group(1))
  830. patch = self.patches[patch_num - 1]
  831. return patch.comments
  832. raise ValueError('Fake Patchwork does not understand: %s' % subpath)
  833. def testCreateBranch(self):
  834. """Test operation of create_branch()"""
  835. repo = self.make_git_tree()
  836. branch = 'first'
  837. dest_branch = 'first2'
  838. count = 2
  839. gitdir = os.path.join(self.gitdir, '.git')
  840. # Set up the test git tree. We use branch 'first' which has two commits
  841. # in it
  842. series = patchstream.get_metadata_for_list(branch, gitdir, count)
  843. self.assertEqual(2, len(series.commits))
  844. patch1 = status.Patch('1')
  845. patch1.parse_subject('[1/2] %s' % series.commits[0].subject)
  846. patch1.name = patch1.raw_subject
  847. patch1.content = 'This is my patch content'
  848. comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
  849. patch1.comments = [comment1a]
  850. patch2 = status.Patch('2')
  851. patch2.parse_subject('[2/2] %s' % series.commits[1].subject)
  852. patch2.name = patch2.raw_subject
  853. patch2.content = 'Some other patch content'
  854. comment2a = {
  855. 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
  856. (self.mary, self.leb)}
  857. comment2b = {
  858. 'content': 'Reviewed-by: %s' % self.fred}
  859. patch2.comments = [comment2a, comment2b]
  860. # This test works by setting up patches for use by the fake Rest API
  861. # function _fake_patchwork3(). The fake patch comments above should
  862. # result in new review tags that are collected and added to the commits
  863. # created in the destination branch.
  864. self.patches = [patch1, patch2]
  865. count = 2
  866. # Expected output:
  867. # 1 i2c: I2C things
  868. # + Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
  869. # 2 spi: SPI fixes
  870. # + Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
  871. # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
  872. # + Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
  873. # 4 new responses available in patchwork
  874. # 4 responses added from patchwork into new branch 'first2'
  875. # <unittest.result.TestResult run=8 errors=0 failures=0>
  876. terminal.SetPrintTestMode()
  877. status.check_patchwork_status(series, '1234', branch, dest_branch,
  878. False, False, None, self._fake_patchwork3,
  879. repo)
  880. lines = terminal.GetPrintTestLines()
  881. self.assertEqual(12, len(lines))
  882. self.assertEqual(
  883. "4 responses added from patchwork into new branch 'first2'",
  884. lines[11].text)
  885. # Check that the destination branch has the new tags
  886. new_series = patchstream.get_metadata_for_list(dest_branch, gitdir,
  887. count)
  888. self.assertEqual(
  889. {'Reviewed-by': {self.joe}},
  890. new_series.commits[0].rtags)
  891. self.assertEqual(
  892. {'Tested-by': {self.leb},
  893. 'Reviewed-by': {self.fred, self.mary}},
  894. new_series.commits[1].rtags)
  895. # Now check the actual test of the first commit message. We expect to
  896. # see the new tags immediately below the old ones.
  897. stdout = patchstream.get_list(dest_branch, count=count, git_dir=gitdir)
  898. lines = iter([line.strip() for line in stdout.splitlines()
  899. if '-by:' in line])
  900. # First patch should have the review tag
  901. self.assertEqual('Reviewed-by: %s' % self.joe, next(lines))
  902. # Second patch should have the sign-off then the tested-by and two
  903. # reviewed-by tags
  904. self.assertEqual('Signed-off-by: %s' % self.leb, next(lines))
  905. self.assertEqual('Reviewed-by: %s' % self.fred, next(lines))
  906. self.assertEqual('Reviewed-by: %s' % self.mary, next(lines))
  907. self.assertEqual('Tested-by: %s' % self.leb, next(lines))
  908. def testParseSnippets(self):
  909. """Test parsing of review snippets"""
  910. text = '''Hi Fred,
  911. This is a comment from someone.
  912. Something else
  913. On some recent date, Fred wrote:
  914. > This is why I wrote the patch
  915. > so here it is
  916. Now a comment about the commit message
  917. A little more to say
  918. Even more
  919. > diff --git a/file.c b/file.c
  920. > Some more code
  921. > Code line 2
  922. > Code line 3
  923. > Code line 4
  924. > Code line 5
  925. > Code line 6
  926. > Code line 7
  927. > Code line 8
  928. > Code line 9
  929. And another comment
  930. > @@ -153,8 +143,13 @@ def CheckPatch(fname, show_types=False):
  931. > further down on the file
  932. > and more code
  933. > +Addition here
  934. > +Another addition here
  935. > codey
  936. > more codey
  937. and another thing in same file
  938. > @@ -253,8 +243,13 @@
  939. > with no function context
  940. one more thing
  941. > diff --git a/tools/patman/main.py b/tools/patman/main.py
  942. > +line of code
  943. now a very long comment in a different file
  944. line2
  945. line3
  946. line4
  947. line5
  948. line6
  949. line7
  950. line8
  951. '''
  952. pstrm = PatchStream.process_text(text, True)
  953. self.assertEqual([], pstrm.commit.warn)
  954. # We expect to the filename and up to 5 lines of code context before
  955. # each comment. The 'On xxx wrote:' bit should be removed.
  956. self.assertEqual(
  957. [['Hi Fred,',
  958. 'This is a comment from someone.',
  959. 'Something else'],
  960. ['> This is why I wrote the patch',
  961. '> so here it is',
  962. 'Now a comment about the commit message',
  963. 'A little more to say', 'Even more'],
  964. ['> File: file.c', '> Code line 5', '> Code line 6',
  965. '> Code line 7', '> Code line 8', '> Code line 9',
  966. 'And another comment'],
  967. ['> File: file.c',
  968. '> Line: 153 / 143: def CheckPatch(fname, show_types=False):',
  969. '> and more code', '> +Addition here', '> +Another addition here',
  970. '> codey', '> more codey', 'and another thing in same file'],
  971. ['> File: file.c', '> Line: 253 / 243',
  972. '> with no function context', 'one more thing'],
  973. ['> File: tools/patman/main.py', '> +line of code',
  974. 'now a very long comment in a different file',
  975. 'line2', 'line3', 'line4', 'line5', 'line6', 'line7', 'line8']],
  976. pstrm.snippets)
  977. def testReviewSnippets(self):
  978. """Test showing of review snippets"""
  979. def _to_submitter(who):
  980. m_who = re.match('(.*) <(.*)>', who)
  981. return {
  982. 'name': m_who.group(1),
  983. 'email': m_who.group(2)
  984. }
  985. commit1 = Commit('abcd')
  986. commit1.subject = 'Subject 1'
  987. commit2 = Commit('ef12')
  988. commit2.subject = 'Subject 2'
  989. patch1 = status.Patch('1')
  990. patch1.parse_subject('[1/2] Subject 1')
  991. patch1.name = patch1.raw_subject
  992. patch1.content = 'This is my patch content'
  993. comment1a = {'submitter': _to_submitter(self.joe),
  994. 'content': '''Hi Fred,
  995. On some date Fred wrote:
  996. > diff --git a/file.c b/file.c
  997. > Some code
  998. > and more code
  999. Here is my comment above the above...
  1000. Reviewed-by: %s
  1001. ''' % self.joe}
  1002. patch1.comments = [comment1a]
  1003. patch2 = status.Patch('2')
  1004. patch2.parse_subject('[2/2] Subject 2')
  1005. patch2.name = patch2.raw_subject
  1006. patch2.content = 'Some other patch content'
  1007. comment2a = {
  1008. 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
  1009. (self.mary, self.leb)}
  1010. comment2b = {'submitter': _to_submitter(self.fred),
  1011. 'content': '''Hi Fred,
  1012. On some date Fred wrote:
  1013. > diff --git a/tools/patman/commit.py b/tools/patman/commit.py
  1014. > @@ -41,6 +41,9 @@ class Commit:
  1015. > self.rtags = collections.defaultdict(set)
  1016. > self.warn = []
  1017. >
  1018. > + def __str__(self):
  1019. > + return self.subject
  1020. > +
  1021. > def AddChange(self, version, info):
  1022. > """Add a new change line to the change list for a version.
  1023. >
  1024. A comment
  1025. Reviewed-by: %s
  1026. ''' % self.fred}
  1027. patch2.comments = [comment2a, comment2b]
  1028. # This test works by setting up commits and patch for use by the fake
  1029. # Rest API function _fake_patchwork2(). It calls various functions in
  1030. # the status module after setting up tags in the commits, checking that
  1031. # things behaves as expected
  1032. self.commits = [commit1, commit2]
  1033. self.patches = [patch1, patch2]
  1034. # Check that the output patches expectations:
  1035. # 1 Subject 1
  1036. # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
  1037. # 2 Subject 2
  1038. # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
  1039. # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
  1040. # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
  1041. # 1 new response available in patchwork
  1042. series = Series()
  1043. series.commits = [commit1, commit2]
  1044. terminal.SetPrintTestMode()
  1045. status.check_patchwork_status(series, '1234', None, None, False, True,
  1046. None, self._fake_patchwork2)
  1047. lines = iter(terminal.GetPrintTestLines())
  1048. col = terminal.Color()
  1049. self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.BLUE),
  1050. next(lines))
  1051. self.assertEqual(
  1052. terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
  1053. next(lines))
  1054. self.assertEqual(terminal.PrintLine(self.joe, col.WHITE), next(lines))
  1055. self.assertEqual(terminal.PrintLine('Review: %s' % self.joe, col.RED),
  1056. next(lines))
  1057. self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(lines))
  1058. self.assertEqual(terminal.PrintLine('', None), next(lines))
  1059. self.assertEqual(terminal.PrintLine(' > File: file.c', col.MAGENTA),
  1060. next(lines))
  1061. self.assertEqual(terminal.PrintLine(' > Some code', col.MAGENTA),
  1062. next(lines))
  1063. self.assertEqual(terminal.PrintLine(' > and more code', col.MAGENTA),
  1064. next(lines))
  1065. self.assertEqual(terminal.PrintLine(
  1066. ' Here is my comment above the above...', None), next(lines))
  1067. self.assertEqual(terminal.PrintLine('', None), next(lines))
  1068. self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.BLUE),
  1069. next(lines))
  1070. self.assertEqual(
  1071. terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
  1072. next(lines))
  1073. self.assertEqual(terminal.PrintLine(self.fred, col.WHITE),
  1074. next(lines))
  1075. self.assertEqual(
  1076. terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
  1077. next(lines))
  1078. self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
  1079. next(lines))
  1080. self.assertEqual(
  1081. terminal.PrintLine(' + Tested-by: ', col.GREEN, newline=False),
  1082. next(lines))
  1083. self.assertEqual(terminal.PrintLine(self.leb, col.WHITE),
  1084. next(lines))
  1085. self.assertEqual(terminal.PrintLine('Review: %s' % self.fred, col.RED),
  1086. next(lines))
  1087. self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(lines))
  1088. self.assertEqual(terminal.PrintLine('', None), next(lines))
  1089. self.assertEqual(terminal.PrintLine(
  1090. ' > File: tools/patman/commit.py', col.MAGENTA), next(lines))
  1091. self.assertEqual(terminal.PrintLine(
  1092. ' > Line: 41 / 41: class Commit:', col.MAGENTA), next(lines))
  1093. self.assertEqual(terminal.PrintLine(
  1094. ' > + return self.subject', col.MAGENTA), next(lines))
  1095. self.assertEqual(terminal.PrintLine(
  1096. ' > +', col.MAGENTA), next(lines))
  1097. self.assertEqual(
  1098. terminal.PrintLine(' > def AddChange(self, version, info):',
  1099. col.MAGENTA),
  1100. next(lines))
  1101. self.assertEqual(terminal.PrintLine(
  1102. ' > """Add a new change line to the change list for a version.',
  1103. col.MAGENTA), next(lines))
  1104. self.assertEqual(terminal.PrintLine(
  1105. ' >', col.MAGENTA), next(lines))
  1106. self.assertEqual(terminal.PrintLine(
  1107. ' A comment', None), next(lines))
  1108. self.assertEqual(terminal.PrintLine('', None), next(lines))
  1109. self.assertEqual(terminal.PrintLine(
  1110. '4 new responses available in patchwork (use -d to write them to a new branch)',
  1111. None), next(lines))
  1112. def testInsertTags(self):
  1113. """Test inserting of review tags"""
  1114. msg = '''first line
  1115. second line.'''
  1116. tags = [
  1117. 'Reviewed-by: Bin Meng <bmeng.cn@gmail.com>',
  1118. 'Tested-by: Bin Meng <bmeng.cn@gmail.com>'
  1119. ]
  1120. signoff = 'Signed-off-by: Simon Glass <sjg@chromium.com>'
  1121. tag_str = '\n'.join(tags)
  1122. new_msg = patchstream.insert_tags(msg, tags)
  1123. self.assertEqual(msg + '\n\n' + tag_str, new_msg)
  1124. new_msg = patchstream.insert_tags(msg + '\n', tags)
  1125. self.assertEqual(msg + '\n\n' + tag_str, new_msg)
  1126. msg += '\n\n' + signoff
  1127. new_msg = patchstream.insert_tags(msg, tags)
  1128. self.assertEqual(msg + '\n' + tag_str, new_msg)