~bzr-pqm/bzr/bzr.dev

2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
1
# Copyright (C) 2007 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
16
17
"""Tests for interface conformance of 'WorkingTree.add'"""
18
19
from bzrlib import (
20
    errors,
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
21
    inventory,
2255.11.1 by Martin Pool
fix missing import
22
    tests,
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
23
    )
6072.2.2 by Jelmer Vernooij
Use HasLayout matcher.
24
from bzrlib.tests.matchers import HasLayout
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
25
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
26
27
28
class TestAdd(TestCaseWithWorkingTree):
29
30
    def assertTreeLayout(self, expected, tree):
31
        """Check that the tree has the correct layout."""
6072.2.2 by Jelmer Vernooij
Use HasLayout matcher.
32
        self.assertThat(tree, HasLayout(expected))
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
33
34
    def test_add_one(self):
35
        tree = self.make_branch_and_tree('.')
36
        self.build_tree(['one'])
37
        tree.add('one', 'one-id')
38
        root_id = tree.get_root_id()
39
40
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
41
2255.7.16 by John Arbash Meinel
Make sure adding a duplicate file_id raises DuplicateFileId.
42
    def test_add_existing_id(self):
43
        """Adding an entry with a pre-existing id raises DuplicateFileId"""
44
        tree = self.make_branch_and_tree('.')
45
        self.build_tree(['a', 'b'])
46
        tree.add(['a'], ['an-id'])
47
        self.assertRaises(errors.DuplicateFileId,
48
                          tree.add, ['b'], ['an-id'])
49
        root_id = tree.get_root_id()
50
        # And the entry should not have been added.
51
        self.assertTreeLayout([('', root_id), ('a', 'an-id')], tree)
52
2255.7.17 by John Arbash Meinel
Add another test that makes sure we can add as long as the file_id isn't current.
53
    def test_add_old_id(self):
54
        """We can add an old id, as long as it doesn't exist now."""
55
        tree = self.make_branch_and_tree('.')
56
        self.build_tree(['a', 'b'])
57
        tree.add(['a'], ['an-id'])
58
        tree.commit('first', rev_id='rev-1')
59
        root_id = tree.get_root_id()
60
        # And the entry should not have been added.
61
        tree.unversion(['an-id'])
62
        tree.add(['b'], ['an-id'])
63
        self.assertTreeLayout([('', root_id), ('b', 'an-id')], tree)
64
        self.assertTreeLayout([('', root_id), ('a', 'an-id')],
65
                              tree.basis_tree())
66
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
67
    def test_add_one_list(self):
68
        tree = self.make_branch_and_tree('.')
69
        self.build_tree(['one'])
70
        tree.add(['one'], ['one-id'])
71
        root_id = tree.get_root_id()
72
73
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
74
75
    def test_add_one_new_id(self):
76
        tree = self.make_branch_and_tree('.')
77
        self.build_tree(['one'])
78
        tree.add(['one'])
79
        root_id = tree.get_root_id()
80
        one_id = tree.path2id('one')
81
82
        self.assertTreeLayout([('', root_id), ('one', one_id)], tree)
83
84
    def test_add_unicode(self):
85
        tree = self.make_branch_and_tree('.')
86
        try:
87
            self.build_tree([u'f\xf6'])
88
        except UnicodeError:
89
            raise tests.TestSkipped('Filesystem does not support filename.')
90
        tree.add([u'f\xf6'])
91
        root_id = tree.get_root_id()
92
        foo_id = tree.path2id(u'f\xf6')
93
94
        self.assertTreeLayout([('', root_id), (u'f\xf6', foo_id)], tree)
95
96
    def test_add_subdir(self):
97
        tree = self.make_branch_and_tree('.')
98
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
99
        tree.add(['dir'], ['dir-id'])
100
        tree.add(['dir/subdir'], ['subdir-id'])
101
        tree.add(['dir/subdir/foo'], ['foo-id'])
102
        root_id = tree.get_root_id()
103
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
104
        self.assertTreeLayout([('', root_id), ('dir/', 'dir-id'),
105
                               ('dir/subdir/', 'subdir-id'),
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
106
                               ('dir/subdir/foo', 'foo-id')], tree)
107
108
    def test_add_multiple(self):
109
        tree = self.make_branch_and_tree('.')
110
        self.build_tree(['a', 'b', 'dir/', 'dir/subdir/', 'dir/subdir/foo'])
111
        tree.add(['a', 'b', 'dir', 'dir/subdir', 'dir/subdir/foo'],
112
                 ['a-id', 'b-id', 'dir-id', 'subdir-id', 'foo-id'])
113
        root_id = tree.get_root_id()
114
115
        self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
116
                               ('dir/', 'dir-id'), ('dir/subdir/', 'subdir-id'),
2255.7.10 by John Arbash Meinel
Handle the case when we are adding a file to an empty directory.
117
                               ('dir/subdir/foo', 'foo-id')], tree)
118
119
    def test_add_invalid(self):
120
        tree = self.make_branch_and_tree('.')
121
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
122
        root_id = tree.get_root_id()
123
124
        self.assertRaises(errors.NotVersionedError,
125
                          tree.add, ['dir/subdir'])
126
        self.assertTreeLayout([('', root_id)], tree)
127
128
    def test_add_after_remove(self):
129
        tree = self.make_branch_and_tree('.')
130
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
131
        root_id = tree.get_root_id()
132
        tree.add(['dir'], ['dir-id'])
133
        tree.commit('dir', rev_id='rev-1')
134
        tree.unversion(['dir-id'])
135
        self.assertRaises(errors.NotVersionedError,
136
                          tree.add, ['dir/subdir'])
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
137
138
    def test_add_root(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
139
        # adding the root should be a no-op, or at least not
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
140
        # do anything whacky.
141
        tree = self.make_branch_and_tree('.')
142
        tree.lock_write()
143
        tree.add('')
5837.2.2 by Jelmer Vernooij
Fix more uses of Tree.__iter__
144
        self.assertEqual([tree.path2id('')], list(tree.all_file_ids()))
2255.7.74 by Robert Collins
Test adding of roots to trees, it was broken on WorkingTree4.
145
        # the root should have been changed to be a new unique root.
146
        self.assertNotEqual(inventory.ROOT_ID, tree.path2id(''))
147
        tree.unlock()
2323.4.1 by Robert Collins
dirstate correctness: allow adding paths present in the basis.
148
149
    def test_add_previously_added(self):
150
        # adding a path that was previously added should work
151
        tree = self.make_branch_and_tree('.')
152
        self.build_tree(['foo'])
153
        tree.add(['foo'], ['foo-id'])
154
        tree.unversion(['foo-id'])
155
        tree.add(['foo'], ['foo-id'])
156
        self.assertEqual('foo-id', tree.path2id('foo'))
157
158
    def test_add_present_in_basis(self):
159
        # adding a path that was present in the basis should work.
160
        tree = self.make_branch_and_tree('.')
161
        self.build_tree(['foo'])
162
        tree.add(['foo'], ['foo-id'])
163
        tree.commit('add foo')
164
        tree.unversion(['foo-id'])
165
        tree.add(['foo'], ['foo-id'])
166
        self.assertEqual('foo-id', tree.path2id('foo'))