/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
1
# Copyright (C) 2010 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
17
"""Test symlink support.
18
"""
19
4634.159.2 by Martin Pool
Add reproduction for bug 192859
20
import os
21
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
22
from bzrlib import (
4634.160.2 by Martin Pool
Add test (already passing) for symlink changing to dir
23
    osutils,
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
24
    tests,
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
25
    workingtree,
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
26
    )
27
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
28
29
30
class TestSmartAddTree(TestCaseWithWorkingTree):
31
4634.159.1 by Martin Pool
Move tests into a class that describes their function
32
    # See eg <https://bugs.launchpad.net/bzr/+bug/192859>
33
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
34
    _test_needs_features = [tests.SymlinkFeature]
35
4634.157.5 by Martin Pool
One more symlink add test
36
    def test_smart_add_symlink(self):
4634.157.4 by Martin Pool
Add a basic (already passing) test for smart_add of a symlink
37
        tree = self.make_branch_and_tree('tree')
38
        self.build_tree_contents([
39
            ('tree/link@', 'target'),
40
            ])
41
        tree.smart_add(['tree/link'])
42
        self.assertIsNot(None, tree.path2id('link'))
43
        self.assertIs(None, tree.path2id('target'))
44
        self.assertEqual('symlink',
45
            tree.kind(tree.path2id('link')))
4634.157.5 by Martin Pool
One more symlink add test
46
47
    def test_smart_add_symlink_pointing_outside(self):
48
        tree = self.make_branch_and_tree('tree')
49
        self.build_tree_contents([
50
            ('tree/link@', '../../../../target'),
51
            ])
52
        tree.smart_add(['tree/link'])
53
        self.assertIsNot(None, tree.path2id('link'))
54
        self.assertIs(None, tree.path2id('target'))
55
        self.assertEqual('symlink',
56
            tree.kind(tree.path2id('link')))
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
57
4634.159.8 by Martin Pool
Handle adding a file under a symlink whose real parent is not yet versioned
58
    def test_add_file_under_symlink(self):
59
        # similar to 
60
        # https://bugs.launchpad.net/bzr/+bug/192859/comments/3
61
        tree = self.make_branch_and_tree('tree')
62
        self.build_tree_contents([
63
            ('tree/link@', 'dir'),
64
            ('tree/dir/',),
65
            ('tree/dir/file', 'content'),
66
            ])
67
        self.assertEquals(
68
            tree.smart_add(['tree/link/file']),
69
            ([u'dir', u'dir/file'], {}))
70
        # should add the actual parent directory, not the apparent parent
71
        # (which is actually a symlink)
72
        self.assertTrue(tree.path2id('dir/file'))
73
        self.assertTrue(tree.path2id('dir'))
74
        self.assertIs(None, tree.path2id('link'))
75
        self.assertIs(None, tree.path2id('link/file'))
76
4634.159.1 by Martin Pool
Move tests into a class that describes their function
77
4634.159.2 by Martin Pool
Add reproduction for bug 192859
78
class TestKindChanges(TestCaseWithWorkingTree):
79
4634.159.7 by Martin Pool
Add symlink test dependency
80
    _test_needs_features = [tests.SymlinkFeature]
81
4634.160.1 by Martin Pool
The test now actually passes on all wt formats
82
    def test_symlink_changes_to_dir(self):
4634.159.4 by Martin Pool
comment
83
        # <https://bugs.launchpad.net/bzr/+bug/192859>:
84
        # we had some past problems with the workingtree remembering for too
85
        # long what kind of object was at a particular name; we really
86
        # shouldn't do that.  Operating on the dirstate through passing
87
        # inventory deltas rather than mutating the inventory largely avoids
88
        # that.
4634.159.2 by Martin Pool
Add reproduction for bug 192859
89
        tree = self.make_branch_and_tree('tree')
90
        self.build_tree_contents([
91
            ('tree/a@', 'target')])
92
        tree.smart_add(['tree/a'])
93
        tree.commit('add symlink')
94
        os.unlink('tree/a')
95
        self.build_tree_contents([
96
            ('tree/a/',),
97
            ('tree/a/f', 'content'),
98
            ])
4634.159.3 by Martin Pool
Better, now failing, test for symlink kind changes
99
        tree.smart_add(['tree/a/f'])
4634.159.2 by Martin Pool
Add reproduction for bug 192859
100
        tree.commit('change to dir')
