/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
23
import bzrlib.msgeditor 
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
24
from bzrlib.tests import TestCaseWithTransport, TestSkipped
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
25
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.
26
27
28
class MsgEditorTest(TestCaseWithTransport):
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
29
1526.1.4 by Robert Collins
forgot my self.
30
    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.
31
        """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.
32
        working_tree = self.make_branch_and_tree('.')
33
        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.
34
        filename = u'hell\u00d8'
1526.1.3 by Robert Collins
Merge from upstream.
35
        try:
36
            self.build_tree_contents([(filename, 'contents of hello')])
37
        except UnicodeEncodeError:
38
            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.
39
                "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.
40
        working_tree.add(filename)
41
        return working_tree
42
    
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
43
    def test_commit_template(self):
44
        """Test building a commit message template"""
1526.1.2 by Robert Collins
Fix typo in my msgeditor test changes.
45
        working_tree = self.make_uncommitted_tree()
1685.1.1 by John Arbash Meinel
[merge] the old bzr-encoding changes, reparenting them on bzr.dev
46
        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
47
        self.assertEqualDiff(template,
48
u"""\
49
added:
50
  hell\u00d8
51
""")
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
52
53
    def setUp(self):
54
        super(MsgEditorTest, self).setUp()
55
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
56
57
    def tearDown(self):
58
        if self._bzr_editor != None:
59
            os.environ['BZR_EDITOR'] = self._bzr_editor
60
        else:
61
            if os.environ.get('BZR_EDITOR', None) != None:
62
                del os.environ['BZR_EDITOR']
63
        super(MsgEditorTest, self).tearDown()
64
65
    def test_get_editor(self):
66
        os.environ['BZR_EDITOR'] = 'fed'
67
        editors = [i for i in bzrlib.msgeditor._get_editor()]
68
        self.assertNotEqual([], editors, 'No editor found')
69
        self.assertEqual('fed', editors[0])
70
71
    def test_run_editor(self):
72
        if sys.platform == "win32":
73
            f = file('fed.bat', 'w')
74
            f.write('@rem dummy fed')
75
            f.close()
76
            os.environ['BZR_EDITOR'] = 'fed.bat'
77
        else:
78
            f = file('fed.sh', 'wb')
79
            f.write('#!/bin/sh\n')
80
            f.close()
81
            os.chmod('fed.sh', 0755)
82
            os.environ['BZR_EDITOR'] = './fed.sh'
83
84
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
85
                         'Unable to run dummy fake editor')
86
87
    def test_edit_commit_message(self):
88
        working_tree = self.make_uncommitted_tree()
89
        # make fake editor
90
        f = file('fed.py', 'wb')
91
        f.write('#!%s\n' % sys.executable)
92
        f.write("""\
93
import sys
94
if len(sys.argv) == 2:
95
    fn = sys.argv[1]
96
    f = file(fn, 'rb')
97
    s = f.read()
98
    f.close()
99
    f = file(fn, 'wb')
100
    f.write('test message from fed\\n')
101
    f.write(s)
102
    f.close()
103
""")
104
        f.close()
105
        if sys.platform == "win32":
106
            # [win32] make batch file and set BZR_EDITOR
107
            f = file('fed.bat', 'w')
108
            f.write("""\
109
@echo off
110
%s fed.py %%1
111
""" % sys.executable)
112
            f.close()
113
            os.environ['BZR_EDITOR'] = 'fed.bat'
114
        else:
115
            # [non-win32] make python script executable and set BZR_EDITOR
116
            os.chmod('fed.py', 0755)
117
            os.environ['BZR_EDITOR'] = './fed.py'
118
119
        mutter('edit_commit_message without infotext')
120
        self.assertEqual('test message from fed\n',
121
                         bzrlib.msgeditor.edit_commit_message(''))
122
123
        mutter('edit_commit_message with unicode infotext')
124
        self.assertEqual('test message from fed\n',
125
                         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.
126
127
    def test_deleted_commit_message(self):
128
        working_tree = self.make_uncommitted_tree()
129
130
        if sys.platform == 'win32':
131
            os.environ['BZR_EDITOR'] = 'del'
132
        else:
133
            os.environ['BZR_EDITOR'] = 'rm'
134
1185.85.16 by John Arbash Meinel
Fixed the msgeditor test to actually catch the exception
135
        self.assertRaises(IOError, bzrlib.msgeditor.edit_commit_message, '')
1185.85.14 by John Arbash Meinel
Change exception handling for msgeditor.py to only catch specific exceptions.
136