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