~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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
1
#! /usr/bin/python
16
2
 
17
3
import os
18
4
import unittest
19
5
 
20
 
from bzrlib import (
21
 
    osutils,
22
 
    )
23
 
from bzrlib.tests import TestCaseWithTransport, TestCase
24
 
from bzrlib.branch import Branch
25
 
from bzrlib.errors import PathNotChild
26
 
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
27
 
 
28
 
 
29
 
class MoreTests(TestCaseWithTransport):
30
 
 
31
 
    def test_relpath(self):
32
 
        """test for branch path lookups
33
 
 
34
 
        bzrlib.osutils._relpath do a simple but subtle
35
 
        job: given a path (either relative to cwd or absolute), work out
36
 
        if it is inside a branch and return the path relative to the base.
37
 
        """
38
 
        import tempfile
39
 
 
 
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
 
123
        
40
124
        savedir = os.getcwdu()
41
 
        dtmp = osutils.mkdtemp()
42
 
        # On Mac OSX, /tmp actually expands to /private/tmp
43
 
        dtmp = realpath(dtmp)
 
125
        dtmp = tempfile.mkdtemp()
44
126
 
45
127
        def rp(p):
46
 
            return relpath(dtmp, p)
47
 
 
 
128
            return _relpath(dtmp, p)
 
129
        
48
130
        try:
49
131
            # check paths inside dtmp while standing outside it
50
 
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
132
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
51
133
 
52
134
            # root = nothing
53
135
            self.assertEqual(rp(dtmp), '')
54
136
 
55
 
            self.assertRaises(PathNotChild,
 
137
            self.assertRaises(NotBranchError,
56
138
                              rp,
57
139
                              '/etc')
58
140
 
59
141
            # now some near-miss operations -- note that
60
142
            # os.path.commonprefix gets these wrong!
61
 
            self.assertRaises(PathNotChild,
 
143
            self.assertRaises(NotBranchError,
62
144
                              rp,
63
145
                              dtmp.rstrip('\\/') + '2')
64
146
 
65
 
            self.assertRaises(PathNotChild,
 
147
            self.assertRaises(NotBranchError,
66
148
                              rp,
67
149
                              dtmp.rstrip('\\/') + '2/foo')
68
150
 
76
158
 
77
159
            self.assertEqual(rp('./foo'), 'foo')
78
160
 
79
 
            self.assertEqual(rp(abspath('foo')), 'foo')
 
161
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
80
162
 
81
 
            self.assertRaises(PathNotChild,
 
163
            self.assertRaises(NotBranchError,
82
164
                              rp, '../foo')
83
165
 
84
166
        finally:
85
167
            os.chdir(savedir)
86
 
            osutils.rmtree(dtmp)
 
168
            shutil.rmtree(dtmp)