summaryrefslogtreecommitdiff
path: root/patchwork/tests/test_series.py
blob: e68ee88e92ab33130081d044ca36c5b8af29882b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Stephen Finucane <stephenfinucane@hotmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later

import mailbox
import os

from django.test import TestCase

from patchwork import models
from patchwork import parser
from patchwork.tests import utils
from patchwork.views.utils import patch_to_mbox


TEST_SERIES_DIR = os.path.join(os.path.dirname(__file__), 'series')


class _BaseTestCase(TestCase):

    def setUp(self):
        utils.create_state()

    def _parse_mbox(self, name, counts, project=None):
        """Parse an mbox file and return the results.

        :param name: Name of mbox file
        :param counts: A three-tuple of expected number of cover
            letters, patches and replies parsed
        """
        results = [[], [], []]
        project = project or utils.create_project()

        mbox = mailbox.mbox(os.path.join(TEST_SERIES_DIR, name), create=False)
        for msg in mbox:
            obj = parser.parse_mail(msg, project.listid)
            if type(obj) == models.Cover:
                results[0].append(obj)
            elif type(obj) == models.Patch:
                results[1].append(obj)
            else:
                results[2].append(obj)
        mbox.close()

        self.assertParsed(results, counts)

        return results

    def assertParsed(self, results, counts):
        self.assertEqual([len(x) for x in results], counts)

    def assertSerialized(self, patches, counts):
        """Validate correct series-ification.

        TODO(stephen): Eventually this should ensure the series
          revisions correctly linked and ordered in a series group

        :param patches: list of Patch instances
        :param count: list of integers corrsponding to number of
            patches per series
        """
        series = models.Series.objects.all().order_by('date')

        # sort this lists by series date so we don't have simple sorting issues
        patches.sort(key=lambda x: x.series.date)

        # sanity checks
        self.assertEqual(series.count(), len(counts))
        self.assertEqual(sum(counts), len(patches))

        # walk through each series, ensuring each indexed patch
        # corresponds to the correct series
        start_idx = 0
        for idx, count in enumerate(counts):
            end_idx = start_idx + count

            for patch in patches[start_idx:end_idx]:
                self.assertEqual(patch.series, series[idx])

                # TODO(stephenfin): Rework this function into two different
                # functions - we're clearly not always testing patches here
                if isinstance(patch, models.Patch):
                    self.assertEqual(series[idx].patches.get(id=patch.id),
                                     patch)
                else:
                    self.assertEqual(series[idx].cover_letter, patch)

            start_idx = end_idx


