/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/whitebox.py

  • Committer: Martin Pool
  • Date: 2005-06-24 10:42:47 UTC
  • Revision ID: mbp@sourcefrog.net-20050624104247-9cab76149a672c5b
- simple revert of text files 

- test cases 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 Revert(InTempDir):
 
12
    """Test selected-file revert"""
 
13
    def runTest(self):
 
14
        b = Branch('.', init=True)
 
15
 
 
16
        self.build_tree(['hello.txt'])
 
17
        file('hello.txt', 'w').write('initial hello')
 
18
 
 
19
        self.assertRaises(NotVersionedError,
 
20
                          b.revert, ['hello.txt'])
 
21
        
 
22
        b.add(['hello.txt'])
 
23
        b.commit('create initial hello.txt')
 
24
 
 
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')
 
28
 
 
29
        # revert file modified since last revision
 
30
        b.revert(['hello.txt'])
 
31
        self.check_file_contents('hello.txt', 'initial hello')
 
32
 
 
33
        # reverting again causes no change
 
34
        b.revert(['hello.txt'])
 
35
        self.check_file_contents('hello.txt', 'initial hello')
 
36
 
 
37
 
 
38
 
 
39
class RenameDirs(InTempDir):
 
40
    """Test renaming directories and the files within them."""
 
41
    def runTest(self):
 
42
        b = Branch('.', init=True)
 
43
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
44
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
45
 
 
46
        b.commit('create initial state')
 
47
 
 
48
        # TODO: lift out to a test helper that checks the shape of
 
49
        # an inventory
 
50
        
 
51
        revid = b.revision_history()[0]
 
52
        self.log('first revision_id is {%s}' % revid)
 
53
        
 
54
        inv = b.get_revision_inventory(revid)
 
55
        self.log('contents of inventory: %r' % inv.entries())
 
56
 
 
57
        self.check_inventory_shape(inv,
 
58
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
59
 
 
60
        b.rename_one('dir', 'newdir')
 
61
 
 
62
        self.check_inventory_shape(b.inventory,
 
63
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
64
 
 
65
        b.rename_one('newdir/sub', 'newdir/newsub')
 
66
        self.check_inventory_shape(b.inventory,
 
67
                                   ['newdir', 'newdir/newsub',
 
68
                                    'newdir/newsub/file'])
 
69
 
 
70
        
 
71
 
 
72
 
 
73
class BranchPathTestCase(TestBase):
 
74
    """test for branch path lookups
 
75
 
 
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.
 
79
    """
 
80
 
 
81
    def runTest(self):
 
82
        from bzrlib.branch import _relpath
 
83
        import tempfile, shutil
 
84
        
 
85
        savedir = os.getcwdu()
 
86
        dtmp = tempfile.mkdtemp()
 
87
 
 
88
        def rp(p):
 
89
            return _relpath(dtmp, p)
 
90
        
 
91
        try:
 
92
            # check paths inside dtmp while standing outside it
 
93
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
 
94
 
 
95
            # root = nothing
 
96
            self.assertEqual(rp(dtmp), '')
 
97
 
 
98
            self.assertRaises(NotBranchError,
 
99
                              rp,
 
100
                              '/etc')
 
101
 
 
102
            # now some near-miss operations -- note that
 
103
            # os.path.commonprefix gets these wrong!
 
104
            self.assertRaises(NotBranchError,
 
105
                              rp,
 
106
                              dtmp.rstrip('\\/') + '2')
 
107
 
 
108
            self.assertRaises(NotBranchError,
 
109
                              rp,
 
110
                              dtmp.rstrip('\\/') + '2/foo')
 
111
 
 
112
            # now operations based on relpath of files in current
 
113
            # directory, or nearby
 
114
            os.chdir(dtmp)
 
115
 
 
116
            self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux')
 
117
 
 
118
            self.assertEqual(rp('foo'), 'foo')
 
119
 
 
120
            self.assertEqual(rp('./foo'), 'foo')
 
121
 
 
122
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
 
123
 
 
124
            self.assertRaises(NotBranchError,
 
125
                              rp, '../foo')
 
126
 
 
127
        finally:
 
128
            os.chdir(savedir)
 
129
            shutil.rmtree(dtmp)