4634.160.3 by Martin Pool
Check tree after commit of kind change
101
        tree.lock_read()
102
        self.addCleanup(tree.unlock)
103
        self.assertEquals([], list(tree.iter_changes(tree.basis_tree())))
5050.24.2 by Andrew Bennetts
Minimal fix for test_symlink_changes_to_dir test failure.
104
        self.assertEquals(
105
            ['a', 'a/f'], sorted(info[0] for info in tree.list_files()))
4634.159.2 by Martin Pool
Add reproduction for bug 192859
106
4634.160.2 by Martin Pool
Add test (already passing) for symlink changing to dir
107
    def test_dir_changes_to_symlink(self):
108
        # <https://bugs.launchpad.net/bzr/+bug/192859>:
109
        # we had some past problems with the workingtree remembering for too
110
        # long what kind of object was at a particular name; we really
111
        # shouldn't do that.  Operating on the dirstate through passing
112
        # inventory deltas rather than mutating the inventory largely avoids
113
        # that.
114
        tree = self.make_branch_and_tree('tree')
115
        self.build_tree_contents([
116
            ('tree/a/',),
117
            ('tree/a/file', 'content'),
118
            ])
119
        tree.smart_add(['tree/a'])
120
        tree.commit('add dir')
121
        osutils.rmtree('tree/a')
122
        self.build_tree_contents([
123
            ('tree/a@', 'target'),
124
            ])
125
        tree.commit('change to symlink')
126
4634.159.2 by Martin Pool
Add reproduction for bug 192859
127
4634.159.1 by Martin Pool
Move tests into a class that describes their function
128
class TestOpenTree(TestCaseWithWorkingTree):
129
130
    _test_needs_features = [tests.SymlinkFeature]
131
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
132
    def test_open_containing_through_symlink(self):
4634.157.7 by Martin Pool
Further open_containing tests
133
        self.make_test_tree()
134
        self.check_open_containing('link/content', 'tree', 'content')
135
        self.check_open_containing('link/sublink', 'tree', 'sublink')
136
        # this next one is a bit debatable, but arguably it's better that
137
        # open_containing is only concerned with opening the tree 
138
        # and then you can deal with symlinks along the way if you want
139
        self.check_open_containing('link/sublink/subcontent', 'tree',
140
            'sublink/subcontent')
141
142
    def check_open_containing(self, to_open, expected_tree_name,
143
        expected_relpath):
144
        wt, relpath = workingtree.WorkingTree.open_containing(to_open)
145
        self.assertEquals(relpath, expected_relpath)
146
        self.assertEndsWith(wt.basedir, expected_tree_name)
147
4634.157.8 by Martin Pool
tree_files shouldn't dereference the first argument
148
    def test_tree_files(self):
149
        # not strictly a WorkingTree method, but it should be
150
        # probably the root cause for
151
        # <https://bugs.launchpad.net/bzr/+bug/128562>
152
        self.make_test_tree()
153
        self.check_tree_files(['tree/outerlink'],
154
            'tree', ['outerlink'])
155
        self.check_tree_files(['link/outerlink'],
156
            'tree', ['outerlink'])
157
        self.check_tree_files(['link/sublink/subcontent'],
158
            'tree', ['subdir/subcontent'])
159
160
    def check_tree_files(self, to_open, expected_tree, expect_paths):
5346.4.5 by Martin Pool
Deprecate and avoid internal_tree_files and tree_files.
161
        tree, relpaths = workingtree.WorkingTree.open_containing_paths(to_open)
4634.157.8 by Martin Pool
tree_files shouldn't dereference the first argument
162
        self.assertEndsWith(tree.basedir, expected_tree)
163
        self.assertEquals(expect_paths, relpaths)
164
4634.157.7 by Martin Pool
Further open_containing tests
165
    def make_test_tree(self):
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
166
        tree = self.make_branch_and_tree('tree')
167
        self.build_tree_contents([
168
            ('link@', 'tree'),
4634.157.8 by Martin Pool
tree_files shouldn't dereference the first argument
169
            ('tree/outerlink@', '/not/there'),
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
170
            ('tree/content', 'hello'),
4634.157.7 by Martin Pool
Further open_containing tests
171
            ('tree/sublink@', 'subdir'),
172
            ('tree/subdir/',),
173
            ('tree/subdir/subcontent', 'subcontent stuff')
4634.157.6 by Martin Pool
Add test for opening a branch through a symlink
174
            ])