class BaseSeriesTest(_BaseTestCase):
    """Tests for a series without any revisions."""

    def test_single_patch(self):
        """Series with only a single patch.

        Parse a "series" with only a single patch and no subject prefixes.

        Input:

          - [PATCH] test: Add some lorem ipsum
        """
        _, patches, _ = self._parse_mbox(
            'base-single-patch.mbox', [0, 1, 0])

        self.assertSerialized(patches, [1])

    def test_cover_letter(self):
        """Series with a cover letter.

        Parse a series with a cover letter and two patches.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'base-cover-letter.mbox', [1, 2, 0])

        self.assertSerialized(patches, [2])
        self.assertSerialized(covers, [1])

    def test_no_cover_letter(self):
        """Series without a cover letter.

        Parse a series with two patches but no cover letter.

        Input:

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        _, patches, _ = self._parse_mbox(
            'base-no-cover-letter.mbox', [0, 2, 0])

        self.assertSerialized(patches, [2])

    def test_deep_threaded(self):
        """Series with deep threading.

        Parse a series with a cover letter and two patches that uses
        deep threading (git-format-patch --thread=deep).

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
              - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'base-deep-threaded.mbox', [1, 2, 0])

        self.assertSerialized(patches, [2])
        self.assertSerialized(covers, [1])

    def test_out_of_order(self):
        """Series received out of order.

        Parse a series with a cover letter and two patches that is
        received out of order.

        Input:

            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH 1/2] test: Add some lorem ipsum
          - [PATCH 0/2] A sample series
        """
        covers, patches, _ = self._parse_mbox(
            'base-out-of-order.mbox', [1, 2, 0])

        self.assertSerialized(patches, [2])
        self.assertSerialized(covers, [1])

    def test_duplicated(self):
        """Series received on multiple mailing lists.

        Parse a series with a two patches sent to two mailing lists
        at the same time.

        Input:

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        project_a = utils.create_project()
        project_b = utils.create_project()

        _, patches_a, _ = self._parse_mbox(
            'base-no-cover-letter.mbox', [0, 2, 0], project=project_a)
        _, patches_b, _ = self._parse_mbox(
            'base-no-cover-letter.mbox', [0, 2, 0], project=project_b)

        self.assertSerialized(patches_a + patches_b, [2, 2])

    def test_different_versions(self):
        """Series received with different version on cover to patches.

        Input:
          - [PATCH net-next v3 0/4] net: dsa: Multi-CPU ground work (v3)
            - [PATCH 1/4] net: dsa: Remove master_netdev and use
                dst->cpu_dp->netdev
            - [PATCH 2/4] net: dsa: Relocate master ethtool operations
            - [PATCH 3/4] net: dsa: Associate slave network device with CPU
                port
            - [PATCH 4/4] net: dsa: Introduce dsa_get_cpu_port()
        """
        covers, patches, _ = self._parse_mbox(
            'base-different-versions.mbox', [1, 4, 0])

        self.assertSerialized(covers, [1])
        self.assertSerialized(patches, [4])

    def test_multiple_references(self):
        """Series received with multiple reference headers.

        Parse a series with four patches and a cover letter that is received
        with multiple reference headers.

        Input:
          - [PATCH V2 0/4] PM / OPP: Minor cleanups
            - [PATCH V2 1/4] PM / OPP: Reorganize _generic_set_opp_regulator()
            - [PATCH V2 2/4] PM / OPP: Don't create copy of regulators
                unnecessarily
            - PATCH V2 3/4] PM / OPP: opp-microvolt is not optional if
                regulators are set
            - [PATCH V2 4/4] PM / OPP: Don't create debugfs "supply-0"
                directory unnecessarily
        """
        covers, patches, _ = self._parse_mbox(
            'bugs-multiple-references.mbox', [1, 4, 0])

        self.assertSerialized(covers, [1])
        self.assertSerialized(patches, [4])

    def test_no_references(self):
        """Series received with no reference headers.

        Parse a series with two patches that is received without any
        reference headers.

        Input:
          - [PATCH 1/2] net: ieee802154: remove explicit set skb->sk
          - [PATCH 2/2] net: ieee802154: fix net_device reference release too
        """
        _, patches, _ = self._parse_mbox(
            'base-no-references.mbox', [0, 2, 0])

        self.assertSerialized(patches, [2])

    def test_no_references_no_cover(self):
        """Series received with no reference headers or cover letter.

        Parse a series with a cover letter and two patches that is received
        without any reference headers.

        Input:
          - [PATCH 0/2] powerpc/dlpar: Correct display of hot-add/hot-remove
                CPUs
            - [PATCH 1/2] powerpc/numa: Update CPU topology when VPHN enabled
            - [Patch 2/2]: powerpc/hotplug/mm: Fix hot-add memory node assoc
        """
        covers, patches, _ = self._parse_mbox(
            'base-no-references-no-cover.mbox', [1, 2, 0])

        self.assertSerialized(patches, [2])
        self.assertSerialized(covers, [1])

    def test_multiple_content_types(self):
        """Test what happens when a patch and comment have different
        Content-Type headers."""

        _, patches, _ = self._parse_mbox(
            'bugs-multiple-content-types.mbox', [0, 1, 1])

        patch = patches[0]
        self.assertEqual(patch_to_mbox(patch).count('Content-Type:'), 1)


