6
 
from bzrlib.selftest import InTempDir, TestBase
 
7
 
from bzrlib.branch import ScratchBranch, Branch
 
8
 
from bzrlib.errors import NotBranchError, NotVersionedError
 
11
 
class Unknowns(InTempDir):
 
13
 
        b = Branch('.', init=True)
 
15
 
        self.build_tree(['hello.txt',
 
18
 
        self.assertEquals(list(b.unknowns()),
 
23
 
class NoChanges(InTempDir):
 
25
 
        from bzrlib.errors import EmptyCommit
 
27
 
        b = Branch('.', init=True)
 
29
 
        self.build_tree(['hello.txt'])
 
31
 
        self.assertRaises(EmptyCommit,
 
33
 
                          'commit without adding',
 
39
 
class ValidateRevisionId(TestBase):
 
41
 
        from bzrlib.revision import validate_revision_id
 
42
 
        validate_revision_id('mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
 
44
 
        self.assertRaises(ValueError,
 
49
 
        self.assertRaises(ValueError,
 
51
 
                          'mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe\n')
 
54
 
        self.assertRaises(ValueError,
 
56
 
                          ' mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
 
58
 
        self.assertRaises(ValueError,
 
60
 
                          'Martin Pool <mbp@sourcefrog.net>-20050311061123-96a255005c7c9dbe')
 
64
 
class PendingMerges(InTempDir):
 
65
 
    """Tracking pending-merged revisions."""
 
67
 
        b = Branch('.', init=True)
 
69
 
        self.assertEquals(b.pending_merges(), [])
 
71
 
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
73
 
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
75
 
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
77
 
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
79
 
        b.add_pending_merge('wibble@fofof--20050401--1928390812')
 
80
 
        self.assertEquals(b.pending_merges(),
 
81
 
                          ['foo@azkhazan-123123-abcabc',
 
82
 
                           'wibble@fofof--20050401--1928390812'])
 
84
 
        b.commit("commit from base with two merges")
 
86
 
        rev = b.get_revision(b.revision_history()[0])
 
87
 
        self.assertEquals(len(rev.parents), 2)
 
88
 
        self.assertEquals(rev.parents[0].revision_id,
 
89
 
                          'foo@azkhazan-123123-abcabc')
 
90
 
        self.assertEquals(rev.parents[1].revision_id,
 
91
 
                           'wibble@fofof--20050401--1928390812')
 
93
 
        # list should be cleared when we do a commit
 
94
 
        self.assertEquals(b.pending_merges(), [])
 
99
 
class Revert(InTempDir):
 
100
 
    """Test selected-file revert"""
 
102
 
        b = Branch('.', init=True)
 
104
 
        self.build_tree(['hello.txt'])
 
105
 
        file('hello.txt', 'w').write('initial hello')
 
107
 
        self.assertRaises(NotVersionedError,
 
108
 
                          b.revert, ['hello.txt'])
 
111
 
        b.commit('create initial hello.txt')
 
113
 
        self.check_file_contents('hello.txt', 'initial hello')
 
114
 
        file('hello.txt', 'w').write('new hello')
 
115
 
        self.check_file_contents('hello.txt', 'new hello')
 
117
 
        # revert file modified since last revision
 
118
 
        b.revert(['hello.txt'])
 
119
 
        self.check_file_contents('hello.txt', 'initial hello')
 
120
 
        self.check_file_contents('hello.txt~', 'new hello')
 
122
 
        # reverting again clobbers the backup
 
123
 
        b.revert(['hello.txt'])
 
124
 
        self.check_file_contents('hello.txt', 'initial hello')
 
125
 
        self.check_file_contents('hello.txt~', 'initial hello')
 
129
 
class RenameDirs(InTempDir):
 
130
 
    """Test renaming directories and the files within them."""
 
132
 
        b = Branch('.', init=True)
 
133
 
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
134
 
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
136
 
        b.commit('create initial state')
 
138
 
        # TODO: lift out to a test helper that checks the shape of
 
141
 
        revid = b.revision_history()[0]
 
142
 
        self.log('first revision_id is {%s}' % revid)
 
144
 
        inv = b.get_revision_inventory(revid)
 
145
 
        self.log('contents of inventory: %r' % inv.entries())
 
147
 
        self.check_inventory_shape(inv,
 
148
 
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
150
 
        b.rename_one('dir', 'newdir')
 
152
 
        self.check_inventory_shape(b.inventory,
 
153
 
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
155
 
        b.rename_one('newdir/sub', 'newdir/newsub')
 
156
 
        self.check_inventory_shape(b.inventory,
 
157
 
                                   ['newdir', 'newdir/newsub',
 
158
 
                                    'newdir/newsub/file'])
 
163
 
class BranchPathTestCase(TestBase):
 
164
 
    """test for branch path lookups
 
166
 
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
167
 
    job: given a path (either relative to cwd or absolute), work out
 
168
 
    if it is inside a branch and return the path relative to the base.
 
172
 
        from bzrlib.branch import _relpath
 
173
 
        import tempfile, shutil
 
175
 
        savedir = os.getcwdu()
 
176
 
        dtmp = tempfile.mkdtemp()
 
179
 
            return _relpath(dtmp, p)
 
182
 
            # check paths inside dtmp while standing outside it
 
183
 
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
186
 
            self.assertEqual(rp(dtmp), '')
 
188
 
            self.assertRaises(NotBranchError,
 
192
 
            # now some near-miss operations -- note that
 
193
 
            # os.path.commonprefix gets these wrong!
 
194
 
            self.assertRaises(NotBranchError,
 
196
 
                              dtmp.rstrip('\\/') + '2')
 
198
 
            self.assertRaises(NotBranchError,
 
200
 
                              dtmp.rstrip('\\/') + '2/foo')
 
202
 
            # now operations based on relpath of files in current
 
203
 
            # directory, or nearby
 
206
 
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
208
 
            self.assertEqual(rp('foo'), 'foo')
 
210
 
            self.assertEqual(rp('./foo'), 'foo')
 
212
 
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
214
 
            self.assertRaises(NotBranchError,
 
224
 
TEST_CLASSES = [Unknowns,