~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_conflicts.py

  • Committer: Ian Clatworthy
  • Date: 2009-01-19 02:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 3944.
  • Revision ID: ian.clatworthy@canonical.com-20090119022415-mo0mcfeiexfktgwt
apply jam's log --short fix (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
import os
19
19
 
 
20
from bzrlib import bzrdir
20
21
from bzrlib.tests import TestCaseWithTransport, TestCase
21
22
from bzrlib.branch import Branch
22
 
from bzrlib.conflicts import *
 
23
from bzrlib.conflicts import (
 
24
    ConflictList,
 
25
    ContentsConflict,
 
26
    DuplicateID,
 
27
    DuplicateEntry,
 
28
    MissingParent,
 
29
    NonDirectoryParent,
 
30
    ParentLoop,
 
31
    PathConflict,
 
32
    TextConflict,
 
33
    UnversionedParent,
 
34
    resolve,
 
35
    restore,
 
36
    )
23
37
from bzrlib.errors import NotConflicted
24
38
 
25
39
 
28
42
 
29
43
# The order of 'path' here is important - do not let it
30
44
# be a sorted list.
 
45
# u'\xe5' == a with circle
 
46
# '\xc3\xae' == u'\xee' == i with hat
 
47
# So these are u'pathg' and 'idg' only with a circle and a hat. (shappo?)
31
48
example_conflicts = ConflictList([ 
32
 
    MissingParent('Not deleting', 'pathg', 'idg'),
33
 
    ContentsConflict('patha', 'ida'), 
34
 
    TextConflict('patha'),
35
 
    PathConflict('pathb', 'pathc', 'idb'),
36
 
    DuplicateID('Unversioned existing file', 'pathc', 'pathc2', 'idc', 'idc'),
37
 
    DuplicateEntry('Moved existing file to',  'pathdd.moved', 'pathd', 'idd', 
38
 
                   None),
39
 
    ParentLoop('Cancelled move', 'pathe', 'path2e', None, 'id2e'),
40
 
    UnversionedParent('Versioned directory', 'pathf', 'idf'),
 
49
    MissingParent('Not deleting', u'p\xe5thg', '\xc3\xaedg'),
 
50
    ContentsConflict(u'p\xe5tha', None, '\xc3\xaeda'), 
 
51
    TextConflict(u'p\xe5tha'),
 
52
    PathConflict(u'p\xe5thb', u'p\xe5thc', '\xc3\xaedb'),
 
53
    DuplicateID('Unversioned existing file', u'p\xe5thc', u'p\xe5thc2',
 
54
                '\xc3\xaedc', '\xc3\xaedc'),
 
55
    DuplicateEntry('Moved existing file to',  u'p\xe5thdd.moved', u'p\xe5thd',
 
56
                   '\xc3\xaedd', None),
 
57
    ParentLoop('Cancelled move', u'p\xe5the', u'p\xe5th2e',
 
58
               None, '\xc3\xaed2e'),
 
59
    UnversionedParent('Versioned directory', u'p\xe5thf', '\xc3\xaedf'),
 
60
    NonDirectoryParent('Created directory', u'p\xe5thg', '\xc3\xaedg'),
41
61
])
42
62
 
43
63
 
46
66
    def test_conflicts(self):
47
67
        """Conflicts are detected properly"""
48
68
        tree = self.make_branch_and_tree('.',
49
 
            format=bzrlib.bzrdir.BzrDirFormat6())
 
69
            format=bzrdir.BzrDirFormat6())
50
70
        b = tree.branch
51
71
        file('hello', 'w').write('hello world4')
52
72
        file('hello.THIS', 'w').write('hello world2')
54
74
        file('hello.OTHER', 'w').write('hello world3')
55
75
        file('hello.sploo.BASE', 'w').write('yellow world')
56
76
        file('hello.sploo.OTHER', 'w').write('yellow world2')
 
77
        tree.lock_read()
57
78
        self.assertEqual(len(list(tree.list_files())), 6)
 
79
        tree.unlock()
58
80
        conflicts = tree.conflicts()
59
81
        self.assertEqual(len(conflicts), 2)
60
82
        self.assert_('hello' in conflicts[0].path)
63
85
        restore('hello.sploo')
64
86
        self.assertEqual(len(tree.conflicts()), 0)
65
87
        self.assertFileEqual('hello world2', 'hello')
66
 
        assert not os.path.lexists('hello.sploo')
 
88
        self.assertFalse(os.path.lexists('hello.sploo'))
67
89
        self.assertRaises(NotConflicted, restore, 'hello')
68
90
        self.assertRaises(NotConflicted, restore, 'hello.sploo')
69
91
 
