/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 (
3642.2.2 by Jelmer Vernooij
Add tests for commit_message_template hooks.
24
    commit,
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
25
    errors,
26
    msgeditor,
27
    osutils,
3477.1.1 by John Arbash Meinel
Move UnicodeFeature into a core 'tests' feature, rather than living in test_diff.
28
    tests,
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
29
    )
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
30
from bzrlib.branch import Branch
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
31
from bzrlib.config import ensure_config_dir_exists, config_filename
2598.6.24 by ghigo
update on the basis of Aaron suggestions
32
from bzrlib.msgeditor import (
33
    make_commit_message_template_encoded,
2598.6.27 by ghigo
small cleanup (line lenght)
34
    edit_commit_message_encoded
2598.6.24 by ghigo
update on the basis of Aaron suggestions
35
)
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
36
from bzrlib.tests import (
2921.6.14 by Robert Collins
Push the message editor test parameterisation down into the message editor
37
    iter_suite_tests,
2839.6.2 by Alexander Belchenko
changes after Martin's review
38
    probe_bad_non_ascii,
2921.6.14 by Robert Collins
Push the message editor test parameterisation down into the message editor
39
    split_suite_by_re,
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
40
    TestCaseWithTransport,
3004.1.8 by Daniel Watkins
Changed TestSkipped to TestNotApplicable as suggested by Aaron.
41
    TestNotApplicable,
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
42
    TestSkipped,
43
    )
2921.6.14 by Robert Collins
Push the message editor test parameterisation down into the message editor
44
from bzrlib.tests.EncodingAdapter import EncodingTestAdapter
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
45
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.
46
2592.3.164 by Robert Collins
Reduce spurious differences to bzr.dev from merge mishaps.
47
2921.6.14 by Robert Collins
Push the message editor test parameterisation down into the message editor
48
def load_tests(standard_tests, module, loader):
3128.1.3 by Vincent Ladeuil
Since we are there s/parameteris.*/parameteriz&/.
49
    """Parameterize the test for tempfile creation with different encodings."""
2921.6.14 by Robert Collins
Push the message editor test parameterisation down into the message editor
50
    to_adapt, result = split_suite_by_re(standard_tests,
51
        "test__create_temp_file_with_commit_template_in_unicode_dir")
52
    for test in iter_suite_tests(to_adapt):
53
        result.addTests(EncodingTestAdapter().adapt(test))
54
    return result
55
56
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
57
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
58
1526.1.4 by Robert Collins
forgot my self.
59
    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.
60
        """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.
61
        working_tree = self.make_branch_and_tree('.')
62
        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.
63
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
64
        try:
65
            self.build_tree_contents([(filename, 'contents of hello')])
66
        except UnicodeEncodeError:
67
            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.
68
                "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.
69
        working_tree.add(filename)
70
        return working_tree
71
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
72
    def test_commit_template(self):
73
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
74
        working_tree = self.make_uncommitted_tree()
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
75
        template = msgeditor.make_commit_message_template(working_tree,
2598.6.24 by ghigo
update on the basis of Aaron suggestions
76
                                                                 None)
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
77
        self.assertEqualDiff(template,
78
u"""\
79
added:
80
  hell\u00d8
81
""")
82
83
    def test_commit_template_encoded(self):
84
        """Test building a commit message template"""
85
        working_tree = self.make_uncommitted_tree()
2598.6.24 by ghigo
update on the basis of Aaron suggestions
86
        template = make_commit_message_template_encoded(working_tree,
87
                                                        None,
88
                                                        output_encoding='utf8')
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
89
        self.assertEqualDiff(template,
90
u"""\
91
added:
92
  hell\u00d8
2598.6.14 by ghigo
Update the test
93
""".encode("utf8"))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
94
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
95
2598.6.2 by ghigo
Add testcase
96
    def test_commit_template_and_diff(self):
97
        """Test building a commit message template"""
98
        working_tree = self.make_uncommitted_tree()
2598.6.24 by ghigo
update on the basis of Aaron suggestions
99
        template = make_commit_message_template_encoded(working_tree,
100
                                                        None,
101
                                                        diff=True,
102
                                                        output_encoding='utf8')
2598.6.2 by ghigo
Add testcase
103
2598.6.10 by ghigo
In the commit dialog, the diff is stored as 8-bit raw data
104
        self.assertTrue("""\
2598.6.2 by ghigo
Add testcase
105
@@ -0,0 +1,1 @@
106
+contents of hello
2598.6.14 by ghigo
Update the test
107
""" in template)
2598.6.3 by ghigo
Add test case
108
        self.assertTrue(u"""\
109
added:
110
  hell\u00d8
