/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
2
#
3
# This program is free software; you can redistribute it and/or modify
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Test commit message editor.
18
"""
19
20
import os
21
import sys
22
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
23
from bzrlib import (
24
    errors,
25
    msgeditor,
26
    osutils,
27
    )
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
28
from bzrlib.branch import Branch
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
29
from bzrlib.config import ensure_config_dir_exists, config_filename
2598.6.24 by ghigo
update on the basis of Aaron suggestions
30
from bzrlib.msgeditor import (
31
    make_commit_message_template_encoded,
2598.6.27 by ghigo
small cleanup (line lenght)
32
    edit_commit_message_encoded
2598.6.24 by ghigo
update on the basis of Aaron suggestions
33
)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
34
from bzrlib.tests import TestCaseWithTransport, TestSkipped
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
35
from bzrlib.trace import mutter
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
36
37
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
38
1526.1.4 by Robert Collins
forgot my self.
39
    def make_uncommitted_tree(self):
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
40
        """Build a branch with uncommitted unicode named changes in the cwd."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
41
        working_tree = self.make_branch_and_tree('.')
42
        b = working_tree.branch
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
43
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
44
        try:
45
            self.build_tree_contents([(filename, 'contents of hello')])
46
        except UnicodeEncodeError:
47
            raise TestSkipped("can't build unicode working tree in "
1185.33.97 by Martin Pool
MsgEditor tests should be skipped on platforms without unicode fs.
48
                "filesystem encoding %s" % sys.getfilesystemencoding())
1526.1.1 by Robert Collins
Run the test suite with no locale as well as the default locale. Also add a test for build_tree_shape to selftest.
49
        working_tree.add(filename)
50
        return working_tree
51
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
52
    def test_commit_template(self):
53
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
54
        working_tree = self.make_uncommitted_tree()
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
55
        template = msgeditor.make_commit_message_template(working_tree,
2598.6.24 by ghigo
update on the basis of Aaron suggestions
56
                                                                 None)
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
57
        self.assertEqualDiff(template,
58
u"""\
59
added:
60
  hell\u00d8
61
""")
62
63
    def test_commit_template_encoded(self):
64
        """Test building a commit message template"""
65
        working_tree = self.make_uncommitted_tree()
2598.6.24 by ghigo
update on the basis of Aaron suggestions
66
        template = make_commit_message_template_encoded(working_tree,
67
                                                        None,
68
                                                        output_encoding='utf8')
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
69
        self.assertEqualDiff(template,
70
u"""\
71
added:
72
  hell\u00d8
2598.6.14 by ghigo
Update the test
73
""".encode("utf8"))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
74
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
75
2598.6.2 by ghigo
Add testcase
76
    def test_commit_template_and_diff(self):
77
        """Test building a commit message template"""
78
        working_tree = self.make_uncommitted_tree()
2598.6.24 by ghigo
update on the basis of Aaron suggestions
79
        template = make_commit_message_template_encoded(working_tree,
80
                                                        None,
81
                                                        diff=True,
82
                                                        output_encoding='utf8')
2598.6.2 by ghigo
Add testcase
83
2598.6.10 by ghigo
In the commit dialog, the diff is stored as 8-bit raw data
84
        self.assertTrue("""\
2598.6.2 by ghigo
Add testcase
85
@@ -0,0 +1,1 @@
86
+contents of hello
2598.6.14 by ghigo
Update the test
87
""" in template)
2598.6.3 by ghigo
Add test case
88
        self.assertTrue(u"""\
89
added:
90
  hell\u00d8
2598.6.14 by ghigo
Update the test
91
""".encode('utf8') in template)
2598.6.2 by ghigo
Add testcase
92
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
93
    def setUp(self):
94
        super(MsgEditorTest, self).setUp()
95
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
96
97
    def tearDown(self):
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
98
        if self._bzr_editor is not None:
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
99
            os.environ['BZR_EDITOR'] = self._bzr_editor
100
        else:
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
101
            if os.environ.get('BZR_EDITOR', None) is not None:
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
102
                del os.environ['BZR_EDITOR']
103
        super(MsgEditorTest, self).tearDown()
104
105
    def test_run_editor(self):
106
        if sys.platform == "win32":
107
            f = file('fed.bat', 'w')
108
            f.write('@rem dummy fed')
109
            f.close()
110
            os.environ['BZR_EDITOR'] = 'fed.bat'
111
        else:
112
            f = file('fed.sh', 'wb')
113
            f.write('#!/bin/sh\n')
114
            f.close()
115
            os.chmod('fed.sh', 0755)
116
            os.environ['BZR_EDITOR'] = './fed.sh'
117
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
118
        self.assertEqual(True, msgeditor._run_editor(''),
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
119
                         'Unable to run dummy fake editor')
120
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
121
    def make_fake_editor(self, message='test message from fed\\n'):
2258.3.2 by James Westby
Allow an empty start message.
122
        """Set up environment so that an editor will be a known script.
123
124
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
125
        script that just adds a known message to the start of the file.
126
        """
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
127
        f = file('fed.py', 'wb')
128
        f.write('#!%s\n' % sys.executable)
129
        f.write("""\
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
130
# coding=utf-8
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
131
import sys
132
if len(sys.argv) == 2:
133
    fn = sys.argv[1]
134
    f = file(fn, 'rb')
135
    s = f.read()
136
    f.close()
137
    f = file(fn, 'wb')
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
138
    f.write('%s')
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
139
    f.write(s)
140
    f.close()
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
141
""" % (message, ))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
142
        f.close()
143
        if sys.platform == "win32":
144
            # [win32] make batch file and set BZR_EDITOR
