~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_msgeditor.py

  • Committer: John Arbash Meinel
  • Date: 2006-01-05 21:36:37 UTC
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060105213637-f6f19f470e1396bd
[patch] Alexander Belchenko: test spawning a msg editor

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import sys
21
21
 
22
22
from bzrlib.branch import Branch
23
 
from bzrlib.msgeditor import make_commit_message_template
 
23
import bzrlib.msgeditor
24
24
from bzrlib.tests import TestCaseInTempDir, TestSkipped
 
25
from bzrlib.trace import mutter
25
26
 
26
27
 
27
28
class MsgEditorTest(TestCaseInTempDir):
28
29
 
29
30
    def make_uncommitted_tree(self):
30
31
        """Build a branch with uncommitted unicode named changes in the cwd."""
31
 
        b = Branch.initialize('.')
 
32
        b = Branch.initialize(u'.')
32
33
        working_tree = b.working_tree()
33
34
        filename = u'hell\u00d8'
34
35
        try:
42
43
    def test_commit_template(self):
43
44
        """Test building a commit message template"""
44
45
        working_tree = self.make_uncommitted_tree()
45
 
        template = make_commit_message_template(working_tree, None)
 
46
        template = bzrlib.msgeditor.make_commit_message_template(working_tree,
 
47
                                                                 None)
46
48
        self.assertEqualDiff(template,
47
49
u"""\
48
50
added:
49
51
  hell\u00d8
50
52
""")
 
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'))