Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 3 | # |
| 4 | # Copyright 2017 Google, Inc |
| 5 | # |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 6 | |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 7 | """Functional tests for checking that patman behaves correctly""" |
| 8 | |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 9 | import os |
| 10 | import re |
| 11 | import shutil |
| 12 | import sys |
| 13 | import tempfile |
| 14 | import unittest |
| 15 | |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 16 | from patman import control |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 17 | from patman import gitutil |
| 18 | from patman import patchstream |
Simon Glass | 47f6295 | 2020-10-29 21:46:26 -0600 | [diff] [blame] | 19 | from patman.patchstream import PatchStream |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 20 | from patman import settings |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 21 | from patman import terminal |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 22 | from patman import tools |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 23 | from patman.test_util import capture_sys_output |
| 24 | |
| 25 | try: |
| 26 | import pygit2 |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 27 | HAVE_PYGIT2 = True |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 28 | except ModuleNotFoundError: |
| 29 | HAVE_PYGIT2 = False |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 30 | |
| 31 | |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 32 | class TestFunctional(unittest.TestCase): |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 33 | """Functional tests for checking that patman behaves correctly""" |
Simon Glass | 7457051 | 2020-10-29 21:46:27 -0600 | [diff] [blame^] | 34 | leb = (b'Lord Edmund Blackadd\xc3\xabr <weasel@blackadder.org>'. |
| 35 | decode('utf-8')) |
| 36 | |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 37 | def setUp(self): |
| 38 | self.tmpdir = tempfile.mkdtemp(prefix='patman.') |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 39 | self.gitdir = os.path.join(self.tmpdir, 'git') |
| 40 | self.repo = None |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 41 | |
| 42 | def tearDown(self): |
| 43 | shutil.rmtree(self.tmpdir) |
| 44 | |
| 45 | @staticmethod |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 46 | def _get_path(fname): |
| 47 | """Get the path to a test file |
| 48 | |
| 49 | Args: |
| 50 | fname (str): Filename to obtain |
| 51 | |
| 52 | Returns: |
| 53 | str: Full path to file in the test directory |
| 54 | """ |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 55 | return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 56 | 'test', fname) |
| 57 | |
| 58 | @classmethod |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 59 | def _get_text(cls, fname): |
| 60 | """Read a file as text |
| 61 | |
| 62 | Args: |
| 63 | fname (str): Filename to read |
| 64 | |
| 65 | Returns: |
| 66 | str: Contents of file |
| 67 | """ |
| 68 | return open(cls._get_path(fname), encoding='utf-8').read() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 69 | |
| 70 | @classmethod |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 71 | def _get_patch_name(cls, subject): |
| 72 | """Get the filename of a patch given its subject |
| 73 | |
| 74 | Args: |
| 75 | subject (str): Patch subject |
| 76 | |
| 77 | Returns: |
| 78 | str: Filename for that patch |
| 79 | """ |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 80 | fname = re.sub('[ :]', '-', subject) |
| 81 | return fname.replace('--', '-') |
| 82 | |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 83 | def _create_patches_for_test(self, series): |
| 84 | """Create patch files for use by tests |
| 85 | |
| 86 | This copies patch files from the test directory as needed by the series |
| 87 | |
| 88 | Args: |
| 89 | series (Series): Series containing commits to convert |
| 90 | |
| 91 | Returns: |
| 92 | tuple: |
| 93 | str: Cover-letter filename, or None if none |
| 94 | fname_list: list of str, each a patch filename |
| 95 | """ |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 96 | cover_fname = None |
| 97 | fname_list = [] |
| 98 | for i, commit in enumerate(series.commits): |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 99 | clean_subject = self._get_patch_name(commit.subject) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 100 | src_fname = '%04d-%s.patch' % (i + 1, clean_subject[:52]) |
| 101 | fname = os.path.join(self.tmpdir, src_fname) |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 102 | shutil.copy(self._get_path(src_fname), fname) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 103 | fname_list.append(fname) |
| 104 | if series.get('cover'): |
| 105 | src_fname = '0000-cover-letter.patch' |
| 106 | cover_fname = os.path.join(self.tmpdir, src_fname) |
| 107 | fname = os.path.join(self.tmpdir, src_fname) |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 108 | shutil.copy(self._get_path(src_fname), fname) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 109 | |
| 110 | return cover_fname, fname_list |
| 111 | |
| 112 | def testBasic(self): |
| 113 | """Tests the basic flow of patman |
| 114 | |
| 115 | This creates a series from some hard-coded patches build from a simple |
| 116 | tree with the following metadata in the top commit: |
| 117 | |
| 118 | Series-to: u-boot |
| 119 | Series-prefix: RFC |
| 120 | Series-cc: Stefan Brüns <stefan.bruens@rwth-aachen.de> |
| 121 | Cover-letter-cc: Lord Mëlchett <clergy@palace.gov> |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 122 | Series-version: 3 |
| 123 | Patch-cc: fred |
| 124 | Series-process-log: sort, uniq |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 125 | Series-changes: 4 |
| 126 | - Some changes |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 127 | - Multi |
| 128 | line |
| 129 | change |
| 130 | |
| 131 | Commit-changes: 2 |
| 132 | - Changes only for this commit |
| 133 | |
| 134 | Cover-changes: 4 |
| 135 | - Some notes for the cover letter |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 136 | |
| 137 | Cover-letter: |
| 138 | test: A test patch series |
| 139 | This is a test of how the cover |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 140 | letter |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 141 | works |
| 142 | END |
| 143 | |
| 144 | and this in the first commit: |
| 145 | |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 146 | Commit-changes: 2 |
| 147 | - second revision change |
| 148 | |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 149 | Series-notes: |
| 150 | some notes |
| 151 | about some things |
| 152 | from the first commit |
| 153 | END |
| 154 | |
| 155 | Commit-notes: |
| 156 | Some notes about |
| 157 | the first commit |
| 158 | END |
| 159 | |
| 160 | with the following commands: |
| 161 | |
| 162 | git log -n2 --reverse >/path/to/tools/patman/test/test01.txt |
| 163 | git format-patch --subject-prefix RFC --cover-letter HEAD~2 |
| 164 | mv 00* /path/to/tools/patman/test |
| 165 | |
| 166 | It checks these aspects: |
| 167 | - git log can be processed by patchstream |
| 168 | - emailing patches uses the correct command |
| 169 | - CC file has information on each commit |
| 170 | - cover letter has the expected text and subject |
| 171 | - each patch has the correct subject |
| 172 | - dry-run information prints out correctly |
| 173 | - unicode is handled correctly |
| 174 | - Series-to, Series-cc, Series-prefix, Cover-letter |
| 175 | - Cover-letter-cc, Series-version, Series-changes, Series-notes |
| 176 | - Commit-notes |
| 177 | """ |
| 178 | process_tags = True |
| 179 | ignore_bad_tags = True |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 180 | stefan = b'Stefan Br\xc3\xbcns <stefan.bruens@rwth-aachen.de>'.decode('utf-8') |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 181 | rick = 'Richard III <richard@palace.gov>' |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 182 | mel = b'Lord M\xc3\xablchett <clergy@palace.gov>'.decode('utf-8') |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 183 | fred = 'Fred Bloggs <f.bloggs@napier.net>' |
| 184 | add_maintainers = [stefan, rick] |
| 185 | dry_run = True |
| 186 | in_reply_to = mel |
| 187 | count = 2 |
| 188 | settings.alias = { |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 189 | 'fdt': ['simon'], |
| 190 | 'u-boot': ['u-boot@lists.denx.de'], |
Simon Glass | 7457051 | 2020-10-29 21:46:27 -0600 | [diff] [blame^] | 191 | 'simon': [self.leb], |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 192 | 'fred': [fred], |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 195 | text = self._get_text('test01.txt') |
Simon Glass | d93720e | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 196 | series = patchstream.get_metadata_for_test(text) |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 197 | cover_fname, args = self._create_patches_for_test(series) |
Simon Glass | 366954f | 2020-10-29 21:46:14 -0600 | [diff] [blame] | 198 | with capture_sys_output() as out: |
Simon Glass | d93720e | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 199 | patchstream.fix_patches(series, args) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 200 | if cover_fname and series.get('cover'): |
Simon Glass | d93720e | 2020-10-29 21:46:19 -0600 | [diff] [blame] | 201 | patchstream.insert_cover_letter(cover_fname, series, count) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 202 | series.DoChecks() |
| 203 | cc_file = series.MakeCcFile(process_tags, cover_fname, |
Chris Packham | 4fb3502 | 2018-06-07 20:45:06 +1200 | [diff] [blame] | 204 | not ignore_bad_tags, add_maintainers, |
| 205 | None) |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 206 | cmd = gitutil.EmailPatches( |
| 207 | series, cover_fname, args, dry_run, not ignore_bad_tags, |
| 208 | cc_file, in_reply_to=in_reply_to, thread=None) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 209 | series.ShowActions(args, cmd, process_tags) |
Simon Glass | 272cd85 | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 210 | cc_lines = open(cc_file, encoding='utf-8').read().splitlines() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 211 | os.remove(cc_file) |
| 212 | |
Simon Glass | 366954f | 2020-10-29 21:46:14 -0600 | [diff] [blame] | 213 | lines = out[0].getvalue().splitlines() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 214 | self.assertEqual('Cleaned %s patches' % len(series.commits), lines[0]) |
| 215 | self.assertEqual('Change log missing for v2', lines[1]) |
| 216 | self.assertEqual('Change log missing for v3', lines[2]) |
| 217 | self.assertEqual('Change log for unknown version v4', lines[3]) |
| 218 | self.assertEqual("Alias 'pci' not found", lines[4]) |
| 219 | self.assertIn('Dry run', lines[5]) |
| 220 | self.assertIn('Send a total of %d patches' % count, lines[7]) |
| 221 | line = 8 |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 222 | for i in range(len(series.commits)): |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 223 | self.assertEqual(' %s' % args[i], lines[line + 0]) |
| 224 | line += 1 |
| 225 | while 'Cc:' in lines[line]: |
| 226 | line += 1 |
| 227 | self.assertEqual('To: u-boot@lists.denx.de', lines[line]) |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 228 | self.assertEqual('Cc: %s' % tools.FromUnicode(stefan), |
| 229 | lines[line + 1]) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 230 | self.assertEqual('Version: 3', lines[line + 2]) |
| 231 | self.assertEqual('Prefix:\t RFC', lines[line + 3]) |
| 232 | self.assertEqual('Cover: 4 lines', lines[line + 4]) |
| 233 | line += 5 |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 234 | self.assertEqual(' Cc: %s' % fred, lines[line + 0]) |
Simon Glass | 7457051 | 2020-10-29 21:46:27 -0600 | [diff] [blame^] | 235 | self.assertEqual(' Cc: %s' % tools.FromUnicode(self.leb), |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 236 | lines[line + 1]) |
| 237 | self.assertEqual(' Cc: %s' % tools.FromUnicode(mel), |
| 238 | lines[line + 2]) |
Simon Glass | b644c66 | 2019-05-14 15:53:51 -0600 | [diff] [blame] | 239 | self.assertEqual(' Cc: %s' % rick, lines[line + 3]) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 240 | expected = ('Git command: git send-email --annotate ' |
| 241 | '--in-reply-to="%s" --to "u-boot@lists.denx.de" ' |
| 242 | '--cc "%s" --cc-cmd "%s --cc-cmd %s" %s %s' |
| 243 | % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname, |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 244 | ' '.join(args))) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 245 | line += 4 |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 246 | self.assertEqual(expected, tools.ToUnicode(lines[line])) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 247 | |
Dmitry Torokhov | 8ab452d | 2019-10-21 20:09:56 -0700 | [diff] [blame] | 248 | self.assertEqual(('%s %s\0%s' % (args[0], rick, stefan)), |
Simon Glass | e6dca5e | 2019-05-14 15:53:53 -0600 | [diff] [blame] | 249 | tools.ToUnicode(cc_lines[0])) |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 250 | self.assertEqual( |
Simon Glass | 7457051 | 2020-10-29 21:46:27 -0600 | [diff] [blame^] | 251 | '%s %s\0%s\0%s\0%s' % (args[1], fred, self.leb, rick, stefan), |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 252 | tools.ToUnicode(cc_lines[1])) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 253 | |
| 254 | expected = ''' |
| 255 | This is a test of how the cover |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 256 | letter |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 257 | works |
| 258 | |
| 259 | some notes |
| 260 | about some things |
| 261 | from the first commit |
| 262 | |
| 263 | Changes in v4: |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 264 | - Multi |
| 265 | line |
| 266 | change |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 267 | - Some changes |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 268 | - Some notes for the cover letter |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 269 | |
| 270 | Simon Glass (2): |
| 271 | pci: Correct cast for sandbox |
Siva Durga Prasad Paladugu | 12308b1 | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 272 | fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 273 | |
| 274 | cmd/pci.c | 3 ++- |
| 275 | fs/fat/fat.c | 1 + |
| 276 | lib/efi_loader/efi_memory.c | 1 + |
| 277 | lib/fdtdec.c | 3 ++- |
| 278 | 4 files changed, 6 insertions(+), 2 deletions(-) |
| 279 | |
| 280 | --\x20 |
| 281 | 2.7.4 |
| 282 | |
| 283 | ''' |
Simon Glass | 272cd85 | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 284 | lines = open(cover_fname, encoding='utf-8').read().splitlines() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 285 | self.assertEqual( |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 286 | 'Subject: [RFC PATCH v3 0/2] test: A test patch series', |
| 287 | lines[3]) |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 288 | self.assertEqual(expected.splitlines(), lines[7:]) |
| 289 | |
| 290 | for i, fname in enumerate(args): |
Simon Glass | 272cd85 | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 291 | lines = open(fname, encoding='utf-8').read().splitlines() |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 292 | subject = [line for line in lines if line.startswith('Subject')] |
| 293 | self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count), |
| 294 | subject[0][:18]) |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 295 | |
| 296 | # Check that we got our commit notes |
| 297 | start = 0 |
| 298 | expected = '' |
| 299 | |
Simon Glass | 6e87ae1 | 2017-05-29 15:31:31 -0600 | [diff] [blame] | 300 | if i == 0: |
Sean Anderson | dc03ba4 | 2020-05-04 16:28:36 -0400 | [diff] [blame] | 301 | start = 17 |
| 302 | expected = '''--- |
| 303 | Some notes about |
| 304 | the first commit |
| 305 | |
| 306 | (no changes since v2) |
| 307 | |
| 308 | Changes in v2: |
| 309 | - second revision change''' |
| 310 | elif i == 1: |
| 311 | start = 17 |
| 312 | expected = '''--- |
| 313 | |
| 314 | Changes in v4: |
| 315 | - Multi |
| 316 | line |
| 317 | change |
| 318 | - Some changes |
| 319 | |
| 320 | Changes in v2: |
| 321 | - Changes only for this commit''' |
| 322 | |
| 323 | if expected: |
| 324 | expected = expected.splitlines() |
| 325 | self.assertEqual(expected, lines[start:(start+len(expected))]) |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 326 | |
| 327 | def make_commit_with_file(self, subject, body, fname, text): |
| 328 | """Create a file and add it to the git repo with a new commit |
| 329 | |
| 330 | Args: |
| 331 | subject (str): Subject for the commit |
| 332 | body (str): Body text of the commit |
| 333 | fname (str): Filename of file to create |
| 334 | text (str): Text to put into the file |
| 335 | """ |
| 336 | path = os.path.join(self.gitdir, fname) |
| 337 | tools.WriteFile(path, text, binary=False) |
| 338 | index = self.repo.index |
| 339 | index.add(fname) |
Simon Glass | 427b028 | 2020-10-29 21:46:13 -0600 | [diff] [blame] | 340 | author = pygit2.Signature('Test user', 'test@email.com') |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 341 | committer = author |
| 342 | tree = index.write_tree() |
| 343 | message = subject + '\n' + body |
| 344 | self.repo.create_commit('HEAD', author, committer, message, tree, |
| 345 | [self.repo.head.target]) |
| 346 | |
| 347 | def make_git_tree(self): |
| 348 | """Make a simple git tree suitable for testing |
| 349 | |
| 350 | It has three branches: |
| 351 | 'base' has two commits: PCI, main |
| 352 | 'first' has base as upstream and two more commits: I2C, SPI |
| 353 | 'second' has base as upstream and three more: video, serial, bootm |
| 354 | |
| 355 | Returns: |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 356 | pygit2.Repository: repository |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 357 | """ |
| 358 | repo = pygit2.init_repository(self.gitdir) |
| 359 | self.repo = repo |
| 360 | new_tree = repo.TreeBuilder().write() |
| 361 | |
| 362 | author = pygit2.Signature('Test user', 'test@email.com') |
| 363 | committer = author |
Simon Glass | fca9911 | 2020-10-29 21:46:15 -0600 | [diff] [blame] | 364 | _ = repo.create_commit('HEAD', author, committer, 'Created master', |
| 365 | new_tree, []) |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 366 | |
| 367 | self.make_commit_with_file('Initial commit', ''' |
| 368 | Add a README |
| 369 | |
| 370 | ''', 'README', '''This is the README file |
| 371 | describing this project |
| 372 | in very little detail''') |
| 373 | |
| 374 | self.make_commit_with_file('pci: PCI implementation', ''' |
| 375 | Here is a basic PCI implementation |
| 376 | |
| 377 | ''', 'pci.c', '''This is a file |
| 378 | it has some contents |
| 379 | and some more things''') |
| 380 | self.make_commit_with_file('main: Main program', ''' |
| 381 | Hello here is the second commit. |
| 382 | ''', 'main.c', '''This is the main file |
| 383 | there is very little here |
| 384 | but we can always add more later |
| 385 | if we want to |
| 386 | |
| 387 | Series-to: u-boot |
| 388 | Series-cc: Barry Crump <bcrump@whataroa.nz> |
| 389 | ''') |
| 390 | base_target = repo.revparse_single('HEAD') |
| 391 | self.make_commit_with_file('i2c: I2C things', ''' |
| 392 | This has some stuff to do with I2C |
| 393 | ''', 'i2c.c', '''And this is the file contents |
| 394 | with some I2C-related things in it''') |
| 395 | self.make_commit_with_file('spi: SPI fixes', ''' |
| 396 | SPI needs some fixes |
| 397 | and here they are |
| 398 | ''', 'spi.c', '''Some fixes for SPI in this |
| 399 | file to make SPI work |
| 400 | better than before''') |
| 401 | first_target = repo.revparse_single('HEAD') |
| 402 | |
| 403 | target = repo.revparse_single('HEAD~2') |
| 404 | repo.reset(target.oid, pygit2.GIT_CHECKOUT_FORCE) |
| 405 | self.make_commit_with_file('video: Some video improvements', ''' |
| 406 | Fix up the video so that |
| 407 | it looks more purple. Purple is |
| 408 | a very nice colour. |
| 409 | ''', 'video.c', '''More purple here |
| 410 | Purple and purple |
| 411 | Even more purple |
| 412 | Could not be any more purple''') |
| 413 | self.make_commit_with_file('serial: Add a serial driver', ''' |
| 414 | Here is the serial driver |
| 415 | for my chip. |
| 416 | |
| 417 | Cover-letter: |
| 418 | Series for my board |
| 419 | This series implements support |
| 420 | for my glorious board. |
| 421 | END |
Simon Glass | f9e4284 | 2020-10-29 21:46:16 -0600 | [diff] [blame] | 422 | Series-links: 183237 |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 423 | ''', 'serial.c', '''The code for the |
| 424 | serial driver is here''') |
| 425 | self.make_commit_with_file('bootm: Make it boot', ''' |
| 426 | This makes my board boot |
| 427 | with a fix to the bootm |
| 428 | command |
| 429 | ''', 'bootm.c', '''Fix up the bootm |
| 430 | command to make the code as |
| 431 | complicated as possible''') |
| 432 | second_target = repo.revparse_single('HEAD') |
| 433 | |
| 434 | repo.branches.local.create('first', first_target) |
| 435 | repo.config.set_multivar('branch.first.remote', '', '.') |
| 436 | repo.config.set_multivar('branch.first.merge', '', 'refs/heads/base') |
| 437 | |
| 438 | repo.branches.local.create('second', second_target) |
| 439 | repo.config.set_multivar('branch.second.remote', '', '.') |
| 440 | repo.config.set_multivar('branch.second.merge', '', 'refs/heads/base') |
| 441 | |
| 442 | repo.branches.local.create('base', base_target) |
| 443 | return repo |
| 444 | |
| 445 | @unittest.skipIf(not HAVE_PYGIT2, 'Missing python3-pygit2') |
| 446 | def testBranch(self): |
| 447 | """Test creating patches from a branch""" |
| 448 | repo = self.make_git_tree() |
| 449 | target = repo.lookup_reference('refs/heads/first') |
| 450 | self.repo.checkout(target, strategy=pygit2.GIT_CHECKOUT_FORCE) |
| 451 | control.setup() |
| 452 | try: |
| 453 | orig_dir = os.getcwd() |
| 454 | os.chdir(self.gitdir) |
| 455 | |
| 456 | # Check that it can detect the current branch |
Simon Glass | 262130f | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 457 | self.assertEqual(2, gitutil.CountCommitsToBranch(None)) |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 458 | col = terminal.Color() |
| 459 | with capture_sys_output() as _: |
| 460 | _, cover_fname, patch_files = control.prepare_patches( |
Simon Glass | 137947e | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 461 | col, branch=None, count=-1, start=0, end=0, |
| 462 | ignore_binary=False) |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 463 | self.assertIsNone(cover_fname) |
| 464 | self.assertEqual(2, len(patch_files)) |
Simon Glass | 262130f | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 465 | |
| 466 | # Check that it can detect a different branch |
| 467 | self.assertEqual(3, gitutil.CountCommitsToBranch('second')) |
| 468 | with capture_sys_output() as _: |
| 469 | _, cover_fname, patch_files = control.prepare_patches( |
Simon Glass | 137947e | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 470 | col, branch='second', count=-1, start=0, end=0, |
Simon Glass | 262130f | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 471 | ignore_binary=False) |
| 472 | self.assertIsNotNone(cover_fname) |
| 473 | self.assertEqual(3, len(patch_files)) |
Simon Glass | 137947e | 2020-07-05 21:41:52 -0600 | [diff] [blame] | 474 | |
| 475 | # Check that it can skip patches at the end |
| 476 | with capture_sys_output() as _: |
| 477 | _, cover_fname, patch_files = control.prepare_patches( |
| 478 | col, branch='second', count=-1, start=0, end=1, |
| 479 | ignore_binary=False) |
| 480 | self.assertIsNotNone(cover_fname) |
| 481 | self.assertEqual(2, len(patch_files)) |
Simon Glass | fd70986 | 2020-07-05 21:41:50 -0600 | [diff] [blame] | 482 | finally: |
| 483 | os.chdir(orig_dir) |
Simon Glass | 7457051 | 2020-10-29 21:46:27 -0600 | [diff] [blame^] | 484 | |
| 485 | def testTags(self): |
| 486 | """Test collection of tags in a patchstream""" |
| 487 | text = '''This is a patch |
| 488 | |
| 489 | Signed-off-by: Terminator |
| 490 | Reviewed-by: Joe Bloggs <joe@napierwallies.co.nz> |
| 491 | Reviewed-by: Mary Bloggs <mary@napierwallies.co.nz> |
| 492 | Tested-by: %s |
| 493 | ''' % self.leb |
| 494 | pstrm = PatchStream.process_text(text) |
| 495 | self.assertEqual(pstrm.commit.rtags, { |
| 496 | 'Reviewed-by': {'Mary Bloggs <mary@napierwallies.co.nz>', |
| 497 | 'Joe Bloggs <joe@napierwallies.co.nz>'}, |
| 498 | 'Tested-by': {self.leb}}) |