class RevisedSeriesTest(_BaseTestCase):
    """Tests for a series plus a single revision.

    NOTE(stephenfin): In each sample mbox, it is necessary to ensure
      the first series is placed before the revision. If not, they will
      not be parsed correctly. This is OK as in practice it would very
      unlikely to receive a revision before a previous revision.
    """

    def test_basic(self):
        """Series with a simple revision.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is correctly
        labeled and is not sent in reply to the first revision.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
          - [PATCH v2 0/2] A sample series
            - [PATCH v2 1/2] test: Add some lorem ipsum
            - [PATCH v2 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'revision-basic.mbox', [2, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 1])

    def test_threaded_to_single_patch(self):
        """Series with a revision sent in-reply-to a single-patch series.

        Parse a series with a single patch, followed by a second revision of
        the same. The second revision is correctly labeled but is sent in reply
        to the original patch.

          - [PATCH] test: Add some lorem ipsum
            - [PATCH v2] test: Add some lorem ipsum
        """
        _, patches, _ = self._parse_mbox(
            'revision-threaded-to-single-patch.mbox', [0, 2, 0])

        self.assertSerialized(patches, [1, 1])

    def test_threaded_to_cover(self):
        """Series with a revision sent in-reply-to a cover.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is correctly
        labeled but is sent in reply to the cover letter of the first
        revision.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH v2 0/2] A sample series
              - [PATCH v2 1/2] test: Add some lorem ipsum
              - [PATCH v2 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'revision-threaded-to-cover.mbox', [2, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 1])

    def test_threaded_to_patch(self):
        """Series with a revision sent in-reply-to a patch.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is correctly
        labeled but is sent in reply to the second patch of the first
        revision.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
              - [PATCH v2 0/2] A sample series
                - [PATCH v2 1/2] test: Add some lorem ipsum
                - [PATCH v2 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'revision-threaded-to-patch.mbox', [2, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 1])

    def test_out_of_order(self):
        """Series with a revision received out-of-order.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is correctly
        labeled but is sent in reply to the second patch and is
        received out of order.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
                - [PATCH v2 2/2] test: Convert to Markdown
                - [PATCH v2 1/2] test: Add some lorem ipsum
              - [PATCH v2 0/2] A sample series
        """
        covers, patches, _ = self._parse_mbox(
            'revision-out-of-order.mbox', [2, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 1])

    def test_no_cover_letter(self):
        """Series with a revision sent without a cover letter.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is not
        labeled with a series version marker.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'revision-no-cover-letter.mbox', [1, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 0])

    def test_unlabeled(self):
        """Series with a revision sent without a version label.

        Parse a series with a cover letter and two patches, followed by
        a second revision of the same. The second revision is not
        labeled with a series version marker.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'revision-unlabeled.mbox', [2, 4, 0])

        self.assertSerialized(patches, [2, 2])
        self.assertSerialized(covers, [1, 1])

    def test_unlabeled_noreferences(self):
        """Series with a revision sent without a version label or
        reference headers.

        Parse a series with two patches, followed by a second revision of the
        same. The second revision is not labeled with a series version marker
        and neither revision contains reference headers. Only the message-ids
        and receipt times vary (by one hour).

        Input:
          - [PATCH 1/2] net: ieee802154: remove explicit set skb->sk
          - [PATCH 2/2] net: ieee802154: fix net_device reference release too
          - [PATCH 1/2] net: ieee802154: remove explicit set skb->sk
          - [PATCH 2/2] net: ieee802154: fix net_device reference release too
        """
        _, patches, _ = self._parse_mbox(
            'revision-unlabeled-noreferences.mbox', [0, 4, 0])

        self.assertSerialized(patches, [2, 2])

    def test_unnumbered(self):
        """Series with a reply with a diff but no number.

        The random message with the diff should not belong to the
        series, as it lacks a n/N label. We expect two series and the
        random message to be assigned its own series.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - This is an orphaned patch!
        """
        covers, patches, _ = self._parse_mbox(
            'bugs-unnumbered.mbox', [1, 2, 0])

        self.assertSerialized(patches, [1, 1])
        self.assertSerialized(covers, [1, 0])

    def test_reply_nocover_noversion(self):
        """Series with a revision sent without a version label or cover
        letter, in reply to earlier version of the same series.

        Parse a series with two patches, followed by a second revision
        of the same. The second revision is not labeled with a series
        version marker.

        This is really, really annoying and people shouldn't do it.

        Input:

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH 1/2] test: Add some lorem ipsum
              - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'bugs-nocover-noversion.mbox', [0, 4, 0])

        self.assertSerialized(patches, [2, 2])

    def test_reply_nocover(self):
        """Series with a revision sent in-reply-to a patch, no cover letters.

        Parse a series with two patches, followed by a second revision
        of the same. The second revision is correctly labeled but is
        sent in reply to the second patch of the first revision.

        Input:

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
              - [PATCH v2 1/2] test: Add some lorem ipsum
                - [PATCH v2 2/2] test: Convert to Markdown
        """
        _, patches, _ = self._parse_mbox(
            'bugs-nocover.mbox', [0, 4, 0])

        self.assertSerialized(patches, [2, 2])

    def test_spamming(self):
        """Series submitted multiple times to the mailing list in quick
        succession.

        Parse a series being submitted multiple times in quick succession,
        which prevents our timeboxing from splitting the lists up. This should
        result in multiple separate series.

        Input:

          - [PATCH v2 1/4] Rework tagging infrastructure
          - [PATCH v2 1/4] Rework tagging infrastructure
          - [PATCH v2 1/4] Rework tagging infrastructure
        """
        _, patches, _ = self._parse_mbox(
            'bugs-spamming.mbox', [0, 3, 0])

        self.assertSerialized(patches, [1, 1, 1])

    def test_mixed_versions(self):
        """Series with a revision sent in reply to an incompleted series.

        Parse a series with two patches, one of which has been lost or
        miscategorized, followed by a second revision of the missing patch.
        None of the patches of the second revision should be included in the
        first revision.

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH v2 2/2] test: Convert to Markdown
        """
        _, patches, _ = self._parse_mbox(
            'bugs-mixed-versions.mbox', [0, 2, 0],
        )

        self.assertSerialized(patches, [1, 1])


