Ensure that files are always closed in the tests.

A failure to close files in a timely fassion means
that data is not necessarily written immediately
on Pythons which do not use reference counting
(e.g. PyPy).

Change-Id: I5d363249676032a025a22a67275c2eed3151b264
Merge "Ensure that files are always closed in the tests."