/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_workingtree.py

  • Committer: Robert Collins
  • Date: 2006-05-18 12:40:03 UTC
  • mto: (1714.1.2 integration)
  • mto: This revision was merged to the branch mainline in revision 1716.
  • Revision ID: robertc@robertcollins.net-20060518124003-5f97fb42c52ca941
Combine ignore rules into a single regex preventing pathological behaviour during add.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
# Authors:  Robert Collins <robert.collins@canonical.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
from cStringIO import StringIO
 
19
import os
 
20
import _sre
 
21
 
 
22
import bzrlib
 
23
from bzrlib.branch import Branch
 
24
import bzrlib.bzrdir as bzrdir
 
25
from bzrlib.bzrdir import BzrDir
 
26
from bzrlib.conflicts import *
 
27
import bzrlib.errors as errors
 
28
from bzrlib.errors import NotBranchError, NotVersionedError
 
29
from bzrlib.lockdir import LockDir
 
30
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
 
31
from bzrlib.tests import TestCaseWithTransport, TestSkipped
 
32
from bzrlib.trace import mutter
 
33
from bzrlib.transport import get_transport
 
34
import bzrlib.workingtree as workingtree
 
35
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
 
36
                                WorkingTree)
 
37
 
 
38
class TestTreeDirectory(TestCaseWithTransport):
 
39
 
 
40
    def test_kind_character(self):
 
41
        self.assertEqual(TreeDirectory().kind_character(), '/')
 
42
 
 
43
 
 
44
class TestTreeEntry(TestCaseWithTransport):
 
45
 
 
46
    def test_kind_character(self):
 
47
        self.assertEqual(TreeEntry().kind_character(), '???')
 
48
 
 
49
 
 
50
class TestTreeFile(TestCaseWithTransport):
 
51
 
 
52
    def test_kind_character(self):
 
53
        self.assertEqual(TreeFile().kind_character(), '')
 
54
 
 
55
 
 
56
class TestTreeLink(TestCaseWithTransport):
 
57
 
 
58
    def test_kind_character(self):
 
59
        self.assertEqual(TreeLink().kind_character(), '')
 
60
 
 
61
 
 
62
class TestDefaultFormat(TestCaseWithTransport):
 
63
 
 
64
    def test_get_set_default_format(self):
 
65
        old_format = workingtree.WorkingTreeFormat.get_default_format()
 
66
        # default is 3
 
67
        self.assertTrue(isinstance(old_format, workingtree.WorkingTreeFormat3))
 
68
        workingtree.WorkingTreeFormat.set_default_format(SampleTreeFormat())
 
69
        try:
 
70
            # the default branch format is used by the meta dir format
 
71
            # which is not the default bzrdir format at this point
 
72
            dir = bzrdir.BzrDirMetaFormat1().initialize('.')
 
73
            dir.create_repository()
 
74
            dir.create_branch()
 
75
            result = dir.create_workingtree()
 
76
            self.assertEqual(result, 'A tree')
 
77
        finally:
 
78
            workingtree.WorkingTreeFormat.set_default_format(old_format)
 
79
        self.assertEqual(old_format, workingtree.WorkingTreeFormat.get_default_format())
 
80
 
 
81
 
 
82
class SampleTreeFormat(workingtree.WorkingTreeFormat):
 
83
    """A sample format
 
84
 
 
85
    this format is initializable, unsupported to aid in testing the 
 
86
    open and open_downlevel routines.
 
87
    """
 
88
 
 
89
    def get_format_string(self):
 
90
        """See WorkingTreeFormat.get_format_string()."""
 
91
        return "Sample tree format."
 
92
 
 
93
    def initialize(self, a_bzrdir, revision_id=None):
 
94
        """Sample branches cannot be created."""
 
95
        t = a_bzrdir.get_workingtree_transport(self)
 
96
        t.put('format', StringIO(self.get_format_string()))
 
97
        return 'A tree'
 
98
 
 
99
    def is_supported(self):
 
100
        return False
 
101
 
 
102
    def open(self, transport, _found=False):
 
103
        return "opened tree."
 
104
 
 
105
 
 
106
class TestWorkingTreeFormat(TestCaseWithTransport):
 
107
    """Tests for the WorkingTreeFormat facility."""
 
108
 
 
109
    def test_find_format(self):
 
