~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 07:34:10 UTC
  • Revision ID: mbp@sourcefrog.net-20050704073408-faed1ea9d8b586d4
- Test case for validate_revision_id

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
1
#! /usr/bin/python
16
2
 
17
3
import os
18
4
import unittest
19
5
 
20
 
from bzrlib.tests import TestCaseWithTransport, TestCase
21
 
from bzrlib.branch import Branch
22
 
from bzrlib.errors import PathNotChild
23
 
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
24
 
 
25
 
 
26
 
class MoreTests(TestCaseWithTransport):
27
 
 
28
 
    def test_relpath(self):
29
 
        """test for branch path lookups
30
 
    
31
 
        bzrlib.osutils._relpath do a simple but subtle
32
 
        job: given a path (either relative to cwd or absolute), work out
33
 
        if it is inside a branch and return the path relative to the base.
34
 
        """
35
 
        import tempfile
36
 
        from bzrlib.osutils import rmtree
 
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 Revert(InTempDir):
 
49
    """Test selected-file revert"""
 
50
    def runTest(self):
 
51
        b = Branch('.', init=True)
 
52
 
 
53
        self.build_tree(['hello.txt'])
 
54
        file('hello.txt', 'w').write('initial hello')
 
55
 
 
56
        self.assertRaises(NotVersionedError,
 
57
                          b.revert, ['hello.txt'])
 
58
        
 
59
        b.add(['hello.txt'])
 
60
        b.commit('create initial hello.txt')
 
61
 
 
62
        self.check_file_contents('hello.txt', 'initial hello')
 
63
        file('hello.txt', 'w').write('new hello')
 
64
        self.check_file_contents('hello.txt', 'new hello')
 
65
 
 
66
        # revert file modified since last revision
 
67
        b.revert(['hello.txt'])
 
68
        self.check_file_contents('hello.txt', 'initial hello')
 
69
        self.check_file_contents('hello.txt~', 'new hello')
 
70
 
 
71
        # reverting again clobbers the backup
 
72
        b.revert(['hello.txt'])
 
73
        self.check_file_contents('hello.txt', 'initial hello')
 
74
        self.check_file_contents('hello.txt~', 'initial hello')
 
75
 
 
76
 
 
77
 
 
78
class RenameDirs(InTempDir):
 
79
    """Test renaming directories and the files within them."""
 
80
    def runTest(self):
 
81
        b = Branch('.', init=True)
 
82
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
83
        b.add(['dir', 'dir/sub', 'dir/sub/file'])
 
84
 
 
85
        b.commit('create initial state')
 
86
 
 
87
        # TODO: lift out to a test helper that checks the shape of
 
88
        # an inventory
 
89
        
 
90
        revid = b.revision_history()[0]
 
91
        self.log('first revision_id is {%s}' % revid)
 
92
        
 
93
        inv = b.get_revision_inventory(revid)
 
94
        self.log('contents of inventory: %r' % inv.entries())
 
95
 
 
96
        self.check_inventory_shape(inv,
 
97
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
98
 
 
99
        b.rename_one('dir', 'newdir')
 
100
 
 
101
        self.check_inventory_shape(b.inventory,
 
102
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
103
 
 
104
        b.rename_one('newdir/sub', 'newdir/newsub')
 
105
        self.check_inventory_shape(b.inventory,
 
106
                                   ['newdir', 'newdir/newsub',
 
107
                                    'newdir/newsub/file'])
 
108
 
 
109
        
 
110
 
 
111
 
 
112
class BranchPathTestCase(TestBase):
 
113
    """test for branch path lookups
 
114
 
 
115
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
116
    job: given a path (either relative to cwd or absolute), work out
 
117
    if it is inside a branch and return the path relative to the base.
 
118
    """
 
119
 
 
120
    def runTest(self):
 
121
        from bzrlib.branch import _relpath
 
122
        import tempfile, shutil
37
123
        
38
124
        savedir = os.getcwdu()
39
125
        dtmp = tempfile.mkdtemp()
40
 
        # On Mac OSX, /tmp actually expands to /private/tmp
41
 
        dtmp = realpath(dtmp)
42
126
 
43
127
        def rp(p):
44
 
            return relpath(dtmp, p)
 
128
            return _relpath(dtmp, p)
45
129
        
46
130
        try:
47
131
            # check paths inside dtmp while standing outside it
48
 
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
132
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
49
133
 
50
134
            # root = nothing
51
135
            self.assertEqual(rp(dtmp), '')
52
136
 
53
 
            self.assertRaises(PathNotChild,
 
137
            self.assertRaises(NotBranchError,
54
138
                              rp,
55
139
                              '/etc')
56
140
 
57
141
            # now some near-miss operations -- note that
58
142
            # os.path.commonprefix gets these wrong!
59
 
            self.assertRaises(PathNotChild,
 
143
            self.assertRaises(NotBranchError,
60
144
                              rp,
61
145
                              dtmp.rstrip('\\/') + '2')
62
146
 
63
 
            self.assertRaises(PathNotChild,
 
147
            self.assertRaises(NotBranchError,
64
148
                              rp,
65
149
                              dtmp.rstrip('\\/') + '2/foo')
66
150
 
74
158
 
75
159
            self.assertEqual(rp('./foo'), 'foo')
76
160
 
77
 
            self.assertEqual(rp(abspath('foo')), 'foo')
 
161
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
78
162
 
79
 
            self.assertRaises(PathNotChild,
 
163
            self.assertRaises(NotBranchError,
80
164
                              rp, '../foo')
81
165
 
82
166
        finally:
83
167
            os.chdir(savedir)
84
 
            rmtree(dtmp)
 
168
            shutil.rmtree(dtmp)