~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/whitebox.py

  • Committer: Martin Pool
  • Date: 2005-06-22 06:37:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050622063743-e395f04c4db8977f
- move old blackbox code from testbzr into bzrlib.selftest.blackbox

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
import unittest
3
 
 
4
 
from bzrlib.tests import TestCaseInTempDir, TestCase
5
 
from bzrlib.branch import ScratchBranch, Branch
6
 
from bzrlib.errors import PathNotChild
7
 
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
8
 
 
9
 
 
10
 
class TestBranch(TestCaseInTempDir):
11
 
 
12
 
    def test_no_changes(self):
13
 
        from bzrlib.errors import PointlessCommit
14
 
        
15
 
        b = Branch.initialize(u'.')
16
 
 
17
 
        self.build_tree(['hello.txt'])
18
 
 
19
 
        self.assertRaises(PointlessCommit,
20
 
                          b.working_tree().commit,
21
 
                          'commit without adding',
22
 
                          allow_pointless=False)
23
 
 
24
 
        b.working_tree().commit('commit pointless tree',
25
 
                 allow_pointless=True)
26
 
 
27
 
        b.working_tree().add('hello.txt')
28
 
        
29
 
        b.working_tree().commit('commit first added file',
30
 
                 allow_pointless=False)
31
 
        
32
 
        self.assertRaises(PointlessCommit,
33
 
                          b.working_tree().commit,
34
 
                          'commit after adding file',
35
 
                          allow_pointless=False)
36
 
        
37
 
        b.working_tree().commit('commit pointless revision with one file',
38
 
                 allow_pointless=True)
39
 
 
40
 
 
41
 
class MoreTests(TestCaseInTempDir):
42
 
 
43
 
    def test_rename_dirs(self):
44
 
        """Test renaming directories and the files within them."""
45
 
        b = Branch.initialize(u'.')
46
 
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
47
 
        b.working_tree().add(['dir', 'dir/sub', 'dir/sub/file'])
48
 
 
49
 
        b.working_tree().commit('create initial state')
50
 
 
51
 
        # TODO: lift out to a test helper that checks the shape of
52
 
        # an inventory
53
 
        
54
 
        revid = b.revision_history()[0]
55
 
        self.log('first revision_id is {%s}' % revid)
56
 
        
57
 
        inv = b.get_revision_inventory(revid)
58
 
        self.log('contents of inventory: %r' % inv.entries())
59
 
 
60
 
        self.check_inventory_shape(inv,
61
 
                                   ['dir', 'dir/sub', 'dir/sub/file'])
62
 
 
63
 
        b.working_tree().rename_one('dir', 'newdir')
64
 
 
65
 
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
66
 
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
67
 
 
68
 
        b.working_tree().rename_one('newdir/sub', 'newdir/newsub')
69
 
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
70
 
                                   ['newdir', 'newdir/newsub',
71
 
                                    'newdir/newsub/file'])
72
 
 
73
 
    def test_relpath(self):
74
 
        """test for branch path lookups
 
1
#! /usr/bin/python
 
2
 
 
3
from bzrlib.branch import ScratchBranch
 
4
from bzrlib.errors import NotBranchError
 
5
from unittest import TestCase
 
6
import os, unittest
 
7
 
 
8
class BranchPathTestCase(TestCase):
 
9
    """test for branch path lookups
 
10
 
 
11
    Branch.relpath and bzrlib.branch._relpath do a simple but subtle
 
12
    job: given a path (either relative to cwd or absolute), work out
 
13
    if it is inside a branch and return the path relative to the base.
 
14
    """
75
15
    
76
 
        bzrlib.osutils._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
 
        """
 
16
    def runTest(self):
 
17
        from bzrlib.branch import _relpath
80
18
        import tempfile, shutil
81
19
        
82
20
        savedir = os.getcwdu()
83
21
        dtmp = tempfile.mkdtemp()
84
 
        # On Mac OSX, /tmp actually expands to /private/tmp
85
 
        dtmp = realpath(dtmp)
86
22
 
87
23
        def rp(p):
88
 
            return relpath(dtmp, p)
 
24
            return _relpath(dtmp, p)
89
25
        
90
26
        try:
91
27
            # check paths inside dtmp while standing outside it
92
 
            self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo')
 
28
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
93
29
 
94
30
            # root = nothing
95
31
            self.assertEqual(rp(dtmp), '')
96
32
 
97
 
            self.assertRaises(PathNotChild,
 
33
            self.assertRaises(NotBranchError,
98
34
                              rp,
99
35
                              '/etc')
100
36
 
101
37
            # now some near-miss operations -- note that
102
38
            # os.path.commonprefix gets these wrong!
103
 
            self.assertRaises(PathNotChild,
 
39
            self.assertRaises(NotBranchError,
104
40
                              rp,
105
41
                              dtmp.rstrip('\\/') + '2')
106
42
 
107
 
            self.assertRaises(PathNotChild,
 
43
            self.assertRaises(NotBranchError,
108
44
                              rp,
109
45
                              dtmp.rstrip('\\/') + '2/foo')
110
46
 
118
54
 
119
55
            self.assertEqual(rp('./foo'), 'foo')
120
56
 
121
 
            self.assertEqual(rp(abspath('foo')), 'foo')
 
57
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
122
58
 
123
 
            self.assertRaises(PathNotChild,
 
59
            self.assertRaises(NotBranchError,
124
60
                              rp, '../foo')
125
61
 
126
62
        finally:
127
63
            os.chdir(savedir)
128
64
            shutil.rmtree(dtmp)
 
65
 
 
66
                              
 
67
if __name__ == '__main__':
 
68
    unittest.main()
 
69