~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

Don't encode unicode messages to UTF-8 in mutter() (the stream writer does it).

Use a codec wrapped log file in tests to match the real environment.

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