6
from bzrlib.selftest import InTempDir, TestBase
 
 
7
from bzrlib.branch import ScratchBranch, Branch
 
 
8
from bzrlib.errors import NotBranchError, NotVersionedError
 
 
11
class Revert(InTempDir):
 
 
12
    """Test selected-file revert"""
 
 
14
        b = Branch('.', init=True)
 
 
16
        self.build_tree(['hello.txt'])
 
 
17
        file('hello.txt', 'w').write('initial hello')
 
 
19
        self.assertRaises(NotVersionedError,
 
 
20
                          b.revert, ['hello.txt'])
 
 
23
        b.commit('create initial hello.txt')
 
 
25
        self.check_file_contents('hello.txt', 'initial hello')
 
 
26
        file('hello.txt', 'w').write('new hello')
 
 
27
        self.check_file_contents('hello.txt', 'new hello')
 
 
29
        # revert file modified since last revision
 
 
30
        b.revert(['hello.txt'])
 
 
31
        self.check_file_contents('hello.txt', 'initial hello')
 
 
33
        # reverting again causes no change
 
 
34
        b.revert(['hello.txt'])
 
 
35
        self.check_file_contents('hello.txt', 'initial hello')
 
 
39
class RenameDirs(InTempDir):
 
 
40
    """Test renaming directories and the files within them."""
 
 
42
        b = Branch('.', init=True)
 
 
43
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
 
44
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
 
46
        b.commit('create initial state')
 
 
48
        # TODO: lift out to a test helper that checks the shape of
 
 
51
        revid = b.revision_history()[0]
 
 
52
        self.log('first revision_id is {%s}' % revid)
 
 
54
        inv = b.get_revision_inventory(revid)
 
 
55
        self.log('contents of inventory: %r' % inv.entries())
 
 
57
        self.check_inventory_shape(inv,
 
 
58
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
 
60
        b.rename_one('dir', 'newdir')
 
 
62
        self.check_inventory_shape(b.inventory,
 
 
63
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
 
65
        b.rename_one('newdir/sub', 'newdir/newsub')
 
 
66
        self.check_inventory_shape(b.inventory,
 
 
67
                                   ['newdir', 'newdir/newsub',
 
 
68
                                    'newdir/newsub/file'])
 
 
73
class BranchPathTestCase(TestBase):
 
 
74
    """test for branch path lookups
 
 
76
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
 
77
    job: given a path (either relative to cwd or absolute), work out
 
 
78
    if it is inside a branch and return the path relative to the base.
 
 
82
        from bzrlib.branch import _relpath
 
 
83
        import tempfile, shutil
 
 
85
        savedir = os.getcwdu()
 
 
86
        dtmp = tempfile.mkdtemp()
 
 
89
            return _relpath(dtmp, p)
 
 
92
            # check paths inside dtmp while standing outside it
 
 
93
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
 
96
            self.assertEqual(rp(dtmp), '')
 
 
98
            self.assertRaises(NotBranchError,
 
 
102
            # now some near-miss operations -- note that
 
 
103
            # os.path.commonprefix gets these wrong!
 
 
104
            self.assertRaises(NotBranchError,
 
 
106
                              dtmp.rstrip('\\/') + '2')
 
 
108
            self.assertRaises(NotBranchError,
 
 
110
                              dtmp.rstrip('\\/') + '2/foo')
 
 
112
            # now operations based on relpath of files in current
 
 
113
            # directory, or nearby
 
 
116
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
 
118
            self.assertEqual(rp('foo'), 'foo')
 
 
120
            self.assertEqual(rp('./foo'), 'foo')
 
 
122
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
 
124
            self.assertRaises(NotBranchError,