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