/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-10 19:43:34 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060510194334-0c20aad23237d047
Working on getting normalize_url working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
23
import bzrlib.msgeditor 
 
24
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
25
from bzrlib.trace import mutter
 
26
 
 
27
 
 
28
class MsgEditorTest(TestCaseWithTransport):
 
29
 
 
30
    def make_uncommitted_tree(self):
 
31
        """Build a branch with uncommitted unicode named changes in the cwd."""
 
32
        working_tree = self.make_branch_and_tree('.')
 
33
        b = working_tree.branch
 
34
        filename = u'hell\u00d8'
 
35
        try:
 
36
            self.build_tree_contents([(filename, 'contents of hello')])
 
37
        except UnicodeEncodeError:
 
38
            raise TestSkipped("can't build unicode working tree in "
 
39
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
40
        working_tree.add(filename)
 
41
        return working_tree
 
42
    
 
43
    def test_commit_template(self):
 
44
        """Test building a commit message template"""
 
45
        working_tree = self.make_uncommitted_tree()
 
46
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
 
47
        self.assertEqualDiff(template,
 
48
u"""\
 
49
added:
 
50
  hell\u00d8
 
51
""")
 
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'))
 
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
 
 
135
        self.assertRaises(IOError, bzrlib.msgeditor.edit_commit_message, '')
 
136