blob: d055a7e595230f96bdae31a64b1131cff31532a4 [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 Glass7cb21f02025-05-08 05:02:07 +0200451 common = ['git', f'--git-dir={self.gitdir}', 'config']
452 tools.run(*(common + ['user.name', 'Dummy']), cwd=self.gitdir)
453 tools.run(*(common + ['user.email', 'dumdum@dummy.com']),
454 cwd=self.gitdir)
455
Simon Glass547cba62022-02-11 13:23:18 -0700456 # pylint doesn't seem to find this
457 # pylint: disable=E1101
Simon Glass54f1c5b2020-07-05 21:41:50 -0600458 author = pygit2.Signature('Test user', 'test@email.com')
459 committer = author
Simon Glasseb209e52020-10-29 21:46:15 -0600460 _ = repo.create_commit('HEAD', author, committer, 'Created master',
461 new_tree, [])
Simon Glass54f1c5b2020-07-05 21:41:50 -0600462
463 self.make_commit_with_file('Initial commit', '''
464Add a README
465
466''', 'README', '''This is the README file
467describing this project
468in very little detail''')
469
470 self.make_commit_with_file('pci: PCI implementation', '''
471Here is a basic PCI implementation
472
473''', 'pci.c', '''This is a file
474it has some contents
475and some more things''')
476 self.make_commit_with_file('main: Main program', '''
477Hello here is the second commit.
478''', 'main.c', '''This is the main file
479there is very little here
480but we can always add more later
481if we want to
482
483Series-to: u-boot
484Series-cc: Barry Crump <bcrump@whataroa.nz>
485''')
486 base_target = repo.revparse_single('HEAD')
487 self.make_commit_with_file('i2c: I2C things', '''
488This has some stuff to do with I2C
489''', 'i2c.c', '''And this is the file contents
490with some I2C-related things in it''')
491 self.make_commit_with_file('spi: SPI fixes', '''
492SPI needs some fixes
493and here they are
Simon Glassd0a0a582020-10-29 21:46:36 -0600494
495Signed-off-by: %s
496
497Series-to: u-boot
498Commit-notes:
499title of the series
500This is the cover letter for the series
501with various details
502END
503''' % self.leb, 'spi.c', '''Some fixes for SPI in this
Simon Glass54f1c5b2020-07-05 21:41:50 -0600504file to make SPI work
505better than before''')
506 first_target = repo.revparse_single('HEAD')
507
508 target = repo.revparse_single('HEAD~2')
Simon Glass547cba62022-02-11 13:23:18 -0700509 # pylint doesn't seem to find this
510 # pylint: disable=E1101
Simon Glass573abf82025-05-08 05:23:41 +0200511 repo.reset(target.oid, pygit2.enums.ResetMode.HARD)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600512 self.make_commit_with_file('video: Some video improvements', '''
513Fix up the video so that
514it looks more purple. Purple is
515a very nice colour.
516''', 'video.c', '''More purple here
517Purple and purple
518Even more purple
519Could not be any more purple''')
Simon Glassb8ca4692025-05-08 05:26:16 +0200520 self.make_commit_with_file('serial: Add a serial driver', f'''
Simon Glass54f1c5b2020-07-05 21:41:50 -0600521Here is the serial driver
522for my chip.
523
524Cover-letter:
Simon Glassb8ca4692025-05-08 05:26:16 +0200525{self.TITLE_SECOND}
Simon Glass54f1c5b2020-07-05 21:41:50 -0600526This series implements support
527for my glorious board.
528END
Simon Glassb8ca4692025-05-08 05:26:16 +0200529Series-to: u-boot
530Series-links: {self.SERIES_ID_SECOND_V1}
Simon Glass54f1c5b2020-07-05 21:41:50 -0600531''', 'serial.c', '''The code for the
532serial driver is here''')
533 self.make_commit_with_file('bootm: Make it boot', '''
534This makes my board boot
535with a fix to the bootm
536command
537''', 'bootm.c', '''Fix up the bootm
538command to make the code as
539complicated as possible''')
540 second_target = repo.revparse_single('HEAD')
541
542 repo.branches.local.create('first', first_target)
543 repo.config.set_multivar('branch.first.remote', '', '.')
544 repo.config.set_multivar('branch.first.merge', '', 'refs/heads/base')
545
546 repo.branches.local.create('second', second_target)
547 repo.config.set_multivar('branch.second.remote', '', '.')
548 repo.config.set_multivar('branch.second.merge', '', 'refs/heads/base')
549
550 repo.branches.local.create('base', base_target)
Simon Glass573abf82025-05-08 05:23:41 +0200551
552 target = repo.lookup_reference('refs/heads/first')
553 repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
554 target = repo.revparse_single('HEAD')
555 repo.reset(target.oid, pygit2.enums.ResetMode.HARD)
556
557 self.assertFalse(gitutil.check_dirty(self.gitdir, self.tmpdir))
Simon Glass54f1c5b2020-07-05 21:41:50 -0600558 return repo
559
Simon Glassd85bb8f2022-01-29 14:14:09 -0700560 def test_branch(self):
Simon Glass54f1c5b2020-07-05 21:41:50 -0600561 """Test creating patches from a branch"""
562 repo = self.make_git_tree()
563 target = repo.lookup_reference('refs/heads/first')
Simon Glass547cba62022-02-11 13:23:18 -0700564 # pylint doesn't seem to find this
565 # pylint: disable=E1101
Simon Glass54f1c5b2020-07-05 21:41:50 -0600566 self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
567 control.setup()
Heinrich Schuchardtd01d6672023-04-20 20:07:29 +0200568 orig_dir = os.getcwd()
Simon Glass54f1c5b2020-07-05 21:41:50 -0600569 try:
Simon Glass41dfb6e2025-05-08 05:13:35 +0200570 os.chdir(self.tmpdir)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600571
572 # Check that it can detect the current branch
Simon Glass761648b2022-01-29 14:14:11 -0700573 self.assertEqual(2, gitutil.count_commits_to_branch(None))
Simon Glass54f1c5b2020-07-05 21:41:50 -0600574 col = terminal.Color()
Simon Glass14d64e32025-04-29 07:21:59 -0600575 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600576 _, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600577 col, branch=None, count=-1, start=0, end=0,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100578 ignore_binary=False, signoff=True)
Simon Glass54f1c5b2020-07-05 21:41:50 -0600579 self.assertIsNone(cover_fname)
580 self.assertEqual(2, len(patch_files))
Simon Glass2eb4da72020-07-05 21:41:51 -0600581
582 # Check that it can detect a different branch
Simon Glass761648b2022-01-29 14:14:11 -0700583 self.assertEqual(3, gitutil.count_commits_to_branch('second'))
Simon Glass14d64e32025-04-29 07:21:59 -0600584 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600585 series, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600586 col, branch='second', count=-1, start=0, end=0,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100587 ignore_binary=False, signoff=True)
Simon Glass2eb4da72020-07-05 21:41:51 -0600588 self.assertIsNotNone(cover_fname)
589 self.assertEqual(3, len(patch_files))
Simon Glassb3bf4e12020-07-05 21:41:52 -0600590
Simon Glass414f1e02025-02-27 12:27:30 -0700591 cover = tools.read_file(cover_fname, binary=False)
592 lines = cover.splitlines()[-2:]
593 base = repo.lookup_reference('refs/heads/base').target
594 self.assertEqual(f'base-commit: {base}', lines[0])
595 self.assertEqual('branch: second', lines[1])
596
Simon Glassda1a6ec2025-03-28 07:02:20 -0600597 # Make sure that the base-commit is not present when it is in the
598 # cover letter
599 for fname in patch_files:
600 self.assertNotIn(b'base-commit:', tools.read_file(fname))
601
Simon Glassb3bf4e12020-07-05 21:41:52 -0600602 # Check that it can skip patches at the end
Simon Glass14d64e32025-04-29 07:21:59 -0600603 with terminal.capture() as _:
Simon Glassc0257982025-04-29 07:22:11 -0600604 _, cover_fname, patch_files = send.prepare_patches(
Simon Glassb3bf4e12020-07-05 21:41:52 -0600605 col, branch='second', count=-1, start=0, end=1,
Philipp Tomsich858531a2020-11-24 18:14:52 +0100606 ignore_binary=False, signoff=True)
Simon Glassb3bf4e12020-07-05 21:41:52 -0600607 self.assertIsNotNone(cover_fname)
608 self.assertEqual(2, len(patch_files))
Simon Glass414f1e02025-02-27 12:27:30 -0700609
610 cover = tools.read_file(cover_fname, binary=False)
611 lines = cover.splitlines()[-2:]
612 base2 = repo.lookup_reference('refs/heads/second')
613 ref = base2.peel(pygit2.GIT_OBJ_COMMIT).parents[0].parents[0].id
614 self.assertEqual(f'base-commit: {ref}', lines[0])
615 self.assertEqual('branch: second', lines[1])
Simon Glass54f1c5b2020-07-05 21:41:50 -0600616 finally:
617 os.chdir(orig_dir)
Simon Glass06202d62020-10-29 21:46:27 -0600618
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500619 def test_custom_get_maintainer_script(self):
620 """Validate that a custom get_maintainer script gets used."""
621 self.make_git_tree()
Simon Glass41dfb6e2025-05-08 05:13:35 +0200622 with directory_excursion(self.tmpdir):
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500623 # Setup git.
624 os.environ['GIT_CONFIG_GLOBAL'] = '/dev/null'
625 os.environ['GIT_CONFIG_SYSTEM'] = '/dev/null'
626 tools.run('git', 'config', 'user.name', 'Dummy')
627 tools.run('git', 'config', 'user.email', 'dumdum@dummy.com')
628 tools.run('git', 'branch', 'upstream')
629 tools.run('git', 'branch', '--set-upstream-to=upstream')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500630
631 # Setup patman configuration.
632 with open('.patman', 'w', buffering=1) as f:
633 f.write('[settings]\n'
634 'get_maintainer_script: dummy-script.sh\n'
Sean Andersona06df742024-04-18 22:36:30 -0400635 'check_patch: False\n'
636 'add_maintainers: True\n')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500637 with open('dummy-script.sh', 'w', buffering=1) as f:
638 f.write('#!/usr/bin/env python\n'
639 'print("hello@there.com")\n')
640 os.chmod('dummy-script.sh', 0x555)
Simon Glass41dfb6e2025-05-08 05:13:35 +0200641 tools.run('git', 'add', '.')
642 tools.run('git', 'commit', '-m', 'new commit')
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500643
644 # Finally, do the test
Simon Glass14d64e32025-04-29 07:21:59 -0600645 with terminal.capture():
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -0500646 output = tools.run(PATMAN_DIR / 'patman', '--dry-run')
647 # Assert the email address is part of the dry-run
648 # output.
649 self.assertIn('hello@there.com', output)
650
Simon Glassd85bb8f2022-01-29 14:14:09 -0700651 def test_tags(self):
Simon Glass06202d62020-10-29 21:46:27 -0600652 """Test collection of tags in a patchstream"""
653 text = '''This is a patch
654
655Signed-off-by: Terminator
Simon Glass3b762cc2020-10-29 21:46:28 -0600656Reviewed-by: %s
657Reviewed-by: %s
Simon Glass06202d62020-10-29 21:46:27 -0600658Tested-by: %s
Simon Glass3b762cc2020-10-29 21:46:28 -0600659''' % (self.joe, self.mary, self.leb)
Simon Glass06202d62020-10-29 21:46:27 -0600660 pstrm = PatchStream.process_text(text)
661 self.assertEqual(pstrm.commit.rtags, {
Simon Glass3b762cc2020-10-29 21:46:28 -0600662 'Reviewed-by': {self.joe, self.mary},
Simon Glass06202d62020-10-29 21:46:27 -0600663 'Tested-by': {self.leb}})
Simon Glass3b762cc2020-10-29 21:46:28 -0600664
Simon Glassd85bb8f2022-01-29 14:14:09 -0700665 def test_invalid_tag(self):
Patrick Delaunay6bbdd0c2021-07-22 16:51:42 +0200666 """Test invalid tag in a patchstream"""
667 text = '''This is a patch
668
669Serie-version: 2
670'''
671 with self.assertRaises(ValueError) as exc:
672 pstrm = PatchStream.process_text(text)
673 self.assertEqual("Line 3: Invalid tag = 'Serie-version: 2'",
674 str(exc.exception))
675
Simon Glassd85bb8f2022-01-29 14:14:09 -0700676 def test_missing_end(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600677 """Test a missing END tag"""
678 text = '''This is a patch
679
680Cover-letter:
681This is the title
682missing END after this line
683Signed-off-by: Fred
684'''
685 pstrm = PatchStream.process_text(text)
686 self.assertEqual(["Missing 'END' in section 'cover'"],
687 pstrm.commit.warn)
688
Simon Glassd85bb8f2022-01-29 14:14:09 -0700689 def test_missing_blank_line(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600690 """Test a missing blank line after a tag"""
691 text = '''This is a patch
692
693Series-changes: 2
694- First line of changes
695- Missing blank line after this line
696Signed-off-by: Fred
697'''
698 pstrm = PatchStream.process_text(text)
699 self.assertEqual(["Missing 'blank line' in section 'Series-changes'"],
700 pstrm.commit.warn)
701
Simon Glassd85bb8f2022-01-29 14:14:09 -0700702 def test_invalid_commit_tag(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600703 """Test an invalid Commit-xxx tag"""
704 text = '''This is a patch
705
706Commit-fred: testing
707'''
708 pstrm = PatchStream.process_text(text)
709 self.assertEqual(["Line 3: Ignoring Commit-fred"], pstrm.commit.warn)
710
Simon Glassd85bb8f2022-01-29 14:14:09 -0700711 def test_self_test(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600712 """Test a tested by tag by this user"""
713 test_line = 'Tested-by: %s@napier.com' % os.getenv('USER')
714 text = '''This is a patch
715
716%s
717''' % test_line
718 pstrm = PatchStream.process_text(text)
719 self.assertEqual(["Ignoring '%s'" % test_line], pstrm.commit.warn)
720
Simon Glassd85bb8f2022-01-29 14:14:09 -0700721 def test_space_before_tab(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600722 """Test a space before a tab"""
723 text = '''This is a patch
724
725+ \tSomething
726'''
727 pstrm = PatchStream.process_text(text)
728 self.assertEqual(["Line 3/0 has space before tab"], pstrm.commit.warn)
729
Simon Glassd85bb8f2022-01-29 14:14:09 -0700730 def test_lines_after_test(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600731 """Test detecting lines after TEST= line"""
732 text = '''This is a patch
733
734TEST=sometest
735more lines
736here
737'''
738 pstrm = PatchStream.process_text(text)
739 self.assertEqual(["Found 2 lines after TEST="], pstrm.commit.warn)
740
Simon Glassd85bb8f2022-01-29 14:14:09 -0700741 def test_blank_line_at_end(self):
Simon Glass3b762cc2020-10-29 21:46:28 -0600742 """Test detecting a blank line at the end of a file"""
743 text = '''This is a patch
744
745diff --git a/lib/fdtdec.c b/lib/fdtdec.c
746index c072e54..942244f 100644
747--- a/lib/fdtdec.c
748+++ b/lib/fdtdec.c
749@@ -1200,7 +1200,8 @@ int fdtdec_setup_mem_size_base(void)
750 }
751
752 gd->ram_size = (phys_size_t)(res.end - res.start + 1);
753- debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
754+ debug("%s: Initial DRAM size %llx\n", __func__,
755+ (unsigned long long)gd->ram_size);
756+
757diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
758
759--
7602.7.4
761
762 '''
763 pstrm = PatchStream.process_text(text)
764 self.assertEqual(
765 ["Found possible blank line(s) at end of file 'lib/fdtdec.c'"],
766 pstrm.commit.warn)
Simon Glass1c1f2072020-10-29 21:46:34 -0600767
Simon Glassd85bb8f2022-01-29 14:14:09 -0700768 def test_no_upstream(self):
Simon Glass1c1f2072020-10-29 21:46:34 -0600769 """Test CountCommitsToBranch when there is no upstream"""
770 repo = self.make_git_tree()
771 target = repo.lookup_reference('refs/heads/base')
Simon Glass547cba62022-02-11 13:23:18 -0700772 # pylint doesn't seem to find this
773 # pylint: disable=E1101
Simon Glass1c1f2072020-10-29 21:46:34 -0600774 self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
775
776 # Check that it can detect the current branch
Heinrich Schuchardtd01d6672023-04-20 20:07:29 +0200777 orig_dir = os.getcwd()
Simon Glass1c1f2072020-10-29 21:46:34 -0600778 try:
Simon Glass1c1f2072020-10-29 21:46:34 -0600779 os.chdir(self.gitdir)
780 with self.assertRaises(ValueError) as exc:
Simon Glass761648b2022-01-29 14:14:11 -0700781 gitutil.count_commits_to_branch(None)
Simon Glass1c1f2072020-10-29 21:46:34 -0600782 self.assertIn(
783 "Failed to determine upstream: fatal: no upstream configured for branch 'base'",
784 str(exc.exception))
785 finally:
786 os.chdir(orig_dir)
Simon Glass3db916d2020-10-29 21:46:35 -0600787
788 @staticmethod
Simon Glass25b91c12025-04-29 07:22:19 -0600789 def _fake_patchwork(subpath):
Simon Glass3db916d2020-10-29 21:46:35 -0600790 """Fake Patchwork server for the function below
791
792 This handles accessing a series, providing a list consisting of a
793 single patch
Simon Glassf9b03cf2020-11-03 13:54:14 -0700794
795 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -0700796 subpath (str): URL subpath to use
Simon Glass3db916d2020-10-29 21:46:35 -0600797 """
798 re_series = re.match(r'series/(\d*)/$', subpath)
799 if re_series:
800 series_num = re_series.group(1)
801 if series_num == '1234':
802 return {'patches': [
803 {'id': '1', 'name': 'Some patch'}]}
804 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
805
Simon Glassd85bb8f2022-01-29 14:14:09 -0700806 def test_status_mismatch(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600807 """Test Patchwork patches not matching the series"""
Simon Glass25b91c12025-04-29 07:22:19 -0600808 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork)
Simon Glass14d64e32025-04-29 07:21:59 -0600809 with terminal.capture() as (_, err):
Simon Glass3729b8b2025-04-29 07:22:24 -0600810 patches = asyncio.run(status.check_status(1234, pwork))
Simon Glass27280f42025-04-29 07:22:17 -0600811 status.check_patch_count(0, len(patches))
Simon Glass3db916d2020-10-29 21:46:35 -0600812 self.assertIn('Warning: Patchwork reports 1 patches, series has 0',
813 err.getvalue())
814
Simon Glassd85bb8f2022-01-29 14:14:09 -0700815 def test_status_read_patch(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600816 """Test handling a single patch in Patchwork"""
Simon Glass25b91c12025-04-29 07:22:19 -0600817 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork)
Simon Glass3729b8b2025-04-29 07:22:24 -0600818 patches = asyncio.run(status.check_status(1234, pwork))
Simon Glass3db916d2020-10-29 21:46:35 -0600819 self.assertEqual(1, len(patches))
820 patch = patches[0]
821 self.assertEqual('1', patch.id)
822 self.assertEqual('Some patch', patch.raw_subject)
823
Simon Glassd85bb8f2022-01-29 14:14:09 -0700824 def test_parse_subject(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600825 """Test parsing of the patch subject"""
Simon Glass232eefd2025-04-29 07:22:14 -0600826 patch = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600827
828 # Simple patch not in a series
829 patch.parse_subject('Testing')
830 self.assertEqual('Testing', patch.raw_subject)
831 self.assertEqual('Testing', patch.subject)
832 self.assertEqual(1, patch.seq)
833 self.assertEqual(1, patch.count)
834 self.assertEqual(None, patch.prefix)
835 self.assertEqual(None, patch.version)
836
837 # First patch in a series
838 patch.parse_subject('[1/2] Testing')
839 self.assertEqual('[1/2] Testing', patch.raw_subject)
840 self.assertEqual('Testing', patch.subject)
841 self.assertEqual(1, patch.seq)
842 self.assertEqual(2, patch.count)
843 self.assertEqual(None, patch.prefix)
844 self.assertEqual(None, patch.version)
845
846 # Second patch in a series
847 patch.parse_subject('[2/2] Testing')
848 self.assertEqual('Testing', patch.subject)
849 self.assertEqual(2, patch.seq)
850 self.assertEqual(2, patch.count)
851 self.assertEqual(None, patch.prefix)
852 self.assertEqual(None, patch.version)
853
854 # RFC patch
855 patch.parse_subject('[RFC,3/7] Testing')
856 self.assertEqual('Testing', patch.subject)
857 self.assertEqual(3, patch.seq)
858 self.assertEqual(7, patch.count)
859 self.assertEqual('RFC', patch.prefix)
860 self.assertEqual(None, patch.version)
861
862 # Version patch
863 patch.parse_subject('[v2,3/7] Testing')
864 self.assertEqual('Testing', patch.subject)
865 self.assertEqual(3, patch.seq)
866 self.assertEqual(7, patch.count)
867 self.assertEqual(None, patch.prefix)
868 self.assertEqual('v2', patch.version)
869
870 # All fields
871 patch.parse_subject('[RESEND,v2,3/7] Testing')
872 self.assertEqual('Testing', patch.subject)
873 self.assertEqual(3, patch.seq)
874 self.assertEqual(7, patch.count)
875 self.assertEqual('RESEND', patch.prefix)
876 self.assertEqual('v2', patch.version)
877
878 # RFC only
879 patch.parse_subject('[RESEND] Testing')
880 self.assertEqual('Testing', patch.subject)
881 self.assertEqual(1, patch.seq)
882 self.assertEqual(1, patch.count)
883 self.assertEqual('RESEND', patch.prefix)
884 self.assertEqual(None, patch.version)
885
Simon Glassd85bb8f2022-01-29 14:14:09 -0700886 def test_compare_series(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600887 """Test operation of compare_with_series()"""
888 commit1 = Commit('abcd')
889 commit1.subject = 'Subject 1'
890 commit2 = Commit('ef12')
891 commit2.subject = 'Subject 2'
892 commit3 = Commit('3456')
893 commit3.subject = 'Subject 2'
894
Simon Glass232eefd2025-04-29 07:22:14 -0600895 patch1 = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600896 patch1.subject = 'Subject 1'
Simon Glass232eefd2025-04-29 07:22:14 -0600897 patch2 = patchwork.Patch('2')
Simon Glass3db916d2020-10-29 21:46:35 -0600898 patch2.subject = 'Subject 2'
Simon Glass232eefd2025-04-29 07:22:14 -0600899 patch3 = patchwork.Patch('3')
Simon Glass3db916d2020-10-29 21:46:35 -0600900 patch3.subject = 'Subject 2'
901
902 series = Series()
903 series.commits = [commit1]
904 patches = [patch1]
905 patch_for_commit, commit_for_patch, warnings = (
906 status.compare_with_series(series, patches))
907 self.assertEqual(1, len(patch_for_commit))
908 self.assertEqual(patch1, patch_for_commit[0])
909 self.assertEqual(1, len(commit_for_patch))
910 self.assertEqual(commit1, commit_for_patch[0])
911
912 series.commits = [commit1]
913 patches = [patch1, patch2]
914 patch_for_commit, commit_for_patch, warnings = (
915 status.compare_with_series(series, patches))
916 self.assertEqual(1, len(patch_for_commit))
917 self.assertEqual(patch1, patch_for_commit[0])
918 self.assertEqual(1, len(commit_for_patch))
919 self.assertEqual(commit1, commit_for_patch[0])
920 self.assertEqual(["Cannot find commit for patch 2 ('Subject 2')"],
921 warnings)
922
923 series.commits = [commit1, commit2]
924 patches = [patch1]
925 patch_for_commit, commit_for_patch, warnings = (
926 status.compare_with_series(series, patches))
927 self.assertEqual(1, len(patch_for_commit))
928 self.assertEqual(patch1, patch_for_commit[0])
929 self.assertEqual(1, len(commit_for_patch))
930 self.assertEqual(commit1, commit_for_patch[0])
931 self.assertEqual(["Cannot find patch for commit 2 ('Subject 2')"],
932 warnings)
933
934 series.commits = [commit1, commit2, commit3]
935 patches = [patch1, patch2]
936 patch_for_commit, commit_for_patch, warnings = (
937 status.compare_with_series(series, patches))
938 self.assertEqual(2, len(patch_for_commit))
939 self.assertEqual(patch1, patch_for_commit[0])
940 self.assertEqual(patch2, patch_for_commit[1])
941 self.assertEqual(1, len(commit_for_patch))
942 self.assertEqual(commit1, commit_for_patch[0])
943 self.assertEqual(["Cannot find patch for commit 3 ('Subject 2')",
944 "Multiple commits match patch 2 ('Subject 2'):\n"
945 ' Subject 2\n Subject 2'],
946 warnings)
947
948 series.commits = [commit1, commit2]
949 patches = [patch1, patch2, patch3]
950 patch_for_commit, commit_for_patch, warnings = (
951 status.compare_with_series(series, patches))
952 self.assertEqual(1, len(patch_for_commit))
953 self.assertEqual(patch1, patch_for_commit[0])
954 self.assertEqual(2, len(commit_for_patch))
955 self.assertEqual(commit1, commit_for_patch[0])
956 self.assertEqual(["Multiple patches match commit 2 ('Subject 2'):\n"
957 ' Subject 2\n Subject 2',
958 "Cannot find commit for patch 3 ('Subject 2')"],
959 warnings)
960
Simon Glass25b91c12025-04-29 07:22:19 -0600961 def _fake_patchwork2(self, subpath):
Simon Glass3db916d2020-10-29 21:46:35 -0600962 """Fake Patchwork server for the function below
963
964 This handles accessing series, patches and comments, providing the data
965 in self.patches to the caller
Simon Glassf9b03cf2020-11-03 13:54:14 -0700966
967 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -0700968 subpath (str): URL subpath to use
Simon Glass3db916d2020-10-29 21:46:35 -0600969 """
970 re_series = re.match(r'series/(\d*)/$', subpath)
971 re_patch = re.match(r'patches/(\d*)/$', subpath)
972 re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
973 if re_series:
974 series_num = re_series.group(1)
975 if series_num == '1234':
976 return {'patches': self.patches}
977 elif re_patch:
978 patch_num = int(re_patch.group(1))
979 patch = self.patches[patch_num - 1]
980 return patch
981 elif re_comments:
982 patch_num = int(re_comments.group(1))
983 patch = self.patches[patch_num - 1]
984 return patch.comments
985 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
986
Simon Glassd85bb8f2022-01-29 14:14:09 -0700987 def test_find_new_responses(self):
Simon Glass3db916d2020-10-29 21:46:35 -0600988 """Test operation of find_new_responses()"""
989 commit1 = Commit('abcd')
990 commit1.subject = 'Subject 1'
991 commit2 = Commit('ef12')
992 commit2.subject = 'Subject 2'
993
Simon Glass232eefd2025-04-29 07:22:14 -0600994 patch1 = patchwork.Patch('1')
Simon Glass3db916d2020-10-29 21:46:35 -0600995 patch1.parse_subject('[1/2] Subject 1')
996 patch1.name = patch1.raw_subject
997 patch1.content = 'This is my patch content'
998 comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
999
1000 patch1.comments = [comment1a]
1001
Simon Glass232eefd2025-04-29 07:22:14 -06001002 patch2 = patchwork.Patch('2')
Simon Glass3db916d2020-10-29 21:46:35 -06001003 patch2.parse_subject('[2/2] Subject 2')
1004 patch2.name = patch2.raw_subject
1005 patch2.content = 'Some other patch content'
1006 comment2a = {
1007 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
1008 (self.mary, self.leb)}
1009 comment2b = {'content': 'Reviewed-by: %s' % self.fred}
1010 patch2.comments = [comment2a, comment2b]
1011
1012 # This test works by setting up commits and patch for use by the fake
1013 # Rest API function _fake_patchwork2(). It calls various functions in
1014 # the status module after setting up tags in the commits, checking that
1015 # things behaves as expected
1016 self.commits = [commit1, commit2]
1017 self.patches = [patch1, patch2]
1018 count = 2
Simon Glass3db916d2020-10-29 21:46:35 -06001019
1020 # Check that the tags are picked up on the first patch
Simon Glass29771962025-04-29 07:22:20 -06001021 new_rtags, _ = status.process_reviews(patch1.content, patch1.comments,
1022 commit1.rtags)
1023 self.assertEqual(new_rtags, {'Reviewed-by': {self.joe}})
Simon Glass3db916d2020-10-29 21:46:35 -06001024
1025 # Now the second patch
Simon Glass29771962025-04-29 07:22:20 -06001026 new_rtags, _ = status.process_reviews(patch2.content, patch2.comments,
1027 commit2.rtags)
1028 self.assertEqual(new_rtags, {
Simon Glass3db916d2020-10-29 21:46:35 -06001029 'Reviewed-by': {self.mary, self.fred},
1030 'Tested-by': {self.leb}})
1031
1032 # Now add some tags to the commit, which means they should not appear as
1033 # 'new' tags when scanning comments
Simon Glass3db916d2020-10-29 21:46:35 -06001034 commit1.rtags = {'Reviewed-by': {self.joe}}
Simon Glass29771962025-04-29 07:22:20 -06001035 new_rtags, _ = status.process_reviews(patch1.content, patch1.comments,
1036 commit1.rtags)
1037 self.assertEqual(new_rtags, {})
Simon Glass3db916d2020-10-29 21:46:35 -06001038
1039 # For the second commit, add Ed and Fred, so only Mary should be left
1040 commit2.rtags = {
1041 'Tested-by': {self.leb},
1042 'Reviewed-by': {self.fred}}
Simon Glass29771962025-04-29 07:22:20 -06001043 new_rtags, _ = status.process_reviews(patch2.content, patch2.comments,
1044 commit2.rtags)
1045 self.assertEqual(new_rtags, {'Reviewed-by': {self.mary}})
Simon Glass3db916d2020-10-29 21:46:35 -06001046
1047 # Check that the output patches expectations:
1048 # 1 Subject 1
1049 # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1050 # 2 Subject 2
1051 # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1052 # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1053 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1054 # 1 new response available in patchwork
1055
1056 series = Series()
1057 series.commits = [commit1, commit2]
Simon Glass02811582022-01-29 14:14:18 -07001058 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001059 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork2)
Simon Glassc100b262025-04-29 07:22:16 -06001060 status.check_and_show_status(series, '1234', None, None, False, False,
Simon Glass25b91c12025-04-29 07:22:19 -06001061 pwork)
Simon Glassb3080ec2025-05-08 04:58:49 +02001062 itr = iter(terminal.get_print_test_lines())
Simon Glass3db916d2020-10-29 21:46:35 -06001063 col = terminal.Color()
Simon Glassd4d3fb42025-04-29 07:22:21 -06001064 self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001065 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001066 self.assertEqual(
1067 terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
1068 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001069 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001070 self.assertEqual(terminal.PrintLine(self.joe, col.WHITE, bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001071 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001072
Simon Glassd4d3fb42025-04-29 07:22:21 -06001073 self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001074 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001075 self.assertEqual(
Simon Glass2112d072020-10-29 21:46:38 -06001076 terminal.PrintLine(' Reviewed-by: ', col.GREEN, newline=False,
Simon Glass3db916d2020-10-29 21:46:35 -06001077 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001078 next(itr))
1079 self.assertEqual(terminal.PrintLine(self.fred, col.WHITE,
1080 bright=False), next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001081 self.assertEqual(
Simon Glass2112d072020-10-29 21:46:38 -06001082 terminal.PrintLine(' Tested-by: ', col.GREEN, newline=False,
Simon Glass3db916d2020-10-29 21:46:35 -06001083 bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001084 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001085 self.assertEqual(terminal.PrintLine(self.leb, col.WHITE, bright=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001086 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001087 self.assertEqual(
1088 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001089 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001090 self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001091 next(itr))
Simon Glass3db916d2020-10-29 21:46:35 -06001092 self.assertEqual(terminal.PrintLine(
Simon Glassd0a0a582020-10-29 21:46:36 -06001093 '1 new response available in patchwork (use -d to write them to a new branch)',
Simon Glassb3080ec2025-05-08 04:58:49 +02001094 None), next(itr))
Simon Glassd0a0a582020-10-29 21:46:36 -06001095
Simon Glass25b91c12025-04-29 07:22:19 -06001096 def _fake_patchwork3(self, subpath):
Simon Glassd0a0a582020-10-29 21:46:36 -06001097 """Fake Patchwork server for the function below
1098
1099 This handles accessing series, patches and comments, providing the data
1100 in self.patches to the caller
Simon Glassf9b03cf2020-11-03 13:54:14 -07001101
1102 Args:
Simon Glassf9b03cf2020-11-03 13:54:14 -07001103 subpath (str): URL subpath to use
Simon Glassd0a0a582020-10-29 21:46:36 -06001104 """
1105 re_series = re.match(r'series/(\d*)/$', subpath)
1106 re_patch = re.match(r'patches/(\d*)/$', subpath)
1107 re_comments = re.match(r'patches/(\d*)/comments/$', subpath)
1108 if re_series:
1109 series_num = re_series.group(1)
1110 if series_num == '1234':
1111 return {'patches': self.patches}
1112 elif re_patch:
1113 patch_num = int(re_patch.group(1))
1114 patch = self.patches[patch_num - 1]
1115 return patch
1116 elif re_comments:
1117 patch_num = int(re_comments.group(1))
1118 patch = self.patches[patch_num - 1]
1119 return patch.comments
1120 raise ValueError('Fake Patchwork does not understand: %s' % subpath)
1121
Simon Glassd85bb8f2022-01-29 14:14:09 -07001122 def test_create_branch(self):
Simon Glassd0a0a582020-10-29 21:46:36 -06001123 """Test operation of create_branch()"""
1124 repo = self.make_git_tree()
1125 branch = 'first'
1126 dest_branch = 'first2'
1127 count = 2
Simon Glass41dfb6e2025-05-08 05:13:35 +02001128 gitdir = self.gitdir
Simon Glassd0a0a582020-10-29 21:46:36 -06001129
1130 # Set up the test git tree. We use branch 'first' which has two commits
1131 # in it
1132 series = patchstream.get_metadata_for_list(branch, gitdir, count)
1133 self.assertEqual(2, len(series.commits))
1134
Simon Glass232eefd2025-04-29 07:22:14 -06001135 patch1 = patchwork.Patch('1')
Simon Glassd0a0a582020-10-29 21:46:36 -06001136 patch1.parse_subject('[1/2] %s' % series.commits[0].subject)
1137 patch1.name = patch1.raw_subject
1138 patch1.content = 'This is my patch content'
1139 comment1a = {'content': 'Reviewed-by: %s\n' % self.joe}
1140
1141 patch1.comments = [comment1a]
1142
Simon Glass232eefd2025-04-29 07:22:14 -06001143 patch2 = patchwork.Patch('2')
Simon Glassd0a0a582020-10-29 21:46:36 -06001144 patch2.parse_subject('[2/2] %s' % series.commits[1].subject)
1145 patch2.name = patch2.raw_subject
1146 patch2.content = 'Some other patch content'
1147 comment2a = {
1148 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
1149 (self.mary, self.leb)}
1150 comment2b = {
1151 'content': 'Reviewed-by: %s' % self.fred}
1152 patch2.comments = [comment2a, comment2b]
1153
1154 # This test works by setting up patches for use by the fake Rest API
1155 # function _fake_patchwork3(). The fake patch comments above should
1156 # result in new review tags that are collected and added to the commits
1157 # created in the destination branch.
1158 self.patches = [patch1, patch2]
1159 count = 2
1160
1161 # Expected output:
1162 # 1 i2c: I2C things
1163 # + Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1164 # 2 spi: SPI fixes
1165 # + Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1166 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1167 # + Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1168 # 4 new responses available in patchwork
1169 # 4 responses added from patchwork into new branch 'first2'
1170 # <unittest.result.TestResult run=8 errors=0 failures=0>
1171
Simon Glass02811582022-01-29 14:14:18 -07001172 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001173 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork3)
Simon Glassc100b262025-04-29 07:22:16 -06001174 status.check_and_show_status(series, '1234', branch, dest_branch,
Simon Glass25b91c12025-04-29 07:22:19 -06001175 False, False, pwork, repo)
Simon Glass02811582022-01-29 14:14:18 -07001176 lines = terminal.get_print_test_lines()
Simon Glassd0a0a582020-10-29 21:46:36 -06001177 self.assertEqual(12, len(lines))
1178 self.assertEqual(
1179 "4 responses added from patchwork into new branch 'first2'",
1180 lines[11].text)
1181
1182 # Check that the destination branch has the new tags
1183 new_series = patchstream.get_metadata_for_list(dest_branch, gitdir,
1184 count)
1185 self.assertEqual(
1186 {'Reviewed-by': {self.joe}},
1187 new_series.commits[0].rtags)
1188 self.assertEqual(
1189 {'Tested-by': {self.leb},
1190 'Reviewed-by': {self.fred, self.mary}},
1191 new_series.commits[1].rtags)
1192
1193 # Now check the actual test of the first commit message. We expect to
1194 # see the new tags immediately below the old ones.
1195 stdout = patchstream.get_list(dest_branch, count=count, git_dir=gitdir)
Simon Glassb3080ec2025-05-08 04:58:49 +02001196 itr = iter([line.strip() for line in stdout.splitlines()
1197 if '-by:' in line])
Simon Glassd0a0a582020-10-29 21:46:36 -06001198
1199 # First patch should have the review tag
Simon Glassb3080ec2025-05-08 04:58:49 +02001200 self.assertEqual('Reviewed-by: %s' % self.joe, next(itr))
Simon Glassd0a0a582020-10-29 21:46:36 -06001201
1202 # Second patch should have the sign-off then the tested-by and two
1203 # reviewed-by tags
Simon Glassb3080ec2025-05-08 04:58:49 +02001204 self.assertEqual('Signed-off-by: %s' % self.leb, next(itr))
1205 self.assertEqual('Reviewed-by: %s' % self.fred, next(itr))
1206 self.assertEqual('Reviewed-by: %s' % self.mary, next(itr))
1207 self.assertEqual('Tested-by: %s' % self.leb, next(itr))
Simon Glassda8a2922020-10-29 21:46:37 -06001208
Simon Glassd85bb8f2022-01-29 14:14:09 -07001209 def test_parse_snippets(self):
Simon Glassda8a2922020-10-29 21:46:37 -06001210 """Test parsing of review snippets"""
1211 text = '''Hi Fred,
1212
1213This is a comment from someone.
1214
1215Something else
1216
1217On some recent date, Fred wrote:
1218> This is why I wrote the patch
1219> so here it is
1220
1221Now a comment about the commit message
1222A little more to say
1223
1224Even more
1225
1226> diff --git a/file.c b/file.c
1227> Some more code
1228> Code line 2
1229> Code line 3
1230> Code line 4
1231> Code line 5
1232> Code line 6
1233> Code line 7
1234> Code line 8
1235> Code line 9
1236
1237And another comment
1238
Simon Glassd85bb8f2022-01-29 14:14:09 -07001239> @@ -153,8 +143,13 @@ def check_patch(fname, show_types=False):
Simon Glassda8a2922020-10-29 21:46:37 -06001240> further down on the file
1241> and more code
1242> +Addition here
1243> +Another addition here
1244> codey
1245> more codey
1246
1247and another thing in same file
1248
1249> @@ -253,8 +243,13 @@
1250> with no function context
1251
1252one more thing
1253
1254> diff --git a/tools/patman/main.py b/tools/patman/main.py
1255> +line of code
1256now a very long comment in a different file
1257line2
1258line3
1259line4
1260line5
1261line6
1262line7
1263line8
1264'''
1265 pstrm = PatchStream.process_text(text, True)
1266 self.assertEqual([], pstrm.commit.warn)
1267
1268 # We expect to the filename and up to 5 lines of code context before
1269 # each comment. The 'On xxx wrote:' bit should be removed.
1270 self.assertEqual(
1271 [['Hi Fred,',
1272 'This is a comment from someone.',
1273 'Something else'],
1274 ['> This is why I wrote the patch',
1275 '> so here it is',
1276 'Now a comment about the commit message',
1277 'A little more to say', 'Even more'],
1278 ['> File: file.c', '> Code line 5', '> Code line 6',
1279 '> Code line 7', '> Code line 8', '> Code line 9',
1280 'And another comment'],
1281 ['> File: file.c',
Simon Glassd85bb8f2022-01-29 14:14:09 -07001282 '> Line: 153 / 143: def check_patch(fname, show_types=False):',
Simon Glassda8a2922020-10-29 21:46:37 -06001283 '> and more code', '> +Addition here', '> +Another addition here',
1284 '> codey', '> more codey', 'and another thing in same file'],
1285 ['> File: file.c', '> Line: 253 / 243',
1286 '> with no function context', 'one more thing'],
1287 ['> File: tools/patman/main.py', '> +line of code',
1288 'now a very long comment in a different file',
1289 'line2', 'line3', 'line4', 'line5', 'line6', 'line7', 'line8']],
1290 pstrm.snippets)
Simon Glass2112d072020-10-29 21:46:38 -06001291
Simon Glassd85bb8f2022-01-29 14:14:09 -07001292 def test_review_snippets(self):
Simon Glass2112d072020-10-29 21:46:38 -06001293 """Test showing of review snippets"""
1294 def _to_submitter(who):
1295 m_who = re.match('(.*) <(.*)>', who)
1296 return {
1297 'name': m_who.group(1),
1298 'email': m_who.group(2)
1299 }
1300
1301 commit1 = Commit('abcd')
1302 commit1.subject = 'Subject 1'
1303 commit2 = Commit('ef12')
1304 commit2.subject = 'Subject 2'
1305
Simon Glass232eefd2025-04-29 07:22:14 -06001306 patch1 = patchwork.Patch('1')
Simon Glass2112d072020-10-29 21:46:38 -06001307 patch1.parse_subject('[1/2] Subject 1')
1308 patch1.name = patch1.raw_subject
1309 patch1.content = 'This is my patch content'
1310 comment1a = {'submitter': _to_submitter(self.joe),
1311 'content': '''Hi Fred,
1312
1313On some date Fred wrote:
1314
1315> diff --git a/file.c b/file.c
1316> Some code
1317> and more code
1318
1319Here is my comment above the above...
1320
1321
1322Reviewed-by: %s
1323''' % self.joe}
1324
1325 patch1.comments = [comment1a]
1326
Simon Glass232eefd2025-04-29 07:22:14 -06001327 patch2 = patchwork.Patch('2')
Simon Glass2112d072020-10-29 21:46:38 -06001328 patch2.parse_subject('[2/2] Subject 2')
1329 patch2.name = patch2.raw_subject
1330 patch2.content = 'Some other patch content'
1331 comment2a = {
1332 'content': 'Reviewed-by: %s\nTested-by: %s\n' %
1333 (self.mary, self.leb)}
1334 comment2b = {'submitter': _to_submitter(self.fred),
1335 'content': '''Hi Fred,
1336
1337On some date Fred wrote:
1338
1339> diff --git a/tools/patman/commit.py b/tools/patman/commit.py
1340> @@ -41,6 +41,9 @@ class Commit:
1341> self.rtags = collections.defaultdict(set)
1342> self.warn = []
1343>
1344> + def __str__(self):
1345> + return self.subject
1346> +
Simon Glassd85bb8f2022-01-29 14:14:09 -07001347> def add_change(self, version, info):
Simon Glass2112d072020-10-29 21:46:38 -06001348> """Add a new change line to the change list for a version.
1349>
1350A comment
1351
1352Reviewed-by: %s
1353''' % self.fred}
1354 patch2.comments = [comment2a, comment2b]
1355
1356 # This test works by setting up commits and patch for use by the fake
1357 # Rest API function _fake_patchwork2(). It calls various functions in
1358 # the status module after setting up tags in the commits, checking that
1359 # things behaves as expected
1360 self.commits = [commit1, commit2]
1361 self.patches = [patch1, patch2]
1362
1363 # Check that the output patches expectations:
1364 # 1 Subject 1
1365 # Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz>
1366 # 2 Subject 2
1367 # Tested-by: Lord Edmund Blackaddër <weasel@blackadder.org>
1368 # Reviewed-by: Fred Bloggs <f.bloggs@napier.net>
1369 # + Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz>
1370 # 1 new response available in patchwork
1371
1372 series = Series()
1373 series.commits = [commit1, commit2]
Simon Glass02811582022-01-29 14:14:18 -07001374 terminal.set_print_test_mode()
Simon Glass25b91c12025-04-29 07:22:19 -06001375 pwork = patchwork.Patchwork.for_testing(self._fake_patchwork2)
Simon Glassc100b262025-04-29 07:22:16 -06001376 status.check_and_show_status(series, '1234', None, None, False, True,
Simon Glass25b91c12025-04-29 07:22:19 -06001377 pwork)
Simon Glassb3080ec2025-05-08 04:58:49 +02001378 itr = iter(terminal.get_print_test_lines())
Simon Glass2112d072020-10-29 21:46:38 -06001379 col = terminal.Color()
Simon Glassd4d3fb42025-04-29 07:22:21 -06001380 self.assertEqual(terminal.PrintLine(' 1 Subject 1', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001381 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001382 self.assertEqual(
1383 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001384 next(itr))
1385 self.assertEqual(terminal.PrintLine(self.joe, col.WHITE), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001386
1387 self.assertEqual(terminal.PrintLine('Review: %s' % self.joe, col.RED),
Simon Glassb3080ec2025-05-08 04:58:49 +02001388 next(itr))
1389 self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(itr))
1390 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001391 self.assertEqual(terminal.PrintLine(' > File: file.c', col.MAGENTA),
Simon Glassb3080ec2025-05-08 04:58:49 +02001392 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001393 self.assertEqual(terminal.PrintLine(' > Some code', col.MAGENTA),
Simon Glassb3080ec2025-05-08 04:58:49 +02001394 next(itr))
1395 self.assertEqual(terminal.PrintLine(' > and more code',
1396 col.MAGENTA),
1397 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001398 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001399 ' Here is my comment above the above...', None), next(itr))
1400 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001401
Simon Glassd4d3fb42025-04-29 07:22:21 -06001402 self.assertEqual(terminal.PrintLine(' 2 Subject 2', col.YELLOW),
Simon Glassb3080ec2025-05-08 04:58:49 +02001403 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001404 self.assertEqual(
1405 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001406 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001407 self.assertEqual(terminal.PrintLine(self.fred, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001408 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001409 self.assertEqual(
1410 terminal.PrintLine(' + Reviewed-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001411 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001412 self.assertEqual(terminal.PrintLine(self.mary, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001413 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001414 self.assertEqual(
1415 terminal.PrintLine(' + Tested-by: ', col.GREEN, newline=False),
Simon Glassb3080ec2025-05-08 04:58:49 +02001416 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001417 self.assertEqual(terminal.PrintLine(self.leb, col.WHITE),
Simon Glassb3080ec2025-05-08 04:58:49 +02001418 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001419
1420 self.assertEqual(terminal.PrintLine('Review: %s' % self.fred, col.RED),
Simon Glassb3080ec2025-05-08 04:58:49 +02001421 next(itr))
1422 self.assertEqual(terminal.PrintLine(' Hi Fred,', None), next(itr))
1423 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001424 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001425 ' > File: tools/patman/commit.py', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001426 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001427 ' > Line: 41 / 41: class Commit:', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001428 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001429 ' > + return self.subject', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001430 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001431 ' > +', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001432 self.assertEqual(
Simon Glassb3080ec2025-05-08 04:58:49 +02001433 terminal.PrintLine(
1434 ' > def add_change(self, version, info):',
1435 col.MAGENTA),
1436 next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001437 self.assertEqual(terminal.PrintLine(
1438 ' > """Add a new change line to the change list for a version.',
Simon Glassb3080ec2025-05-08 04:58:49 +02001439 col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001440 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001441 ' >', col.MAGENTA), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001442 self.assertEqual(terminal.PrintLine(
Simon Glassb3080ec2025-05-08 04:58:49 +02001443 ' A comment', None), next(itr))
1444 self.assertEqual(terminal.PrintLine('', None), next(itr))
Simon Glass2112d072020-10-29 21:46:38 -06001445
1446 self.assertEqual(terminal.PrintLine(
1447 '4 new responses available in patchwork (use -d to write them to a new branch)',
Simon Glassb3080ec2025-05-08 04:58:49 +02001448 None), next(itr))
Simon Glass6a222e62021-08-01 16:02:39 -06001449
Simon Glassd85bb8f2022-01-29 14:14:09 -07001450 def test_insert_tags(self):
Simon Glass6a222e62021-08-01 16:02:39 -06001451 """Test inserting of review tags"""
1452 msg = '''first line
1453second line.'''
1454 tags = [
1455 'Reviewed-by: Bin Meng <bmeng.cn@gmail.com>',
1456 'Tested-by: Bin Meng <bmeng.cn@gmail.com>'
1457 ]
1458 signoff = 'Signed-off-by: Simon Glass <sjg@chromium.com>'
1459 tag_str = '\n'.join(tags)
1460
1461 new_msg = patchstream.insert_tags(msg, tags)
1462 self.assertEqual(msg + '\n\n' + tag_str, new_msg)
1463
1464 new_msg = patchstream.insert_tags(msg + '\n', tags)
1465 self.assertEqual(msg + '\n\n' + tag_str, new_msg)
1466
1467 msg += '\n\n' + signoff
1468 new_msg = patchstream.insert_tags(msg, tags)
1469 self.assertEqual(msg + '\n' + tag_str, new_msg)