2598.6.14 by ghigo
Update the test
111
""".encode('utf8') in template)
2598.6.2 by ghigo
Add testcase
112
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
113
    def test_run_editor(self):
114
        if sys.platform == "win32":
115
            f = file('fed.bat', 'w')
116
            f.write('@rem dummy fed')
117
            f.close()
118
            os.environ['BZR_EDITOR'] = 'fed.bat'
119
        else:
120
            f = file('fed.sh', 'wb')
121
            f.write('#!/bin/sh\n')
122
            f.close()
123
            os.chmod('fed.sh', 0755)
124
            os.environ['BZR_EDITOR'] = './fed.sh'
125
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
126
        self.assertEqual(True, msgeditor._run_editor(''),
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
127
                         'Unable to run dummy fake editor')
128
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
129
    def make_fake_editor(self, message='test message from fed\\n'):
2258.3.2 by James Westby
Allow an empty start message.
130
        """Set up environment so that an editor will be a known script.
131
132
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
133
        script that just adds a known message to the start of the file.
134
        """
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
135
        f = file('fed.py', 'wb')
136
        f.write('#!%s\n' % sys.executable)
137
        f.write("""\
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
138
# coding=utf-8
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
139
import sys
140
if len(sys.argv) == 2:
141
    fn = sys.argv[1]
142
    f = file(fn, 'rb')
143
    s = f.read()
144
    f.close()
145
    f = file(fn, 'wb')
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
146
    f.write('%s')
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
147
    f.write(s)
148
    f.close()
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
149
""" % (message, ))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
150
        f.close()
151
        if sys.platform == "win32":
152
            # [win32] make batch file and set BZR_EDITOR
153
            f = file('fed.bat', 'w')
154
            f.write("""\
