~bzr-pqm/bzr/bzr.dev

601 by Martin Pool
- whitebox tests for branch path handling
1
#! /usr/bin/python
2
768 by Martin Pool
- start some tests for directory renames
3
import os
4
import unittest
5
6
from bzrlib.selftest import InTempDir, TestBase
7
from bzrlib.branch import ScratchBranch, Branch
778 by Martin Pool
- simple revert of text files
8
from bzrlib.errors import NotBranchError, NotVersionedError
9
10
781 by Martin Pool
- start of simple test for unknown-file reporting
11
class Unknowns(InTempDir):
12
    def runTest(self):
13
        b = Branch('.', init=True)
14
15
        self.build_tree(['hello.txt',
16
                         'hello.txt~'])
17
18
        self.assertEquals(list(b.unknowns()),
19
                          ['hello.txt'])
20
21
811 by Martin Pool
- Test case for validate_revision_id
22
23
class ValidateRevisionId(TestBase):
24
    def runTest(self):
25
        from bzrlib.revision import validate_revision_id
26
        validate_revision_id('mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
27
        
28
        self.assertRaises(ValueError,
29
                          validate_revision_id,
30
                          ' asdkjas')
31
32
33
        self.assertRaises(ValueError,
34
                          validate_revision_id,
35
                          'mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe\n')
36
37
38
        self.assertRaises(ValueError,
39
                          validate_revision_id,
40
                          ' mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
41
42
        self.assertRaises(ValueError,
43
                          validate_revision_id,
44
                          'Martin Pool <mbp@sourcefrog.net>-20050311061123-96a255005c7c9dbe')
45
46
47
778 by Martin Pool
- simple revert of text files
48
class Revert(InTempDir):
49
    """Test selected-file revert"""
50
    def runTest(self):
51
        b = Branch('.', init=True)
52
53
        self.build_tree(['hello.txt'])
54
        file('hello.txt', 'w').write('initial hello')
55
56
        self.assertRaises(NotVersionedError,
57
                          b.revert, ['hello.txt'])
58
        
59
        b.add(['hello.txt'])
60
        b.commit('create initial hello.txt')
61
62
        self.check_file_contents('hello.txt', 'initial hello')
63
        file('hello.txt', 'w').write('new hello')
64
        self.check_file_contents('hello.txt', 'new hello')
65
66
        # revert file modified since last revision
67
        b.revert(['hello.txt'])
68
        self.check_file_contents('hello.txt', 'initial hello')
782 by Martin Pool
- Branch.revert copies files to backups before reverting them
69
        self.check_file_contents('hello.txt~', 'new hello')
778 by Martin Pool
- simple revert of text files
70
782 by Martin Pool
- Branch.revert copies files to backups before reverting them
71
        # reverting again clobbers the backup
778 by Martin Pool
- simple revert of text files
72
        b.revert(['hello.txt'])
73
        self.check_file_contents('hello.txt', 'initial hello')
782 by Martin Pool
- Branch.revert copies files to backups before reverting them
74
        self.check_file_contents('hello.txt~', 'initial hello')
778 by Martin Pool
- simple revert of text files
75
768 by Martin Pool
- start some tests for directory renames
76
77
78
class RenameDirs(InTempDir):
79
    """Test renaming directories and the files within them."""
80
    def runTest(self):
81
        b = Branch('.', init=True)
82
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
83
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
84
85
        b.commit('create initial state')
86
87
        # TODO: lift out to a test helper that checks the shape of
88
        # an inventory
89
        
90
        revid = b.revision_history()[0]
91
        self.log('first revision_id is {%s}' % revid)
92
        
93
        inv = b.get_revision_inventory(revid)
94
        self.log('contents of inventory: %r' % inv.entries())
95
96
        self.check_inventory_shape(inv,
97
                                   ['dir', 'dir/sub', 'dir/sub/file'])
98
771 by Martin Pool
- more tests of directory renames
99
        b.rename_one('dir', 'newdir')
100
101
        self.check_inventory_shape(b.inventory,
102
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
103
104
        b.rename_one('newdir/sub', 'newdir/newsub')
105
        self.check_inventory_shape(b.inventory,
106
                                   ['newdir', 'newdir/newsub',
107
                                    'newdir/newsub/file'])
108
768 by Martin Pool
- start some tests for directory renames
109
        
110
111
112
class BranchPathTestCase(TestBase):
601 by Martin Pool
- whitebox tests for branch path handling
113
    """test for branch path lookups
114
115
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
116
    job: given a path (either relative to cwd or absolute), work out
117
    if it is inside a branch and return the path relative to the base.
118
    """
768 by Martin Pool
- start some tests for directory renames
119
601 by Martin Pool
- whitebox tests for branch path handling
120
    def runTest(self):
121
        from bzrlib.branch import _relpath
122
        import tempfile, shutil
123
        
124
        savedir = os.getcwdu()
125
        dtmp = tempfile.mkdtemp()
126
127
        def rp(p):
128
            return _relpath(dtmp, p)
129
        
130
        try:
131
            # check paths inside dtmp while standing outside it
132
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
133
134
            # root = nothing
135
            self.assertEqual(rp(dtmp), '')
136
137
            self.assertRaises(NotBranchError,
138
                              rp,
139
                              '/etc')
140
141
            # now some near-miss operations -- note that
142
            # os.path.commonprefix gets these wrong!
143
            self.assertRaises(NotBranchError,
144
                              rp,
145
                              dtmp.rstrip('\\/') + '2')
146
147
            self.assertRaises(NotBranchError,
148
                              rp,
149
                              dtmp.rstrip('\\/') + '2/foo')
150
151
            # now operations based on relpath of files in current
152
            # directory, or nearby
153
            os.chdir(dtmp)
154
155
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
156
157
            self.assertEqual(rp('foo'), 'foo')
158
159
            self.assertEqual(rp('./foo'), 'foo')
160
161
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
162
163
            self.assertRaises(NotBranchError,
164
                              rp, '../foo')
165
166
        finally:
167
            os.chdir(savedir)
168
            shutil.rmtree(dtmp)