blob: 588be73ef48ce7a7a75385e32aa1ae3bf36b8dce [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
7import contextlib
8import os
9import re
10import shutil
11import sys
12import tempfile
13import unittest
14
Simon Glass945328b2020-04-17 18:08:55 -060015from io import StringIO
Simon Glass68475f92019-05-14 15:53:49 -060016
Simon Glassa12b1ab2020-07-05 21:41:50 -060017from patman import control
Simon Glassa997ea52020-04-17 18:09:04 -060018from patman import gitutil
19from patman import patchstream
20from patman import settings
Simon Glassa12b1ab2020-07-05 21:41:50 -060021from patman import terminal
Simon Glassa997ea52020-04-17 18:09:04 -060022from patman import tools
Simon Glassa12b1ab2020-07-05 21:41:50 -060023from patman.test_util import capture_sys_output
Simon Glassdf1bc5c2017-05-29 15:31:31 -060024
Simon Glassa12b1ab2020-07-05 21:41:50 -060025try:
26 import pygit2
27 HAVE_PYGIT2= True
28except ModuleNotFoundError:
29 HAVE_PYGIT2 = False
30
Simon Glassdf1bc5c2017-05-29 15:31:31 -060031
32@contextlib.contextmanager
33def capture():
Simon Glassdf1bc5c2017-05-29 15:31:31 -060034 oldout,olderr = sys.stdout, sys.stderr
35 try:
36 out=[StringIO(), StringIO()]
37 sys.stdout,sys.stderr = out
38 yield out
39 finally:
40 sys.stdout,sys.stderr = oldout, olderr
41 out[0] = out[0].getvalue()
42 out[1] = out[1].getvalue()
43
44
45class TestFunctional(unittest.TestCase):
46 def setUp(self):
47 self.tmpdir = tempfile.mkdtemp(prefix='patman.')
Simon Glassa12b1ab2020-07-05 21:41:50 -060048 self.gitdir = os.path.join(self.tmpdir, 'git')
49 self.repo = None
Simon Glassdf1bc5c2017-05-29 15:31:31 -060050
51 def tearDown(self):
52 shutil.rmtree(self.tmpdir)
53
54 @staticmethod
55 def GetPath(fname):
56 return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
57 'test', fname)
58
59 @classmethod
60 def GetText(self, fname):
Simon Glassf544a2d2019-10-31 07:42:51 -060061 return open(self.GetPath(fname), encoding='utf-8').read()
Simon Glassdf1bc5c2017-05-29 15:31:31 -060062
63 @classmethod
64 def GetPatchName(self, subject):
65 fname = re.sub('[ :]', '-', subject)
66 return fname.replace('--', '-')
67
68 def CreatePatchesForTest(self, series):
69 cover_fname = None
70 fname_list = []
71 for i, commit in enumerate(series.commits):
72 clean_subject = self.GetPatchName(commit.subject)
73 src_fname = '%04d-%s.patch' % (i + 1, clean_subject[:52])
74 fname = os.path.join(self.tmpdir, src_fname)
75 shutil.copy(self.GetPath(src_fname), fname)
76 fname_list.append(fname)
77 if series.get('cover'):
78 src_fname = '0000-cover-letter.patch'
79 cover_fname = os.path.join(self.tmpdir, src_fname)
80 fname = os.path.join(self.tmpdir, src_fname)
81 shutil.copy(self.GetPath(src_fname), fname)
82
83 return cover_fname, fname_list
84
85 def testBasic(self):
86 """Tests the basic flow of patman
87
88 This creates a series from some hard-coded patches build from a simple
89 tree with the following metadata in the top commit:
90
91 Series-to: u-boot
92 Series-prefix: RFC
93 Series-cc: Stefan Brüns <stefan.bruens@rwth-aachen.de>
94 Cover-letter-cc: Lord Mëlchett <clergy@palace.gov>
Sean Andersoncf13b862020-05-04 16:28:36 -040095 Series-version: 3
96 Patch-cc: fred
97 Series-process-log: sort, uniq
Simon Glassdf1bc5c2017-05-29 15:31:31 -060098 Series-changes: 4
99 - Some changes
Sean Andersoncf13b862020-05-04 16:28:36 -0400100 - Multi
101 line
102 change
103
104 Commit-changes: 2
105 - Changes only for this commit
106
107 Cover-changes: 4
108 - Some notes for the cover letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600109
110 Cover-letter:
111 test: A test patch series
112 This is a test of how the cover
Sean Andersoncf13b862020-05-04 16:28:36 -0400113 letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600114 works
115 END
116
117 and this in the first commit:
118
Sean Andersoncf13b862020-05-04 16:28:36 -0400119 Commit-changes: 2
120 - second revision change
121
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600122 Series-notes:
123 some notes
124 about some things
125 from the first commit
126 END
127
128 Commit-notes:
129 Some notes about
130 the first commit
131 END
132
133 with the following commands:
134
135 git log -n2 --reverse >/path/to/tools/patman/test/test01.txt
136 git format-patch --subject-prefix RFC --cover-letter HEAD~2
137 mv 00* /path/to/tools/patman/test
138
139 It checks these aspects:
140 - git log can be processed by patchstream
141 - emailing patches uses the correct command
142 - CC file has information on each commit
143 - cover letter has the expected text and subject
144 - each patch has the correct subject
145 - dry-run information prints out correctly
146 - unicode is handled correctly
147 - Series-to, Series-cc, Series-prefix, Cover-letter
148 - Cover-letter-cc, Series-version, Series-changes, Series-notes
149 - Commit-notes
150 """
151 process_tags = True
152 ignore_bad_tags = True
Simon Glass4f817892019-05-14 15:53:53 -0600153 stefan = b'Stefan Br\xc3\xbcns <stefan.bruens@rwth-aachen.de>'.decode('utf-8')
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600154 rick = 'Richard III <richard@palace.gov>'
Simon Glass4f817892019-05-14 15:53:53 -0600155 mel = b'Lord M\xc3\xablchett <clergy@palace.gov>'.decode('utf-8')
156 ed = b'Lond Edmund Blackadd\xc3\xabr <weasel@blackadder.org'.decode('utf-8')
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600157 fred = 'Fred Bloggs <f.bloggs@napier.net>'
158 add_maintainers = [stefan, rick]
159 dry_run = True
160 in_reply_to = mel
161 count = 2
162 settings.alias = {
163 'fdt': ['simon'],
164 'u-boot': ['u-boot@lists.denx.de'],
165 'simon': [ed],
166 'fred': [fred],
167 }
168
169 text = self.GetText('test01.txt')
170 series = patchstream.GetMetaDataForTest(text)
171 cover_fname, args = self.CreatePatchesForTest(series)
172 with capture() as out:
173 patchstream.FixPatches(series, args)
174 if cover_fname and series.get('cover'):
175 patchstream.InsertCoverLetter(cover_fname, series, count)
176 series.DoChecks()
177 cc_file = series.MakeCcFile(process_tags, cover_fname,
Chris Packhamb84fb482018-06-07 20:45:06 +1200178 not ignore_bad_tags, add_maintainers,
179 None)
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600180 cmd = gitutil.EmailPatches(series, cover_fname, args,
181 dry_run, not ignore_bad_tags, cc_file,
182 in_reply_to=in_reply_to, thread=None)
183 series.ShowActions(args, cmd, process_tags)
Simon Glassf544a2d2019-10-31 07:42:51 -0600184 cc_lines = open(cc_file, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600185 os.remove(cc_file)
186
187 lines = out[0].splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600188 self.assertEqual('Cleaned %s patches' % len(series.commits), lines[0])
189 self.assertEqual('Change log missing for v2', lines[1])
190 self.assertEqual('Change log missing for v3', lines[2])
191 self.assertEqual('Change log for unknown version v4', lines[3])
192 self.assertEqual("Alias 'pci' not found", lines[4])
193 self.assertIn('Dry run', lines[5])
194 self.assertIn('Send a total of %d patches' % count, lines[7])
195 line = 8
196 for i, commit in enumerate(series.commits):
197 self.assertEqual(' %s' % args[i], lines[line + 0])
198 line += 1
199 while 'Cc:' in lines[line]:
200 line += 1
201 self.assertEqual('To: u-boot@lists.denx.de', lines[line])
Simon Glass4f817892019-05-14 15:53:53 -0600202 self.assertEqual('Cc: %s' % tools.FromUnicode(stefan),
203 lines[line + 1])
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600204 self.assertEqual('Version: 3', lines[line + 2])
205 self.assertEqual('Prefix:\t RFC', lines[line + 3])
206 self.assertEqual('Cover: 4 lines', lines[line + 4])
207 line += 5
Simon Glass3ab178f2019-05-14 15:53:51 -0600208 self.assertEqual(' Cc: %s' % fred, lines[line + 0])
Simon Glass4f817892019-05-14 15:53:53 -0600209 self.assertEqual(' Cc: %s' % tools.FromUnicode(ed),
210 lines[line + 1])
211 self.assertEqual(' Cc: %s' % tools.FromUnicode(mel),
212 lines[line + 2])
Simon Glass3ab178f2019-05-14 15:53:51 -0600213 self.assertEqual(' Cc: %s' % rick, lines[line + 3])
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600214 expected = ('Git command: git send-email --annotate '
215 '--in-reply-to="%s" --to "u-boot@lists.denx.de" '
216 '--cc "%s" --cc-cmd "%s --cc-cmd %s" %s %s'
217 % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname,
Simon Glass4f817892019-05-14 15:53:53 -0600218 ' '.join(args)))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600219 line += 4
Simon Glass4f817892019-05-14 15:53:53 -0600220 self.assertEqual(expected, tools.ToUnicode(lines[line]))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600221
Dmitry Torokhovef7f67d2019-10-21 20:09:56 -0700222 self.assertEqual(('%s %s\0%s' % (args[0], rick, stefan)),
Simon Glass4f817892019-05-14 15:53:53 -0600223 tools.ToUnicode(cc_lines[0]))
Dmitry Torokhovef7f67d2019-10-21 20:09:56 -0700224 self.assertEqual(('%s %s\0%s\0%s\0%s' % (args[1], fred, ed, rick,
Simon Glass4f817892019-05-14 15:53:53 -0600225 stefan)), tools.ToUnicode(cc_lines[1]))
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600226
227 expected = '''
228This is a test of how the cover
Sean Andersoncf13b862020-05-04 16:28:36 -0400229letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600230works
231
232some notes
233about some things
234from the first commit
235
236Changes in v4:
Sean Andersoncf13b862020-05-04 16:28:36 -0400237- Multi
238 line
239 change
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600240- Some changes
Sean Andersoncf13b862020-05-04 16:28:36 -0400241- Some notes for the cover letter
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600242
243Simon Glass (2):
244 pci: Correct cast for sandbox
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530245 fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600246
247 cmd/pci.c | 3 ++-
248 fs/fat/fat.c | 1 +
249 lib/efi_loader/efi_memory.c | 1 +
250 lib/fdtdec.c | 3 ++-
251 4 files changed, 6 insertions(+), 2 deletions(-)
252
253--\x20
2542.7.4
255
256'''
Simon Glassf544a2d2019-10-31 07:42:51 -0600257 lines = open(cover_fname, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600258 self.assertEqual(
259 'Subject: [RFC PATCH v3 0/2] test: A test patch series',
260 lines[3])
261 self.assertEqual(expected.splitlines(), lines[7:])
262
263 for i, fname in enumerate(args):
Simon Glassf544a2d2019-10-31 07:42:51 -0600264 lines = open(fname, encoding='utf-8').read().splitlines()
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600265 subject = [line for line in lines if line.startswith('Subject')]
266 self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count),
267 subject[0][:18])
Sean Andersoncf13b862020-05-04 16:28:36 -0400268
269 # Check that we got our commit notes
270 start = 0
271 expected = ''
272
Simon Glassdf1bc5c2017-05-29 15:31:31 -0600273 if i == 0:
Sean Andersoncf13b862020-05-04 16:28:36 -0400274 start = 17
275 expected = '''---
276Some notes about
277the first commit
278
279(no changes since v2)
280
281Changes in v2:
282- second revision change'''
283 elif i == 1:
284 start = 17
285 expected = '''---
286
287Changes in v4:
288- Multi
289 line
290 change
291- Some changes
292
293Changes in v2:
294- Changes only for this commit'''
295
296 if expected:
297 expected = expected.splitlines()
298 self.assertEqual(expected, lines[start:(start+len(expected))])
Simon Glassa12b1ab2020-07-05 21:41:50 -0600299
300 def make_commit_with_file(self, subject, body, fname, text):
301 """Create a file and add it to the git repo with a new commit
302
303 Args:
304 subject (str): Subject for the commit
305 body (str): Body text of the commit
306 fname (str): Filename of file to create
307 text (str): Text to put into the file
308 """
309 path = os.path.join(self.gitdir, fname)
310 tools.WriteFile(path, text, binary=False)
311 index = self.repo.index
312 index.add(fname)
313 author = pygit2.Signature('Test user', 'test@email.com')
314 committer = author
315 tree = index.write_tree()
316 message = subject + '\n' + body
317 self.repo.create_commit('HEAD', author, committer, message, tree,
318 [self.repo.head.target])
319
320 def make_git_tree(self):
321 """Make a simple git tree suitable for testing
322
323 It has three branches:
324 'base' has two commits: PCI, main
325 'first' has base as upstream and two more commits: I2C, SPI
326 'second' has base as upstream and three more: video, serial, bootm
327
328 Returns:
329 pygit2 repository
330 """
331 repo = pygit2.init_repository(self.gitdir)
332 self.repo = repo
333 new_tree = repo.TreeBuilder().write()
334
335 author = pygit2.Signature('Test user', 'test@email.com')
336 committer = author
337 commit = repo.create_commit('HEAD', author, committer,
338 'Created master', new_tree, [])
339
340 self.make_commit_with_file('Initial commit', '''
341Add a README
342
343''', 'README', '''This is the README file
344describing this project
345in very little detail''')
346
347 self.make_commit_with_file('pci: PCI implementation', '''
348Here is a basic PCI implementation
349
350''', 'pci.c', '''This is a file
351it has some contents
352and some more things''')
353 self.make_commit_with_file('main: Main program', '''
354Hello here is the second commit.
355''', 'main.c', '''This is the main file
356there is very little here
357but we can always add more later
358if we want to
359
360Series-to: u-boot
361Series-cc: Barry Crump <bcrump@whataroa.nz>
362''')
363 base_target = repo.revparse_single('HEAD')
364 self.make_commit_with_file('i2c: I2C things', '''
365This has some stuff to do with I2C
366''', 'i2c.c', '''And this is the file contents
367with some I2C-related things in it''')
368 self.make_commit_with_file('spi: SPI fixes', '''
369SPI needs some fixes
370and here they are
371''', 'spi.c', '''Some fixes for SPI in this
372file to make SPI work
373better than before''')
374 first_target = repo.revparse_single('HEAD')
375
376 target = repo.revparse_single('HEAD~2')
377 repo.reset(target.oid, pygit2.GIT_CHECKOUT_FORCE)
378 self.make_commit_with_file('video: Some video improvements', '''
379Fix up the video so that
380it looks more purple. Purple is
381a very nice colour.
382''', 'video.c', '''More purple here
383Purple and purple
384Even more purple
385Could not be any more purple''')
386 self.make_commit_with_file('serial: Add a serial driver', '''
387Here is the serial driver
388for my chip.
389
390Cover-letter:
391Series for my board
392This series implements support
393for my glorious board.
394END
395''', 'serial.c', '''The code for the
396serial driver is here''')
397 self.make_commit_with_file('bootm: Make it boot', '''
398This makes my board boot
399with a fix to the bootm
400command
401''', 'bootm.c', '''Fix up the bootm
402command to make the code as
403complicated as possible''')
404 second_target = repo.revparse_single('HEAD')
405
406 repo.branches.local.create('first', first_target)
407 repo.config.set_multivar('branch.first.remote', '', '.')
408 repo.config.set_multivar('branch.first.merge', '', 'refs/heads/base')
409
410 repo.branches.local.create('second', second_target)
411 repo.config.set_multivar('branch.second.remote', '', '.')
412 repo.config.set_multivar('branch.second.merge', '', 'refs/heads/base')
413
414 repo.branches.local.create('base', base_target)
415 return repo
416
417 @unittest.skipIf(not HAVE_PYGIT2, 'Missing python3-pygit2')
418 def testBranch(self):
419 """Test creating patches from a branch"""
420 repo = self.make_git_tree()
421 target = repo.lookup_reference('refs/heads/first')
422 self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE)
423 control.setup()
424 try:
425 orig_dir = os.getcwd()
426 os.chdir(self.gitdir)
427
428 # Check that it can detect the current branch
Simon Glassb4aa8aa2020-07-05 21:41:51 -0600429 self.assertEqual(2, gitutil.CountCommitsToBranch(None))
Simon Glassa12b1ab2020-07-05 21:41:50 -0600430 col = terminal.Color()
431 with capture_sys_output() as _:
432 _, cover_fname, patch_files = control.prepare_patches(
Simon Glassb4aa8aa2020-07-05 21:41:51 -0600433 col, branch=None, count=-1, start=0, ignore_binary=False)
Simon Glassa12b1ab2020-07-05 21:41:50 -0600434 self.assertIsNone(cover_fname)
435 self.assertEqual(2, len(patch_files))
Simon Glassb4aa8aa2020-07-05 21:41:51 -0600436
437 # Check that it can detect a different branch
438 self.assertEqual(3, gitutil.CountCommitsToBranch('second'))
439 with capture_sys_output() as _:
440 _, cover_fname, patch_files = control.prepare_patches(
441 col, branch='second', count=-1, start=0,
442 ignore_binary=False)
443 self.assertIsNotNone(cover_fname)
444 self.assertEqual(3, len(patch_files))
Simon Glassa12b1ab2020-07-05 21:41:50 -0600445 finally:
446 os.chdir(orig_dir)