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