~bzr-pqm/bzr/bzr.dev

1399.1.12 by Robert Collins
add new test script
1
# (C) 2005 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
import os
19
from bzrlib.branch import Branch
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
20
from bzrlib.errors import NotBranchError, NotVersionedError
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
21
from bzrlib.tests import TestCaseInTempDir
1399.1.12 by Robert Collins
add new test script
22
from bzrlib.trace import mutter
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
23
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
24
                                WorkingTree)
1399.1.12 by Robert Collins
add new test script
25
26
class TestTreeDirectory(TestCaseInTempDir):
27
28
    def test_kind_character(self):
29
        self.assertEqual(TreeDirectory().kind_character(), '/')
30
31
32
class TestTreeEntry(TestCaseInTempDir):
33
34
    def test_kind_character(self):
35
        self.assertEqual(TreeEntry().kind_character(), '???')
36
37
38
class TestTreeFile(TestCaseInTempDir):
39
40
    def test_kind_character(self):
41
        self.assertEqual(TreeFile().kind_character(), '')
42
43
44
class TestTreeLink(TestCaseInTempDir):
45
46
    def test_kind_character(self):
47
        self.assertEqual(TreeLink().kind_character(), '')
48
49
50
class TestWorkingTree(TestCaseInTempDir):
51
52
    def test_listfiles(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
53
        branch = Branch.initialize(u'.')
1399.1.12 by Robert Collins
add new test script
54
        os.mkdir('dir')
55
        print >> open('file', 'w'), "content"
56
        os.symlink('target', 'symlink')
57
        tree = branch.working_tree()
58
        files = list(tree.list_files())
59
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
60
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
61
        self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
62
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
63
    def test_open_containing(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
64
        branch = Branch.initialize(u'.')
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
65
        wt, relpath = WorkingTree.open_containing()
66
        self.assertEqual('', relpath)
67
        self.assertEqual(wt.basedir, branch.base)
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
68
        wt, relpath = WorkingTree.open_containing(u'.')
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
69
        self.assertEqual('', relpath)
70
        self.assertEqual(wt.basedir, branch.base)
71
        wt, relpath = WorkingTree.open_containing('./foo')
72
        self.assertEqual('foo', relpath)
73
        self.assertEqual(wt.basedir, branch.base)
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
74
        # paths that are urls are just plain wrong for working trees.
75
        self.assertRaises(NotBranchError,
76
                          WorkingTree.open_containing, 
77
                          'file:///' + os.getcwdu())
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
78
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
79
    def test_construct_with_branch(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
80
        branch = Branch.initialize(u'.')
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
81
        tree = WorkingTree(branch.base, branch)
82
        self.assertEqual(branch, tree.branch)
83
        self.assertEqual(branch.base, tree.basedir)
84
    
85
    def test_construct_without_branch(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
86
        branch = Branch.initialize(u'.')
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
87
        tree = WorkingTree(branch.base)
88
        self.assertEqual(branch.base, tree.branch.base)
89
        self.assertEqual(branch.base, tree.basedir)
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
90
91
    def test_basic_relpath(self):
92
        # for comprehensive relpath tests, see whitebox.py.
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
93
        branch = Branch.initialize(u'.')
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
94
        tree = WorkingTree(branch.base)
95
        self.assertEqual('child',
96
                         tree.relpath(os.path.join(os.getcwd(), 'child')))
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
97
98
    def test_lock_locks_branch(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
99
        branch = Branch.initialize(u'.')
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
100
        tree = WorkingTree(branch.base)
101
        tree.lock_read()
102
        self.assertEqual(1, tree.branch._lock_count)
103
        self.assertEqual('r', tree.branch._lock_mode)
104
        tree.unlock()
105
        self.assertEqual(None, tree.branch._lock_count)
106
        tree.lock_write()
107
        self.assertEqual(1, tree.branch._lock_count)
108
        self.assertEqual('w', tree.branch._lock_mode)
109
        tree.unlock()
110
        self.assertEqual(None, tree.branch._lock_count)
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
111
 
112
    def get_pullable_branches(self):
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
113
        self.build_tree(['from/', 'from/file', 'to/'])
114
        br_a = Branch.initialize('from')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
115
        tree = br_a.working_tree()
116
        tree.add('file')
117
        tree.commit('foo', rev_id='A')
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
118
        br_b = Branch.initialize('to')
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
119
        return br_a, br_b
120
 
121
    def test_pull(self):
122
        br_a, br_b = self.get_pullable_branches()
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
123
        br_b.working_tree().pull(br_a)
124
        self.failUnless(br_b.has_revision('A'))
125
        self.assertEqual(['A'], br_b.revision_history())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
126
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
127
    def test_pull_overwrites(self):
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
128
        br_a, br_b = self.get_pullable_branches()
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
129
        br_b.working_tree().commit('foo', rev_id='B')
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
130
        self.assertEqual(['B'], br_b.revision_history())
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
131
        br_b.working_tree().pull(br_a, overwrite=True)
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
132
        self.failUnless(br_b.has_revision('A'))
133
        self.failUnless(br_b.has_revision('B'))
134
        self.assertEqual(['A'], br_b.revision_history())
1501 by Robert Collins
Move revert from Branch to WorkingTree.
135
136
    def test_revert(self):
137
        """Test selected-file revert"""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
138
        b = Branch.initialize(u'.')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
139
140
        self.build_tree(['hello.txt'])
141
        file('hello.txt', 'w').write('initial hello')
142
143
        self.assertRaises(NotVersionedError,
144
                          b.working_tree().revert, ['hello.txt'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
145
        tree = WorkingTree(b.base, b)
146
        tree.add(['hello.txt'])
147
        tree.commit('create initial hello.txt')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
148
149
        self.check_file_contents('hello.txt', 'initial hello')
150
        file('hello.txt', 'w').write('new hello')
151
        self.check_file_contents('hello.txt', 'new hello')
152
153
        # revert file modified since last revision
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
154
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
155
        self.check_file_contents('hello.txt', 'initial hello')
156
        self.check_file_contents('hello.txt~', 'new hello')
157
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
158
        # reverting again does not clobber the backup
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
159
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
160
        self.check_file_contents('hello.txt', 'initial hello')
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
161
        self.check_file_contents('hello.txt~', 'new hello')
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
162
163
    def test_unknowns(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
164
        b = Branch.initialize(u'.')
165
        tree = WorkingTree(u'.', b)
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
166
        self.build_tree(['hello.txt',
167
                         'hello.txt~'])
168
        self.assertEquals(list(tree.unknowns()),
169
                          ['hello.txt'])
170