io
IO-related testing tools.
The io module is accessible via the testtools module.
An example ...
>>> from testtools.io import TempIO, readfile >>> from os.path import exists, join >>> tmp = TempIO() >>> tmp.putfile('data.txt', 'lots of nonsense') >>> print readfile(join(tmp.root, 'data.txt')) lots of nonsense >>> assert exists(tmp.newdir('incoming')) >>> assert exists(tmp.incoming) >>> del tmp ## or let it destruct naturally
Functions
f mkdirall(path, mkdir=<built-in function mkdir>) ...
walks the path and makes any non-existant dirs.
optional keyword mkdir is the callback for making a single dir
f readfile(filename, mode='rU', pad=False) ...
debug util that returns file contents.
NOTE: does a full read on filename, you might not want to do that.
if pad == True then contents will be padded with --begin-- and --end-- blocks. returns contents as a string.
f putfile(filename, contents, filelike=None, mode=None) ...
opens filename in writing mode, writes contents and closes.
if filelike is None then it will be created with open() and the prefixed path will be walked to create non-existant dirs.
Classes
C TempIO(...) ...
self-destructable temporary directory root.
Takes the same keyword args as tempfile.mkdtemp.
You will most likely create this in a test module like so (nosetests/ py.test style) :
>>> tmp = None >>> def setup_module(self): ... self.tmp = TempIO() >>> def teardown_module(self): ... del self.tmp >>> def test_something(): ... tmp.root ... # ... >>>
NOTE: due to the unpredictability of when destructors get called, you may want to explicitly delete your instance in a teardown method. however, an atexit function will try and clean up too.
This class contains 6 members.
See the source for more information.