<commit_before>from   datetime              import datetime, timedelta, timezone
from   email                 import message_from_binary_file, policy
from   email.headerregistry  import Address
import mailbox
import os
import subprocess
from   types                 import SimpleNamespace
from   click.testing         import CliRunner
import pytest
from   daemail.__main__      import main
from   daemail.message       import USER_AGENT
from   test_lib_emailmatcher import TextMessage

w4 = timezone(timedelta(hours=-4))

MOCK_START = datetime(2020, 3, 11, 16, 22, 32,  10203, w4)
MOCK_END   = datetime(2020, 3, 11, 16, 24, 19, 102030, w4)

def msg_factory(fp):
    return message_from_binary_file(fp, policy=policy.default)

@pytest.mark.parametrize('opts,argv,run_kwargs,cmdresult,mailspec', [

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
        ],
        ['not-a-real-command', '-x', 'foo.txt'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (Address(addr_spec='<EMAIL>'),),
                "Subject": '[DONE] not-a-real-command -x foo.txt',
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
            '-t', 'Interested Party <<EMAIL>>',
        ],
        ['not-a-real-command', '-x', 'foo.txt'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (
                    Address(addr_spec='<EMAIL>'),
                    Address('Interested Party', addr_spec='<EMAIL>'),
                ),
                "Subject": '[DONE] not-a-real-command -x foo.txt',
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
        ],
        ['space command', 'space file'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (Address(addr_spec='<EMAIL>'),),
                "Subject": "[DONE] 'space command' 'space file'",
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

])
def test_daemail(mocker, opts, argv, run_kwargs, cmdresult, mailspec):
    daemon_mock = mocker.patch('daemon.DaemonContext', autospec=True)
    run_mock = mocker.patch('subprocess.run', return_value=cmdresult)
    tsiter = iter([MOCK_START, MOCK_END])
    dtnow_mock = mocker.patch(
        'daemail.util.dtnow',
        side_effect=lambda: next(tsiter),
    )
    runner = CliRunner()
    with runner.isolated_filesystem():
        r = runner.invoke(main, [*opts, '--mbox', 'daemail.mbox', *argv])
        assert r.exit_code == 0, r.output
        daemon_mock.assert_called_once()
        daemon_mock.return_value.__enter__.assert_called_once()
        run_mock.assert_called_once_with(argv, **run_kwargs)
        assert dtnow_mock.call_count == 2<commit_msg>That method's not in Python 3.5<commit_after>from   datetime              import datetime, timedelta, timezone
from   email                 import message_from_binary_file, policy
from   email.headerregistry  import Address
import mailbox
import os
import subprocess
from   types                 import SimpleNamespace
from   click.testing         import CliRunner
import pytest
from   daemail.__main__      import main
from   daemail.message       import USER_AGENT
from   test_lib_emailmatcher import TextMessage

w4 = timezone(timedelta(hours=-4))

MOCK_START = datetime(2020, 3, 11, 16, 22, 32,  10203, w4)
MOCK_END   = datetime(2020, 3, 11, 16, 24, 19, 102030, w4)

def msg_factory(fp):
    return message_from_binary_file(fp, policy=policy.default)

@pytest.mark.parametrize('opts,argv,run_kwargs,cmdresult,mailspec', [

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
        ],
        ['not-a-real-command', '-x', 'foo.txt'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (Address(addr_spec='<EMAIL>'),),
                "Subject": '[DONE] not-a-real-command -x foo.txt',
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
            '-t', 'Interested Party <<EMAIL>>',
        ],
        ['not-a-real-command', '-x', 'foo.txt'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (
                    Address(addr_spec='<EMAIL>'),
                    Address('Interested Party', addr_spec='<EMAIL>'),
                ),
                "Subject": '[DONE] not-a-real-command -x foo.txt',
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

    (
        [
            '-t', '<EMAIL>',
            '-f', 'Me <<EMAIL>>',
        ],
        ['space command', 'space file'],
        {"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT},
        SimpleNamespace(
            returncode = 0,
            stdout     = b'This is the output.\n',
            stderr     = None,
        ),
        TextMessage(
            {
                "From": (Address('Me', addr_spec='<EMAIL>'),),
                "To": (Address(addr_spec='<EMAIL>'),),
                "Subject": "[DONE] 'space command' 'space file'",
                "User-Agent": USER_AGENT,
            },
            'Start Time:  2020-03-11 16:22:32.010203-04:00\n'
            'End Time:    2020-03-11 16:24:19.102030-04:00\n'
            'Exit Status: 0\n'
            '\n'
            'Output:\n'
            '> This is the output.\n'
        ),
    ),

])
def test_daemail(mocker, opts, argv, run_kwargs, cmdresult, mailspec):
    daemon_mock = mocker.patch('daemon.DaemonContext', autospec=True)
    run_mock = mocker.patch('subprocess.run', return_value=cmdresult)
    tsiter = iter([MOCK_START, MOCK_END])
    dtnow_mock = mocker.patch(
        'daemail.util.dtnow',
        side_effect=lambda: next(tsiter),
    )
    runner = CliRunner()
    with runner.isolated_filesystem():
        r = runner.invoke(main, [*opts, '--mbox', 'daemail.mbox', *argv])
        assert r.exit_code == 0, r.output
        assert daemon_mock.call_count == 1
        assert daemon_mock.return_value.__enter__.call_count == 1
        run_mock.assert_called_once_with(argv, **run_kwargs)
        assert dtnow_mock.call_count == 2
{'fraction_non_alphanumeric': 0.14832645575424117, 'fraction_numerical': 0.04470426409903714, 'mean_word_length': 1.6987318280235075, 'pattern_counts': {'":': 36, '<': 39, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717927', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2752, 'n_words': 560}