summaryrefslogtreecommitdiff
path: root/bzrlib/tests/per_workingtree/test_symlinks.py
blob: 72d63dcf022c65873766bd475c7527121ecca99e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright (C) 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test symlink support.
"""

import os

from bzrlib import (
    osutils,
    tests,
    workingtree,
    )
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
from bzrlib.tests import (
    features,
    )


class TestSmartAddTree(TestCaseWithWorkingTree):

    # See eg <https://bugs.launchpad.net/bzr/+bug/192859>

    _test_needs_features = [features.SymlinkFeature]

    def test_smart_add_symlink(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('tree/link@', 'target'),
            ])
        tree.smart_add(['tree/link'])
        self.assertIsNot(None, tree.path2id('link'))
        self.assertIs(None, tree.path2id('target'))
        self.assertEqual('symlink',
            tree.kind(tree.path2id('link')))

    def test_smart_add_symlink_pointing_outside(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('tree/link@', '../../../../target'),
            ])
        tree.smart_add(['tree/link'])
        self.assertIsNot(None, tree.path2id('link'))
        self.assertIs(None, tree.path2id('target'))
        self.assertEqual('symlink',
            tree.kind(tree.path2id('link')))

    def test_add_file_under_symlink(self):
        # similar to 
        # https://bugs.launchpad.net/bzr/+bug/192859/comments/3
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('tree/link@', 'dir'),
            ('tree/dir/',),
            ('tree/dir/file', 'content'),
            ])
        self.assertEquals(
            tree.smart_add(['tree/link/file']),
            ([u'dir', u'dir/file'], {}))
        # should add the actual parent directory, not the apparent parent
        # (which is actually a symlink)
        self.assertTrue(tree.path2id('dir/file'))
        self.assertTrue(tree.path2id('dir'))
        self.assertIs(None, tree.path2id('link'))
        self.assertIs(None, tree.path2id('link/file'))


class TestKindChanges(TestCaseWithWorkingTree):

    _test_needs_features = [features.SymlinkFeature]

    def test_symlink_changes_to_dir(self):
        # <https://bugs.launchpad.net/bzr/+bug/192859>:
        # we had some past problems with the workingtree remembering for too
        # long what kind of object was at a particular name; we really
        # shouldn't do that.  Operating on the dirstate through passing
        # inventory deltas rather than mutating the inventory largely avoids
        # that.
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('tree/a@', 'target')])
        tree.smart_add(['tree/a'])
        tree.commit('add symlink')
        os.unlink('tree/a')
        self.build_tree_contents([
            ('tree/a/',),
            ('tree/a/f', 'content'),
            ])
        tree.smart_add(['tree/a/f'])
        tree.commit('change to dir')
        tree.lock_read()
        self.addCleanup(tree.unlock)
        self.assertEquals([], list(tree.iter_changes(tree.basis_tree())))
        if tree._format.supports_versioned_directories:
            self.assertEquals(
                ['a', 'a/f'], sorted(info[0] for info in tree.list_files()))
        else:
            self.assertEquals([], list(tree.list_files()))

    def test_dir_changes_to_symlink(self):
        # <https://bugs.launchpad.net/bzr/+bug/192859>:
        # we had some past problems with the workingtree remembering for too
        # long what kind of object was at a particular name; we really
        # shouldn't do that.  Operating on the dirstate through passing
        # inventory deltas rather than mutating the inventory largely avoids
        # that.
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('tree/a/',),
            ('tree/a/file', 'content'),
            ])
        tree.smart_add(['tree/a'])
        tree.commit('add dir')
        osutils.rmtree('tree/a')
        self.build_tree_contents([
            ('tree/a@', 'target'),
            ])
        tree.commit('change to symlink')


class TestOpenTree(TestCaseWithWorkingTree):

    _test_needs_features = [features.SymlinkFeature]

    def test_open_containing_through_symlink(self):
        self.make_test_tree()
        self.check_open_containing('link/content', 'tree', 'content')
        self.check_open_containing('link/sublink', 'tree', 'sublink')
        # this next one is a bit debatable, but arguably it's better that
        # open_containing is only concerned with opening the tree 
        # and then you can deal with symlinks along the way if you want
        self.check_open_containing('link/sublink/subcontent', 'tree',
            'sublink/subcontent')

    def check_open_containing(self, to_open, expected_tree_name,
        expected_relpath):
        wt, relpath = workingtree.WorkingTree.open_containing(to_open)
        self.assertEquals(relpath, expected_relpath)
        self.assertEndsWith(wt.basedir, expected_tree_name)

    def test_tree_files(self):
        # not strictly a WorkingTree method, but it should be
        # probably the root cause for
        # <https://bugs.launchpad.net/bzr/+bug/128562>
        self.make_test_tree()
        self.check_tree_files(['tree/outerlink'],
            'tree', ['outerlink'])
        self.check_tree_files(['link/outerlink'],
            'tree', ['outerlink'])
        self.check_tree_files(['link/sublink/subcontent'],
            'tree', ['subdir/subcontent'])

    def check_tree_files(self, to_open, expected_tree, expect_paths):
        tree, relpaths = workingtree.WorkingTree.open_containing_paths(to_open)
        self.assertEndsWith(tree.basedir, expected_tree)
        self.assertEquals(expect_paths, relpaths)

    def make_test_tree(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree_contents([
            ('link@', 'tree'),
            ('tree/outerlink@', '/not/there'),
            ('tree/content', 'hello'),
            ('tree/sublink@', 'subdir'),
            ('tree/subdir/',),
            ('tree/subdir/subcontent', 'subcontent stuff')
            ])