blob: 9c7d5d8c381da4a771fed428a64a5b79eee35ae1 [file] [log] [blame]
Simon Glassdf1bc5c2017-05-29 15:31:31 -06001# -*- coding: utf-8 -*-
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassdf1bc5c2017-05-29 15:31:31 -06003#
4# Copyright 2017 Google, Inc
5#
Simon Glassdf1bc5c2017-05-29 15:31:31 -06006
Simon Glasseb209e52020-10-29 21:46:15 -06007"""Functional tests for checking that patman behaves correctly"""
8
Simon Glass25b91c12025-04-29 07:22:19 -06009import asyncio
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050010import contextlib
Simon Glassdf1bc5c2017-05-29 15:31:31 -060011import os
Maxim Cournoyer0331edb2022-12-19 17:32:39 -050012import pathlib
Simon Glassdf1bc5c2017-05-29 15:31:31 -060013import re
14import shutil
15import sys
16import tempfile
17import unittest
18
Simon Glass3db916d2020-10-29 21:46:35 -060019
20from patman.commit import Commit
Simon Glass54f1c5b2020-07-05 21:41:50 -060021from patman import control
Simon Glassa997ea52020-04-17 18:09:04 -060022from patman import patchstream
Simon Glassa7fadab2020-10-29 21:46:26 -060023from patman.patchstream import PatchStream
Simon Glass232eefd2025-04-29 07:22:14 -060024from patman import patchwork
Simon Glassc0257982025-04-29 07:22:11 -060025from patman import send
Simon Glass3db916d2020-10-29 21:46:35 -060026from patman.series import Series
Simon Glassa997ea52020-04-17 18:09:04 -060027from patman import settings
Simon Glassba1b3b92025-02-09 14:26:00 -070028from u_boot_pylib import gitutil
Simon Glass131444f2023-02-23 18:18:04 -070029from u_boot_pylib import terminal
30from u_boot_pylib import tools
Simon Glassdf1bc5c2017-05-29 15:31:31 -060031
Tom Rini488ea972021-02-26 07:52:31 -050032import pygit2
33from patman import status
Simon Glassdf1bc5c2017-05-29 15:31:31 -060034
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050035PATMAN_DIR = pathlib.Path(__file__).parent
36TEST_DATA_DIR = PATMAN_DIR / 'test/'
Maxim Cournoyer0331edb2022-12-19 17:32:39 -050037
Maxim Cournoyer0331edb2022-12-19 17:32:39 -050038
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050039@contextlib.contextmanager
40def directory_excursion(directory):
41 """Change directory to `directory` for a limited to the context block."""
42 current = os.getcwd()
43 try:
44 os.chdir(directory)
45 yield
46 finally:
47 os.chdir(current)
48
Maxim Cournoyer0331edb2022-12-19 17:32:39 -050049
Simon Glassdf1bc5c2017-05-29 15:31:31 -060050class TestFunctional(unittest.TestCase):
Simon Glasseb209e52020-10-29 21:46:15 -060051 """Functional tests for checking that patman behaves correctly"""
Simon Glass06202d62020-10-29 21:46:27 -060052 leb = (b'Lord Edmund Blackadd\xc3\xabr <weasel@blackadder.org>'.
53 decode('utf-8'))
Simon Glass3b762cc2020-10-29 21:46:28 -060054 fred = 'Fred Bloggs <f.bloggs@napier.net>'
55 joe = 'Joe Bloggs <joe@napierwallies.co.nz>'
56 mary = 'Mary Bloggs <mary@napierwallies.co.nz>'
Simon Glass3db916d2020-10-29 21:46:35 -060057 commits = None
58 patches = None
Simon Glassed831d12025-04-29 07:22:10 -060059 verbosity = False
60 preserve_outdirs = False
61
Simon Glassb8ca4692025-05-08 05:26:16 +020062 # Fake patchwork info for testing
63 SERIES_ID_SECOND_V1 = 456
64 TITLE_SECOND = 'Series for my board'
65
Simon Glassed831d12025-04-29 07:22:10 -060066 @classmethod
67 def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False,
68 toolpath=None, verbosity=None, no_capture=False):
69 """Accept arguments controlling test execution
70
71 Args:
72 preserve_indir: not used
73 preserve_outdir: Preserve the output directories used by tests.
74 Each test has its own, so this is normally only useful when
75 running a single test.
76 toolpath: not used
77 """
78 cls.preserve_outdirs = preserve_outdirs
79 cls.toolpath = toolpath
80 cls.verbosity = verbosity
81 cls.no_capture = no_capture
Simon Glass06202d62020-10-29 21:46:27 -060082
Simon Glassdf1bc5c2017-05-29 15:31:31 -060083 def setUp(self):
84 self.tmpdir = tempfile.mkdtemp(prefix='patman.')
Simon Glass41dfb6e2025-05-08 05:13:35 +020085 self.gitdir = os.path.join(self.tmpdir, '.git')
Simon Glass54f1c5b2020-07-05 21:41:50 -060086 self.repo = None
Simon Glassdf1bc5c2017-05-29 15:31:31 -060087
88 def tearDown(self):
Simon Glassed831d12025-04-29 07:22:10 -060089 if self.preserve_outdirs:
90 print(f'Output dir: {self.tmpdir}')
91 else:
92 shutil.rmtree(self.tmpdir)
Simon Glass02811582022-01-29 14:14:18 -070093 terminal.set_print_test_mode(False)
Simon Glassdf1bc5c2017-05-29 15:31:31 -060094
95 @staticmethod
Simon Glasseb209e52020-10-29 21:46:15 -060096 def _get_path(fname):
97 """Get the path to a test file
98
99 Args:
100 fname (str): Filename to obtain
101
102 Returns:
103 str: Full path to file in the test directory
104 """
Maxim Cournoyer0331edb2022-12-19 17:32:39 -0500105 return TEST_DATA_DIR / fname
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600106
107 @classmethod
Simon Glasseb209e52020-10-29 21:46:15 -0600108 def _get_text(cls, fname):
109 """Read a file as text
110
111 Args:
112 fname (str): Filename to read
113
114 Returns:
115 str: Contents of file
116 """
117 return open(cls._get_path(fname), encoding='utf-8').read()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600118
119 @classmethod
Simon Glasseb209e52020-10-29 21:46:15 -0600120 def _get_patch_name(cls, subject):
121 """Get the filename of a patch given its subject
122
123 Args:
124 subject (str): Patch subject
125
126 Returns:
127 str: Filename for that patch
128 """
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600129 fname = re.sub('[ :]', '-', subject)
130 return fname.replace('--', '-')
131
Simon Glasseb209e52020-10-29 21:46:15 -0600132 def _create_patches_for_test(self, series):
133 """Create patch files for use by tests
134
135 This copies patch files from the test directory as needed by the series
136
137 Args:
138 series (Series): Series containing commits to convert
139
140 Returns:
141 tuple:
142 str: Cover-letter filename, or None if none
143 fname_list: list of str, each a patch filename
144 """
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600145 cover_fname = None
146 fname_list = []
147 for i, commit in enumerate(series.commits):
Simon Glasseb209e52020-10-29 21:46:15 -0600148 clean_subject = self._get_patch_name(commit.subject)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600149 src_fname = '%04d-%s.patch' % (i + 1, clean_subject[:52])
150 fname = os.path.join(self.tmpdir, src_fname)
Simon Glasseb209e52020-10-29 21:46:15 -0600151 shutil.copy(self._get_path(src_fname), fname)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600152 fname_list.append(fname)
153 if series.get('cover'):
154 src_fname = '0000-cover-letter.patch'
155 cover_fname = os.path.join(self.tmpdir, src_fname)
156 fname = os.path.join(self.tmpdir, src_fname)
Simon Glasseb209e52020-10-29 21:46:15 -0600157 shutil.copy(self._get_path(src_fname), fname)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600158
159 return cover_fname, fname_list
160
Simon Glassd85bb8f2022-01-29 14:14:09 -0700161 def test_basic(self):
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600162 """Tests the basic flow of patman
163
164 This creates a series from some hard-coded patches build from a simple
165 tree with the following metadata in the top commit:
166
167 Series-to: u-boot
168 Series-prefix: RFC
Sean Andersondc1cd132021-10-22 19:07:04 -0400169 Series-postfix: some-branch
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600170 Series-cc: Stefan Brüns <stefan.bruens@rwth-aachen.de>
171 Cover-letter-cc: Lord Mëlchett <clergy@palace.gov>
Sean Andersoncf13b862020-05-04 16:28:36 -0400172 Series-version: 3
173 Patch-cc: fred
174 Series-process-log: sort, uniq
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600175 Series-changes: 4
176 - Some changes
Sean Andersoncf13b862020-05-04 16:28:36 -0400177 - Multi
178 line
179 change
180
181 Commit-changes: 2
182 - Changes only for this commit
183
Simon Glassf1aab6f2025-04-29 07:22:07 -0600184 Cover-changes: 4
Sean Andersoncf13b862020-05-04 16:28:36 -0400185 - Some notes for the cover letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600186
187 Cover-letter:
188 test: A test patch series
189 This is a test of how the cover
Sean Andersoncf13b862020-05-04 16:28:36 -0400190 letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600191 works
192 END
193
194 and this in the first commit:
195
Sean Andersoncf13b862020-05-04 16:28:36 -0400196 Commit-changes: 2
197 - second revision change
198
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600199 Series-notes:
200 some notes
201 about some things
202 from the first commit
203 END
204
205 Commit-notes:
206 Some notes about
207 the first commit
208 END
209
210 with the following commands:
211
212 git log -n2 --reverse >/path/to/tools/patman/test/test01.txt
213 git format-patch --subject-prefix RFC --cover-letter HEAD~2
214 mv 00* /path/to/tools/patman/test
215
216 It checks these aspects:
217 - git log can be processed by patchstream
218 - emailing patches uses the correct command
219 - CC file has information on each commit
220 - cover letter has the expected text and subject
221 - each patch has the correct subject
222 - dry-run information prints out correctly
223 - unicode is handled correctly
Sean Andersondc1cd132021-10-22 19:07:04 -0400224 - Series-to, Series-cc, Series-prefix, Series-postfix, Cover-letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600225 - Cover-letter-cc, Series-version, Series-changes, Series-notes
226 - Commit-notes
227 """
228 process_tags = True
Simon Glass1f975b92021-01-23 08:56:15 -0700229 ignore_bad_tags = False
Simon Glassb3080ec2025-05-08 04:58:49 +0200230 stefan = (b'Stefan Br\xc3\xbcns <stefan.bruens@rwth-aachen.de>'
231 .decode('utf-8'))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600232 rick = 'Richard III <richard@palace.gov>'
Simon Glass4f817892019-05-14 15:53:53 -0600233 mel = b'Lord M\xc3\xablchett <clergy@palace.gov>'.decode('utf-8')
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600234 add_maintainers = [stefan, rick]
235 dry_run = True
236 in_reply_to = mel
237 count = 2
Simon Glass5efa3662025-04-07 22:51:45 +1200238 alias = {
Simon Glass95745aa2020-10-29 21:46:13 -0600239 'fdt': ['simon'],
240 'u-boot': ['u-boot@lists.denx.de'],
Simon Glass06202d62020-10-29 21:46:27 -0600241 'simon': [self.leb],
Simon Glass3b762cc2020-10-29 21:46:28 -0600242 'fred': [self.fred],
Sean Anderson25978092024-04-18 22:36:31 -0400243 'joe': [self.joe],
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600244 }
245
Simon Glasseb209e52020-10-29 21:46:15 -0600246 text = self._get_text('test01.txt')
Simon Glass93f61c02020-10-29 21:46:19 -0600247 series = patchstream.get_metadata_for_test(text)
Simon Glass414f1e02025-02-27 12:27:30 -0700248 series.base_commit = Commit('1a44532')
249 series.branch = 'mybranch'
Simon Glasseb209e52020-10-29 21:46:15 -0600250 cover_fname, args = self._create_patches_for_test(series)
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500251 get_maintainer_script = str(pathlib.Path(__file__).parent.parent.parent
252 / 'get_maintainer.pl') + ' --norolestats'
Simon Glass14d64e32025-04-29 07:21:59 -0600253 with terminal.capture() as out:
Simon Glass93f61c02020-10-29 21:46:19 -0600254 patchstream.fix_patches(series, args)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600255 if cover_fname and series.get('cover'):
Simon Glass93f61c02020-10-29 21:46:19 -0600256 patchstream.insert_cover_letter(cover_fname, series, count)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600257 series.DoChecks()
258 cc_file = series.MakeCcFile(process_tags, cover_fname,
Chris Packhamb84fb482018-06-07 20:45:06 +1200259 not ignore_bad_tags, add_maintainers,
Simon Glass9938b7b2025-04-07 22:51:46 +1200260 None, get_maintainer_script, alias)
Simon Glass761648b2022-01-29 14:14:11 -0700261 cmd = gitutil.email_patches(
Simon Glass95745aa2020-10-29 21:46:13 -0600262 series, cover_fname, args, dry_run, not ignore_bad_tags,
Simon Glass5efa3662025-04-07 22:51:45 +1200263 cc_file, alias, in_reply_to=in_reply_to, thread=None)
Simon Glass32f12a7e2025-04-07 22:51:47 +1200264 series.ShowActions(args, cmd, process_tags, alias)
Simon Glassf544a2d2019-10-31 07:42:51 -0600265 cc_lines = open(cc_file, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600266 os.remove(cc_file)
267
Simon Glassb3080ec2025-05-08 04:58:49 +0200268 itr = iter(out[0].getvalue().splitlines())
Simon Glass42e3d392020-10-29 21:46:29 -0600269 self.assertEqual('Cleaned %s patches' % len(series.commits),
Simon Glassb3080ec2025-05-08 04:58:49 +0200270 next(itr))
271 self.assertEqual('Change log missing for v2', next(itr))
272 self.assertEqual('Change log missing for v3', next(itr))
273 self.assertEqual('Change log for unknown version v4', next(itr))
274 self.assertEqual("Alias 'pci' not found", next(itr))
275 while next(itr) != 'Cc processing complete':
Simon Glass620639c2023-03-08 10:52:54 -0800276 pass
Simon Glassb3080ec2025-05-08 04:58:49 +0200277 self.assertIn('Dry run', next(itr))
278 self.assertEqual('', next(itr))
279 self.assertIn('Send a total of %d patches' % count, next(itr))
280 prev = next(itr)
Simon Glass42e3d392020-10-29 21:46:29 -0600281 for i, commit in enumerate(series.commits):
282 self.assertEqual(' %s' % args[i], prev)
283 while True:
Simon Glassb3080ec2025-05-08 04:58:49 +0200284 prev = next(itr)
Simon Glass42e3d392020-10-29 21:46:29 -0600285 if 'Cc:' not in prev:
286 break
287 self.assertEqual('To: u-boot@lists.denx.de', prev)
Simon Glassb3080ec2025-05-08 04:58:49 +0200288 self.assertEqual('Cc: %s' % stefan, next(itr))
289 self.assertEqual('Version: 3', next(itr))
290 self.assertEqual('Prefix:\t RFC', next(itr))
291 self.assertEqual('Postfix:\t some-branch', next(itr))
292 self.assertEqual('Cover: 4 lines', next(itr))
293 self.assertEqual(' Cc: %s' % self.fred, next(itr))
294 self.assertEqual(' Cc: %s' % self.joe, next(itr))
Simon Glass9dfb3112020-11-08 20:36:18 -0700295 self.assertEqual(' Cc: %s' % self.leb,
Simon Glassb3080ec2025-05-08 04:58:49 +0200296 next(itr))
297 self.assertEqual(' Cc: %s' % mel, next(itr))
298 self.assertEqual(' Cc: %s' % rick, next(itr))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600299 expected = ('Git command: git send-email --annotate '
Simon Glassa8ba0792025-05-08 04:38:30 +0200300 '--in-reply-to="%s" --to u-boot@lists.denx.de '
Simon Glass1ee91c12020-11-03 13:54:10 -0700301 '--cc "%s" --cc-cmd "%s send --cc-cmd %s" %s %s'
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600302 % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname,
Simon Glass4f817892019-05-14 15:53:53 -0600303 ' '.join(args)))
Simon Glassb3080ec2025-05-08 04:58:49 +0200304 self.assertEqual(expected, next(itr))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600305
Simon Glass9dfb3112020-11-08 20:36:18 -0700306 self.assertEqual(('%s %s\0%s' % (args[0], rick, stefan)), cc_lines[0])
Simon Glass95745aa2020-10-29 21:46:13 -0600307 self.assertEqual(
Sean Anderson25978092024-04-18 22:36:31 -0400308 '%s %s\0%s\0%s\0%s\0%s' % (args[1], self.fred, self.joe, self.leb,
309 rick, stefan),
Simon Glass9dfb3112020-11-08 20:36:18 -0700310 cc_lines[1])
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600311
312 expected = '''
313This is a test of how the cover
Sean Andersoncf13b862020-05-04 16:28:36 -0400314letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600315works
316
317some notes
318about some things
319from the first commit
320
321Changes in v4:
Sean Andersoncf13b862020-05-04 16:28:36 -0400322- Multi
323 line
324 change
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600325- Some changes
Sean Andersoncf13b862020-05-04 16:28:36 -0400326- Some notes for the cover letter
Sean Andersone45678c2024-04-18 22:36:32 -0400327- fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600328
329Simon Glass (2):
330 pci: Correct cast for sandbox
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530331 fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600332
333 cmd/pci.c | 3 ++-
334 fs/fat/fat.c | 1 +
335 lib/efi_loader/efi_memory.c | 1 +
336 lib/fdtdec.c | 3 ++-
337 4 files changed, 6 insertions(+), 2 deletions(-)
338
339--\x20
3402.7.4
341
Simon Glass414f1e02025-02-27 12:27:30 -0700342base-commit: 1a44532
343branch: mybranch
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600344'''
Simon Glassf544a2d2019-10-31 07:42:51 -0600345 lines = open(cover_fname, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600346 self.assertEqual(
Sean Andersondc1cd132021-10-22 19:07:04 -0400347 'Subject: [RFC PATCH some-branch v3 0/2] test: A test patch series',
Simon Glass95745aa2020-10-29 21:46:13 -0600348 lines[3])
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600349 self.assertEqual(expected.splitlines(), lines[7:])
350
351 for i, fname in enumerate(args):
Simon Glassf544a2d2019-10-31 07:42:51 -0600352 lines = open(fname, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600353 subject = [line for line in lines if line.startswith('Subject')]
354 self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count),
355 subject[0][:18])
Sean Andersoncf13b862020-05-04 16:28:36 -0400356
357 # Check that we got our commit notes
358 start = 0
359 expected = ''
360
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600361 if i == 0:
Sean Andersoncf13b862020-05-04 16:28:36 -0400362 start = 17
363 expected = '''---
364Some notes about
365the first commit
366
367(no changes since v2)
368
369Changes in v2:
370- second revision change'''
371 elif i == 1:
372 start = 17
373 expected = '''---
374
375Changes in v4:
376- Multi
377 line
378 change
Sean Andersone45678c2024-04-18 22:36:32 -0400379- New
Sean Andersoncf13b862020-05-04 16:28:36 -0400380- Some changes
381
382Changes in v2:
383- Changes only for this commit'''
384
385 if expected:
386 expected = expected.splitlines()
387 self.assertEqual(expected, lines[start:(start+len(expected))])
Simon Glass54f1c5b2020-07-05 21:41:50 -0600388
Simon Glassda1a6ec2025-03-28 07:02:20 -0600389 def test_base_commit(self):
390 """Test adding a base commit with no cover letter"""
391 orig_text = self._get_text('test01.txt')
392 pos = orig_text.index('commit 5ab48490f03051875ab13d288a4bf32b507d76fd')
393 text = orig_text[:pos]
394 series = patchstream.get_metadata_for_test(text)
395 series.base_commit = Commit('1a44532')
396 series.branch = 'mybranch'
397 cover_fname, args = self._create_patches_for_test(series)
398 self.assertFalse(cover_fname)
Simon Glass14d64e32025-04-29 07:21:59 -0600399 with terminal.capture() as out:
Simon Glassda1a6ec2025-03-28 07:02:20 -0600400 patchstream.fix_patches(series, args, insert_base_commit=True)
401 self.assertEqual('Cleaned 1 patch\n', out[0].getvalue())
402 lines = tools.read_file(args[0], binary=False).splitlines()
403 pos = lines.index('-- ')
404
405 # We expect these lines at the end:
406 # -- (with trailing space)
407 # 2.7.4
408 # (empty)
409 # base-commit: xxx
410 # branch: xxx
411 self.assertEqual('base-commit: 1a44532', lines[pos + 3])
412 self.assertEqual('branch: mybranch', lines[pos + 4])
413
Simon Glass54f1c5b2020-07-05 21:41:50 -0600414 def make_commit_with_file(self, subject, body, fname, text):
415 """Create a file and add it to the git repo with a new commit
416
417 Args:
418 subject (str): Subject for the commit
419 body (str): Body text of the commit
420 fname (str): Filename of file to create
421 text (str): Text to put into the file
422 """
Simon Glass41dfb6e2025-05-08 05:13:35 +0200423 path = os.path.join(self.tmpdir, fname)
Simon Glass80025522022-01-29 14:14:04 -0700424 tools.write_file(path, text, binary=False)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600425 index = self.repo.index
426 index.add(fname)
Simon Glass547cba62022-02-11 13:23:18 -0700427 # pylint doesn't seem to find this
428 # pylint: disable=E1101
Simon Glass95745aa2020-10-29 21:46:13 -0600429 author = pygit2.Signature('Test user', 'test@email.com')
Simon Glass54f1c5b2020-07-05 21:41:50 -0600430 committer = author
431 tree = index.write_tree()
432 message = subject + '\n' + body
433 self.repo.create_commit('HEAD', author, committer, message, tree,
434 [self.repo.head.target])
435
436 def make_git_tree(self):
437 """Make a simple git tree suitable for testing
438
439 It has three branches:
440 'base' has two commits: PCI, main
441 'first' has base as upstream and two more commits: I2C, SPI
442 'second' has base as upstream and three more: video, serial, bootm
443
444 Returns:
Simon Glasseb209e52020-10-29 21:46:15 -0600445 pygit2.Repository: repository
Simon Glass54f1c5b2020-07-05 21:41:50 -0600446 """
447 repo = pygit2.init_repository(self.gitdir)
448 self.repo = repo
449 new_tree = repo.TreeBuilder().write()
450
Simon Glass547cba62022-02-11 13:23:18 -0700451 # pylint doesn't seem to find this
452 # pylint: disable=E1101
Simon Glass54f1c5b2020-07-05 21:41:50 -0600453 author = pygit2.Signature('Test user', 'test@email.com')
454 committer = author
Simon Glasseb209e52020-10-29 21:46:15 -0600455 _ = repo.create_commit('HEAD', author, committer, 'Created master',
456 new_tree, [])
Simon Glass54f1c5b2020-07-05 21:41:50 -0600457
458 self.make_commit_with_file('Initial commit', '''
459Add a README
460
461''', 'README', '''This is the README file
462describing this project
463in very little detail''')
464
465 self.make_commit_with_file('pci: PCI implementation', '''
466Here is a basic PCI implementation
467
468''', 'pci.c', '''This is a file
469it has some contents
470and some more things''')
471 self.make_commit_with_file('main: Main program', '''
472Hello here is the second commit.
473''', 'main.c', '''This is the main file
474there is very little here
475but we can always add more later
476if we want to
477
478Series-to: u-boot
479Series-cc: Barry Crump <bcrump@whataroa.nz>
480''')
481 base_target = repo.revparse_single('HEAD')
482 self.make_commit_with_file('i2c: I2C things', '''
483This has some stuff to do with I2C
484''', 'i2c.c', '''And this is the file contents
485with some I2C-related things in it''')
486 self.make_commit_with_file('spi: SPI fixes', '''
487SPI needs some fixes
488and here they are
Simon Glassd0a0a582020-10-29 21:46:36 -0600489
490Signed-off-by: %s
491
492Series-to: u-boot
493Commit-notes:
494title of the series
495This is the cover letter for the series
496with various details
497END
498''' % self.leb, 'spi.c', '''Some fixes for SPI in this
Simon Glass54f1c5b2020-07-05 21:41:50 -0600499file to make SPI work
500better than before''')
501 first_target = repo.revparse_single('HEAD')
502
503 target = repo.revparse_single('HEAD~2')
Simon Glass547cba62022-02-11 13:23:18 -0700504 # pylint doesn't seem to find this
505 # pylint: disable=E1101
Simon Glass54f1c5b2020-07-05 21:41:50 -0600506 repo.reset(target.oid, pygit2.GIT_CHECKOUT_FORCE)
507 self.make_commit_with_file('video: Some video improvements', '''
508Fix up the video so that
509it looks more purple. Purple is
510a very nice colour.
511''', 'video.c', '''More purple here
512Purple and purple
513Even more purple
514Could not be any more purple''')
Simon Glassb8ca4692025-05-08 05:26:16 +0200515 self.make_commit_with_file('serial: Add a serial driver', f'''
Simon Glass54f1c5b2020-07-05 21:41:50 -0600516Here is the serial driver
517for my chip.
518
519Cover-letter:
Simon Glassb8ca4692025-05-08 05:26:16 +0200520{self.TITLE_SECOND}
Simon Glass54f1c5b2020-07-05 21:41:50 -0600521This series implements support
522for my glorious board.
523END
Simon Glassb8ca4692025-05-08 05:26:16 +0200524Series-to: u-boot
525Series-links: {self.SERIES_ID_SECOND_V1}
Simon Glass54f1c5b2020-07-05 21:41:50 -0600526''', 'serial.c', '''The code for the
527serial driver is here''')
528 self.make_commit_with_file('bootm: Make it boot', '''
529This makes my board boot
530with a fix to the bootm
531command
532''', 'bootm.c', '''Fix up the bootm
533command to make the code as
534complicated as possible''')
535 second_target = repo.revparse_single('HEAD')
536
537 repo.branches.local.create('first', first_target)
538 repo.config.set_multivar('branch.first.remote', '', '.')
539 repo.config.set_multivar('branch.first.merge', '', 'refs/heads/base')
540
541 repo.branches.local.create('second', second_target)
542 repo.config.set_multivar('branch.second.remote', '', '.')
543 repo.config.set_multivar('branch.second.merge', '', 'refs/heads/base')
544
545 repo.branches.local.create('base', base_target)
546 return repo
547
Simon Glassd85bb8f2022-01-29 14:14:09 -0700548 def test_branch(self):
Simon Glass54f1c5b2020-07-05 21:41:50 -0600549 """Test creating patches from a branch"""
550 repo = self.make_git_tree()
551 target = repo.lookup_reference('refs/heads/first')
Simon Glass547cba62022-02-11 13:23:18 -0700552 # pylint doesn't seem to find this
553 # pylint: disable=E1101
Simon Glass54f1c5b2020-07-05 21:41:50 -0600554 self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
555 control.setup()
Heinrich Schuchardtd01d6672023-04-20 20:07:29 +0200556 orig_dir = os.getcwd()
Simon Glass54f1c5b2020-07-05 21:41:50 -0600557 try:
Simon Glass41dfb6e2025-05-08 05:13:35 +0200558 os.chdir(self.tmpdir)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600559
560 # Check that it can detect the current branch
Simon Glass761648b2022-01-29 14:14:11 -0700561 self.assertEqual(2, gitutil.count_commits_to_branch(None))
Simon Glass54f1c5b2020-07-05 21:41:50 -0600562 col = terminal.Color()
Simon Glass14d64e32025-04-29 07:21:59 -0600563 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600564 _, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600565 col, branch=None, count=-1, start=0, end=0,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100566 ignore_binary=False, signoff=True)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600567 self.assertIsNone(cover_fname)
568 self.assertEqual(2, len(patch_files))
Simon Glass2eb4da72020-07-05 21:41:51 -0600569
570 # Check that it can detect a different branch
Simon Glass761648b2022-01-29 14:14:11 -0700571 self.assertEqual(3, gitutil.count_commits_to_branch('second'))
Simon Glass14d64e32025-04-29 07:21:59 -0600572 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600573 series, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600574 col, branch='second', count=-1, start=0, end=0,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100575 ignore_binary=False, signoff=True)
Simon Glass2eb4da72020-07-05 21:41:51 -0600576 self.assertIsNotNone(cover_fname)
577 self.assertEqual(3, len(patch_files))
Simon Glassb3bf4e12020-07-05 21:41:52 -0600578
Simon Glass414f1e02025-02-27 12:27:30 -0700579 cover = tools.read_file(cover_fname, binary=False)
580 lines = cover.splitlines()[-2:]
581 base = repo.lookup_reference('refs/heads/base').target
582 self.assertEqual(f'base-commit: {base}', lines[0])
583 self.assertEqual('branch: second', lines[1])
584
Simon Glassda1a6ec2025-03-28 07:02:20 -0600585 # Make sure that the base-commit is not present when it is in the
586 # cover letter
587 for fname in patch_files:
588 self.assertNotIn(b'base-commit:', tools.read_file(fname))
589
Simon Glassb3bf4e12020-07-05 21:41:52 -0600590 # Check that it can skip patches at the end
Simon Glass14d64e32025-04-29 07:21:59 -0600591 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600592 _, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600593 col, branch='second', count=-1, start=0, end=1,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100594 ignore_binary=False, signoff=True)
Simon Glassb3bf4e12020-07-05 21:41:52 -0600595 self.assertIsNotNone(cover_fname)
596 self.assertEqual(2, len(patch_files))
Simon Glass414f1e02025-02-27 12:27:30 -0700597
598 cover = tools.read_file(cover_fname, binary=False)
599 lines = cover.splitlines()[-2:]
600 base2 = repo.lookup_reference('refs/heads/second')
601 ref = base2.peel(pygit2.GIT_OBJ_COMMIT).parents[0].parents[0].id
602 self.assertEqual(f'base-commit: {ref}', lines[0])
603 self.assertEqual('branch: second', lines[1])
Simon Glass54f1c5b2020-07-05 21:41:50 -0600604 finally:
605 os.chdir(orig_dir)
Simon Glass06202d62020-10-29 21:46:27 -0600606
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500607 def test_custom_get_maintainer_script(self):
608 """Validate that a custom get_maintainer script gets used."""
609 self.make_git_tree()
Simon Glass41dfb6e2025-05-08 05:13:35 +0200610 with directory_excursion(self.tmpdir):
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500611 # Setup git.
612 os.environ['GIT_CONFIG_GLOBAL'] = '/dev/null'
613 os.environ['GIT_CONFIG_SYSTEM'] = '/dev/null'
614 tools.run('git', 'config', 'user.name', 'Dummy')
615 tools.run('git', 'config', 'user.email', 'dumdum@dummy.com')
616 tools.run('git', 'branch', 'upstream')
617 tools.run('git', 'branch', '--set-upstream-to=upstream')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500618
619 # Setup patman configuration.
620 with open('.patman', 'w', buffering=1) as f:
621 f.write('[settings]\n'
622 'get_maintainer_script: dummy-script.sh\n'
Sean Andersona06df742024-04-18 22:36:30 -0400623 'check_patch: False\n'
624 'add_maintainers: True\n')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500625 with open('dummy-script.sh', 'w', buffering=1) as f:
626 f.write('#!/usr/bin/env python\n'
627 'print("hello@there.com")\n')
628 os.chmod('dummy-script.sh', 0x555)
Simon Glass41dfb6e2025-05-08 05:13:35 +0200629 tools.run('git', 'add', '.')
630 tools.run('git', 'commit', '-m', 'new commit')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500631
632 # Finally, do the test
Simon Glass14d64e32025-04-29 07:21:59 -0600633 with terminal.capture():
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500634 output = tools.run(PATMAN_DIR / 'patman', '--dry-run')
635 # Assert the email address is part of the dry-run
636 # output.
637 self.assertIn('hello@there.com', output)
638
Simon Glassd85bb8f2022-01-29 14:14:09 -0700639 def test_tags(self):
Simon Glass06202d62020-10-29 21:46:27 -0600640 """Test collection of tags in a patchstream"""
641 text = '''This is a patch
642
643Signed-off-by: Terminator
Simon Glass3b762cc2020-10-29 21:46:28 -0600644Reviewed-by: %s
645Reviewed-by: %s
Simon Glass06202d62020-10-29 21:46:27 -0600646Tested-by: %s
Simon Glass3b762cc2020-10-29 21:46:28 -0600647''' % (self.joe, self.mary, self.leb)
Simon Glass06202d62020-10-29 21:46:27 -0600648 pstrm = PatchStream.process_text(text)
649 self.assertEqual(pstrm.commit.rtags, {
Simon Glass3b762cc2020-10-29 21:46:28 -0600650 'Reviewed-by': {self.joe, self.mary},
Simon Glass06202d62020-10-29 21:46:27 -0600651 'Tested-by': {self.leb}})
Simon Glass3b762cc2020-10-29 21:46:28 -0600652
Simon Glassd85bb8f2022-01-29 14:14:09 -0700653 def test_invalid_tag(self):
Patrick Delaunay6bbdd0c2021-07-22 16:51:42 +0200654 """Test invalid tag in a patchstream"""
655 text = '''This is a patch
656
657Serie-version: 2
658'''
659 with self.assertRaises(ValueError) as exc:
660 pstrm = PatchStream.process_text(text)
661 self.assertEqual("Line 3: Invalid tag = 'Serie-version: 2'",
662 str(exc.exception))
663
Simon Glassd85bb8f2022-01-29 14:14:09 -0700664 def test_missing_end(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600665 """Test a missing END tag"""
666 text = '''This is a patch
667
668Cover-letter:
669This is the title
670missing END after this line
671Signed-off-by: Fred
672'''
673 pstrm = PatchStream.process_text(text)
674 self.assertEqual(["Missing 'END' in section 'cover'"],
675 pstrm.commit.warn)
676
Simon Glassd85bb8f2022-01-29 14:14:09 -0700677 def test_missing_blank_line(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600678 """Test a missing blank line after a tag"""
679 text = '''This is a patch
680
681Series-changes: 2
682- First line of changes
683- Missing blank line after this line
684Signed-off-by: Fred
685'''
686 pstrm = PatchStream.process_text(text)
687 self.assertEqual(["Missing 'blank line' in section 'Series-changes'"],
688 pstrm.commit.warn)
689
Simon Glassd85bb8f2022-01-29 14:14:09 -0700690 def test_invalid_commit_tag(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600691 """Test an invalid Commit-xxx tag"""
692 text = '''This is a patch
693
694Commit-fred: testing
695'''
696 pstrm = PatchStream.process_text(text)
697 self.assertEqual(["Line 3: Ignoring Commit-fred"], pstrm.commit.warn)
698
Simon Glassd85bb8f2022-01-29 14:14:09 -0700699 def test_self_test(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600700 """Test a tested by tag by this user"""
701 test_line = 'Tested-by: %s@napier.com' % os.getenv('USER')
702 text = '''This is a patch
703
704%s
705''' % test_line
706 pstrm = PatchStream.process_text(text)
707 self.assertEqual(["Ignoring '%s'" % test_line], pstrm.commit.warn)
708
Simon Glassd85bb8f2022-01-29 14:14:09 -0700709 def test_space_before_tab(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600710 """Test a space before a tab"""
711 text = '''This is a patch
712
713+ \tSomething
714'''
715 pstrm = PatchStream.process_text(text)
716 self.assertEqual(["Line 3/0 has space before tab"], pstrm.commit.warn)
717
Simon Glassd85bb8f2022-01-29 14:14:09 -0700718 def test_lines_after_test(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600719 """Test detecting lines after TEST= line"""
720 text = '''This is a patch
721
722TEST=sometest
723more lines
724here
725'''
726 pstrm = PatchStream.process_text(text)
727 self.assertEqual(["Found 2 lines after TEST="], pstrm.commit.warn)
728
Simon Glassd85bb8f2022-01-29 14:14:09 -0700729 def test_blank_line_at_end(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600730 """Test detecting a blank line at the end of a file"""
731 text = '''This is a patch
732
733diff --git a/lib/fdtdec.c b/lib/fdtdec.c
734index c072e54..942244f 100644
735--- a/lib/fdtdec.c
736+++ b/lib/fdtdec.c
737@@ -1200,7 +1200,8 @@ int fdtdec_setup_mem_size_base(void)
738 }
739
740 gd->ram_size = (phys_size_t)(res.end - res.start + 1);
741- debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
742+ debug("%s: Initial DRAM size %llx\n", __func__,
743+ (unsigned long long)gd->ram_size);
744+
745diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
746
747--
7482.7.4
749
750 '''
751 pstrm = PatchStream.process_text(text)
752 self.assertEqual(
753 ["Found possible blank line(s) at end of file 'lib/fdtdec.c'"],
754 pstrm.commit.warn)
Simon Glass1c1f2072020-10-29 21:46:34 -0600755
Simon Glassd85bb8f2022-01-29 14:14:09 -0700756 def test_no_upstream(self):
Simon Glass1c1f2072020-10-29 21:46:34 -0600757 """Test CountCommitsToBranch when there is no upstream"""
758 repo = self.make_git_tree()
759 target = repo.lookup_reference('refs/heads/base')
Simon Glass547cba62022-02-11 13:23:18 -0700760 # pylint doesn't seem to find this
761 # pylint: disable=E1101
Simon Glass1c1f2072020-10-29 21:46:34 -0600762 self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
763
764 # Check that it can detect the current branch
Heinrich Schuchardtd01d6672023-04-20 20:07:29 +0200765 orig_dir = os.getcwd()
Simon Glass1c1f2072020-10-29 21:46:34 -0600766 try:
Simon Glass1c1f2072020-10-29 21:46:34 -0600767 os.chdir(self.gitdir)
768 with self.assertRaises(ValueError) as exc:
Simon Glass761648b2022-01-29 14:14:11 -0700769 gitutil.count_commits_to_branch(None)
Simon Glass1c1f2072020-10-29 21:46:34 -0600770 self.assertIn(
771 "Failed to determine upstream: fatal: no upstream configured for branch 'base'",
772 str(exc.exception))
773 finally:
774 os.chdir(orig_dir)
Simon Glass3db916d2020-10-29 21:46:35 -0600775
776 @staticmethod
Simon Glass25b91c12025-04-29 07:22:19 -0600777 def _fake_patchwork(subpath):
Simon Glass3db916d2020-10-29 21:46:35 -0600778 """Fake Patchwork server for the function below
779
780 This handles accessing a series, providing a list consisting of a
781 single patch
Simon Glassf9b03cf2020-11-03 13:54:14 -0700782
783 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -0700784 subpath (str): URL subpath to use
Simon Glass3db916d2020-10-29 21:46:35 -0600785 """
786 re_series = re.match(r'series/(\d*)/$', subpath)
787 if re_series:
788 series_num = re_series.group(1)
789 if series_num == '1234':
790 return {'patches': [
791 {'id': '1', 'name': 'Some patch'}]}
792 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
793
Simon Glassd85bb8f2022-01-29 14:14:09 -0700794 def test_status_mismatch(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600795 """Test Patchwork patches not matching the series"""
Simon Glass25b91c12025-04-29 07:22:19 -0600796 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork)
Simon Glass14d64e32025-04-29 07:21:59 -0600797 with terminal.capture() as (_, err):
Simon Glass3729b8b2025-04-29 07:22:24 -0600798 patches = asyncio.run(status.check_status(1234, pwork))
Simon Glass27280f42025-04-29 07:22:17 -0600799 status.check_patch_count(0, len(patches))
Simon Glass3db916d2020-10-29 21:46:35 -0600800 self.assertIn('Warning: Patchwork reports 1 patches, series has 0',
801 err.getvalue())
802
Simon Glassd85bb8f2022-01-29 14:14:09 -0700803 def test_status_read_patch(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600804 """Test handling a single patch in Patchwork"""
Simon Glass25b91c12025-04-29 07:22:19 -0600805 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork)
Simon Glass3729b8b2025-04-29 07:22:24 -0600806 patches = asyncio.run(status.check_status(1234, pwork))
Simon Glass3db916d2020-10-29 21:46:35 -0600807 self.assertEqual(1, len(patches))
808 patch = patches[0]
809 self.assertEqual('1', patch.id)
810 self.assertEqual('Some patch', patch.raw_subject)
811
Simon Glassd85bb8f2022-01-29 14:14:09 -0700812 def test_parse_subject(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600813 """Test parsing of the patch subject"""
Simon Glass232eefd2025-04-29 07:22:14 -0600814 patch = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600815
816 # Simple patch not in a series
817 patch.parse_subject('Testing')
818 self.assertEqual('Testing', patch.raw_subject)
819 self.assertEqual('Testing', patch.subject)
820 self.assertEqual(1, patch.seq)
821 self.assertEqual(1, patch.count)
822 self.assertEqual(None, patch.prefix)
823 self.assertEqual(None, patch.version)
824
825 # First patch in a series
826 patch.parse_subject('[1/2] Testing')
827 self.assertEqual('[1/2] Testing', patch.raw_subject)
828 self.assertEqual('Testing', patch.subject)
829 self.assertEqual(1, patch.seq)
830 self.assertEqual(2, patch.count)
831 self.assertEqual(None, patch.prefix)
832 self.assertEqual(None, patch.version)
833
834 # Second patch in a series
835 patch.parse_subject('[2/2] Testing')
836 self.assertEqual('Testing', patch.subject)
837 self.assertEqual(2, patch.seq)
838 self.assertEqual(2, patch.count)
839 self.assertEqual(None, patch.prefix)
840 self.assertEqual(None, patch.version)
841
842 # RFC patch
843 patch.parse_subject('[RFC,3/7] Testing')
844 self.assertEqual('Testing', patch.subject)
845 self.assertEqual(3, patch.seq)
846 self.assertEqual(7, patch.count)
847 self.assertEqual('RFC', patch.prefix)
848 self.assertEqual(None, patch.version)
849
850 # Version patch
851 patch.parse_subject('[v2,3/7] Testing')
852 self.assertEqual('Testing', patch.subject)
853 self.assertEqual(3, patch.seq)
854 self.assertEqual(7, patch.count)
855 self.assertEqual(None, patch.prefix)
856 self.assertEqual('v2', patch.version)
857
858 # All fields
859 patch.parse_subject('[RESEND,v2,3/7] Testing')
860 self.assertEqual('Testing', patch.subject)
861 self.assertEqual(3, patch.seq)
862 self.assertEqual(7, patch.count)
863 self.assertEqual('RESEND', patch.prefix)
864 self.assertEqual('v2', patch.version)
865
866 # RFC only
867 patch.parse_subject('[RESEND] Testing')
868 self.assertEqual('Testing', patch.subject)
869 self.assertEqual(1, patch.seq)
870 self.assertEqual(1, patch.count)
871 self.assertEqual('RESEND', patch.prefix)
872 self.assertEqual(None, patch.version)
873
Simon Glassd85bb8f2022-01-29 14:14:09 -0700874 def test_compare_series(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600875 """Test operation of compare_with_series()"""
876 commit1 = Commit('abcd')
877 commit1.subject = 'Subject 1'
878 commit2 = Commit('ef12')
879 commit2.subject = 'Subject 2'
880 commit3 = Commit('3456')
881 commit3.subject = 'Subject 2'
882
Simon Glass232eefd2025-04-29 07:22:14 -0600883 patch1 = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600884 patch1.subject = 'Subject 1'
Simon Glass232eefd2025-04-29 07:22:14 -0600885 patch2 = patchwork.Patch('2')
Simon Glass3db916d2020-10-29 21:46:35 -0600886 patch2.subject = 'Subject 2'
Simon Glass232eefd2025-04-29 07:22:14 -0600887 patch3 = patchwork.Patch('3')
Simon Glass3db916d2020-10-29 21:46:35 -0600888 patch3.subject = 'Subject 2'
889
890 series = Series()
891 series.commits = [commit1]
892 patches = [patch1]
893 patch_for_commit, commit_for_patch, warnings = (
894 status.compare_with_series(series, patches))
895 self.assertEqual(1, len(patch_for_commit))
896 self.assertEqual(patch1, patch_for_commit[0])
897 self.assertEqual(1, len(commit_for_patch))
898 self.assertEqual(commit1, commit_for_patch[0])
899
900 series.commits = [commit1]
901 patches = [patch1, patch2]
902 patch_for_commit, commit_for_patch, warnings = (
903 status.compare_with_series(series, patches))
904 self.assertEqual(1, len(patch_for_commit))
905 self.assertEqual(patch1, patch_for_commit[0])
906 self.assertEqual(1, len(commit_for_patch))
907 self.assertEqual(commit1, commit_for_patch[0])
908 self.assertEqual(["Cannot find commit for patch 2 ('Subject 2')"],
909 warnings)
910
911 series.commits = [commit1, commit2]
912 patches = [patch1]
913 patch_for_commit, commit_for_patch, warnings = (
914 status.compare_with_series(series, patches))
915 self.assertEqual(1, len(patch_for_commit))
916 self.assertEqual(patch1, patch_for_commit[0])
917 self.assertEqual(1, len(commit_for_patch))
918 self.assertEqual(commit1, commit_for_patch[0])
919 self.assertEqual(["Cannot find patch for commit 2 ('Subject 2')"],
920 warnings)
921
922 series.commits = [commit1, commit2, commit3]
923 patches = [patch1, patch2]
924 patch_for_commit, commit_for_patch, warnings = (
925 status.compare_with_series(series, patches))
926 self.assertEqual(2, len(patch_for_commit))
927 self.assertEqual(patch1, patch_for_commit[0])
928 self.assertEqual(patch2, patch_for_commit[1])
929 self.assertEqual(1, len(commit_for_patch))
930 self.assertEqual(commit1, commit_for_patch[0])
931 self.assertEqual(["Cannot find patch for commit 3 ('Subject 2')",
932 "Multiple commits match patch 2 ('Subject 2'):\n"
933 ' Subject 2\n Subject 2'],
934 warnings)
935
936 series.commits = [commit1, commit2]
937 patches = [patch1, patch2, patch3]
938 patch_for_commit, commit_for_patch, warnings = (
939 status.compare_with_series(series, patches))
940 self.assertEqual(1, len(patch_for_commit))
941 self.assertEqual(patch1, patch_for_commit[0])
942 self.assertEqual(2, len(commit_for_patch))
943 self.assertEqual(commit1, commit_for_patch[0])
944 self.assertEqual(["Multiple patches match commit 2 ('Subject 2'):\n"
945 ' Subject 2\n Subject 2',
946 "Cannot find commit for patch 3 ('Subject 2')"],
947 warnings)
948
Simon Glass25b91c12025-04-29 07:22:19 -0600949 def _fake_patchwork2(self, subpath):
Simon Glass3db916d2020-10-29 21:46:35 -0600950 """Fake Patchwork server for the function below
951
952 This handles accessing series, patches and comments, providing the data
953 in self.patches to the caller
Simon Glassf9b03cf2020-11-03 13:54:14 -0700954
955 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -0700956 subpath (str): URL subpath to use
Simon Glass3db916d2020-10-29 21:46:35 -0600957 """
958 re_series = re.match(r'series/(\d*)/$', subpath)
959 re_patch = re.match(r'patches/(\d*)/$', subpath)
960 re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
961 if re_series:
962 series_num = re_series.group(1)
963 if series_num == '1234':
964 return {'patches': self.patches}
965 elif re_patch:
966 patch_num = int(re_patch.group(1))
967 patch = self.patches[patch_num - 1]
968 return patch
969 elif re_comments:
970 patch_num = int(re_comments.group(1))
971 patch = self.patches[patch_num - 1]
972 return patch.comments
973 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
974
Simon Glassd85bb8f2022-01-29 14:14:09 -0700975 def test_find_new_responses(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600976 """Test operation of find_new_responses()"""
977 commit1 = Commit('abcd')
978 commit1.subject = 'Subject 1'
979 commit2 = Commit('ef12')
980 commit2.subject = 'Subject 2'
981
Simon Glass232eefd2025-04-29 07:22:14 -0600982 patch1 = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600983 patch1.parse_subject('[1/2] Subject 1')
984 patch1.name = patch1.raw_subject
985 patch1.content = 'This is my patch content'
986 comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
987
988 patch1.comments = [comment1a]
989
Simon Glass232eefd2025-04-29 07:22:14 -0600990 patch2 = patchwork.Patch('2')
Simon Glass3db916d2020-10-29 21:46:35 -0600991 patch2.parse_subject('[2/2] Subject 2')
992 patch2.name = patch2.raw_subject
993 patch2.content = 'Some other patch content'
994 comment2a = {
995 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
996 (self.mary, self.leb)}
997 comment2b = {'content': 'Reviewed-by: %s' % self.fred}
998 patch2.comments = [comment2a, comment2b]
999
1000 # This test works by setting up commits and patch for use by the fake
1001 # Rest API function _fake_patchwork2(). It calls various functions in
1002 # the status module after setting up tags in the commits, checking that
1003 # things behaves as expected
1004 self.commits = [commit1, commit2]
1005 self.patches = [patch1, patch2]
1006 count = 2
Simon Glass3db916d2020-10-29 21:46:35 -06001007
1008 # Check that the tags are picked up on the first patch
Simon Glass29771962025-04-29 07:22:20 -06001009 new_rtags, _ = status.process_reviews(patch1.content, patch1.comments,
1010 commit1.rtags)
1011 self.assertEqual(new_rtags, {'Reviewed-by': {self.joe}})
Simon Glass3db916d2020-10-29 21:46:35 -06001012
1013 # Now the second patch
Simon Glass29771962025-04-29 07:22:20 -06001014 new_rtags, _ = status.process_reviews(patch2.content, patch2.comments,
1015 commit2.rtags)
1016 self.assertEqual(new_rtags, {
Simon Glass3db916d2020-10-29 21:46:35 -06001017 'Reviewed-by': {self.mary, self.fred},
1018 'Tested-by': {self.leb}})
1019
1020 # Now add some tags to the commit, which means they should not appear as
1021 # 'new' tags when scanning comments
Simon Glass3db916d2020-10-29 21:46:35 -06001022 commit1.rtags = {'Reviewed-by': {self.joe}}
Simon Glass29771962025-04-29 07:22:20 -06001023 new_rtags, _ = status.process_reviews(patch1.content, patch1.comments,
1024 commit1.rtags)
1025 self.assertEqual(new_rtags, {})
Simon Glass3db916d2020-10-29 21:46:35 -06001026
1027 # For the second commit, add Ed and Fred, so only Mary should be left
1028 commit2.rtags = {
1029 'Tested-by': {self.leb},
1030 'Reviewed-by': {self.fred}}
Simon Glass29771962025-04-29 07:22:20 -06001031 new_rtags, _ = status.process_reviews(patch2.content, patch2.comments,
1032 commit2.rtags)
1033 self.assertEqual(new_rtags, {'Reviewed-by': {self.mary}})
Simon Glass3db916d2020-10-29 21:46:35 -06001034
1035 # Check that the output patches expectations:
1036 # 1 Subject 1
1037 # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1038 # 2 Subject 2
1039 # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1040 # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1041 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1042 # 1 new response available in patchwork
1043
1044 series = Series()
1045 series.commits = [commit1, commit2]
Simon Glass02811582022-01-29 14:14:18 -07001046 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001047 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork2)
Simon Glassc100b262025-04-29 07:22:16 -06001048 status.check_and_show_status(series, '1234', None, None, False, False,
Simon Glass25b91c12025-04-29 07:22:19 -06001049 pwork)
Simon Glassb3080ec2025-05-08 04:58:49 +02001050 itr = iter(terminal.get_print_test_lines())
Simon Glass3db916d2020-10-29 21:46:35 -06001051 col = terminal.Color()
Simon Glassd4d3fb42025-04-29 07:22:21 -06001052 self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001053 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001054 self.assertEqual(
1055 terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
1056 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001057 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001058 self.assertEqual(terminal.PrintLine(self.joe, col.WHITE, bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001059 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001060
Simon Glassd4d3fb42025-04-29 07:22:21 -06001061 self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001062 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001063 self.assertEqual(
Simon Glass2112d072020-10-29 21:46:38 -06001064 terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
Simon Glass3db916d2020-10-29 21:46:35 -06001065 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001066 next(itr))
1067 self.assertEqual(terminal.PrintLine(self.fred, col.WHITE,
1068 bright=False), next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001069 self.assertEqual(
Simon Glass2112d072020-10-29 21:46:38 -06001070 terminal.PrintLine(' Tested-by: ', col.GREEN, newline=False,
Simon Glass3db916d2020-10-29 21:46:35 -06001071 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001072 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001073 self.assertEqual(terminal.PrintLine(self.leb, col.WHITE, bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001074 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001075 self.assertEqual(
1076 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001077 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001078 self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001079 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001080 self.assertEqual(terminal.PrintLine(
Simon Glassd0a0a582020-10-29 21:46:36 -06001081 '1 new response available in patchwork (use -d to write them to a new branch)',
Simon Glassb3080ec2025-05-08 04:58:49 +02001082 None), next(itr))
Simon Glassd0a0a582020-10-29 21:46:36 -06001083
Simon Glass25b91c12025-04-29 07:22:19 -06001084 def _fake_patchwork3(self, subpath):
Simon Glassd0a0a582020-10-29 21:46:36 -06001085 """Fake Patchwork server for the function below
1086
1087 This handles accessing series, patches and comments, providing the data
1088 in self.patches to the caller
Simon Glassf9b03cf2020-11-03 13:54:14 -07001089
1090 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -07001091 subpath (str): URL subpath to use
Simon Glassd0a0a582020-10-29 21:46:36 -06001092 """
1093 re_series = re.match(r'series/(\d*)/$', subpath)
1094 re_patch = re.match(r'patches/(\d*)/$', subpath)
1095 re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
1096 if re_series:
1097 series_num = re_series.group(1)
1098 if series_num == '1234':
1099 return {'patches': self.patches}
1100 elif re_patch:
1101 patch_num = int(re_patch.group(1))
1102 patch = self.patches[patch_num - 1]
1103 return patch
1104 elif re_comments:
1105 patch_num = int(re_comments.group(1))
1106 patch = self.patches[patch_num - 1]
1107 return patch.comments
1108 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
1109
Simon Glassd85bb8f2022-01-29 14:14:09 -07001110 def test_create_branch(self):
Simon Glassd0a0a582020-10-29 21:46:36 -06001111 """Test operation of create_branch()"""
1112 repo = self.make_git_tree()
1113 branch = 'first'
1114 dest_branch = 'first2'
1115 count = 2
Simon Glass41dfb6e2025-05-08 05:13:35 +02001116 gitdir = self.gitdir
Simon Glassd0a0a582020-10-29 21:46:36 -06001117
1118 # Set up the test git tree. We use branch 'first' which has two commits
1119 # in it
1120 series = patchstream.get_metadata_for_list(branch, gitdir, count)
1121 self.assertEqual(2, len(series.commits))
1122
Simon Glass232eefd2025-04-29 07:22:14 -06001123 patch1 = patchwork.Patch('1')
Simon Glassd0a0a582020-10-29 21:46:36 -06001124 patch1.parse_subject('[1/2] %s' % series.commits[0].subject)
1125 patch1.name = patch1.raw_subject
1126 patch1.content = 'This is my patch content'
1127 comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
1128
1129 patch1.comments = [comment1a]
1130
Simon Glass232eefd2025-04-29 07:22:14 -06001131 patch2 = patchwork.Patch('2')
Simon Glassd0a0a582020-10-29 21:46:36 -06001132 patch2.parse_subject('[2/2] %s' % series.commits[1].subject)
1133 patch2.name = patch2.raw_subject
1134 patch2.content = 'Some other patch content'
1135 comment2a = {
1136 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
1137 (self.mary, self.leb)}
1138 comment2b = {
1139 'content': 'Reviewed-by: %s' % self.fred}
1140 patch2.comments = [comment2a, comment2b]
1141
1142 # This test works by setting up patches for use by the fake Rest API
1143 # function _fake_patchwork3(). The fake patch comments above should
1144 # result in new review tags that are collected and added to the commits
1145 # created in the destination branch.
1146 self.patches = [patch1, patch2]
1147 count = 2
1148
1149 # Expected output:
1150 # 1 i2c: I2C things
1151 # + Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1152 # 2 spi: SPI fixes
1153 # + Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1154 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1155 # + Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1156 # 4 new responses available in patchwork
1157 # 4 responses added from patchwork into new branch 'first2'
1158 # <unittest.result.TestResult run=8 errors=0 failures=0>
1159
Simon Glass02811582022-01-29 14:14:18 -07001160 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001161 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork3)
Simon Glassc100b262025-04-29 07:22:16 -06001162 status.check_and_show_status(series, '1234', branch, dest_branch,
Simon Glass25b91c12025-04-29 07:22:19 -06001163 False, False, pwork, repo)
Simon Glass02811582022-01-29 14:14:18 -07001164 lines = terminal.get_print_test_lines()
Simon Glassd0a0a582020-10-29 21:46:36 -06001165 self.assertEqual(12, len(lines))
1166 self.assertEqual(
1167 "4 responses added from patchwork into new branch 'first2'",
1168 lines[11].text)
1169
1170 # Check that the destination branch has the new tags
1171 new_series = patchstream.get_metadata_for_list(dest_branch, gitdir,
1172 count)
1173 self.assertEqual(
1174 {'Reviewed-by': {self.joe}},
1175 new_series.commits[0].rtags)
1176 self.assertEqual(
1177 {'Tested-by': {self.leb},
1178 'Reviewed-by': {self.fred, self.mary}},
1179 new_series.commits[1].rtags)
1180
1181 # Now check the actual test of the first commit message. We expect to
1182 # see the new tags immediately below the old ones.
1183 stdout = patchstream.get_list(dest_branch, count=count, git_dir=gitdir)
Simon Glassb3080ec2025-05-08 04:58:49 +02001184 itr = iter([line.strip() for line in stdout.splitlines()
1185 if '-by:' in line])
Simon Glassd0a0a582020-10-29 21:46:36 -06001186
1187 # First patch should have the review tag
Simon Glassb3080ec2025-05-08 04:58:49 +02001188 self.assertEqual('Reviewed-by: %s' % self.joe, next(itr))
Simon Glassd0a0a582020-10-29 21:46:36 -06001189
1190 # Second patch should have the sign-off then the tested-by and two
1191 # reviewed-by tags
Simon Glassb3080ec2025-05-08 04:58:49 +02001192 self.assertEqual('Signed-off-by: %s' % self.leb, next(itr))
1193 self.assertEqual('Reviewed-by: %s' % self.fred, next(itr))
1194 self.assertEqual('Reviewed-by: %s' % self.mary, next(itr))
1195 self.assertEqual('Tested-by: %s' % self.leb, next(itr))
Simon Glassda8a2922020-10-29 21:46:37 -06001196
Simon Glassd85bb8f2022-01-29 14:14:09 -07001197 def test_parse_snippets(self):
Simon Glassda8a2922020-10-29 21:46:37 -06001198 """Test parsing of review snippets"""
1199 text = '''Hi Fred,
1200
1201This is a comment from someone.
1202
1203Something else
1204
1205On some recent date, Fred wrote:
1206> This is why I wrote the patch
1207> so here it is
1208
1209Now a comment about the commit message
1210A little more to say
1211
1212Even more
1213
1214> diff --git a/file.c b/file.c
1215> Some more code
1216> Code line 2
1217> Code line 3
1218> Code line 4
1219> Code line 5
1220> Code line 6
1221> Code line 7
1222> Code line 8
1223> Code line 9
1224
1225And another comment
1226
Simon Glassd85bb8f2022-01-29 14:14:09 -07001227> @@ -153,8 +143,13 @@ def check_patch(fname, show_types=False):
Simon Glassda8a2922020-10-29 21:46:37 -06001228> further down on the file
1229> and more code
1230> +Addition here
1231> +Another addition here
1232> codey
1233> more codey
1234
1235and another thing in same file
1236
1237> @@ -253,8 +243,13 @@
1238> with no function context
1239
1240one more thing
1241
1242> diff --git a/tools/patman/main.py b/tools/patman/main.py
1243> +line of code
1244now a very long comment in a different file
1245line2
1246line3
1247line4
1248line5
1249line6
1250line7
1251line8
1252'''
1253 pstrm = PatchStream.process_text(text, True)
1254 self.assertEqual([], pstrm.commit.warn)
1255
1256 # We expect to the filename and up to 5 lines of code context before
1257 # each comment. The 'On xxx wrote:' bit should be removed.
1258 self.assertEqual(
1259 [['Hi Fred,',
1260 'This is a comment from someone.',
1261 'Something else'],
1262 ['> This is why I wrote the patch',
1263 '> so here it is',
1264 'Now a comment about the commit message',
1265 'A little more to say', 'Even more'],
1266 ['> File: file.c', '> Code line 5', '> Code line 6',
1267 '> Code line 7', '> Code line 8', '> Code line 9',
1268 'And another comment'],
1269 ['> File: file.c',
Simon Glassd85bb8f2022-01-29 14:14:09 -07001270 '> Line: 153 / 143: def check_patch(fname, show_types=False):',
Simon Glassda8a2922020-10-29 21:46:37 -06001271 '> and more code', '> +Addition here', '> +Another addition here',
1272 '> codey', '> more codey', 'and another thing in same file'],
1273 ['> File: file.c', '> Line: 253 / 243',
1274 '> with no function context', 'one more thing'],
1275 ['> File: tools/patman/main.py', '> +line of code',
1276 'now a very long comment in a different file',
1277 'line2', 'line3', 'line4', 'line5', 'line6', 'line7', 'line8']],
1278 pstrm.snippets)
Simon Glass2112d072020-10-29 21:46:38 -06001279
Simon Glassd85bb8f2022-01-29 14:14:09 -07001280 def test_review_snippets(self):
Simon Glass2112d072020-10-29 21:46:38 -06001281 """Test showing of review snippets"""
1282 def _to_submitter(who):
1283 m_who = re.match('(.*) <(.*)>', who)
1284 return {
1285 'name': m_who.group(1),
1286 'email': m_who.group(2)
1287 }
1288
1289 commit1 = Commit('abcd')
1290 commit1.subject = 'Subject 1'
1291 commit2 = Commit('ef12')
1292 commit2.subject = 'Subject 2'
1293
Simon Glass232eefd2025-04-29 07:22:14 -06001294 patch1 = patchwork.Patch('1')
Simon Glass2112d072020-10-29 21:46:38 -06001295 patch1.parse_subject('[1/2] Subject 1')
1296 patch1.name = patch1.raw_subject
1297 patch1.content = 'This is my patch content'
1298 comment1a = {'submitter': _to_submitter(self.joe),
1299 'content': '''Hi Fred,
1300
1301On some date Fred wrote:
1302
1303> diff --git a/file.c b/file.c
1304> Some code
1305> and more code
1306
1307Here is my comment above the above...
1308
1309
1310Reviewed-by: %s
1311''' % self.joe}
1312
1313 patch1.comments = [comment1a]
1314
Simon Glass232eefd2025-04-29 07:22:14 -06001315 patch2 = patchwork.Patch('2')
Simon Glass2112d072020-10-29 21:46:38 -06001316 patch2.parse_subject('[2/2] Subject 2')
1317 patch2.name = patch2.raw_subject
1318 patch2.content = 'Some other patch content'
1319 comment2a = {
1320 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
1321 (self.mary, self.leb)}
1322 comment2b = {'submitter': _to_submitter(self.fred),
1323 'content': '''Hi Fred,
1324
1325On some date Fred wrote:
1326
1327> diff --git a/tools/patman/commit.py b/tools/patman/commit.py
1328> @@ -41,6 +41,9 @@ class Commit:
1329> self.rtags = collections.defaultdict(set)
1330> self.warn = []
1331>
1332> + def __str__(self):
1333> + return self.subject
1334> +
Simon Glassd85bb8f2022-01-29 14:14:09 -07001335> def add_change(self, version, info):
Simon Glass2112d072020-10-29 21:46:38 -06001336> """Add a new change line to the change list for a version.
1337>
1338A comment
1339
1340Reviewed-by: %s
1341''' % self.fred}
1342 patch2.comments = [comment2a, comment2b]
1343
1344 # This test works by setting up commits and patch for use by the fake
1345 # Rest API function _fake_patchwork2(). It calls various functions in
1346 # the status module after setting up tags in the commits, checking that
1347 # things behaves as expected
1348 self.commits = [commit1, commit2]
1349 self.patches = [patch1, patch2]
1350
1351 # Check that the output patches expectations:
1352 # 1 Subject 1
1353 # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1354 # 2 Subject 2
1355 # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1356 # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1357 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1358 # 1 new response available in patchwork
1359
1360 series = Series()
1361 series.commits = [commit1, commit2]
Simon Glass02811582022-01-29 14:14:18 -07001362 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001363 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork2)
Simon Glassc100b262025-04-29 07:22:16 -06001364 status.check_and_show_status(series, '1234', None, None, False, True,
Simon Glass25b91c12025-04-29 07:22:19 -06001365 pwork)
Simon Glassb3080ec2025-05-08 04:58:49 +02001366 itr = iter(terminal.get_print_test_lines())
Simon Glass2112d072020-10-29 21:46:38 -06001367 col = terminal.Color()
Simon Glassd4d3fb42025-04-29 07:22:21 -06001368 self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001369 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001370 self.assertEqual(
1371 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001372 next(itr))
1373 self.assertEqual(terminal.PrintLine(self.joe, col.WHITE), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001374
1375 self.assertEqual(terminal.PrintLine('Review: %s' % self.joe, col.RED),
Simon Glassb3080ec2025-05-08 04:58:49 +02001376 next(itr))
1377 self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(itr))
1378 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001379 self.assertEqual(terminal.PrintLine(' > File: file.c', col.MAGENTA),
Simon Glassb3080ec2025-05-08 04:58:49 +02001380 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001381 self.assertEqual(terminal.PrintLine(' > Some code', col.MAGENTA),
Simon Glassb3080ec2025-05-08 04:58:49 +02001382 next(itr))
1383 self.assertEqual(terminal.PrintLine(' > and more code',
1384 col.MAGENTA),
1385 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001386 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001387 ' Here is my comment above the above...', None), next(itr))
1388 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001389
Simon Glassd4d3fb42025-04-29 07:22:21 -06001390 self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001391 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001392 self.assertEqual(
1393 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001394 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001395 self.assertEqual(terminal.PrintLine(self.fred, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001396 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001397 self.assertEqual(
1398 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001399 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001400 self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001401 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001402 self.assertEqual(
1403 terminal.PrintLine(' + Tested-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001404 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001405 self.assertEqual(terminal.PrintLine(self.leb, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001406 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001407
1408 self.assertEqual(terminal.PrintLine('Review: %s' % self.fred, col.RED),
Simon Glassb3080ec2025-05-08 04:58:49 +02001409 next(itr))
1410 self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(itr))
1411 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001412 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001413 ' > File: tools/patman/commit.py', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001414 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001415 ' > Line: 41 / 41: class Commit:', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001416 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001417 ' > + return self.subject', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001418 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001419 ' > +', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001420 self.assertEqual(
Simon Glassb3080ec2025-05-08 04:58:49 +02001421 terminal.PrintLine(
1422 ' > def add_change(self, version, info):',
1423 col.MAGENTA),
1424 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001425 self.assertEqual(terminal.PrintLine(
1426 ' > """Add a new change line to the change list for a version.',
Simon Glassb3080ec2025-05-08 04:58:49 +02001427 col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001428 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001429 ' >', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001430 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001431 ' A comment', None), next(itr))
1432 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001433
1434 self.assertEqual(terminal.PrintLine(
1435 '4 new responses available in patchwork (use -d to write them to a new branch)',
Simon Glassb3080ec2025-05-08 04:58:49 +02001436 None), next(itr))
Simon Glass6a222e62021-08-01 16:02:39 -06001437
Simon Glassd85bb8f2022-01-29 14:14:09 -07001438 def test_insert_tags(self):
Simon Glass6a222e62021-08-01 16:02:39 -06001439 """Test inserting of review tags"""
1440 msg = '''first line
1441second line.'''
1442 tags = [
1443 'Reviewed-by: Bin Meng <bmeng.cn@gmail.com>',
1444 'Tested-by: Bin Meng <bmeng.cn@gmail.com>'
1445 ]
1446 signoff = 'Signed-off-by: Simon Glass <sjg@chromium.com>'
1447 tag_str = '\n'.join(tags)
1448
1449 new_msg = patchstream.insert_tags(msg, tags)
1450 self.assertEqual(msg + '\n\n' + tag_str, new_msg)
1451
1452 new_msg = patchstream.insert_tags(msg + '\n', tags)
1453 self.assertEqual(msg + '\n\n' + tag_str, new_msg)
1454
1455 msg += '\n\n' + signoff
1456 new_msg = patchstream.insert_tags(msg, tags)
1457 self.assertEqual(msg + '\n' + tag_str, new_msg)