tests.test_action module¶

tests.test_action.BASE_HEADERS = b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\n'¶

Headers of a mail written by hand, with CRLF line endings, up to its body type.

tests.test_action.PLAIN_HEADERS = b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n'¶

Headers of a plain text mail.

tests.test_action.PARTLESS_HEADERS = b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="Kiwi-Boundary"\r\n\r\n'¶

Headers of a multipart mail whose body never opens with its boundary, so it parses with no subpart.

tests.test_action.REWRITTEN_BY_THE_GENERATOR = (((b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="Kiwi-Boundary"\r\n\r\nTaipei: \xe6\x99\xb4\xe5\xa4\xa9, 28 \xc2\xb0C.\r\nTainan: \xe5\xa4\x9a\xe9\x9b\xb2.\r\n',), (), 'partless-8bit'), ((b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="Kiwi-Boundary"\r\n\r\nSunny.\r\n--Kiwi-Boundary--\r\nRain.\r\n',), (), 'partless-closed'), ((b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="Kiwi-Boundary"\r\n\r\nPreamble.\r\n--Kiwi-Boundary  \r\nContent-Type: text/plain\r\n\r\nSunny.\r\n--Kiwi-Boundary\r\nContent-Type: text/html\r\n\r\n<p>Sunny.</p>\r\n',), (), 'padded-boundary-left-open'), ((b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nTaipei: sunny\rTainan: cloudy\r\nKaohsiung: rain\r\n',), (), 'bare-cr'), ((b'Date: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nFrom the north came hail.\r\nFrom the south, sun.\r\n',), (), 'from-lines'), ((b'Received: from mx.orchard.example by mail.orchard.example with ESMTPS id abc123 for <quince@orchard.example>; Thu, 11 Oct 2018 09:30:01 +0800\r\nDate: Thu, 11 Oct 2018 09:30:00 +0800\r\nFrom: Clementine <clementine@orchard.example>\r\nTo: quince@orchard.example\r\nSubject: Weather in Taipei\r\nMessage-ID: <taipei-weather@orchard.example>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nRain.\r\n',), (), 'long-header'))¶

Mails that Python’s email generator would not write back as they are.

It drops the content of a multipart parsed with no subpart, or fails on it; strips the padding after a boundary and closes a boundary left open; turns a bare CR into a line break; and refolds a header past 78 columns.

tests.test_action.test_action_definition(action)[source]¶

Test duplicate action definitions.

tests.test_action.SELECTION = [<tests.conftest.MailFactory object>, <tests.conftest.MailFactory object>]¶

Mails the selection resolves to: the unique mail plus one surviving copy.

tests.test_action.DISCARD = [<tests.conftest.MailFactory object>, <tests.conftest.MailFactory object>]¶

Mails the discard resolves to: the two other copies.

tests.test_action.DISCARD_AND_UNIQUE = [<tests.conftest.MailFactory object>, <tests.conftest.MailFactory object>, <tests.conftest.MailFactory object>]¶

What a delete-selected run leaves behind.

The mail that has no duplicate belongs to the selection, but deleting it would leave no copy of it anywhere, so only the copy the strategy picked is removed. See: https://github.com/kdeldycke/mail-deduplicate/issues/1053

tests.test_action.test_action_matrix(invoke, make_box, action, needs_export, source_after, export_after, dry_run)[source]¶

Every action lands the right mails in the right box.

The stats self-check stays consistent, in real and dry-run mode alike.

Copy actions leave the source intact; move and delete actions strip it down to whichever subset they did not act on, with delete-selected also leaving the mail that has no duplicate. Export actions also cover the re-reading of each mail’s bytes from the source boxes, as mails are dehydrated to lightweight stubs once hashed: a regression there would export empty or truncated mails.

tests.test_action.test_trail_lines_tell_a_dry_run_apart(invoke, make_box, action, needs_export, box_type, outcome, dry_run)[source]¶

Each mail’s line reports what happened to it in a real run only.

A dry run touches no mail, so its lines say what would have happened instead of reading like the record of a run that went through.

