1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
# -*- coding: utf-8; -*-
# test/test_reportbug_program.py
# Part of reportbug, a Debian bug reporting tool.
#
# Copyright © 2008 Ben Finney <ben+python@benfinney.id.au>
# This is free software; you may copy, modify and/or distribute this work
# under the terms of the GNU General Public License, version 2 or later.
# No warranty expressed or implied. See the file LICENSE for details.
""" Unit test for reportbug program
"""
import __builtin__
import os
from StringIO import StringIO
import scaffold
from scaffold import TestCase
module_name = 'reportbug'
module_file_path = os.path.join(scaffold.bin_dir, "reportbug")
reportbug = scaffold.make_module_from_file(module_name, module_file_path)
def setup_include_file_in_report_fixture(testcase):
""" Set up test fixtures for 'include_file_in_report' function """
testcase.temp_filename = "bogus"
testcase.temp_file = StringIO()
testcase.temp_file.actually_close = testcase.temp_file.close
testcase.temp_file.close = lambda: None
def stub_temp_file(
suffix="", prefix=None, dir=None, text=True,
mode="w+", bufsize=-1):
""" Return a stub file and filename
:return value:
Tuple (`temp_file`, `temp_filename`)
The filename will be as set in the testcase's
`temp_filename` attribute.
The file returned will be a StringIO buffer, with the
`close` method disabled. The `actually_close` method
will free the buffer.
"""
temp_filename = testcase.temp_filename
temp_file = testcase.temp_file
return (temp_file, temp_filename)
testcase.stub_temp_file = stub_temp_file
class Test_include_file_in_report(TestCase):
""" Test cases for 'include_file_in_report' function """
def setUp(self):
""" Set up test fixtures """
setup_include_file_in_report_fixture(self)
self.temp_file_func_prev = reportbug.TempFile
reportbug.TempFile = self.stub_temp_file
def tearDown(self):
""" Tear down test fixtures """
reportbug.TempFile = self.temp_file_func_prev
def test_adds_include_filename_to_attachments(self):
""" Filename of include file should be added to attachments
When the `inline` parameter is not True, the filename of
the include file should be appended to the
attachment_filenames list.
"""
message = ""
message_filename = "report"
attachment_filenames = ["foo", "bar"]
package_name = "spam"
charset = 'utf-8'
include_filename = self.temp_filename
expect_attachment_filenames = attachment_filenames
expect_attachment_filenames.append(include_filename)
(nil, nil, attachment_filenames) = reportbug.include_file_in_report(
message, message_filename, attachment_filenames, package_name,
include_filename, charset)
self.failUnlessEqual(
expect_attachment_filenames, attachment_filenames)
class Test_include_file_in_report_inline(TestCase):
""" Test cases for 'include_file_in_report' function, inline=True """
def setUp(self):
""" Set up test fixtures """
setup_include_file_in_report_fixture(self)
self.temp_file_func_prev = reportbug.TempFile
reportbug.TempFile = self.stub_temp_file
self.message = """
Lorem ipsum.
Lorem ipsum.
"""
self.message_filename = "report"
self.attachment_filenames = []
self.package_name = "spam"
self.charset = "utf-8"
self.include_filename = "bogus_include"
self.include_file_content = """
Phasellus posuere. Nulla malesuada lacinia justo.
Nunc condimentum ante vitae erat.
"""
self.include_file = StringIO(self.include_file_content)
self.builtin_file_prev = __builtin__.file
def stub_builtin_file(path, mode=None, buffering=None):
if path != self.include_filename:
raise IOError("Not found: %(path)s" % vars())
return self.include_file
__builtin__.file = stub_builtin_file
self.os_unlink_prev = os.unlink
def stub_os_unlink(filename):
pass
os.unlink = stub_os_unlink
def tearDown(self):
""" Tear down test fixtures """
self.temp_file.actually_close()
reportbug.TempFile = self.temp_file_func_prev
__builtin__.file = self.builtin_file_prev
os.unlink = self.os_unlink_prev
def test_adds_include_file_content_to_message(self):
""" Content of include file should be added to message
When the `inline` parameter is True, the content of the
include file should be added into the report message.
"""
(message, nil, nil) = reportbug.include_file_in_report(
self.message, self.message_filename,
self.attachment_filenames, self.package_name,
self.include_filename, self.charset, inline=True)
self.failUnlessIn(message, self.include_file_content)
def test_returns_new_temp_filename_as_message_filename(self):
""" New message filename should be as generated by TempFile
When the `inline` parameter is True, the returned message
filename should be that of the generated temporary file.
"""
(nil, message_filename, nil) = reportbug.include_file_in_report(
self.message, self.message_filename,
self.attachment_filenames, self.package_name,
self.include_filename, self.charset, inline=True)
temp_filename = self.temp_filename
self.failUnlessEqual(message_filename, temp_filename)
def test_writes_new_message_content_to_report_file(self):
""" New message content should be written to report file
When the `inline` parameter is True, the updated content
of the message should be written to the report file.
"""
(message, nil, nil) = reportbug.include_file_in_report(
self.message, self.message_filename,
self.attachment_filenames, self.package_name,
self.include_filename, self.charset, inline=True)
temp_filename = self.temp_filename
temp_file = self.temp_file
self.failUnlessEqual(message, temp_file.getvalue())
|