/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 11:04:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050624110455-b0e54cd5f96691e5
- better quotefn for windows: use doublequotes for strings with 
  strange characters, not backslashes
- new backup_file() routine

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 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
        
40
85
        savedir = os.getcwdu()
41
 
        dtmp = osutils.mkdtemp()
42
 
        # On Mac OSX, /tmp actually expands to /private/tmp
43
 
        dtmp = realpath(dtmp)
 
86
        dtmp = tempfile.mkdtemp()
44
87
 
45
88
        def rp(p):
46
 
            return relpath(dtmp, p)
47
 
 
 
89
            return _relpath(dtmp, p)
 
90
        
48
91
        try:
49
92
            # check paths inside dtmp while standing outside it
50
 
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
93
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
51
94
 
52
95
            # root = nothing
53
96
            self.assertEqual(rp(dtmp), '')
54
97
 
55
 
            self.assertRaises(PathNotChild,
 
98
            self.assertRaises(NotBranchError,
56
99
                              rp,
57
100
                              '/etc')
58
101
 
59
102
            # now some near-miss operations -- note that
60
103
            # os.path.commonprefix gets these wrong!
61
 
            self.assertRaises(PathNotChild,
 
104
            self.assertRaises(NotBranchError,
62
105
                              rp,
63
106
                              dtmp.rstrip('\\/') + '2')
64
107
 
65
 
            self.assertRaises(PathNotChild,
 
108
            self.assertRaises(NotBranchError,
66
109
                              rp,
67
110
                              dtmp.rstrip('\\/') + '2/foo')
68
111
 
76
119
 
77
120
            self.assertEqual(rp('./foo'), 'foo')
78
121
 
79
 
            self.assertEqual(rp(abspath('foo')), 'foo')
 
122
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
80
123
 
81
 
            self.assertRaises(PathNotChild,
 
124
            self.assertRaises(NotBranchError,
82
125
                              rp, '../foo')
83
126
 
84
127
        finally:
85
128
            os.chdir(savedir)
86
 
            osutils.rmtree(dtmp)
 
129
            shutil.rmtree(dtmp)