~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_whitebox.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil, Patch Queue Manager, Jelmer Vernooij
  • Date: 2017-01-17 16:20:41 UTC
  • mfrom: (6619.1.2 trunk)
  • Revision ID: tarmac-20170117162041-oo62uk1qsmgc9j31
Merge 2.7 into trunk including fixes for bugs #1622039, #1644003, #1579093 and #1645017. [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2008-2012 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
 
16
 
1
17
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
 
 
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'])
 
18
 
 
19
import bzrlib
 
20
from bzrlib import (
 
21
    errors,
 
22
    osutils,
 
23
    tests,
 
24
    )
 
25
from bzrlib.osutils import relpath, pathjoin, abspath, realpath
 
26
 
 
27
 
 
28
class MoreTests(tests.TestCaseWithTransport):
71
29
 
72
30
    def test_relpath(self):
73
31
        """test for branch path lookups
74
 
    
 
32
 
75
33
        bzrlib.osutils._relpath do a simple but subtle
76
34
        job: given a path (either relative to cwd or absolute), work out
77
35
        if it is inside a branch and return the path relative to the base.
78
36
        """
79
 
        from bzrlib.osutils import relpath
80
 
        import tempfile, shutil
81
 
        
82
 
        savedir = os.getcwdu()
83
 
        dtmp = tempfile.mkdtemp()
 
37
        dtmp = osutils.mkdtemp()
 
38
        self.addCleanup(osutils.rmtree, dtmp)
84
39
        # On Mac OSX, /tmp actually expands to /private/tmp
85
 
        dtmp = os.path.realpath(dtmp)
 
40
        dtmp = realpath(dtmp)
86
41
 
87
42
        def rp(p):
88
43
            return relpath(dtmp, p)
89
 
        
90
 
        try:
91
 
            # check paths inside dtmp while standing outside it
92
 
            self.assertEqual(rp(os.path.join(dtmp, 'foo')), 'foo')
93
 
 
94
 
            # root = nothing
95
 
            self.assertEqual(rp(dtmp), '')
96
 
 
97
 
            self.assertRaises(PathNotChild,
98
 
                              rp,
99
 
                              '/etc')
100
 
 
101
 
            # now some near-miss operations -- note that
102
 
            # os.path.commonprefix gets these wrong!
103
 
            self.assertRaises(PathNotChild,
104
 
                              rp,
105
 
                              dtmp.rstrip('\\/') + '2')
106
 
 
107
 
            self.assertRaises(PathNotChild,
108
 
                              rp,
109
 
                              dtmp.rstrip('\\/') + '2/foo')
110
 
 
111
 
            # now operations based on relpath of files in current
112
 
            # directory, or nearby
113
 
            os.chdir(dtmp)
114
 
 
115
 
            FOO_BAR_QUUX = os.path.join('foo', 'bar', 'quux')
116
 
            self.assertEqual(rp('foo/bar/quux'), FOO_BAR_QUUX)
117
 
 
118
 
            self.assertEqual(rp('foo'), 'foo')
119
 
 
120
 
            self.assertEqual(rp('./foo'), 'foo')
121
 
 
122
 
            self.assertEqual(rp(os.path.abspath('foo')), 'foo')
123
 
 
124
 
            self.assertRaises(PathNotChild,
125
 
                              rp, '../foo')
126
 
 
127
 
        finally:
128
 
            os.chdir(savedir)
129
 
            shutil.rmtree(dtmp)
 
44
 
 
45
        # check paths inside dtmp while standing outside it
 
46
        self.assertEqual('foo', rp(pathjoin(dtmp, 'foo')))
 
47
 
 
48
        # root = nothing
 
49
        self.assertEqual('', rp(dtmp))
 
50
        self.assertRaises(errors.PathNotChild, rp, '/etc')
 
51
 
 
52
        # now some near-miss operations -- note that
 
53
        # os.path.commonprefix gets these wrong!
 
54
        self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2')
 
55
        self.assertRaises(errors.PathNotChild, rp, dtmp.rstrip('\\/') + '2/foo')
 
56
 
 
57
        # now operations based on relpath of files in current
 
58
        # directory, or nearby
 
59
 
 
60
        os.chdir(dtmp)
 
61
        self.assertEqual('foo/bar/quux', rp('foo/bar/quux'))
 
62
        self.assertEqual('foo', rp('foo'))
 
63
        self.assertEqual('foo', rp('./foo'))
 
64
        self.assertEqual('foo', rp(abspath('foo')))
 
65
        self.assertRaises(errors.PathNotChild, rp, '../foo')