~bzr-pqm/bzr/bzr.dev

1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
1
# Copyright (C) 2005 by Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License version 2 as published by
5
# the Free Software Foundation.
6
#
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
# GNU General Public License for more details.
11
#
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
16
"""Test commit message editor.
17
"""
18
19
import os
20
import sys
21
22
from bzrlib.branch import Branch
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
23
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
24
import bzrlib.msgeditor 
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
25
from bzrlib.tests import TestCaseWithTransport, TestSkipped
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
26
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.
27
28
29
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
30
1526.1.4 by Robert Collins
forgot my self.
31
    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.
32
        """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.
33
        working_tree = self.make_branch_and_tree('.')
34
        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.
35
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
36
        try:
37
            self.build_tree_contents([(filename, 'contents of hello')])
38
        except UnicodeEncodeError:
39
            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.
40
                "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.
41
        working_tree.add(filename)
42
        return working_tree
43
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
44
    def test_commit_template(self):
45
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
46
        working_tree = self.make_uncommitted_tree()
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
47
        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
48
        self.assertEqualDiff(template,
49
u"""\
50
added:
51
  hell\u00d8
52
""")
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
53
54
    def setUp(self):
55
        super(MsgEditorTest, self).setUp()
56
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
57
58
    def tearDown(self):
59
        if self._bzr_editor != None:
60
            os.environ['BZR_EDITOR'] = self._bzr_editor
61
        else:
62
            if os.environ.get('BZR_EDITOR', None) != None:
63
                del os.environ['BZR_EDITOR']
64
        super(MsgEditorTest, self).tearDown()
65
66
    def test_run_editor(self):
67
        if sys.platform == "win32":
68
            f = file('fed.bat', 'w')
69
            f.write('@rem dummy fed')
70
            f.close()
71
            os.environ['BZR_EDITOR'] = 'fed.bat'
72
        else:
73
            f = file('fed.sh', 'wb')
74
            f.write('#!/bin/sh\n')
75
            f.close()
76
            os.chmod('fed.sh', 0755)
77
            os.environ['BZR_EDITOR'] = './fed.sh'
78
79
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
80
                         'Unable to run dummy fake editor')
81
82
    def test_edit_commit_message(self):
83
        working_tree = self.make_uncommitted_tree()
84
        # make fake editor
85
        f = file('fed.py', 'wb')
86
        f.write('#!%s\n' % sys.executable)
87
        f.write("""\
88
import sys
89
if len(sys.argv) == 2:
90
    fn = sys.argv[1]
91
    f = file(fn, 'rb')
92
    s = f.read()
93
    f.close()
94
    f = file(fn, 'wb')
95
    f.write('test message from fed\\n')
96
    f.write(s)
97
    f.close()
98
""")
99
        f.close()
100
        if sys.platform == "win32":
101
            # [win32] make batch file and set BZR_EDITOR
102
            f = file('fed.bat', 'w')
103
            f.write("""\
104
@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
105
"%s" fed.py %%1
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
106
""" % sys.executable)
107
            f.close()
108
            os.environ['BZR_EDITOR'] = 'fed.bat'
109
        else:
110
            # [non-win32] make python script executable and set BZR_EDITOR
111
            os.chmod('fed.py', 0755)
112
            os.environ['BZR_EDITOR'] = './fed.py'
113
114
        mutter('edit_commit_message without infotext')
115
        self.assertEqual('test message from fed\n',
116
                         bzrlib.msgeditor.edit_commit_message(''))
117
118
        mutter('edit_commit_message with unicode infotext')
119
        self.assertEqual('test message from fed\n',
120
                         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.
121
122
    def test_deleted_commit_message(self):
123
        working_tree = self.make_uncommitted_tree()
124
125
        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
126
            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.
127
        else:
128
            os.environ['BZR_EDITOR'] = 'rm'
129
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
130
        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.
131
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
132
    def test__get_editor(self):
133
        # Test that _get_editor can return a decent list of items
134
        bzr_editor = os.environ.get('BZR_EDITOR')
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
135
        visual = os.environ.get('VISUAL')
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
136
        editor = os.environ.get('EDITOR')
137
        try:
138
            os.environ['BZR_EDITOR'] = 'bzr_editor'
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
139
            os.environ['VISUAL'] = 'visual'
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
140
            os.environ['EDITOR'] = 'editor'
141
142
            ensure_config_dir_exists()
143
            f = open(config_filename(), 'wb')
144
            f.write('editor = config_editor\n')
145
            f.close()
146
1685.1.52 by John Arbash Meinel
[merge] bzr.dev 1704
147
            editors = list(bzrlib.msgeditor._get_editor())
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
148
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
149
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
150
                              'editor'], editors[:4])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
151
152
            if sys.platform == 'win32':
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
153
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
154
            else:
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
155
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
156
                                  'joe'], editors[4:])
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
157
158
        finally:
159
            # Restore the environment
160
            if bzr_editor is None:
161
                del os.environ['BZR_EDITOR']
162
            else:
163
                os.environ['BZR_EDITOR'] = bzr_editor
1668.4.1 by Olaf Conradi
Make msgeditor invocation comply with Debian Policy.
164
            if visual is None:
165
                del os.environ['VISUAL']
166
            else:
167
                os.environ['VISUAL'] = visual
1185.50.93 by John Arbash Meinel
Added a test for the new list of editors.
168
            if editor is None:
169
                del os.environ['EDITOR']
170
            else:
171
                os.environ['EDITOR'] = editor