func_test.py 49 KB

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