~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Robert Collins
  • Date: 2006-05-24 08:14:45 UTC
  • mfrom: (1725.1.1 benchmark)
  • mto: (1725.2.6 commit)
  • mto: This revision was merged to the branch mainline in revision 1729.
  • Revision ID: robertc@robertcollins.net-20060524081445-c046b4406ffc8dfa
(rbc)Merge in benchmark --lsprof-timed lsprofiling feature. (Robert Collins, Martin Pool).

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
from bzrlib.config import ensure_config_dir_exists, config_filename
 
24
from bzrlib.msgeditor import make_commit_message_template, _get_editor
 
25
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
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 = make_commit_message_template(working_tree, None)
 
47
        self.assertEqualDiff(template,
 
48
u"""\
 
49
added:
 
50
  hell\u00d8
 
51
""")
 
52
 
 
53
    def test__get_editor(self):
 
54
        # Test that _get_editor can return a decent list of items
 
55
        bzr_editor = os.environ.get('BZR_EDITOR')
 
56
        visual = os.environ.get('VISUAL')
 
57
        editor = os.environ.get('EDITOR')
 
58
        try:
 
59
            os.environ['BZR_EDITOR'] = 'bzr_editor'
 
60
            os.environ['VISUAL'] = 'visual'
 
61
            os.environ['EDITOR'] = 'editor'
 
62
 
 
63
            ensure_config_dir_exists()
 
64
            f = open(config_filename(), 'wb')
 
65
            f.write('editor = config_editor\n')
 
66
            f.close()
 
67
 
 
68
            editors = list(_get_editor())
 
69
 
 
70
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
 
71
                              'editor'], editors[:4])
 
72
 
 
73
            if sys.platform == 'win32':
 
74
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
75
            else:
 
76
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
 
77
                                  'joe'], editors[4:])
 
78
 
 
79
        finally:
 
80
            # Restore the environment
 
81
            if bzr_editor is None:
 
82
                del os.environ['BZR_EDITOR']
 
83
            else:
 
84
                os.environ['BZR_EDITOR'] = bzr_editor
 
85
            if visual is None:
 
86
                del os.environ['VISUAL']
 
87
            else:
 
88
                os.environ['VISUAL'] = visual
 
89
            if editor is None:
 
90
                del os.environ['EDITOR']
 
91
            else:
 
92
                os.environ['EDITOR'] = editor