/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for interface conformance of 'WorkingTree.add'"""
18
19
import os
20
21
from bzrlib import (
22
    errors,
23
    osutils,
24
    )
25
26
from bzrlib.workingtree_4 import WorkingTreeFormat4
27
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
28
29
30
class TestAdd(TestCaseWithWorkingTree):
31
32
    def get_tree_layout(self, tree):
33
        """Get the (path, file_id) pairs for the current tree."""
34
        tree.lock_read()
35
        try:
36
            return [(path, ie.file_id) for path, ie
37
                    in tree.iter_entries_by_dir()]
38
        finally:
39
            tree.unlock()
40
41
    def assertTreeLayout(self, expected, tree):
42
        """Check that the tree has the correct layout."""
43
        actual = self.get_tree_layout(tree)
44
        self.assertEqual(expected, actual)
45
46
    def test_add_one(self):
47
        tree = self.make_branch_and_tree('.')
48
        self.build_tree(['one'])
49
        tree.add('one', 'one-id')
50
        root_id = tree.get_root_id()
51
52
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
53
54
    def test_add_one_list(self):
55
        tree = self.make_branch_and_tree('.')
56
        self.build_tree(['one'])
57
        tree.add(['one'], ['one-id'])
58
        root_id = tree.get_root_id()
59
60
        self.assertTreeLayout([('', root_id), ('one', 'one-id')], tree)
61
62
    def test_add_one_new_id(self):
63
        tree = self.make_branch_and_tree('.')
64
        self.build_tree(['one'])
65
        tree.add(['one'])
66
        root_id = tree.get_root_id()
67
        one_id = tree.path2id('one')
68
69
        self.assertTreeLayout([('', root_id), ('one', one_id)], tree)
70
71
    def test_add_unicode(self):
72
        tree = self.make_branch_and_tree('.')
73
        try:
74
            self.build_tree([u'f\xf6'])
75
        except UnicodeError:
76
            raise tests.TestSkipped('Filesystem does not support filename.')
77
        tree.add([u'f\xf6'])
78
        root_id = tree.get_root_id()
79
        foo_id = tree.path2id(u'f\xf6')
80
81
        self.assertTreeLayout([('', root_id), (u'f\xf6', foo_id)], tree)
82
83
    def test_add_subdir(self):
84
        tree = self.make_branch_and_tree('.')
85
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
86
        tree.add(['dir'], ['dir-id'])
87
        tree.add(['dir/subdir'], ['subdir-id'])
88
        tree.add(['dir/subdir/foo'], ['foo-id'])
89
        root_id = tree.get_root_id()
90
91
        self.assertTreeLayout([('', root_id), ('dir', 'dir-id'),
92
                               ('dir/subdir', 'subdir-id'),
93
                               ('dir/subdir/foo', 'foo-id')], tree)
94
95
    def test_add_multiple(self):
96
        tree = self.make_branch_and_tree('.')
97
        self.build_tree(['a', 'b', 'dir/', 'dir/subdir/', 'dir/subdir/foo'])
98
        tree.add(['a', 'b', 'dir', 'dir/subdir', 'dir/subdir/foo'],
99
                 ['a-id', 'b-id', 'dir-id', 'subdir-id', 'foo-id'])
100
        root_id = tree.get_root_id()
101
102
        self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
103
                               ('dir', 'dir-id'), ('dir/subdir', 'subdir-id'),
104
                               ('dir/subdir/foo', 'foo-id')], tree)
105
106
    def test_add_invalid(self):
107
        tree = self.make_branch_and_tree('.')
108
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
109
        root_id = tree.get_root_id()
110
111
        self.assertRaises(errors.NotVersionedError,
112
                          tree.add, ['dir/subdir'])
113
        self.assertTreeLayout([('', root_id)], tree)
114
115
    def test_add_after_remove(self):
116
        tree = self.make_branch_and_tree('.')
117
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
118
        root_id = tree.get_root_id()
119
        tree.add(['dir'], ['dir-id'])
120
        tree.commit('dir', rev_id='rev-1')
121
        tree.unversion(['dir-id'])
122
        self.assertRaises(errors.NotVersionedError,
123
                          tree.add, ['dir/subdir'])