~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

merge from aaron - fixes bare excepts, adds ancestor namespace

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
20
 
from bzrlib.errors import NotBranchError, NotVersionedError
21
 
from bzrlib.tests import TestCaseInTempDir
22
 
from bzrlib.trace import mutter
23
 
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
24
 
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
25
 
                                WorkingTree)
26
 
 
27
 
class TestTreeDirectory(TestCaseInTempDir):
28
 
 
29
 
    def test_kind_character(self):
30
 
        self.assertEqual(TreeDirectory().kind_character(), '/')
31
 
 
32
 
 
33
 
class TestTreeEntry(TestCaseInTempDir):
34
 
 
35
 
    def test_kind_character(self):
36
 
        self.assertEqual(TreeEntry().kind_character(), '???')
37
 
 
38
 
 
39
 
class TestTreeFile(TestCaseInTempDir):
40
 
 
41
 
    def test_kind_character(self):
42
 
        self.assertEqual(TreeFile().kind_character(), '')
43
 
 
44
 
 
45
 
class TestTreeLink(TestCaseInTempDir):
46
 
 
47
 
    def test_kind_character(self):
48
 
        self.assertEqual(TreeLink().kind_character(), '')
49
 
 
50
 
 
51
 
class TestWorkingTree(TestCaseInTempDir):
52
 
 
53
 
    def test_listfiles(self):
54
 
        branch = Branch.initialize(u'.')
55
 
        os.mkdir('dir')
56
 
        print >> open('file', 'w'), "content"
57
 
        if has_symlinks():
58
 
            os.symlink('target', 'symlink')
59
 
        tree = branch.working_tree()
60
 
        files = list(tree.list_files())
61
 
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
62
 
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
63
 
        if has_symlinks():
64
 
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
65
 
 
66
 
    def test_open_containing(self):
67
 
        branch = Branch.initialize(u'.')
68
 
        wt, relpath = WorkingTree.open_containing()
69
 
        self.assertEqual('', relpath)
70
 
        self.assertEqual(wt.basedir, branch.base)
71
 
        wt, relpath = WorkingTree.open_containing(u'.')
72
 
        self.assertEqual('', relpath)
73
 
        self.assertEqual(wt.basedir, branch.base)
74
 
        wt, relpath = WorkingTree.open_containing('./foo')
75
 
        self.assertEqual('foo', relpath)
76
 
        self.assertEqual(wt.basedir, branch.base)
77
 
        # paths that are urls are just plain wrong for working trees.
78
 
        self.assertRaises(NotBranchError,
79
 
                          WorkingTree.open_containing, 
80
 
                          'file:///' + getcwd())
81
 
 
82
 
    def test_construct_with_branch(self):
83
 
        branch = Branch.initialize(u'.')
84
 
        tree = WorkingTree(branch.base, branch)
85
 
        self.assertEqual(branch, tree.branch)
86
 
        self.assertEqual(branch.base, tree.basedir)
87
 
    
88
 
    def test_construct_without_branch(self):
89
 
        branch = Branch.initialize(u'.')
90
 
        tree = WorkingTree(branch.base)
91
 
        self.assertEqual(branch.base, tree.branch.base)
92
 
        self.assertEqual(branch.base, tree.basedir)
93
 
 
94
 
    def test_basic_relpath(self):
95
 
        # for comprehensive relpath tests, see whitebox.py.
96
 
        branch = Branch.initialize(u'.')
97
 
        tree = WorkingTree(branch.base)
98
 
        self.assertEqual('child',
99
 
                         tree.relpath(pathjoin(getcwd(), 'child')))
100
 
 
101
 
    def test_lock_locks_branch(self):
102
 
        branch = Branch.initialize(u'.')
103
 
        tree = WorkingTree(branch.base)
104
 
        tree.lock_read()
105
 
        self.assertEqual(1, tree.branch.control_files._lock_count)
106
 
        self.assertEqual('r', tree.branch.control_files._lock_mode)
107
 
        tree.unlock()
