~bzr-pqm/bzr/bzr.dev

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
29
30
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
31
1526.1.4 by Robert Collins
forgot my self.
32
    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.
33
        """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.
34
        working_tree = self.make_branch_and_tree('.')
35
        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.
36
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
37
        try:
38
            self.build_tree_contents([(filename, 'contents of hello')])
39
        except UnicodeEncodeError:
40
            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.
41
                "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.
42
        working_tree.add(filename)
43
        return working_tree
44
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
45
    def test_commit_template(self):
46
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
47
        working_tree = self.make_uncommitted_tree()
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
48
        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
49
        self.assertEqualDiff(template,
50
u"""\
51
added:
52
  hell\u00d8
53
""")
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
54
55
    def setUp(self):
56
        super(MsgEditorTest, self).setUp()
57
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
58
59
    def tearDown(self):
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
60
        if self._bzr_editor is not None:
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
61
            os.environ['BZR_EDITOR'] = self._bzr_editor
62
        else:
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
63
            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
64
                del os.environ['BZR_EDITOR']
65
        super(MsgEditorTest, self).tearDown()
66
67
    def test_run_editor(self):
68
        if sys.platform == "win32":
69
            f = file('fed.bat', 'w')
70
            f.write('@rem dummy fed')
71
            f.close()
72
            os.environ['BZR_EDITOR'] = 'fed.bat'
73
        else:
74
            f = file('fed.sh', 'wb')
75
            f.write('#!/bin/sh\n')
76
            f.close()
77
            os.chmod('fed.sh', 0755)
78
            os.environ['BZR_EDITOR'] = './fed.sh'
79
80
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
81
                         'Unable to run dummy fake editor')
82
2258.3.2 by James Westby
Allow an empty start message.
83
    def make_fake_editor(self):
84
        """Set up environment so that an editor will be a known script.
85
86
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
87
        script that just adds a known message to the start of the file.
88
        """
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
89
        f = file('fed.py', 'wb')
90
        f.write('#!%s\n' % sys.executable)
91
        f.write("""\
92
import sys
93
if len(sys.argv) == 2:
94
    fn = sys.argv[1]
95
    f = file(fn, 'rb')
96
    s = f.read()
97
    f.close()
98
    f = file(fn, 'wb')
99
    f.write('test message from fed\\n')
100
    f.write(s)
101
    f.close()
102
""")
103
        f.close()
104
        if sys.platform == "win32":
105
            # [win32] make batch file and set BZR_EDITOR
106
            f = file('fed.bat', 'w')
107
            f.write("""\
108
@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
109
"%s" fed.py %%1
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
110
""" % sys.executable)
111
            f.close()
112
            os.environ['BZR_EDITOR'] = 'fed.bat'
113
        else:
114
            # [non-win32] make python script executable and set BZR_EDITOR
115
            os.chmod('fed.py', 0755)
116
            os.environ['BZR_EDITOR'] = './fed.py'
117
2258.3.2 by James Westby
Allow an empty start message.
118
    def test_edit_commit_message(self):
119
        working_tree = self.make_uncommitted_tree()
120
        self.make_fake_editor()
121
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
122
        mutter('edit_commit_message without infotext')
123
        self.assertEqual('test message from fed\n',
124
                         bzrlib.msgeditor.edit_commit_message(''))
125
126
        mutter('edit_commit_message with unicode infotext')
127
        self.assertEqual('test message from fed\n',
128
                         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.
129
2258.3.2 by James Westby
Allow an empty start message.
130
    def test_start_message(self):
131
        self.make_uncommitted_tree()
132
        self.make_fake_editor()
2258.3.1 by James Westby
Add a way to specify a template commit message.
133
        self.assertEqual('test message from fed\nstart message\n',
134
                         bzrlib.msgeditor.edit_commit_message('',
135
                                              start_message='start message\n'))
2258.3.2 by James Westby
Allow an empty start message.
136
        self.assertEqual('test message from fed\n',
137
                         bzrlib.msgeditor.edit_commit_message('',
138
                                              start_message=''))
2258.3.1 by James Westby
Add a way to specify a template commit message.
139
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
140
    def test_deleted_commit_message(self):
141
        working_tree = self.make_uncommitted_tree()
142
143
        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
144
            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.
145
        else:
146
            os.environ['BZR_EDITOR'] = 'rm'
147
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
148
        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.
149
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
150
    def test__get_editor(self):
151
        # Test that _get_editor can return a decent list of items
152
        bzr_editor = os.environ.get('BZR_EDITOR')
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
153
        visual = os.environ.get('VISUAL')
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
154
        editor = os.environ.get('EDITOR')
155
        try:
156
            os.environ['BZR_EDITOR'] = 'bzr_editor'
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
157
            os.environ['VISUAL'] = 'visual'
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
158
            os.environ['EDITOR'] = 'editor'
159
160
            ensure_config_dir_exists()
161
            f = open(config_filename(), 'wb')
162
            f.write('editor = config_editor\n')
163
            f.close()
164
1685.1.52 by John Arbash Meinel
[merge] bzr.dev 1704
165
            editors = list(bzrlib.msgeditor._get_editor())
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
166
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
167
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
168
                              'editor'], editors[:4])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
169
170
            if sys.platform == 'win32':
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
171
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
172
            else:
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
173
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
174
                                  'joe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
175
176
        finally:
177
            # Restore the environment
178
            if bzr_editor is None:
179
                del os.environ['BZR_EDITOR']
180
            else:
181
                os.environ['BZR_EDITOR'] = bzr_editor
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
182
            if visual is None:
183
                del os.environ['VISUAL']
184
            else:
185
                os.environ['VISUAL'] = visual
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
186
            if editor is None:
187
                del os.environ['EDITOR']
188
            else:
189
                os.environ['EDITOR'] = editor