/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
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
23
import bzrlib.msgeditor
1185.33.97 by Martin Pool
MsgEditor tests should be skipped on platforms without unicode fs.
24
from bzrlib.tests import TestCaseInTempDir, TestSkipped
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
25
from bzrlib.trace import mutter
1185.33.72 by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of
26
27
28
class MsgEditorTest(TestCaseInTempDir):
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."""
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
32
        b = Branch.initialize(u'.')
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
        working_tree = b.working_tree()
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()
1185.85.9 by John Arbash Meinel
[patch] Alexander Belchenko: test spawning a msg editor
46
        template = bzrlib.msgeditor.make_commit_message_template(working_tree,
47
                                                                 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_get_editor(self):
67
        os.environ['BZR_EDITOR'] = 'fed'
68
        editors = [i for i in bzrlib.msgeditor._get_editor()]
69
        self.assertNotEqual([], editors, 'No editor found')
70
        self.assertEqual('fed', editors[0])
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
88
    def test_edit_commit_message(self):
89
        working_tree = self.make_uncommitted_tree()
90
        # make fake editor
91
        f = file('fed.py', 'wb')
92
        f.write('#!%s\n' % sys.executable)
93
        f.write("""\
94
import sys
95
if len(sys.argv) == 2:
96
    fn = sys.argv[1]
97
    f = file(fn, 'rb')
98
    s = f.read()
99
    f.close()
100
    f = file(fn, 'wb')
101
    f.write('test message from fed\\n')
102
    f.write(s)
103
    f.close()
104
""")
105
        f.close()
106
        if sys.platform == "win32":
107
            # [win32] make batch file and set BZR_EDITOR
108
            f = file('fed.bat', 'w')
109
            f.write("""\
110
@echo off
111
%s fed.py %%1
112
""" % sys.executable)
113
            f.close()
114
            os.environ['BZR_EDITOR'] = 'fed.bat'
115
        else:
116
            # [non-win32] make python script executable and set BZR_EDITOR
117
            os.chmod('fed.py', 0755)
118
            os.environ['BZR_EDITOR'] = './fed.py'
119
120
        mutter('edit_commit_message without infotext')
121
        self.assertEqual('test message from fed\n',
122
                         bzrlib.msgeditor.edit_commit_message(''))
123
124
        mutter('edit_commit_message with unicode infotext')
125
        self.assertEqual('test message from fed\n',
126
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))