155
@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
156
"%s" fed.py %%1
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
157
""" % sys.executable)
158
            f.close()
159
            os.environ['BZR_EDITOR'] = 'fed.bat'
160
        else:
161
            # [non-win32] make python script executable and set BZR_EDITOR
162
            os.chmod('fed.py', 0755)
163
            os.environ['BZR_EDITOR'] = './fed.py'
164
2258.3.2 by James Westby
Allow an empty start message.
165
    def test_edit_commit_message(self):
166
        working_tree = self.make_uncommitted_tree()
167
        self.make_fake_editor()
168
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
169
        mutter('edit_commit_message without infotext')
170
        self.assertEqual('test message from fed\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
171
                         msgeditor.edit_commit_message(''))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
172
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
173
        mutter('edit_commit_message with ascii string infotext')
174
        self.assertEqual('test message from fed\n',
175
                         msgeditor.edit_commit_message('spam'))
176
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
177
        mutter('edit_commit_message with unicode infotext')
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(u'\u1234'))
2598.6.18 by ghigo
Update the tests to the new *_encoded() functions
180
2598.6.27 by ghigo
small cleanup (line lenght)
181
        tmpl = edit_commit_message_encoded(u'\u1234'.encode("utf8"))
182
        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.
183
2258.3.2 by James Westby
Allow an empty start message.
184
    def test_start_message(self):
185
        self.make_uncommitted_tree()
186
        self.make_fake_editor()
2258.3.1 by James Westby
Add a way to specify a template commit message.
187
        self.assertEqual('test message from fed\nstart message\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
188
                         msgeditor.edit_commit_message('',
2258.3.1 by James Westby
Add a way to specify a template commit message.
189
                                              start_message='start message\n'))
2258.3.2 by James Westby
Allow an empty start message.
190
        self.assertEqual('test message from fed\n',
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
191
                         msgeditor.edit_commit_message('',
2258.3.2 by James Westby
Allow an empty start message.
192
                                              start_message=''))
2258.3.1 by James Westby
Add a way to specify a template commit message.
193
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
194
    def test_deleted_commit_message(self):
195
        working_tree = self.make_uncommitted_tree()
196
197
        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
198
            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.
199
        else:
200
            os.environ['BZR_EDITOR'] = 'rm'
201
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
202
        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.
203
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
204
    def test__get_editor(self):
205
        # Test that _get_editor can return a decent list of items
206
        bzr_editor = os.environ.get('BZR_EDITOR')
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
207
        visual = os.environ.get('VISUAL')
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
208
        editor = os.environ.get('EDITOR')
209
        try:
210
            os.environ['BZR_EDITOR'] = 'bzr_editor'
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
211
            os.environ['VISUAL'] = 'visual'
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
212
            os.environ['EDITOR'] = 'editor'
213
214
            ensure_config_dir_exists()
215
            f = open(config_filename(), 'wb')
216
            f.write('editor = config_editor\n')
217
            f.close()
218
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
219
            editors = list(msgeditor._get_editor())
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
220
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
221
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
222
                              'editor'], editors[:4])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
223
224
            if sys.platform == 'win32':
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
225
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
226
            else:
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
227
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
228
                                  'joe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
229
230
        finally:
231
            # Restore the environment
232
            if bzr_editor is None:
233
                del os.environ['BZR_EDITOR']
234
            else:
235
                os.environ['BZR_EDITOR'] = bzr_editor
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
236
            if visual is None:
237
                del os.environ['VISUAL']
238
            else:
239
                os.environ['VISUAL'] = visual
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
240
            if editor is None:
241
                del os.environ['EDITOR']
242
            else:
243
                os.environ['EDITOR'] = editor
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
244
245
    def test__create_temp_file_with_commit_template(self):
246
        # check that commit template written properly
247
        # and has platform native line-endings (CRLF on win32)
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
248
        create_file = msgeditor._create_temp_file_with_commit_template
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
249
        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
250
        self.assertNotEqual(None, msgfilename)
251
        self.assertTrue(hasinfo)
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
252
        expected = os.linesep.join(['start message',
253
                                    '',
254
                                    '',
255
                                    '----',
256
                                    '',
257
                                    'infotext'])
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
258
        self.assertFileEqual(expected, msgfilename)
259
3004.1.1 by Daniel Watkins
Added (failing) test to check if a temp file containing a commit message can be created in a folder with a Unicode name.
260
    def test__create_temp_file_with_commit_template_in_unicode_dir(self):
3477.1.2 by John Arbash Meinel
Rename UnicodeFilename => UnicodeFilenameFeature
261
        self.requireFeature(tests.UnicodeFilenameFeature)
3004.1.7 by Daniel Watkins
test__create_temp_file_with_commit_template_in_unicode_dir now uses EncodingTestAdapter info.
262
        if hasattr(self, 'info'):
263
            os.mkdir(self.info['directory'])
264
            os.chdir(self.info['directory'])
265
            msgeditor._create_temp_file_with_commit_template('infotext')
266
        else:
3004.1.8 by Daniel Watkins
Changed TestSkipped to TestNotApplicable as suggested by Aaron.
267
            raise TestNotApplicable('Test run elsewhere with non-ascii data.')
3004.1.1 by Daniel Watkins
Added (failing) test to check if a temp file containing a commit message can be created in a folder with a Unicode name.
268
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
269
    def test__create_temp_file_with_empty_commit_template(self):
270
        # empty file
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
271
        create_file = msgeditor._create_temp_file_with_commit_template
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
272
        msgfilename, hasinfo = create_file('')
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
273
        self.assertNotEqual(None, msgfilename)
274
        self.assertFalse(hasinfo)
275
        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.
276
277
    def test_unsupported_encoding_commit_message(self):
278
        old_env = osutils.set_or_unset_env('LANG', 'C')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
279
        try:
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
280
            # LANG env variable has no effect on Windows
281
            # but some characters anyway cannot be represented
282
            # in default user encoding
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
283
            char = probe_bad_non_ascii(osutils.get_user_encoding())
2839.6.2 by Alexander Belchenko
changes after Martin's review
284
            if char is None:
285
                raise TestSkipped('Cannot find suitable non-ascii character '
3224.5.4 by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding.
286
                    'for user_encoding (%s)' % osutils.get_user_encoding())
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
287
288
            self.make_fake_editor(message=char)
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.
289
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
290
            working_tree = self.make_uncommitted_tree()
291
            self.assertRaises(errors.BadCommitMessageEncoding,
2772.1.1 by Martin Pool
Merge commit --show-diff feature from Goffredo
292
                              msgeditor.edit_commit_message, '')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
293
        finally:
294
            osutils.set_or_unset_env('LANG', old_env)
3642.2.2 by Jelmer Vernooij
Add tests for commit_message_template hooks.
295
296
    def test_generate_commit_message_template_no_hooks(self):
297
        commit_obj = commit.Commit()
298
        self.assertIs(None, 
299
            msgeditor.generate_commit_message_template(commit_obj))
300
301
    def test_generate_commit_message_template_hook(self):
302
        def restoreDefaults():
303
            msgeditor.hooks['commit_message_template'] = []
304
        self.addCleanup(restoreDefaults)
305
        msgeditor.hooks.install_named_hook("commit_message_template",
306
                lambda commit_obj, msg: "save me some typing\n", None)
307
        commit_obj = commit.Commit()
308
        self.assertEquals("save me some typing\n", 
309
            msgeditor.generate_commit_message_template(commit_obj))