/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
23
from bzrlib.branch import Branch
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
24
from bzrlib.config import ensure_config_dir_exists, config_filename
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
25
import bzrlib.msgeditor 
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
27
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.
28
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.
29
from bzrlib import (
30
    osutils,
31
    errors
32
    )
33
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
34
35
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
36
1526.1.4 by Robert Collins
forgot my self.
37
    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.
38
        """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.
39
        working_tree = self.make_branch_and_tree('.')
40
        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.
41
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
42
        try:
43
            self.build_tree_contents([(filename, 'contents of hello')])
44
        except UnicodeEncodeError:
45
            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.
46
                "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.
47
        working_tree.add(filename)
48
        return working_tree
49
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
50
    def test_commit_template(self):
51
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
52
        working_tree = self.make_uncommitted_tree()
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
53
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
54
        self.assertEqualDiff(template,
55
u"""\
56
added:
57
  hell\u00d8
58
""")
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
59
60
    def setUp(self):
61
        super(MsgEditorTest, self).setUp()
62
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
63
64
    def tearDown(self):
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
65
        if self._bzr_editor is not None:
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
66
            os.environ['BZR_EDITOR'] = self._bzr_editor
67
        else:
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
68
            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
69
                del os.environ['BZR_EDITOR']
70
        super(MsgEditorTest, self).tearDown()
71
72
    def test_run_editor(self):
73
        if sys.platform == "win32":
74
            f = file('fed.bat', 'w')
75
            f.write('@rem dummy fed')
76
            f.close()
77
            os.environ['BZR_EDITOR'] = 'fed.bat'
78
        else:
79
            f = file('fed.sh', 'wb')
80
            f.write('#!/bin/sh\n')
81
            f.close()
82
            os.chmod('fed.sh', 0755)
83
            os.environ['BZR_EDITOR'] = './fed.sh'
84
85
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
86
                         'Unable to run dummy fake editor')
87
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
88
    def make_fake_editor(self, message='test message from fed\\n'):
2258.3.2 by James Westby
Allow an empty start message.
89
        """Set up environment so that an editor will be a known script.
90
91
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
92
        script that just adds a known message to the start of the file.
93
        """
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
94
        f = file('fed.py', 'wb')
95
        f.write('#!%s\n' % sys.executable)
96
        f.write("""\
2625.9.5 by Daniel Watkins
Modified test_msgeditor.make_fake_editor to allow a custom message to be inserted.
97
# coding=utf-8
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
98
import sys
99
if len(sys.argv) == 2:
100
    fn = sys.argv[1]
101
    f = file(fn, 'rb')
102
    s = f.read()
103
    f.close()
104
    f = file(fn, 'wb')
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
105
    f.write('%s')
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
106
    f.write(s)
107
    f.close()
2625.9.9 by Daniel Watkins
Used format string as suggested by abentley.
108
""" % (message, ))
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
109
        f.close()
110
        if sys.platform == "win32":
111
            # [win32] make batch file and set BZR_EDITOR
112
            f = file('fed.bat', 'w')
113
            f.write("""\
