~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: Aaron Bentley
  • Date: 2007-03-07 23:15:10 UTC
  • mto: (1551.19.24 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2325.
  • Revision ID: abentley@panoramicfeedback.com-20070307231510-jae63zsli83db3eb
Make ChangeReporter private

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
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.
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
6
7
#
7
8
# This program is distributed in the hope that it will be useful,
8
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
22
 
22
23
from bzrlib.branch import Branch
23
24
from bzrlib.config import ensure_config_dir_exists, config_filename
24
 
from bzrlib.msgeditor import make_commit_message_template, _get_editor
 
25
import bzrlib.msgeditor 
25
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
27
from bzrlib.trace import mutter
26
28
 
27
29
 
28
30
class MsgEditorTest(TestCaseWithTransport):
43
45
    def test_commit_template(self):
44
46
        """Test building a commit message template"""
45
47
        working_tree = self.make_uncommitted_tree()
46
 
        template = make_commit_message_template(working_tree, None)
 
48
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
47
49
        self.assertEqualDiff(template,
48
50
u"""\
49
51
added:
50
52
  hell\u00d8
51
53
""")
52
54
 
 
55
    def setUp(self):
 
56
        super(MsgEditorTest, self).setUp()
 
57
        self._bzr_editor = os.environ.get('BZR_EDITOR', None)
 
58
 
 
59
    def tearDown(self):
 
60
        if self._bzr_editor is not None:
 
61
            os.environ['BZR_EDITOR'] = self._bzr_editor
 
62
        else:
 
63
            if os.environ.get('BZR_EDITOR', None) is not None:
 
64
                del os.environ['BZR_EDITOR']
 
65
        super(MsgEditorTest, self).tearDown()
 
66
 
 
67
    def test_run_editor(self):
 
68
        if sys.platform == "win32":
 
69
            f = file('fed.bat', 'w')
 
70
            f.write('@rem dummy fed')
 
71
            f.close()
 
72
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
73
        else:
 
74
            f = file('fed.sh', 'wb')
 
75
            f.write('#!/bin/sh\n')
 
76
            f.close()
 
77
            os.chmod('fed.sh', 0755)
 
78
            os.environ['BZR_EDITOR'] = './fed.sh'
 
79
 
 
80
        self.assertEqual(True, bzrlib.msgeditor._run_editor(''),
 
81
                         'Unable to run dummy fake editor')
 
82
 
 
83
    def make_fake_editor(self):
 
84
        """Set up environment so that an editor will be a known script.
 
85
 
 
86
        Sets up BZR_EDITOR so that if an editor is spawned it will run a
 
87
        script that just adds a known message to the start of the file.
 
88
        """
 
89
        f = file('fed.py', 'wb')
 
90
        f.write('#!%s\n' % sys.executable)
 
91
        f.write("""\
 
92
import sys
 
93
if len(sys.argv) == 2:
 
94
    fn = sys.argv[1]
 
95
    f = file(fn, 'rb')
 
96
    s = f.read()
 
97
    f.close()
 
98
    f = file(fn, 'wb')
 
99
    f.write('test message from fed\\n')
 
100
    f.write(s)
 
101
    f.close()
 
102
""")
 
103
        f.close()
 
104
        if sys.platform == "win32":
 
105
            # [win32] make batch file and set BZR_EDITOR
 
106
            f = file('fed.bat', 'w')
 
107
            f.write("""\
 
108
@echo off
 
109
"%s" fed.py %%1
 
110
""" % sys.executable)
 
111
            f.close()
 
112
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
113
        else:
 
114
            # [non-win32] make python script executable and set BZR_EDITOR
 
115
            os.chmod('fed.py', 0755)
 
116
            os.environ['BZR_EDITOR'] = './fed.py'
 
117
 
 
118
    def test_edit_commit_message(self):
 
119
        working_tree = self.make_uncommitted_tree()
 
120
        self.make_fake_editor()
 
121
 
 
122
        mutter('edit_commit_message without infotext')
 
123
        self.assertEqual('test message from fed\n',
 
124
                         bzrlib.msgeditor.edit_commit_message(''))
 
125
 
 
126
        mutter('edit_commit_message with unicode infotext')
 
127
        self.assertEqual('test message from fed\n',
 
128
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
 
129
 
 
130
    def test_start_message(self):
 
131
        self.make_uncommitted_tree()
 
132
        self.make_fake_editor()
 
133
        self.assertEqual('test message from fed\nstart message\n',
 
134
                         bzrlib.msgeditor.edit_commit_message('',
 
135
                                              start_message='start message\n'))
 
136
        self.assertEqual('test message from fed\n',
 
137
                         bzrlib.msgeditor.edit_commit_message('',
 
138
                                              start_message=''))
 
139
 
 
140
    def test_deleted_commit_message(self):
 
141
        working_tree = self.make_uncommitted_tree()
 
142
 
 
143
        if sys.platform == 'win32':
 
144
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
145
        else:
 
146
            os.environ['BZR_EDITOR'] = 'rm'
 
147
 
 
148
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
 
149
 
53
150
    def test__get_editor(self):
54
151
        # Test that _get_editor can return a decent list of items
55
152
        bzr_editor = os.environ.get('BZR_EDITOR')
65
162
            f.write('editor = config_editor\n')
66
163
            f.close()
67
164
 
68
 
            editors = list(_get_editor())
 
165
            editors = list(bzrlib.msgeditor._get_editor())
69
166
 
70
167
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
71
168
                              'editor'], editors[:4])