~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/whitebox.py

  • Committer: Martin Pool
  • Date: 2005-07-04 08:06:51 UTC
  • Revision ID: mbp@sourcefrog.net-20050704080651-6ecec49164359e48
- track pending-merges

- unit tests for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
import os
 
4
import unittest
 
5
 
 
6
from bzrlib.selftest import InTempDir, TestBase
 
7
from bzrlib.branch import ScratchBranch, Branch
 
8
from bzrlib.errors import NotBranchError, NotVersionedError
 
9
 
 
10
 
 
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
 
 
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
 
 
48
class PendingMerges(InTempDir):
 
49
    """Tracking pending-merged revisions."""
 
50
    def runTest(self):
 
51
        b = Branch('.', init=True)
 
52
 
 
53
        self.assertEquals(b.pending_merges(), [])
 
54
        
 
55
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
56
        
 
57
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
58
    
 
59
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
60
        
 
61
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
62
 
 
63
        b.add_pending_merge('wibble@fofof--20050401--1928390812')
 
64
        self.assertEquals(b.pending_merges(),
 
65
                          ['foo@azkhazan-123123-abcabc',
 
66
                           'wibble@fofof--20050401--1928390812'])
 
67
        
 
68
 
 
69
class Revert(InTempDir):
 
70
    """Test selected-file revert"""
 
71
    def runTest(self):
 
72
        b = Branch('.', init=True)
 
73
 
 
74
        self.build_tree(['hello.txt'])
 
75
        file('hello.txt', 'w').write('initial hello')
 
76
 
 
77
        self.assertRaises(NotVersionedError,
 
78
                          b.revert, ['hello.txt'])
 
79
        
 
80
        b.add(['hello.txt'])
 
81
        b.commit('create initial hello.txt')
 
82
 
 
83
        self.check_file_contents('hello.txt', 'initial hello')
 
84
        file('hello.txt', 'w').write('new hello')
 
85
        self.check_file_contents('hello.txt', 'new hello')
 
86
 
 
87
        # revert file modified since last revision
 
88
        b.revert(['hello.txt'])
 
89
        self.check_file_contents('hello.txt', 'initial hello')
 
90
        self.check_file_contents('hello.txt~', 'new hello')
 
91
 
 
92
        # reverting again clobbers the backup
 
93
        b.revert(['hello.txt'])
 
94
        self.check_file_contents('hello.txt', 'initial hello')
 
95
        self.check_file_contents('hello.txt~', 'initial hello')
 
96
 
 
97
 
 
98
 
 
99
class RenameDirs(InTempDir):
 
100
    """Test renaming directories and the files within them."""
 
101
    def runTest(self):
 
102
        b = Branch('.', init=True)
 
103
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
104
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
105
 
 
106
        b.commit('create initial state')
 
107
 
 
108
        # TODO: lift out to a test helper that checks the shape of
 
109
        # an inventory
 
110
        
 
111
        revid = b.revision_history()[0]
 
112
        self.log('first revision_id is {%s}' % revid)
 
113
        
 
114
        inv = b.get_revision_inventory(revid)
 
115
        self.log('contents of inventory: %r' % inv.entries())
 
116
 
 
117
        self.check_inventory_shape(inv,
 
118
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
119
 
 
120
        b.rename_one('dir', 'newdir')
 
121
 
 
122
        self.check_inventory_shape(b.inventory,
 
123
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
124
 
 
125
        b.rename_one('newdir/sub', 'newdir/newsub')
 
126
        self.check_inventory_shape(b.inventory,
 
127
                                   ['newdir', 'newdir/newsub',
 
128
                                    'newdir/newsub/file'])
 
129
 
 
130
        
 
131
 
 
132
 
 
133
class BranchPathTestCase(TestBase):
 
134
    """test for branch path lookups
 
135
 
 
136
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
137
    job: given a path (either relative to cwd or absolute), work out
 
138
    if it is inside a branch and return the path relative to the base.
 
139
    """
 
140
 
 
141
    def runTest(self):
 
142
        from bzrlib.branch import _relpath
 
143
        import tempfile, shutil
 
144
        
 
145
        savedir = os.getcwdu()
 
146
        dtmp = tempfile.mkdtemp()
 
147
 
 
148
        def rp(p):
 
149
            return _relpath(dtmp, p)
 
150
        
 
151
        try:
 
152
            # check paths inside dtmp while standing outside it
 
153
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
154
 
 
155
            # root = nothing
 
156
            self.assertEqual(rp(dtmp), '')
 
157
 
 
158
            self.assertRaises(NotBranchError,
 
159
                              rp,
 
160
                              '/etc')
 
161
 
 
162
            # now some near-miss operations -- note that
 
163
            # os.path.commonprefix gets these wrong!
 
164
            self.assertRaises(NotBranchError,
 
165
                              rp,
 
166
                              dtmp.rstrip('\\/') + '2')
 
167
 
 
168
            self.assertRaises(NotBranchError,
 
169
                              rp,
 
170
                              dtmp.rstrip('\\/') + '2/foo')
 
171
 
 
172
            # now operations based on relpath of files in current
 
173
            # directory, or nearby
 
174
            os.chdir(dtmp)
 
175
 
 
176
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
177
 
 
178
            self.assertEqual(rp('foo'), 'foo')
 
179
 
 
180
            self.assertEqual(rp('./foo'), 'foo')
 
181
 
 
182
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
183
 
 
184
            self.assertRaises(NotBranchError,
 
185
                              rp, '../foo')
 
186
 
 
187
        finally:
 
188
            os.chdir(savedir)
 
189
            shutil.rmtree(dtmp)