testing-cabal/testtools — pull request #87 by thomir (2014-07-22 03:42:39+00:00)
Title: Raise an error if text_content is passed something other than text.

We frequently find tracebacks that look like this:

File "/home/thomi/code/canonical/autopilot/video-rmd-refactoring1/autopilot/testresult.py", line 57, in _log_details
text = "%s: {{{\n%s}}}" % (detail, detail_content.as_text())
File "/usr/lib/python3/dist-packages/testtools/content.py", line 93, in as_text
return _u('').join(self.iter_text())
File "/usr/lib/python3/dist-packages/testtools/content.py", line 116, in _iter_text
for bytes in self.iter_bytes():
File "/usr/lib/python3/dist-packages/testtools/content.py", line 97, in iter_bytes
return self._get_bytes()
File "/usr/lib/python3/dist-packages/testtools/content.py", line 266, in
return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
AttributeError: 'bytes' object has no attribute 'encode'

Here, a test author has tried to add a content object using 'text_content', but has mistakenly passed a bytes object instead. The traceback doesn't tell us where the text_content call was made though, and doesn't give any other clues to aid debugging.

This branch aims to solve that by making text_content check the type of the 'text' parameter, and raise a TypeError if it's not the appropriate type. While it won't eliminate these issues, it should address the most common cause (for our application anyway).