110
        # is the right format object found for a working tree?
 
111
        # create a branch with a few known format objects.
 
112
        self.build_tree(["foo/", "bar/"])
 
113
        def check_format(format, url):
 
114
            dir = format._matchingbzrdir.initialize(url)
 
115
            dir.create_repository()
 
116
            dir.create_branch()
 
117
            format.initialize(dir)
 
118
            t = get_transport(url)
 
119
            found_format = workingtree.WorkingTreeFormat.find_format(dir)
 
120
            self.failUnless(isinstance(found_format, format.__class__))
 
121
        check_format(workingtree.WorkingTreeFormat3(), "bar")
 
122
        
 
123
    def test_find_format_no_tree(self):
 
124
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
 
125
        self.assertRaises(errors.NoWorkingTree,
 
126
                          workingtree.WorkingTreeFormat.find_format,
 
127
                          dir)
 
128
 
 
129
    def test_find_format_unknown_format(self):
 
130
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
 
131
        dir.create_repository()
 
132
        dir.create_branch()
 
133
        SampleTreeFormat().initialize(dir)
 
134
        self.assertRaises(errors.UnknownFormatError,
 
135
                          workingtree.WorkingTreeFormat.find_format,
 
136
                          dir)
 
137
 
 
138
    def test_register_unregister_format(self):
 
139
        format = SampleTreeFormat()
 
140
        # make a control dir
 
141
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
 
142
        dir.create_repository()
 
143
        dir.create_branch()
 
144
        # make a branch
 
145
        format.initialize(dir)
 
146
        # register a format for it.
 
147
        workingtree.WorkingTreeFormat.register_format(format)
 
148
        # which branch.Open will refuse (not supported)
 
149
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
 
150
        # but open_downlevel will work
 
151
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
 
152
        # unregister the format
 
153
        workingtree.WorkingTreeFormat.unregister_format(format)
 
154
 
 
155
 
 
156
class TestWorkingTreeFormat3(TestCaseWithTransport):
 
157
    """Tests specific to WorkingTreeFormat3."""
 
158
 
 
159
    def test_disk_layout(self):
 
160
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
161
        control.create_repository()
 
162
        control.create_branch()
 
163
        tree = workingtree.WorkingTreeFormat3().initialize(control)
 
164
        # we want:
 
165
        # format 'Bazaar-NG Working Tree format 3'
 
166
        # inventory = blank inventory
 
167
        # pending-merges = ''
 
168
        # stat-cache = ??
 
169
        # no inventory.basis yet
 
170
        t = control.get_workingtree_transport(None)
 
171
        self.assertEqualDiff('Bazaar-NG Working Tree format 3',
 
172
                             t.get('format').read())
 
173
        self.assertEqualDiff('<inventory format="5">\n'
 
174
                             '</inventory>\n',
 
175
                             t.get('inventory').read())
 
176
        self.assertEqualDiff('### bzr hashcache v5\n',
 
177
                             t.get('stat-cache').read())
 
178
        self.assertFalse(t.has('inventory.basis'))
 
179
        # no last-revision file means 'None' or 'NULLREVISION'
 
180
        self.assertFalse(t.has('last-revision'))
 
181
        # TODO RBC 20060210 do a commit, check the inventory.basis is created 
 
182
        # correctly and last-revision file becomes present.
 
183
 
 
184
    def test_uses_lockdir(self):
 
185
        """WorkingTreeFormat3 uses its own LockDir:
 
186
            
 
187
            - lock is a directory
 
188
            - when the WorkingTree is locked, LockDir can see that
 
189
        """
 
190
        t = self.get_transport()
 
191
        url = self.get_url()
 
192
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
 
193
        repo = dir.create_repository()
 
194
        branch = dir.create_branch()
 
195
        try:
 
196
            tree = workingtree.WorkingTreeFormat3().initialize(dir)
 
197
        except errors.NotLocalUrl:
 
198
            raise TestSkipped('Not a local URL')
 
199
        self.assertIsDirectory('.bzr', t)
 
200
        self.assertIsDirectory('.bzr/checkout', t)
 
201
        self.assertIsDirectory('.bzr/checkout/lock', t)
 
202
        our_lock = LockDir(t, '.bzr/checkout/lock')
 
203
        self.assertEquals(our_lock.peek(), None)
 
204
        tree.lock_write()
 