145
            f = file('fed.bat', 'w')
146
            f.write("""\
147
@echo off
1711.4.2 by jfmeinel
current python may be running in a path that has a space, so properly quote the python exe name. for test_msgeditor
148
"%s" fed.py %%1
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
149
""" % sys.executable)
150
            f.close()
151
            os.environ['BZR_EDITOR'] = 'fed.bat'
152
        else:
153
            # [non-win32] make python script executable and set BZR_EDITOR
154
            os.chmod('fed.py', 0755)
155
            os.environ['BZR_EDITOR'] = './fed.py'
156
2258.3.2 by James Westby
Allow an empty start message.
157
    def test_edit_commit_message(self):
158
        working_tree = self.make_uncommitted_tree()
159
        self.make_fake_editor()
160
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
161
        mutter('edit_commit_message without infotext')
162
        self.assertEqual('test message from fed\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
163
                         msgeditor.edit_commit_message(''))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
164
165
        mutter('edit_commit_message with unicode infotext')
166
        self.assertEqual('test message from fed\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
167
                         msgeditor.edit_commit_message(u'\u1234'))
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
168
2598.6.27 by ghigo
small cleanup (line lenght)
169
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
170
        self.assertEqual('test message from fed\n', tmpl)
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
171
2258.3.2 by James Westby
Allow an empty start message.
172
    def test_start_message(self):
173
        self.make_uncommitted_tree()
174
        self.make_fake_editor()
2258.3.1 by James Westby
Add a way to specify a template commit message.
175
        self.assertEqual('test message from fed\nstart message\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
176
                         msgeditor.edit_commit_message('',
2258.3.1 by James Westby
Add a way to specify a template commit message.
177
                                              start_message='start message\n'))
2258.3.2 by James Westby
Allow an empty start message.
178
        self.assertEqual('test message from fed\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
179
                         msgeditor.edit_commit_message('',
2258.3.2 by James Westby
Allow an empty start message.
180
                                              start_message=''))
2258.3.1 by James Westby
Add a way to specify a template commit message.
181
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
182
    def test_deleted_commit_message(self):
183
        working_tree = self.make_uncommitted_tree()
184
185
        if sys.platform == 'win32':
1711.4.1 by John Arbash Meinel
del is not an executable program on win32, you must use cmd /c del
186
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
187
        else:
188
            os.environ['BZR_EDITOR'] = 'rm'
189
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
190
        self.assertRaises((IOError, OSError), msgeditor.edit_commit_message, '')
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
191
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
192
    def test__get_editor(self):
193
        # Test that _get_editor can return a decent list of items
194
        bzr_editor = os.environ.get('BZR_EDITOR')
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
195
        visual = os.environ.get('VISUAL')
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
196
        editor = os.environ.get('EDITOR')
197
        try:
198
            os.environ['BZR_EDITOR'] = 'bzr_editor'
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
199
            os.environ['VISUAL'] = 'visual'
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
200
            os.environ['EDITOR'] = 'editor'
201
202
            ensure_config_dir_exists()
203
            f = open(config_filename(), 'wb')
204
            f.write('editor = config_editor\n')
205
            f.close()
206
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
207
            editors = list(msgeditor._get_editor())
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
208
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
209
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
210
                              'editor'], editors[:4])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
211
212
            if sys.platform == 'win32':
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
213
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
214
            else:
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
215
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
216
                                  'joe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
217
218
        finally:
219
            # Restore the environment
220
            if bzr_editor is None:
221
                del os.environ['BZR_EDITOR']
222
            else:
223
                os.environ['BZR_EDITOR'] = bzr_editor
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
224
            if visual is None:
225
                del os.environ['VISUAL']
226
            else:
227
                os.environ['VISUAL'] = visual
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
228
            if editor is None:
229
                del os.environ['EDITOR']
230
            else:
231
                os.environ['EDITOR'] = editor
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
232
233
    def test__create_temp_file_with_commit_template(self):
234
        # check that commit template written properly
235
        # and has platform native line-endings (CRLF on win32)
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
236
        create_file = msgeditor._create_temp_file_with_commit_template
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
237
        msgfilename, hasinfo = create_file('infotext','----','start message')
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
238
        self.assertNotEqual(None, msgfilename)
239
        self.assertTrue(hasinfo)
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
240
        expected = os.linesep.join(['start message',
241
                                    '',
242
                                    '',
243
                                    '----',
244
                                    '',
245
                                    'infotext'])
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
246
        self.assertFileEqual(expected, msgfilename)
247
248
    def test__create_temp_file_with_empty_commit_template(self):
249
        # empty file
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
250
        create_file = msgeditor._create_temp_file_with_commit_template
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
251
        msgfilename, hasinfo = create_file('')
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
252
        self.assertNotEqual(None, msgfilename)
253
        self.assertFalse(hasinfo)
254
        self.assertFileEqual('', msgfilename)
2625.9.6 by Daniel Watkins
Added test to ensure correct error message is thrown when an unencodable commit message is entered through the editor.
255
256
    def test_unsupported_encoding_commit_message(self):
257
        old_env = osutils.set_or_unset_env('LANG', 'C')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
258
        try:
259
            self.make_fake_editor(message='\xff')
2625.9.6 by Daniel Watkins
Added test to ensure correct error message is thrown when an unencodable commit message is entered through the editor.
260
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
261
            working_tree = self.make_uncommitted_tree()
262
            self.assertRaises(errors.BadCommitMessageEncoding,
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
263
                              msgeditor.edit_commit_message, '')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
264
        finally:
265
            osutils.set_or_unset_env('LANG', old_env)