114
@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
115
"%s" fed.py %%1
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
116
""" % sys.executable)
117
            f.close()
118
            os.environ['BZR_EDITOR'] = 'fed.bat'
119
        else:
120
            # [non-win32] make python script executable and set BZR_EDITOR
121
            os.chmod('fed.py', 0755)
122
            os.environ['BZR_EDITOR'] = './fed.py'
123
2258.3.2 by James Westby
Allow an empty start message.
124
    def test_edit_commit_message(self):
125
        working_tree = self.make_uncommitted_tree()
126
        self.make_fake_editor()
127
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
128
        mutter('edit_commit_message without infotext')
129
        self.assertEqual('test message from fed\n',
130
                         bzrlib.msgeditor.edit_commit_message(''))
131
132
        mutter('edit_commit_message with unicode infotext')
133
        self.assertEqual('test message from fed\n',
134
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
135
2258.3.2 by James Westby
Allow an empty start message.
136
    def test_start_message(self):
137
        self.make_uncommitted_tree()
138
        self.make_fake_editor()
2258.3.1 by James Westby
Add a way to specify a template commit message.
139
        self.assertEqual('test message from fed\nstart message\n',
140
                         bzrlib.msgeditor.edit_commit_message('',
141
                                              start_message='start message\n'))
2258.3.2 by James Westby
Allow an empty start message.
142
        self.assertEqual('test message from fed\n',
143
                         bzrlib.msgeditor.edit_commit_message('',
144
                                              start_message=''))
2258.3.1 by James Westby
Add a way to specify a template commit message.
145
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
146
    def test_deleted_commit_message(self):
147
        working_tree = self.make_uncommitted_tree()
148
149
        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
150
            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.
151
        else:
152
            os.environ['BZR_EDITOR'] = 'rm'
153
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
154
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
155
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
156
    def test__get_editor(self):
157
        # Test that _get_editor can return a decent list of items
158
        bzr_editor = os.environ.get('BZR_EDITOR')
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
159
        visual = os.environ.get('VISUAL')
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
160
        editor = os.environ.get('EDITOR')
161
        try:
162
            os.environ['BZR_EDITOR'] = 'bzr_editor'
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
163
            os.environ['VISUAL'] = 'visual'
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
164
            os.environ['EDITOR'] = 'editor'
165
166
            ensure_config_dir_exists()
167
            f = open(config_filename(), 'wb')
168
            f.write('editor = config_editor\n')
169
            f.close()
170
1685.1.52 by John Arbash Meinel
[merge] bzr.dev 1704
171
            editors = list(bzrlib.msgeditor._get_editor())
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
172
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
173
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
174
                              'editor'], editors[:4])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
175
176
            if sys.platform == 'win32':
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
177
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
178
            else:
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
179
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
180
                                  'joe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
181
182
        finally:
183
            # Restore the environment
184
            if bzr_editor is None:
185
                del os.environ['BZR_EDITOR']
186
            else:
187
                os.environ['BZR_EDITOR'] = bzr_editor
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
188
            if visual is None:
189
                del os.environ['VISUAL']
190
            else:
191
                os.environ['VISUAL'] = visual
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
192
            if editor is None:
193
                del os.environ['EDITOR']
194
            else:
195
                os.environ['EDITOR'] = editor
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
196
197
    def test__create_temp_file_with_commit_template(self):
198
        # check that commit template written properly
199
        # and has platform native line-endings (CRLF on win32)
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
200
        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
201
        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
202
        self.assertNotEqual(None, msgfilename)
203
        self.assertTrue(hasinfo)
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
204
        expected = os.linesep.join(['start message',
205
                                    '',
206
                                    '',
207
                                    '----',
208
                                    '',
209
                                    'infotext'])
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
210
        self.assertFileEqual(expected, msgfilename)
211
212
    def test__create_temp_file_with_empty_commit_template(self):
213
        # empty file
2472.4.2 by Alexander Belchenko
unwrapping long lines in tests
214
        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
215
        msgfilename, hasinfo = create_file('')
2472.4.1 by Alexander Belchenko
Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
216
        self.assertNotEqual(None, msgfilename)
217
        self.assertFalse(hasinfo)
218
        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.
219
220
    def test_unsupported_encoding_commit_message(self):
221
        old_env = osutils.set_or_unset_env('LANG', 'C')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
222
        try:
223
            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.
224
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
225
            working_tree = self.make_uncommitted_tree()
226
            self.assertRaises(errors.BadCommitMessageEncoding,
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.
227
                                    bzrlib.msgeditor.edit_commit_message, '')
2625.9.8 by Daniel Watkins
Updated as per poolie's !tweak.
228
        finally:
229
            osutils.set_or_unset_env('LANG', old_env)