205
        self.assertTrue(our_lock.peek())
 
206
        tree.unlock()
 
207
        self.assertEquals(our_lock.peek(), None)
 
208
 
 
209
 
 
210
class TestFormat2WorkingTree(TestCaseWithTransport):
 
211
    """Tests that are specific to format 2 trees."""
 
212
 
 
213
    def create_format2_tree(self, url):
 
214
        return self.make_branch_and_tree(
 
215
            url, format=bzrlib.bzrdir.BzrDirFormat6())
 
216
 
 
217
    def test_conflicts(self):
 
218
        # test backwards compatability
 
219
        tree = self.create_format2_tree('.')
 
220
        self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
 
221
                          None)
 
222
        file('lala.BASE', 'wb').write('labase')
 
223
        expected = ContentsConflict('lala')
 
224
        self.assertEqual(list(tree.conflicts()), [expected])
 
225
        file('lala', 'wb').write('la')
 
226
        tree.add('lala', 'lala-id')
 
227
        expected = ContentsConflict('lala', file_id='lala-id')
 
228
        self.assertEqual(list(tree.conflicts()), [expected])
 
229
        file('lala.THIS', 'wb').write('lathis')
 
230
        file('lala.OTHER', 'wb').write('laother')
 
231
        # When "text conflict"s happen, stem, THIS and OTHER are text
 
232
        expected = TextConflict('lala', file_id='lala-id')
 
233
        self.assertEqual(list(tree.conflicts()), [expected])
 
234
        os.unlink('lala.OTHER')
 
235
        os.mkdir('lala.OTHER')
 
236
        expected = ContentsConflict('lala', file_id='lala-id')
 
237
        self.assertEqual(list(tree.conflicts()), [expected])
 
238
 
 
239
 
 
240
class TestNonFormatSpecificCode(TestCaseWithTransport):
 
241
    """This class contains tests of workingtree that are not format specific."""
 
242
 
 
243
    def test__translate_ignore_rule(self):
 
244
        tree = self.make_branch_and_tree('.')
 
245
        # translation should return the regex, the number of groups in it,
 
246
        # and the original rule in a tuple.
 
247
        # there are three sorts of ignore rules:
 
248
        # root only - regex is the rule itself without the leading ./
 
249
        self.assertEqual(
 
250
            "(rootdirrule$)", 
 
251
            tree._translate_ignore_rule("./rootdirrule"))
 
252
        # full path - regex is the rule itself
 
253
        self.assertEqual(
 
254
            "(path\\/to\\/file$)",
 
255
            tree._translate_ignore_rule("path/to/file"))
 
256
        # basename only rule - regex is a rule that ignores everything up
 
257
        # to the last / in the filename
 
258
        self.assertEqual(
 
259
            "((?:.*/)?(?!.*/)basenamerule$)",
 
260
            tree._translate_ignore_rule("basenamerule"))
 
261
 
 
262
    def test__combine_ignore_rules(self):
 
263
        tree = self.make_branch_and_tree('.')
 
264
        # the combined ignore regexs need the outer group indices
 
265
        # placed in a dictionary with the rules that were combined.
 
266
        # an empty set of rules
 
267
        compiled_rules = tree._combine_ignore_rules([])
 
268
        # what type *is* the compiled regex to do an isinstance of ?
 
269
        self.assertEqual(0, compiled_rules[0].groups)
 
270
        self.assertEqual({}, compiled_rules[1])
 
271
        # one of each type of rule.
 
272
        compiled_rules = tree._combine_ignore_rules(
 
273
            ["rule1", "rule/two", "./three"])
 
274
        self.assertEqual(3, compiled_rules[0].groups)
 
275
        self.assertEqual(
 
276
            {0:"rule1",1:"rule/two",2:"./three"},
 
277
            compiled_rules[1])
 
278
 
 
279
    def test__get_ignore_rules_as_regex(self):
 
280
        tree = self.make_branch_and_tree('.')
 
281
        # test against the default rules.
 
282
        reference_output = tree._combine_ignore_rules(bzrlib.DEFAULT_IGNORE)
 
283
        regex_rules = tree._get_ignore_rules_as_regex()
 
284
        self.assertEqual(len(reference_output[1]), regex_rules[0].groups)
 
285
        self.assertEqual(reference_output[1], regex_rules[1])