tests.test_action.test_trail_lists_mails_in_a_stable_order(invoke, make_box, box_type)[source]¶

The action trail lists its mails in the same order on every run.

The targets of an action are a set, whose iteration order changes from one process to the next: left unsorted, two runs on the same boxes printed the same lines in different orders, which no diff of two runs survives. The order the run sorts them by is the one a reader compares two runs with: the box path, then the mail’s ID within it.

tests.test_action.test_delete_selected_spares_mails_without_duplicates(invoke, make_box, jobs)[source]¶

A unique mail survives a delete-selected run that has nothing else to act on.

The two copies here differ by far more than the default size threshold, as a mail carrying an attachment differs from the same mail without one. Their set is skipped, which leaves the selection made of the mail that has no duplicate alone. Deleting the whole selection then wipes the one mail the run was never asked to touch, and keeps the two it was. Covers both the sequential and the parallel selection paths, as each merges its own verdict back. See: https://github.com/kdeldycke/mail-deduplicate/issues/1053

tests.test_action.test_assert_stats_consistent_passes(config)[source]¶

A satisfied assertion returns quietly, without exiting.

tests.test_action.inodes(box_path)[source]¶

The distinct files backing the mails of a folder-based box.

One inode per mail before any linking, one per group of linked copies after.

Return type:

set[int]

Discarded copies keep their own name and place, backed by the kept copy’s file.

The unique mail keeps a file to itself.

Runs over both selection paths: pairing each discarded mail with the copy it is linked to is recorded per duplicate set, which a parallel run settles in worker processes and merges back afterwards.

A dry run reports on the links it would create without creating any.

tests.test_action.DIFFERING_COPIES = [<tests.conftest.MailFactory object>, <tests.conftest.MailFactory object>]¶

Two mails sharing a hash, as the body is left out of it by default, but backed by files differing byte for byte.

Copies that are not byte-for-byte identical are only linked when asked for.

Linking swaps the discarded mail’s own content for the kept copy’s.

A second run finds the copies already sharing a file and leaves them be.

A link that cannot replace its mail leaves the mail as it was.

It removes the temporary link it went through, and counts the mail as skipped.

A mail packed into the box’s own file has nothing of its own to link.

The box comes out byte for byte as it went in.

tests.test_action.test_export_keeps_mail_bytes(invoke, tmp_path, raw, action, export_format)[source]¶

A mail reaches every export format as the bytes its source holds.

Only its line endings are reduced to LF, and an mbox or mmdf box quotes the body lines starting with From, as its format requires.

See: https://github.com/kdeldycke/mail-deduplicate/issues/1090

tests.test_action.DELIVERY_TIME = 1600000000¶

Modification time of a mail file, far enough from now to tell the two apart.

tests.test_action.test_export_keeps_maildir_metadata(invoke, tmp_path, action, subdir, info)[source]¶

A maildir mail keeps its folder, flags and delivery time in a maildir export.

The stdlib files a mail given as bytes in new, with no flags and the time of the run, which would mark every exported mail unread and delivered today.

tests.test_action.test_export_keeps_mbox_envelope(invoke, tmp_path, action, export_format)[source]¶

An mbox mail keeps its envelope From line in an mbox or mmdf export.

The stdlib fabricates one naming MAILER-DAEMON and the time of the run for a mail that comes without it.

tests.test_action.test_export_keeps_mh_sequences(invoke, tmp_path, action)[source]¶

An MH mail keeps its sequences in an MH export.

The stdlib enrolls a mail given as bytes in no sequence.

tests.test_action.test_export_translates_no_metadata_across_formats(invoke, tmp_path)[source]¶

A maildir mail copied into an mbox carries its bytes and nothing else.

Its flags have no place in an mbox but a Status header, which would be an addition to the mail: they are dropped, not translated.

tests.test_action.test_unique_only_source_is_exported(invoke, make_box)[source]¶

A source made only of unique mails still exports all of them.

A set of one mail is auto-selected, not skipped as “all mails were selected”.

See: https://github.com/kdeldycke/mail-deduplicate/issues/843 and https://github.com/kdeldycke/mail-deduplicate/issues/599