mailer: Avoid bytes->Text->bytes conversion on mail body
Fixes formatting issue for emails where the body showed text as
b"blah blah".
This is caused by str.encode() being called on the body, which is
already of types bytes.
For clarification, also correct the typing specifiers to correctly
document the type on the message as bytes, not Text. (These specifiers
don't actually change the type of the parameters they refer to, but they
might show up as failures in lint checks, and may lead to incorrect code
docs).
mailer: Connect and disconnect from smtp service when sending emails
Since the importer is a long-running service, there can be a delay
between when the Mailer object is constructed and when it's used. This
can result in a "timeout after CONNECT from localhost[127.0.0.1]"
exactly 5 minutes later, shown in postfix's mail.log.
Along with the connect(), a quit() (SMTP's 'QUIT') is required to be
issued at the completion of the email. From testing, it appears that
under a number of different circumstances, smtplib can cope with it not
being explicitly called - for example it automatically calls it when the
smtplib object goes out of scope - however this is not guaranteed and it
is better practice to call it explicitly. Not doing so could
theoretically disrupt the mail submission, although I've not reproduced
this failure mechanism synthetically.
In practice it is unlikely that the importer would do anything
network-dependent before the network is available, but having
this might prevent weird failure scenarios.
Add support for executing self test in checked out tree.
git-ubuntu is expected to run in the snap environment, and the self test
is also. Building a snap just to run tests is painfully slow.
The solution here is to run the tests in an already-existing snap
environment. This does present a bit of a chicken-egg problem, but one
that is only exposed when we add new python dependencies to git-ubuntu.
git-ubuntu-self-test is modified to accept a TEST_TREE environment
variable and do the right thing in both snap environment and development
tree.
So now both of these work:
# test the snap installed git-ubuntu libraries.
$ git-ubuntu.self-test
# test the tree in "."
$ ./snap-wrappers/wrappers/git-ubuntu-self-test
Also done here:
* place 'SNAP_REVISION' and 'SNAP_VERSION' into environment.
these are just informative. And a useful message is printed
in the test output about the snap environment being used.
README.testing is updated with some documentation on how to run
git-ubuntu.self-test without building a snap and installing it first.
In various places we have two implementations of near-identical code
where the only difference is whether they act on unapplied or applied
branches, tags or structure. Ultimately they do need to be handled
differently at some point, but most of the code in the middle of the
call graph does not need to care.
To avoid code duplication we define a PatchState enum and can then drop
half of the middling code by simply passing through the "unapplied-ness"
or "applied-ness" as a parameter.
The following functions are removed from gitubuntu.git_repository.
Instead, the unapplied previous equivalent should be used with
patch_state=PatchState.APPLIED:
The following methods are removed from
gitubuntu.git_repository.GitUbuntuRepository. Instead, the unapplied
previous equivalent should be used with patch_state=PatchState.APPLIED:
A grep suggests that the only callers are importer.py and
importer_test.py; these are refactored as part of this change, dropping
duplication where there was any.
"git ubuntu tag" is the obvious CLI command that might have been
affected, but it doesn't appear to need any of these functions or
methods. Perhaps it should, but that's a job for a future refactoring.
This change does not cover every instance of code duplication or where
refactoring to use the new enum might be appropriate; we merely fix the
code that needed touching anyway, leaving other refactoring for the
future.
This change should not result in any functional changes.