1 # -*- coding: utf-8 -*-
2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright 2017 Google, Inc
16 from StringIO import StringIO
18 from io import StringIO
26 @contextlib.contextmanager
29 oldout,olderr = sys.stdout, sys.stderr
31 out=[StringIO(), StringIO()]
32 sys.stdout,sys.stderr = out
35 sys.stdout,sys.stderr = oldout, olderr
36 out[0] = out[0].getvalue()
37 out[1] = out[1].getvalue()
40 class TestFunctional(unittest.TestCase):
42 self.tmpdir = tempfile.mkdtemp(prefix='patman.')
45 shutil.rmtree(self.tmpdir)
49 return os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
53 def GetText(self, fname):
54 return open(self.GetPath(fname)).read()
57 def GetPatchName(self, subject):
58 fname = re.sub('[ :]', '-', subject)
59 return fname.replace('--', '-')
61 def CreatePatchesForTest(self, series):
64 for i, commit in enumerate(series.commits):
65 clean_subject = self.GetPatchName(commit.subject)
66 src_fname = '%04d-%s.patch' % (i + 1, clean_subject[:52])
67 fname = os.path.join(self.tmpdir, src_fname)
68 shutil.copy(self.GetPath(src_fname), fname)
69 fname_list.append(fname)
70 if series.get('cover'):
71 src_fname = '0000-cover-letter.patch'
72 cover_fname = os.path.join(self.tmpdir, src_fname)
73 fname = os.path.join(self.tmpdir, src_fname)
74 shutil.copy(self.GetPath(src_fname), fname)
76 return cover_fname, fname_list
79 """Tests the basic flow of patman
81 This creates a series from some hard-coded patches build from a simple
82 tree with the following metadata in the top commit:
86 Series-cc: Stefan Brüns <stefan.bruens@rwth-aachen.de>
87 Cover-letter-cc: Lord Mëlchett <clergy@palace.gov>
93 test: A test patch series
94 This is a test of how the cover
99 and this in the first commit:
104 from the first commit
112 with the following commands:
114 git log -n2 --reverse >/path/to/tools/patman/test/test01.txt
115 git format-patch --subject-prefix RFC --cover-letter HEAD~2
116 mv 00* /path/to/tools/patman/test
118 It checks these aspects:
119 - git log can be processed by patchstream
120 - emailing patches uses the correct command
121 - CC file has information on each commit
122 - cover letter has the expected text and subject
123 - each patch has the correct subject
124 - dry-run information prints out correctly
125 - unicode is handled correctly
126 - Series-to, Series-cc, Series-prefix, Cover-letter
127 - Cover-letter-cc, Series-version, Series-changes, Series-notes
131 ignore_bad_tags = True
132 stefan = u'Stefan Brüns <stefan.bruens@rwth-aachen.de>'
133 rick = 'Richard III <richard@palace.gov>'
134 mel = u'Lord Mëlchett <clergy@palace.gov>'
135 ed = u'Lond Edmund Blackaddër <weasel@blackadder.org'
136 fred = 'Fred Bloggs <f.bloggs@napier.net>'
137 add_maintainers = [stefan, rick]
143 'u-boot': ['u-boot@lists.denx.de'],
148 text = self.GetText('test01.txt')
149 series = patchstream.GetMetaDataForTest(text)
150 cover_fname, args = self.CreatePatchesForTest(series)
151 with capture() as out:
152 patchstream.FixPatches(series, args)
153 if cover_fname and series.get('cover'):
154 patchstream.InsertCoverLetter(cover_fname, series, count)
156 cc_file = series.MakeCcFile(process_tags, cover_fname,
157 not ignore_bad_tags, add_maintainers,
159 cmd = gitutil.EmailPatches(series, cover_fname, args,
160 dry_run, not ignore_bad_tags, cc_file,
161 in_reply_to=in_reply_to, thread=None)
162 series.ShowActions(args, cmd, process_tags)
163 cc_lines = open(cc_file).read().splitlines()
166 lines = out[0].splitlines()
167 self.assertEqual('Cleaned %s patches' % len(series.commits), lines[0])
168 self.assertEqual('Change log missing for v2', lines[1])
169 self.assertEqual('Change log missing for v3', lines[2])
170 self.assertEqual('Change log for unknown version v4', lines[3])
171 self.assertEqual("Alias 'pci' not found", lines[4])
172 self.assertIn('Dry run', lines[5])
173 self.assertIn('Send a total of %d patches' % count, lines[7])
175 for i, commit in enumerate(series.commits):
176 self.assertEqual(' %s' % args[i], lines[line + 0])
178 while 'Cc:' in lines[line]:
180 self.assertEqual('To: u-boot@lists.denx.de', lines[line])
181 self.assertEqual('Cc: %s' % stefan.encode('utf-8'), lines[line + 1])
182 self.assertEqual('Version: 3', lines[line + 2])
183 self.assertEqual('Prefix:\t RFC', lines[line + 3])
184 self.assertEqual('Cover: 4 lines', lines[line + 4])
186 self.assertEqual(' Cc: %s' % mel.encode('utf-8'), lines[line + 0])
187 self.assertEqual(' Cc: %s' % rick, lines[line + 1])
188 self.assertEqual(' Cc: %s' % fred, lines[line + 2])
189 self.assertEqual(' Cc: %s' % ed.encode('utf-8'), lines[line + 3])
190 expected = ('Git command: git send-email --annotate '
191 '--in-reply-to="%s" --to "u-boot@lists.denx.de" '
192 '--cc "%s" --cc-cmd "%s --cc-cmd %s" %s %s'
193 % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname,
194 ' '.join(args))).encode('utf-8')
196 self.assertEqual(expected, lines[line])
198 self.assertEqual(('%s %s, %s' % (args[0], rick, stefan))
199 .encode('utf-8'), cc_lines[0])
200 self.assertEqual(('%s %s, %s, %s, %s' % (args[1], fred, rick, stefan,
201 ed)).encode('utf-8'), cc_lines[1])
204 This is a test of how the cover
210 from the first commit
216 pci: Correct cast for sandbox
217 fdt: Correct cast for sandbox in fdtdec_setup_mem_size_base()
221 lib/efi_loader/efi_memory.c | 1 +
223 4 files changed, 6 insertions(+), 2 deletions(-)
229 lines = open(cover_fname).read().splitlines()
231 'Subject: [RFC PATCH v3 0/2] test: A test patch series',
233 self.assertEqual(expected.splitlines(), lines[7:])
235 for i, fname in enumerate(args):
236 lines = open(fname).read().splitlines()
237 subject = [line for line in lines if line.startswith('Subject')]
238 self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count),
241 # Check that we got our commit notes
242 self.assertEqual('---', lines[17])
243 self.assertEqual('Some notes about', lines[18])
244 self.assertEqual('the first commit', lines[19])