~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (C) 2006, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from bzrlib import (
    conflicts,
    tests,
    workingtree,
    )

# FIXME: These don't really look at the output of the conflict commands, just
# the number of lines - there should be more examination.

class TestConflictsBase(tests.TestCaseWithTransport):

    def setUp(self):
        super(TestConflictsBase, self).setUp()
        self.make_tree_with_conflicts()

    def make_tree_with_conflicts(self):
        a_tree = self.make_branch_and_tree('a')
        self.build_tree_contents([
            ('a/myfile', 'contentsa\n'),
            ('a/my_other_file', 'contentsa\n'),
            ('a/mydir/',),
            ])
        a_tree.add('myfile')
        a_tree.add('my_other_file')
        a_tree.add('mydir')
        a_tree.commit(message="new")
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
        self.build_tree_contents([
            ('b/myfile', 'contentsb\n'),
            ('b/my_other_file', 'contentsb\n'),
            ])
        b_tree.rename_one('mydir', 'mydir2')
        b_tree.commit(message="change")
        self.build_tree_contents([
            ('a/myfile', 'contentsa2\n'),
            ('a/my_other_file', 'contentsa2\n'),
            ])
        a_tree.rename_one('mydir', 'mydir3')
        a_tree.commit(message='change')
        a_tree.merge_from_branch(b_tree.branch)

    def run_bzr(self, cmd, working_dir='a', **kwargs):
        return super(TestConflictsBase, self).run_bzr(
            cmd, working_dir=working_dir, **kwargs)


class TestConflicts(TestConflictsBase):

    def test_conflicts(self):
        out, err = self.run_bzr('conflicts')
        self.assertEqual(3, len(out.splitlines()))

    def test_conflicts_text(self):
        out, err = self.run_bzr('conflicts --text')
        self.assertEqual(['my_other_file', 'myfile'], out.splitlines())


class TestResolve(TestConflictsBase):

    def test_resolve(self):
        self.run_bzr('resolve myfile')
        out, err = self.run_bzr('conflicts')
        self.assertEqual(2, len(out.splitlines()))
        self.run_bzr('resolve my_other_file')
        self.run_bzr('resolve mydir2')
        out, err = self.run_bzr('conflicts')
        self.assertEqual(0, len(out.splitlines()))

    def test_resolve_all(self):
        self.run_bzr('resolve --all')
        out, err = self.run_bzr('conflicts')
        self.assertEqual(0, len(out.splitlines()))

    def test_resolve_in_subdir(self):
        """resolve when run from subdirectory should handle relative paths"""
        self.build_tree(["a/subdir/"])
        self.run_bzr("resolve ../myfile", working_dir='a/subdir')
        self.run_bzr("resolve ../a/myfile", working_dir='b')
        wt = workingtree.WorkingTree.open_containing('b')[0]
        conflicts = wt.conflicts()
        self.assertEqual(True, conflicts.is_empty(),
                         "tree still contains conflicts: %r" % conflicts)

    def test_auto_resolve(self):
        """Text conflicts can be resolved automatically"""
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([('tree/file',
            '<<<<<<<\na\n=======\n>>>>>>>\n')])
        tree.add('file', 'file_id')
        self.assertEqual(tree.kind('file_id'), 'file')
        file_conflict = conflicts.TextConflict('file', file_id='file_id')
        tree.set_conflicts(conflicts.ConflictList([file_conflict]))
        note = self.run_bzr('resolve', retcode=1, working_dir='tree')[1]
        self.assertContainsRe(note, '0 conflict\\(s\\) auto-resolved.')
        self.assertContainsRe(note,
            'Remaining conflicts:\nText conflict in file')
        self.build_tree_contents([('tree/file', 'a\n')])
        note = self.run_bzr('resolve', working_dir='tree')[1]
        self.assertContainsRe(note, 'All conflicts resolved.')