~ubuntu-branches/ubuntu/karmic/reportbug/karmic

« back to all changes in this revision

Viewing changes to test/test_reportbug_program.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Hahler
  • Date: 2008-06-20 18:11:24 UTC
  • Revision ID: james.westby@ubuntu.com-20080620181124-1yyubiito50fn2c2
Tags: 3.41ubuntu1
* Merge from Debian unstable. Remaining Ubuntu specific changes:
  - reportbug.conf:
    - "bts ubuntu"
    - "smtphost fiordland.ubuntu.com":
      Added the fiordland.ubuntu.com SMTP server in reportbug.conf so
      that reportbug works without a MTA.
  - reportbug_submit.py: only display ubuntu-users specific message if
    BTS "ubuntu" is used in send_report.
  - reportbug.1: mention Ubuntu specific changes (LP: #163924)
* Fixes LP: #239124, #204009

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8; -*-
 
2
 
 
3
# test/test_reportbug_program.py
 
4
# Part of reportbug, a Debian bug reporting tool.
 
5
#
 
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.
 
10
 
 
11
""" Unit test for reportbug program
 
12
"""
 
13
 
 
14
import __builtin__
 
15
import os
 
16
from StringIO import StringIO
 
17
 
 
18
import scaffold
 
19
from scaffold import TestCase
 
20
 
 
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)
 
24
 
 
25
 
 
26
def setup_include_file_in_report_fixture(testcase):
 
27
    """ Set up test fixtures for 'include_file_in_report' function """
 
28
 
 
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
 
33
 
 
34
    def stub_temp_file(
 
35
        suffix="", prefix=None, dir=None, text=True,
 
36
        mode="w+", bufsize=-1):
 
37
        """ Return a stub file and filename
 
38
 
 
39
 
 
40
            :return value:
 
41
                Tuple (`temp_file`, `temp_filename`)
 
42
 
 
43
                The filename will be as set in the testcase's
 
44
                `temp_filename` attribute.
 
45
 
 
46
                The file returned will be a StringIO buffer, with the
 
47
                `close` method disabled. The `actually_close` method
 
48
                will free the buffer.
 
49
 
 
50
            """
 
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
 
55
 
 
56
 
 
57
class Test_include_file_in_report(TestCase):
 
58
    """ Test cases for 'include_file_in_report' function """
 
59
 
 
60
    def setUp(self):
 
61
        """ Set up test fixtures """
 
62
 
 
63
        setup_include_file_in_report_fixture(self)
 
64
 
 
65
        self.temp_file_func_prev = reportbug.TempFile
 
66
        reportbug.TempFile = self.stub_temp_file
 
67
 
 
68
    def tearDown(self):
 
69
        """ Tear down test fixtures """
 
70
        reportbug.TempFile = self.temp_file_func_prev
 
71
 
 
72
    def test_adds_include_filename_to_attachments(self):
 
73
        """ Filename of include file should be added to attachments
 
74
 
 
75
            When the `inline` parameter is not True, the filename of
 
76
            the include file should be appended to the
 
77
            attachment_filenames list.
 
78
 
 
79
            """
 
80
        message = ""
 
81
        message_filename = "report"
 
82
        attachment_filenames = ["foo", "bar"]
 
83
        package_name = "spam"
 
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,
 
89
            include_filename)
 
90
        self.failUnlessEqual(
 
91
            expect_attachment_filenames, attachment_filenames)
 
92
 
 
93
class Test_include_file_in_report_inline(TestCase):
 
94
    """ Test cases for 'include_file_in_report' function, inline=True """
 
95
 
 
96
    def setUp(self):
 
97
        """ Set up test fixtures """
 
98
 
 
99
        setup_include_file_in_report_fixture(self)
 
100
 
 
101
        self.temp_file_func_prev = reportbug.TempFile
 
102
        reportbug.TempFile = self.stub_temp_file
 
103
 
 
104
        self.message = """
 
105
            Lorem ipsum.
 
106
            Lorem ipsum.
 
107
            """
 
108
        self.message_filename = "report"
 
109
        self.attachment_filenames = []
 
110
        self.package_name = "spam"
 
111
 
 
112
        self.include_filename = "bogus_include"
 
113
        self.include_file_content = """
 
114
            Phasellus posuere. Nulla malesuada lacinia justo.
 
115
            Nunc condimentum ante vitae erat.
 
116
            """
 
117
        self.include_file = StringIO(self.include_file_content)
 
118
 
 
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
 
125
 
 
126
        self.os_unlink_prev = os.unlink
 
127
        def stub_os_unlink(filename):
 
128
            pass
 
129
        os.unlink = stub_os_unlink
 
130
 
 
131
    def tearDown(self):
 
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
 
137
 
 
138
    def test_adds_include_file_content_to_message(self):
 
139
        """ Content of include file should be added to message
 
140
 
 
141
            When the `inline` parameter is True, the content of the
 
142
            include file should be added into the report message.
 
143
 
 
144
            """
 
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)
 
150
 
 
151
    def test_returns_new_temp_filename_as_message_filename(self):
 
152
        """ New message filename should be as generated by TempFile
 
153
 
 
154
            When the `inline` parameter is True, the returned message
 
155
            filename should be that of the generated temporary file.
 
156
 
 
157
            """
 
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)
 
164
 
 
165
    def test_writes_new_message_content_to_report_file(self):
 
166
        """ New message content should be written to report file
 
167
 
 
168
            When the `inline` parameter is True, the updated content
 
169
            of the message should be written to the report file.
 
170
 
 
171
            """
 
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())