~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_conflicts.py

Major code cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2006, 2007, 2009, 2010, 2011 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
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
import os
18
 
 
19
 
from bzrlib.workingtree import WorkingTree
20
 
from bzrlib.tests.blackbox import ExternalBase
21
 
 
22
 
class TestConflicts(ExternalBase):
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from bzrlib import (
 
18
    conflicts,
 
19
    tests,
 
20
    workingtree,
 
21
    )
 
22
from bzrlib.tests import script, UnicodeFilenameFeature
 
23
 
 
24
 
 
25
def make_tree_with_conflicts(test, this_path='this', other_path='other',
 
26
        prefix='my'):
 
27
    this_tree = test.make_branch_and_tree(this_path)
 
28
    test.build_tree_contents([
 
29
        ('%s/%sfile' % (this_path, prefix), 'this content\n'),
 
30
        ('%s/%s_other_file' % (this_path, prefix), 'this content\n'),
 
31
        ('%s/%sdir/' % (this_path, prefix),),
 
32
        ])
 
33
    this_tree.add(prefix+'file')
 
34
    this_tree.add(prefix+'_other_file')
 
35
    this_tree.add(prefix+'dir')
 
36
    this_tree.commit(message="new")
 
37
    other_tree = this_tree.bzrdir.sprout(other_path).open_workingtree()
 
38
    test.build_tree_contents([
 
39
        ('%s/%sfile' % (other_path, prefix), 'contentsb\n'),
 
40
        ('%s/%s_other_file' % (other_path, prefix), 'contentsb\n'),
 
41
        ])
 
42
    other_tree.rename_one(prefix+'dir', prefix+'dir2')
 
43
    other_tree.commit(message="change")
 
44
    test.build_tree_contents([
 
45
        ('%s/%sfile' % (this_path, prefix), 'contentsa2\n'),
 
46
        ('%s/%s_other_file' % (this_path, prefix), 'contentsa2\n'),
 
47
        ])
 
48
    this_tree.rename_one(prefix+'dir', prefix+'dir3')
 
49
    this_tree.commit(message='change')
 
50
    this_tree.merge_from_branch(other_tree.branch)
 
51
    return this_tree, other_tree
 
52
 
 
53
 
 
54
class TestConflicts(script.TestCaseWithTransportAndScript):
 
55
 
23
56
    def setUp(self):
24
 
        super(ExternalBase, self).setUp()
25
 
        try:
26
 
            os.mkdir('a')
27
 
        except:
28
 
            raise os.getcwd()
29
 
        os.chdir('a')
30
 
        self.runbzr('init')
31
 
        file('myfile', 'wb').write('contentsa\n')
32
 
        file('my_other_file', 'wb').write('contentsa\n')
33
 
        self.runbzr('add')
34
 
        self.runbzr('commit -m new')
35
 
        os.chdir('..')
36
 
        self.runbzr('branch a b')
37
 
        os.chdir('b')
38
 
        file('myfile', 'wb').write('contentsb\n')
39
 
        file('my_other_file', 'wb').write('contentsb\n')
40
 
        self.runbzr('commit -m change')
41
 
        os.chdir('../a')
42
 
        file('myfile', 'wb').write('contentsa2\n')
43
 
        file('my_other_file', 'wb').write('contentsa2\n')
44
 
        self.runbzr('commit -m change')
45
 
        self.runbzr('merge ../b', retcode=1)
 
57
        super(TestConflicts, self).setUp()
 
58
        make_tree_with_conflicts(self, 'branch', 'other')
46
59
 
47
60
    def test_conflicts(self):
48
 
        conflicts = self.runbzr('conflicts', backtick=True)
49
 
        self.assertEqual(len(conflicts.splitlines()), 2)
50
 
 
51
 
    def test_resolve(self):
52
 
        self.runbzr('resolve', retcode=3)
53
 
        self.runbzr('resolve myfile')
54
 
        conflicts = self.runbzr('conflicts', backtick=True)
55
 
        self.assertEqual(len(conflicts.splitlines()), 1)
