6
from bzrlib.selftest import InTempDir, TestCase
 
 
7
from bzrlib.branch import ScratchBranch, Branch
 
 
8
from bzrlib.errors import NotBranchError, NotVersionedError
 
 
11
class TestBranch(InTempDir):
 
 
13
    def test_unknowns(self):
 
 
14
        b = Branch('.', init=True)
 
 
16
        self.build_tree(['hello.txt',
 
 
19
        self.assertEquals(list(b.unknowns()),
 
 
22
    def test_no_changes(self):
 
 
23
        from bzrlib.errors import PointlessCommit
 
 
25
        b = Branch('.', init=True)
 
 
27
        self.build_tree(['hello.txt'])
 
 
29
        self.assertRaises(PointlessCommit,
 
 
31
                          'commit without adding',
 
 
32
                          allow_pointless=False)
 
 
34
        b.commit('commit pointless tree',
 
 
39
        b.commit('commit first added file',
 
 
40
                 allow_pointless=False)
 
 
42
        self.assertRaises(PointlessCommit,
 
 
44
                          'commit after adding file',
 
 
45
                          allow_pointless=False)
 
 
47
        b.commit('commit pointless revision with one file',
 
 
50
        b.add_pending_merge('mbp@892739123-2005-123123')
 
 
51
        b.commit('commit new merge with no text changes',
 
 
52
                 allow_pointless=False)
 
 
56
class TestRevisionId(TestCase):
 
 
58
    def test_validate_revision_id(self):
 
 
59
        from bzrlib.revision import validate_revision_id
 
 
60
        validate_revision_id('mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
 
 
61
        self.assertRaises(ValueError,
 
 
64
        self.assertRaises(ValueError,
 
 
66
                          'mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe\n')
 
 
67
        self.assertRaises(ValueError,
 
 
69
                          ' mbp@sourcefrog.net-20050311061123-96a255005c7c9dbe')
 
 
70
        self.assertRaises(ValueError,
 
 
72
                          'Martin Pool <mbp@sourcefrog.net>-20050311061123-96a255005c7c9dbe')
 
 
75
class PendingMerges(InTempDir):
 
 
77
    def test_pending_merges(self):
 
 
78
        """Tracking pending-merged revisions."""
 
 
79
        b = Branch('.', init=True)
 
 
81
        self.assertEquals(b.pending_merges(), [])
 
 
82
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
 
83
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
 
84
        b.add_pending_merge('foo@azkhazan-123123-abcabc')
 
 
85
        self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
 
 
86
        b.add_pending_merge('wibble@fofof--20050401--1928390812')
 
 
87
        self.assertEquals(b.pending_merges(),
 
 
88
                          ['foo@azkhazan-123123-abcabc',
 
 
89
                           'wibble@fofof--20050401--1928390812'])
 
 
90
        b.commit("commit from base with two merges")
 
 
91
        rev = b.get_revision(b.revision_history()[0])
 
 
92
        self.assertEquals(len(rev.parents), 2)
 
 
93
        self.assertEquals(rev.parents[0].revision_id,
 
 
94
                          'foo@azkhazan-123123-abcabc')
 
 
95
        self.assertEquals(rev.parents[1].revision_id,
 
 
96
                           'wibble@fofof--20050401--1928390812')
 
 
97
        # list should be cleared when we do a commit
 
 
98
        self.assertEquals(b.pending_merges(), [])
 
 
100
    def test_revert(self):
 
 
101
        """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')
 
 
127
    def test_rename_dirs(self):
 
 
128
        """Test renaming directories and the files within them."""
 
 
129
        b = Branch('.', init=True)
 
 
130
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
 
131
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
 
133
        b.commit('create initial state')
 
 
135
        # TODO: lift out to a test helper that checks the shape of
 
 
138
        revid = b.revision_history()[0]
 
 
139
        self.log('first revision_id is {%s}' % revid)
 
 
141
        inv = b.get_revision_inventory(revid)
 
 
142
        self.log('contents of inventory: %r' % inv.entries())
 
 
144
        self.check_inventory_shape(inv,
 
 
145
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
 
147
        b.rename_one('dir', 'newdir')
 
 
149
        self.check_inventory_shape(b.inventory,
 
 
150
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
 
152
        b.rename_one('newdir/sub', 'newdir/newsub')
 
 
153
        self.check_inventory_shape(b.inventory,
 
 
154
                                   ['newdir', 'newdir/newsub',
 
 
155
                                    'newdir/newsub/file'])
 
 
157
    def test_relpath(self):
 
 
158
        """test for branch path lookups
 
 
160
        Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
 
161
        job: given a path (either relative to cwd or absolute), work out
 
 
162
        if it is inside a branch and return the path relative to the base.
 
 
164
        from bzrlib.branch import _relpath
 
 
165
        import tempfile, shutil
 
 
167
        savedir = os.getcwdu()
 
 
168
        dtmp = tempfile.mkdtemp()
 
 
169
        # On Mac OSX, /tmp actually expands to /private/tmp
 
 
170
        dtmp = os.path.realpath(dtmp)
 
 
173
            return _relpath(dtmp, p)
 
 
176
            # check paths inside dtmp while standing outside it
 
 
177
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
 
180
            self.assertEqual(rp(dtmp), '')
 
 
182
            self.assertRaises(NotBranchError,
 
 
186
            # now some near-miss operations -- note that
 
 
187
            # os.path.commonprefix gets these wrong!
 
 
188
            self.assertRaises(NotBranchError,
 
 
190
                              dtmp.rstrip('\\/') + '2')
 
 
192
            self.assertRaises(NotBranchError,
 
 
194
                              dtmp.rstrip('\\/') + '2/foo')
 
 
196
            # now operations based on relpath of files in current
 
 
197
            # directory, or nearby
 
 
200
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
 
202
            self.assertEqual(rp('foo'), 'foo')
 
 
204
            self.assertEqual(rp('./foo'), 'foo')
 
 
206
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
 
208
            self.assertRaises(NotBranchError,