~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: 2007-03-01 21:56:19 UTC
  • mto: (2255.7.84 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: john@arbash-meinel.com-20070301215619-wpt6kz8yem3ypu1b
Update to dirstate locking.
Move all of WT4.lock_* functions locally, so that they can
properly interact and cleanup around when we lock/unlock the
dirstate file.
Change all Lock objects to be non-blocking. So that if someone
grabs a lock on the DirState we find out immediately, rather
than blocking.
Change WT4.unlock() so that if the dirstate is dirty, it will
save the contents even if it only has a read lock.
It does this by trying to take a write lock, if it fails
we just ignore it. If it succeeds, then we can flush to disk.
This is more important now that DirState tracks file changes.
It allows 'bzr status' to update the cached stat and sha values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 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 as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Test commit message editor.
 
18
"""
 
19
 
 
20
import os
 
21
import sys
 
22
 
 
23
from bzrlib.branch import Branch
 
24
from bzrlib.config import ensure_config_dir_exists, config_filename
 
25
import bzrlib.msgeditor 
 
26
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
27
from bzrlib.trace import mutter
 
28
 
 
29
 
 
30
class MsgEditorTest(TestCaseWithTransport):
 
31
 
 
32
    def make_uncommitted_tree(self):
 
33
        """Build a branch with uncommitted unicode named changes in the cwd."""
 
34
        working_tree = self.make_branch_and_tree('.')
 
35
        b = working_tree.branch
 
36
        filename = u'hell\u00d8'
 
37
        try:
 
38
            self.build_tree_contents([(filename, 'contents of hello')])
 
39
        except UnicodeEncodeError:
 
40
            raise TestSkipped("can't build unicode working tree in "
 
41
                "filesystem encoding %s" % sys.getfilesystemencoding())
 
42
        working_tree.add(filename)
 
43
        return working_tree
 
44
    
 
45
    def test_commit_template(self):
 
46
        """Test building a commit message template"""
 
47
        working_tree = self.make_uncommitted_tree()
 
48
        template = bzrlib.msgeditor.make_commit_message_template(working_tree, None)
 
49
        self.assertEqualDiff(template,
 
50
u"""\
 
51
added:
 
52
  hell\u00d8
 
53
""")
 
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 test_edit_commit_message(self):
 
84
        working_tree = self.make_uncommitted_tree()
 
85
        # make fake editor
 
86
        f = file('fed.py', 'wb')
 
87
        f.write('#!%s\n' % sys.executable)
 
88
        f.write("""\
 
89
import sys
 
90
if len(sys.argv) == 2:
 
91
    fn = sys.argv[1]
 
92
    f = file(fn, 'rb')
 
93
    s = f.read()
 
94
    f.close()
 
95
    f = file(fn, 'wb')
 
96
    f.write('test message from fed\\n')
 
97
    f.write(s)
 
98
    f.close()
 
99
""")
 
100
        f.close()
 
101
        if sys.platform == "win32":
 
102
            # [win32] make batch file and set BZR_EDITOR
 
103
            f = file('fed.bat', 'w')
 
104
            f.write("""\
 
105
@echo off
 
106
"%s" fed.py %%1
 
107
""" % sys.executable)
 
108
            f.close()
 
109
            os.environ['BZR_EDITOR'] = 'fed.bat'
 
110
        else:
 
111
            # [non-win32] make python script executable and set BZR_EDITOR
 
112
            os.chmod('fed.py', 0755)
 
113
            os.environ['BZR_EDITOR'] = './fed.py'
 
114
 
 
115
        mutter('edit_commit_message without infotext')
 
116
        self.assertEqual('test message from fed\n',
 
117
                         bzrlib.msgeditor.edit_commit_message(''))
 
118
 
 
119
        mutter('edit_commit_message with unicode infotext')
 
120
        self.assertEqual('test message from fed\n',
 
121
                         bzrlib.msgeditor.edit_commit_message(u'\u1234'))
 
122
 
 
123
    def test_deleted_commit_message(self):
 
124
        working_tree = self.make_uncommitted_tree()
 
125
 
 
126
        if sys.platform == 'win32':
 
127
            os.environ['BZR_EDITOR'] = 'cmd.exe /c del'
 
128
        else:
 
129
            os.environ['BZR_EDITOR'] = 'rm'
 
130
 
 
131
        self.assertRaises((IOError, OSError), bzrlib.msgeditor.edit_commit_message, '')
 
132
 
 
133
    def test__get_editor(self):
 
134
        # Test that _get_editor can return a decent list of items
 
135
        bzr_editor = os.environ.get('BZR_EDITOR')
 
136
        visual = os.environ.get('VISUAL')
 
137
        editor = os.environ.get('EDITOR')
 
138
        try:
 
139
            os.environ['BZR_EDITOR'] = 'bzr_editor'
 
140
            os.environ['VISUAL'] = 'visual'
 
141
            os.environ['EDITOR'] = 'editor'
 
142
 
 
143
            ensure_config_dir_exists()
 
144
            f = open(config_filename(), 'wb')
 
145
            f.write('editor = config_editor\n')
 
146
            f.close()
 
147
 
 
148
            editors = list(bzrlib.msgeditor._get_editor())
 
149
 
 
150
            self.assertEqual(['bzr_editor', 'config_editor', 'visual',
 
151
                              'editor'], editors[:4])
 
152
 
 
153
            if sys.platform == 'win32':
 
154
                self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:])
 
155
            else:
 
156
                self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano',
 
157
                                  'joe'], editors[4:])
 
158
 
 
159
        finally:
 
160
            # Restore the environment
 
161
            if bzr_editor is None:
 
162
                del os.environ['BZR_EDITOR']
 
163
            else:
 
164
                os.environ['BZR_EDITOR'] = bzr_editor
 
165
            if visual is None:
 
166
                del os.environ['VISUAL']
 
167
            else:
 
168
                os.environ['VISUAL'] = visual
 
169
            if editor is None:
 
170
                del os.environ['EDITOR']
 
171
            else:
 
172
                os.environ['EDITOR'] = editor