56
 
        self.runbzr('resolve my_other_file')
57
 
        conflicts = self.runbzr('conflicts', backtick=True)
58
 
        self.assertEqual(len(conflicts.splitlines()), 0)
59
 
 
60
 
    def test_resolve_all(self):
61
 
        self.runbzr('resolve --all')
62
 
        conflicts = self.runbzr('conflicts', backtick=True)
63
 
        self.assertEqual(len(conflicts.splitlines()), 0)
64
 
 
65
 
    def test_resolve_in_subdir(self):
66
 
        """resolve when run from subdirectory should handle relative paths"""
67
 
        orig_dir = os.getcwdu()
68
 
        try:
69
 
            os.mkdir("subdir")
70
 
            os.chdir("subdir")
71
 
            self.runbzr("resolve ../myfile")
72
 
            os.chdir("../../b")
73
 
            self.runbzr("resolve ../a/myfile")
74
 
            wt = WorkingTree.open_containing('.')[0]
75
 
            conflicts = wt.conflicts()
76
 
            if not conflicts.is_empty():
77
 
                self.fail("tree still contains conflicts: %r" % conflicts)
78
 
        finally:
79
 
            os.chdir(orig_dir)
 
61
        self.run_script("""\
 
62
$ cd branch
 
63
$ bzr conflicts
 
64
Text conflict in my_other_file
 
65
Path conflict: mydir3 / mydir2
 
66
Text conflict in myfile
 
67
""")
 
68
 
 
69
    def test_conflicts_text(self):
 
70
        self.run_script("""\
 
71
$ cd branch
 
72
$ bzr conflicts --text
 
73
my_other_file
 
74
myfile
 
75
""")
 
76
 
 
77
    def test_conflicts_directory(self):
 
78
        self.run_script("""\
 
79
$ bzr conflicts  -d branch
 
80
Text conflict in my_other_file
 
81
Path conflict: mydir3 / mydir2
 
82
Text conflict in myfile
 
83
""")
 
84
 
 
85
 
 
86
class TestUnicodePaths(tests.TestCaseWithTransport):
 
87
    """Unicode characters in conflicts should be displayed properly"""
 
88
 
 
89
    _test_needs_features = [UnicodeFilenameFeature]
 
90
    encoding = "UTF-8"
 
91
 
 
92
    def _as_output(self, text):
 
93
        return text
 
94
 
 
95
    def test_messages(self):
 
96
        """Conflict messages involving non-ascii paths are displayed okay"""
 
97
        make_tree_with_conflicts(self, "branch", prefix=u"\xA7")
 
98
        out, err = self.run_bzr(["conflicts", "-d", "branch"],
 
99
            encoding=self.encoding)
 
100
        self.assertEqual(out.decode(self.encoding),
 
101
            u"Text conflict in \xA7_other_file\n"
 
102
            u"Path conflict: \xA7dir3 / \xA7dir2\n"
 
103
            u"Text conflict in \xA7file\n")
 
104
        self.assertEqual(err, "")
 
105
 
 
106
    def test_text_conflict_paths(self):
 
107
        """Text conflicts on non-ascii paths are displayed okay"""
 
108
        make_tree_with_conflicts(self, "branch", prefix=u"\xA7")
 
109
        out, err = self.run_bzr(["conflicts", "-d", "branch", "--text"],
 
110
            encoding=self.encoding)
 
111
        self.assertEqual(out.decode(self.encoding),
 
112
            u"\xA7_other_file\n"
 
113
            u"\xA7file\n")
 
114
        self.assertEqual(err, "")
 
115
 
 
116
 
 
117
class TestUnicodePathsOnAsciiTerminal(TestUnicodePaths):
 
118
    """Undisplayable unicode characters in conflicts should be escaped"""
 
119
 
 
120
    encoding = "ascii"
 
121
 
 
122
    def setUp(self):
 
123
        self.skip("Need to decide if replacing is the desired behaviour")
 
124
 
 
125
    def _as_output(self, text):
 
126
        return text.encode(self.encoding, "replace")