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