~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
1501 by Robert Collins
Move revert from Branch to WorkingTree.
20
from bzrlib.errors import NotVersionedError
1399.1.12 by Robert Collins
add new test script
21
from bzrlib.selftest import TestCaseInTempDir
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):
53
        branch = Branch.initialize('.')
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
63
    def test_construct_with_branch(self):
64
        branch = Branch.initialize('.')
65
        tree = WorkingTree(branch.base, branch)
66
        self.assertEqual(branch, tree.branch)
67
        self.assertEqual(branch.base, tree.basedir)
68
    
69
    def test_construct_without_branch(self):
70
        branch = Branch.initialize('.')
71
        tree = WorkingTree(branch.base)
72
        self.assertEqual(branch.base, tree.branch.base)
73
        self.assertEqual(branch.base, tree.basedir)
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
74
75
    def test_basic_relpath(self):
76
        # for comprehensive relpath tests, see whitebox.py.
77
        branch = Branch.initialize('.')
78
        tree = WorkingTree(branch.base)
79
        self.assertEqual('child',
80
                         tree.relpath(os.path.join(os.getcwd(), 'child')))
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
81
82
    def test_lock_locks_branch(self):
83
        branch = Branch.initialize('.')
84
        tree = WorkingTree(branch.base)
85
        tree.lock_read()
86
        self.assertEqual(1, tree.branch._lock_count)
87
        self.assertEqual('r', tree.branch._lock_mode)
88
        tree.unlock()
89
        self.assertEqual(None, tree.branch._lock_count)
90
        tree.lock_write()
91
        self.assertEqual(1, tree.branch._lock_count)
92
        self.assertEqual('w', tree.branch._lock_mode)
93
        tree.unlock()
94
        self.assertEqual(None, tree.branch._lock_count)
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
95
 
96
    def get_pullable_branches(self):
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
97
        self.build_tree(['from/', 'from/file', 'to/'])
98
        br_a = Branch.initialize('from')
99
        br_a.add('file')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
100
        br_a.working_tree().commit('foo', rev_id='A')
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
101
        br_b = Branch.initialize('to')
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
102
        return br_a, br_b
103
 
104
    def test_pull(self):
105
        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().
106
        br_b.working_tree().pull(br_a)
107
        self.failUnless(br_b.has_revision('A'))
108
        self.assertEqual(['A'], br_b.revision_history())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
109
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
110
    def test_pull_overwrites(self):
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
111
        br_a, br_b = self.get_pullable_branches()
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
112
        br_b.working_tree().commit('foo', rev_id='B')
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
113
        self.assertEqual(['B'], br_b.revision_history())
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
114
        br_b.working_tree().pull(br_a, overwrite=True)
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
115
        self.failUnless(br_b.has_revision('A'))
116
        self.failUnless(br_b.has_revision('B'))
117
        self.assertEqual(['A'], br_b.revision_history())
1501 by Robert Collins
Move revert from Branch to WorkingTree.
118
119
    def test_revert(self):
120
        """Test selected-file revert"""
121
        b = Branch.initialize('.')
122
123
        self.build_tree(['hello.txt'])
124
        file('hello.txt', 'w').write('initial hello')
125
126
        self.assertRaises(NotVersionedError,
127
                          b.working_tree().revert, ['hello.txt'])
128
        
129
        b.add(['hello.txt'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
130
        b.working_tree().commit('create initial hello.txt')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
131
132
        self.check_file_contents('hello.txt', 'initial hello')
133
        file('hello.txt', 'w').write('new hello')
134
        self.check_file_contents('hello.txt', 'new hello')
135
136
        wt = b.working_tree()
137
138
        # revert file modified since last revision
139
        wt.revert(['hello.txt'])
140
        self.check_file_contents('hello.txt', 'initial hello')
141
        self.check_file_contents('hello.txt~', 'new hello')
142
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
143
        # reverting again does not clobber the backup
1501 by Robert Collins
Move revert from Branch to WorkingTree.
144
        wt.revert(['hello.txt'])
145
        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.
146
        self.check_file_contents('hello.txt~', 'new hello')