class SeriesTotalTest(_BaseTestCase):

    def test_incomplete(self):
        """Series received with patches missing.

        Parse a series where not all patches were received.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
        """
        covers, patches, _ = self._parse_mbox(
            'base-incomplete.mbox', [1, 1, 0])

        self.assertSerialized(patches, [1])
        self.assertSerialized(covers, [1])

        series = patches[0].series
        self.assertFalse(series.received_all)

    def test_complete(self):
        """Series received with expected number of patches.

        Parse a series where all patches are received as expected.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        covers, patches, _ = self._parse_mbox(
            'base-cover-letter.mbox', [1, 2, 0])

        self.assertSerialized(covers, [1])
        self.assertSerialized(patches, [2])

        series = patches[0].series
        self.assertTrue(series.received_all)

    def test_extra_patches(self):
        """Series received with additional patches.

        Parse a series where an additional patch was later sent.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH 3/n] test: Remove Markdown formatting
        """
        covers, patches, _ = self._parse_mbox(
            'base-extra-patches.mbox', [1, 3, 0])

        self.assertSerialized(covers, [1])
        self.assertSerialized(patches, [3])

        series = patches[0].series
        self.assertTrue(series.received_all)


class MercurialSeriesTest(_BaseTestCase):
    """Tests for a series without any revisions.

    All patches are generated using hg(1) email, provided via the Patchbomb
    extension.
    """

    def test_cover_letter(self):
        """Series with a cover letter.

        Parse a series with a cover letter and two patches.

        Input:

          - [PATCH 0 of 2] Sample Mercurial patches
            - [PATCH 1 of 2] contrib: fix check-commit to not reject
                commits from `hg sign` and `hg tag`
            - [PATCH 2 of 2] tests: work around FreeBSD's unzip having
                slightly different output
        """
        covers, patches, comments = self._parse_mbox(
            'mercurial-cover-letter.mbox', [1, 2, 0])

        self.assertSerialized(patches, [2])
        self.assertSerialized(covers, [1])

    def test_no_cover_letter(self):
        """Series without a cover letter.

        Parse a series with two patches but no cover letter.

        Input:

          - [PATCH 1 of 2] contrib: fix check-commit to not reject
              commits from `hg sign` and `hg tag`
            - [PATCH 2 of 2] tests: work around FreeBSD's unzip having
                slightly different output
        """
        _, patches, _ = self._parse_mbox(
            'mercurial-no-cover-letter.mbox', [0, 2, 0])

        self.assertSerialized(patches, [2])


class SeriesNameTestCase(TestCase):

    def setUp(self):
        self.project = utils.create_project()
        utils.create_state()

    @staticmethod
    def _get_mbox(name):
        """Open an mbox file.

        :param name: Name of mbox file
        """
        return mailbox.mbox(os.path.join(TEST_SERIES_DIR, name), create=False)

    def _parse_mail(self, mail):
        return parser.parse_mail(mail, self.project.listid)

    def test_cover_letter(self):
        """Cover letter name set as series name.

        Parse a series with a cover letter and two patches and ensure
        the series name is set to the cover letter.

        Input:

          - [PATCH 0/2] A sample series
            - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        mbox = self._get_mbox('base-cover-letter.mbox')

        cover = self._parse_mail(mbox[0])
        cover_name = 'A sample series'
        self.assertEqual(cover.series.name, cover_name)

        self._parse_mail(mbox[1])
        self.assertEqual(cover.series.name, cover_name)

        self._parse_mail(mbox[2])
        self.assertEqual(cover.series.name, cover_name)

        mbox.close()

    def test_no_cover_letter(self):
        """Series without a cover letter.

        Parse a series with two patches but no cover letter and ensure
        the series name is set to the first patch's entire subject.

        Input:

          - [PATCH 1/2] test: Add some lorem ipsum
            - [PATCH 2/2] test: Convert to Markdown
        """
        mbox = self._get_mbox('base-no-cover-letter.mbox')

        patch = self._parse_mail(mbox[0])
        series = patch.series
        self.assertEqual(series.name, patch.name)

        self._parse_mail(mbox[1])
        self.assertEqual(series.name, patch.name)

        mbox.close()

    def test_out_of_order(self):
        """Series received out of order.

        Parse a series with a cover letter and two patches that is
        received out of order. Ensure the name is updated as new
        patches are received.

        Input:

            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH 1/2] test: Add some lorem ipsum
          - [PATCH 0/2] A sample series
        """
        mbox = self._get_mbox('base-out-of-order.mbox')

        patch = self._parse_mail(mbox[0])
        self.assertIsNone(patch.series.name)

        patch = self._parse_mail(mbox[1])
        self.assertEqual(patch.series.name, patch.name)

        cover = self._parse_mail(mbox[2])
        self.assertEqual(cover.series.name, 'A sample series')

        mbox.close()

    def test_custom_name(self):
        """Series with custom name.

        Parse a series with a cover letter and two patches that is
        recevied out of order. Ensure a custom name set on the series
        is not overriden by subsequent patches received.

        Input:

            - [PATCH 2/2] test: Convert to Markdown
            - [PATCH 1/2] test: Add some lorem ipsum
          - [PATCH 0/2] A sample series
        """
        mbox = self._get_mbox('base-out-of-order.mbox')

        series = self._parse_mail(mbox[0]).series
        self.assertIsNone(series.name)

        series_name = 'My custom series name'
        series.name = series_name
        series.save()
        self.assertEqual(series.name, series_name)

        self._parse_mail(mbox[1])
        self.assertEqual(series.name, series_name)

        self._parse_mail(mbox[2])
        self.assertEqual(series.name, series_name)

        mbox.close()