108
 
        self.assertEqual(None, tree.branch.control_files._lock_count)
109
 
        tree.lock_write()
110
 
        self.assertEqual(1, tree.branch.control_files._lock_count)
111
 
        self.assertEqual('w', tree.branch.control_files._lock_mode)
112
 
        tree.unlock()
113
 
        self.assertEqual(None, tree.branch.control_files._lock_count)
114
 
 
115
 
    def get_pullable_branches(self):
116
 
        self.build_tree(['from/', 'from/file', 'to/'])
117
 
        br_a = Branch.initialize('from')
118
 
        tree = br_a.working_tree()
119
 
        tree.add('file')
120
 
        tree.commit('foo', rev_id='A')
121
 
        br_b = Branch.initialize('to')
122
 
        return br_a, br_b
123
 
 
124
 
    def test_pull(self):
125
 
        br_a, br_b = self.get_pullable_branches()
126
 
        br_b.working_tree().pull(br_a)
127
 
        self.failUnless(br_b.repository.has_revision('A'))
128
 
        self.assertEqual(['A'], br_b.revision_history())
129
 
 
130
 
    def test_pull_overwrites(self):
131
 
        br_a, br_b = self.get_pullable_branches()
132
 
        br_b.working_tree().commit('foo', rev_id='B')
133
 
        self.assertEqual(['B'], br_b.revision_history())
134
 
        br_b.working_tree().pull(br_a, overwrite=True)
135
 
        self.failUnless(br_b.repository.has_revision('A'))
136
 
        self.failUnless(br_b.repository.has_revision('B'))
137
 
        self.assertEqual(['A'], br_b.revision_history())
138
 
 
139
 
    def test_revert(self):
140
 
        """Test selected-file revert"""
141
 
        b = Branch.initialize(u'.')
142
 
 
143
 
        self.build_tree(['hello.txt'])
144
 
        file('hello.txt', 'w').write('initial hello')
145
 
 
146
 
        self.assertRaises(NotVersionedError,
147
 
                          b.working_tree().revert, ['hello.txt'])
148
 
        tree = WorkingTree(b.base, b)
149
 
        tree.add(['hello.txt'])
150
 
        tree.commit('create initial hello.txt')
151
 
 
152
 
        self.check_file_contents('hello.txt', 'initial hello')
153
 
        file('hello.txt', 'w').write('new hello')
154
 
        self.check_file_contents('hello.txt', 'new hello')
155
 
 
156
 
        # revert file modified since last revision
157
 
        tree.revert(['hello.txt'])
158
 
        self.check_file_contents('hello.txt', 'initial hello')
159
 
        self.check_file_contents('hello.txt~', 'new hello')
160
 
 
161
 
        # reverting again does not clobber the backup
162
 
        tree.revert(['hello.txt'])
163
 
        self.check_file_contents('hello.txt', 'initial hello')
164
 
        self.check_file_contents('hello.txt~', 'new hello')
165
 
 
166
 
    def test_unknowns(self):
167
 
        b = Branch.initialize(u'.')
168
 
        tree = WorkingTree(u'.', b)
169
 
        self.build_tree(['hello.txt',
170
 
                         'hello.txt~'])
171
 
        self.assertEquals(list(tree.unknowns()),
172
 
                          ['hello.txt'])
173
 
 
174
 
    def test_hashcache(self):
175
 
        from bzrlib.tests.test_hashcache import pause
176
 
        b = Branch.initialize(u'.')
177
 
        tree = WorkingTree(u'.', b)
178
 
        self.build_tree(['hello.txt',
179
 
                         'hello.txt~'])
180
 
        tree.add('hello.txt')
181
 
        pause()
182
 
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
183
 
        self.assertEqual(1, tree._hashcache.miss_count)
184
 
        tree2 = WorkingTree(u'.', b)
185
 
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
186
 
        self.assertEqual(0, tree2._hashcache.miss_count)
187
 
        self.assertEqual(1, tree2._hashcache.hit_count)