78
100
        l = ConflictList([TextConflict('hello')])
79
101
        l.remove_files(tree)
80
102
 
 
103
    def test_select_conflicts(self):
 
104
        tree = self.make_branch_and_tree('.')
 
105
        tree_conflicts = ConflictList([ContentsConflict('foo'),
 
106
                                       ContentsConflict('bar')])
 
107
        self.assertEqual((ConflictList([ContentsConflict('bar')]),
 
108
                          ConflictList([ContentsConflict('foo')])),
 
109
                         tree_conflicts.select_conflicts(tree, ['foo']))
 
110
        self.assertEqual((ConflictList(), tree_conflicts),
 
111
                         tree_conflicts.select_conflicts(tree, [''],
 
112
                         ignore_misses=True, recurse=True))
 
113
        tree_conflicts = ConflictList([ContentsConflict('foo/baz'),
 
114
                                       ContentsConflict('bar')])
 
115
        self.assertEqual((ConflictList([ContentsConflict('bar')]),
 
116
                          ConflictList([ContentsConflict('foo/baz')])),
 
117
                         tree_conflicts.select_conflicts(tree, ['foo'],
 
118
                                                         recurse=True,
 
119
                                                         ignore_misses=True))
 
120
        tree_conflicts = ConflictList([PathConflict('qux', 'foo/baz')])
 
121
        self.assertEqual((ConflictList(), tree_conflicts),
 
122
                         tree_conflicts.select_conflicts(tree, ['foo'],
 
123
                                                         recurse=True,
 
124
                                                         ignore_misses=True))
 
125
        self.assertEqual((tree_conflicts, ConflictList()),
 
126
                         tree_conflicts.select_conflicts(tree, ['foo'],
 
127
                                                         ignore_misses=True))
 
128
 
 
129
    def test_resolve_conflicts_recursive(self):
 
130
        tree = self.make_branch_and_tree('.')
 
131
        self.build_tree(['dir/', 'dir/hello'])
 
132
        tree.add(['dir', 'dir/hello'])
 
133
        tree.set_conflicts(ConflictList([TextConflict('dir/hello')]))
 
134
        resolve(tree, ['dir'], recursive=False, ignore_misses=True)
 
135
        self.assertEqual(ConflictList([TextConflict('dir/hello')]),
 
136
                         tree.conflicts())
 
137
        resolve(tree, ['dir'], recursive=True, ignore_misses=True)
 
138
        self.assertEqual(ConflictList([]),
 
139
                         tree.conflicts())
 
140
 
81
141
 
82
142
class TestConflictStanzas(TestCase):
83
143
 
85
145
        # write and read our example stanza.
86
146
        stanza_iter = example_conflicts.to_stanzas()
87
147
        processed = ConflictList.from_stanzas(stanza_iter)
88
 
        for o,p in zip(processed, example_conflicts):
 
148
        for o, p in zip(processed, example_conflicts):
89
149
            self.assertEqual(o, p)
90
150
 
 
151
            self.assertIsInstance(o.path, unicode)
 
152
 
 
153
            if o.file_id is not None:
 
154
                self.assertIsInstance(o.file_id, str)
 
155
 
 
156
            conflict_path = getattr(o, 'conflict_path', None)
 
157
            if conflict_path is not None:
 
158
                self.assertIsInstance(conflict_path, unicode)
 
159
 
 
160
            conflict_file_id = getattr(o, 'conflict_file_id', None)
 
161
            if conflict_file_id is not None:
 
162
                self.assertIsInstance(conflict_file_id, str)
 
163
 
91
164
    def test_stanzification(self):
92
165
        for stanza in example_conflicts.to_stanzas():
93
 
            try:
94
 
                self.assertStartsWith(stanza['file_id'], 'id')
95
 
            except KeyError:
96
 
                pass
97
 
            self.assertStartsWith(stanza['path'], 'path')
98
 
            try:
99
 
                self.assertStartsWith(stanza['conflict_file_id'], 'id')
100
 
                self.assertStartsWith(stanza['conflict_file_path'], 'path')
101
 
            except KeyError:
102
 
                pass
 
166
            if 'file_id' in stanza:
 
167
                # In Stanza form, the file_id has to be unicode.
 
168
                self.assertStartsWith(stanza['file_id'], u'\xeed')
 
169
            self.assertStartsWith(stanza['path'], u'p\xe5th')
 
170
            if 'conflict_path' in stanza:
 
171
                self.assertStartsWith(stanza['conflict_path'], u'p\xe5th')
 
172
            if 'conflict_file_id' in stanza:
 
173
                self.assertStartsWith(stanza['conflict_file_id'], u'\xeed')