~bzr-pqm/bzr/bzr.dev

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