~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: Martin Pool
  • Date: 2006-01-23 02:09:39 UTC
  • mfrom: (1534.1.11 integration)
  • Revision ID: mbp@sourcefrog.net-20060123020939-567fb193328ed7a6
[merge] robert's integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import os
19
19
from bzrlib.branch import Branch
20
 
from bzrlib.errors import NotVersionedError
21
 
from bzrlib.selftest import TestCaseInTempDir
 
20
from bzrlib.errors import NotBranchError, NotVersionedError
 
21
from bzrlib.tests import TestCaseInTempDir
22
22
from bzrlib.trace import mutter
 
23
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
23
24
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
24
25
                                WorkingTree)
25
26
 
50
51
class TestWorkingTree(TestCaseInTempDir):
51
52
 
52
53
    def test_listfiles(self):
53
 
        branch = Branch.initialize('.')
 
54
        branch = Branch.initialize(u'.')
54
55
        os.mkdir('dir')
55
56
        print >> open('file', 'w'), "content"
56
 
        os.symlink('target', 'symlink')
 
57
        if has_symlinks():
 
58
            os.symlink('target', 'symlink')
57
59
        tree = branch.working_tree()
58
60
        files = list(tree.list_files())
59
61
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
60
62
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
61
 
        self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
 
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())
62
81
 
63
82
    def test_construct_with_branch(self):
64
 
        branch = Branch.initialize('.')
 
83
        branch = Branch.initialize(u'.')
65
84
        tree = WorkingTree(branch.base, branch)
66
85
        self.assertEqual(branch, tree.branch)
67
 
        self.assertEqual(branch.base, tree.basedir)
 
86
        self.assertEqual(branch.base, tree.basedir + '/')
68
87
    
69
88
    def test_construct_without_branch(self):
70
 
        branch = Branch.initialize('.')
 
89
        branch = Branch.initialize(u'.')
71
90
        tree = WorkingTree(branch.base)
72
91
        self.assertEqual(branch.base, tree.branch.base)
73
 
        self.assertEqual(branch.base, tree.basedir)
 
92
        self.assertEqual(branch.base, tree.basedir + '/')
74
93
 
75
94
    def test_basic_relpath(self):
76
95
        # for comprehensive relpath tests, see whitebox.py.
77
 
        branch = Branch.initialize('.')
 
96
        branch = Branch.initialize(u'.')
78
97
        tree = WorkingTree(branch.base)
79
98
        self.assertEqual('child',
80
 
                         tree.relpath(os.path.join(os.getcwd(), 'child')))
 
99
                         tree.relpath(pathjoin(getcwd(), 'child')))
81
100
 
82
101
    def test_lock_locks_branch(self):
83
 
        branch = Branch.initialize('.')
 
102
        branch = Branch.initialize(u'.')
84
103
        tree = WorkingTree(branch.base)
85
104
        tree.lock_read()
86
105
        self.assertEqual(1, tree.branch._lock_count)
96
115
    def get_pullable_branches(self):
97
116
        self.build_tree(['from/', 'from/file', 'to/'])
98
117
        br_a = Branch.initialize('from')
99
 
        br_a.add('file')
100
 
        br_a.working_tree().commit('foo', rev_id='A')
 
118
        tree = br_a.working_tree()
 
119
        tree.add('file')
 
120
        tree.commit('foo', rev_id='A')
101
121
        br_b = Branch.initialize('to')
102
122
        return br_a, br_b
103
123
 
118
138
 
119
139
    def test_revert(self):
120
140
        """Test selected-file revert"""
121
 
        b = Branch.initialize('.')
 
141
        b = Branch.initialize(u'.')
122
142
 
123
143
        self.build_tree(['hello.txt'])
124
144
        file('hello.txt', 'w').write('initial hello')
125
145
 
126
146
        self.assertRaises(NotVersionedError,
127
147
                          b.working_tree().revert, ['hello.txt'])
128
 
        
129
 
        b.add(['hello.txt'])
130
 
        b.working_tree().commit('create initial hello.txt')
 
148
        tree = WorkingTree(b.base, b)
 
149
        tree.add(['hello.txt'])
 
150
        tree.commit('create initial hello.txt')
131
151
 
132
152
        self.check_file_contents('hello.txt', 'initial hello')
133
153
        file('hello.txt', 'w').write('new hello')
134
154
        self.check_file_contents('hello.txt', 'new hello')
135
155
 
136
 
        wt = b.working_tree()
137
 
 
138
156
        # revert file modified since last revision
139
 
        wt.revert(['hello.txt'])
 
157
        tree.revert(['hello.txt'])
140
158
        self.check_file_contents('hello.txt', 'initial hello')
141
159
        self.check_file_contents('hello.txt~', 'new hello')
142
160
 
143
161
        # reverting again does not clobber the backup
144
 
        wt.revert(['hello.txt'])
 
162
        tree.revert(['hello.txt'])
145
163
        self.check_file_contents('hello.txt', 'initial hello')
146
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)