1
# Copyright (C) 2005, 2006, 2007, 2009, 2010 Canonical Ltd
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.
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.
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
26
from ..sixish import (
31
class AddCustomIDAction(add.AddAction):
33
def __call__(self, inv, parent_ie, path, kind):
34
# The first part just logs if appropriate
35
# Now generate a custom id
36
file_id = cache_utf8.encode(kind + '-' + path.replace('/', '%'))
38
self._to_file.write('added %s with id %s\n'
43
class TestAddFrom(tests.TestCaseWithTransport):
44
"""Tests for AddFromBaseAction"""
46
def make_base_tree(self):
47
self.base_tree = self.make_branch_and_tree('base')
48
self.build_tree(['base/a', 'base/b',
49
'base/dir/', 'base/dir/a',
53
self.base_tree.add(['a', 'b', 'dir', 'dir/a',
54
'dir/subdir', 'dir/subdir/b'])
55
self.base_tree.commit('creating initial tree.')
57
def add_helper(self, base_tree, base_path, new_tree, file_list,
64
action = add.AddFromBaseAction(base_tree, base_path,
66
should_print=should_print)
67
new_tree.smart_add(file_list, action=action)
72
return to_file.getvalue()
74
def test_copy_all(self):
76
new_tree = self.make_branch_and_tree('new')
82
self.build_tree(['new/' + fn for fn in files])
83
self.add_helper(self.base_tree, '', new_tree, ['new'])
86
base_file_id = self.base_tree.path2id(fn)
87
new_file_id = new_tree.path2id(fn)
88
self.assertEqual(base_file_id, new_file_id)
90
def test_copy_from_dir(self):
92
new_tree = self.make_branch_and_tree('new')
94
self.build_tree(['new/a', 'new/b', 'new/c',
95
'new/subdir/', 'new/subdir/b', 'new/subdir/d'])
96
new_tree.set_root_id(self.base_tree.get_root_id())
97
self.add_helper(self.base_tree, 'dir', new_tree, ['new'])
99
# We know 'a' and 'b' exist in the root, and they are being added
100
# in a new 'root'. Since ROOT ids have been set as the same, we will
102
self.assertEqual(self.base_tree.path2id('a'),
103
new_tree.path2id('a'))
104
self.assertEqual(self.base_tree.path2id('b'),
105
new_tree.path2id('b'))
107
# Because we specified 'dir/' as the base path, and we have
108
# nothing named 'subdir' in the base tree, we should grab the
110
self.assertEqual(self.base_tree.path2id('dir/subdir'),
111
new_tree.path2id('subdir'))
112
self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
113
new_tree.path2id('subdir/b'))
115
# These should get newly generated ids
116
c_id = new_tree.path2id('c')
117
self.assertNotEqual(None, c_id)
118
self.base_tree.lock_read()
119
self.addCleanup(self.base_tree.unlock)
120
self.assertFalse(self.base_tree.has_id(c_id))
122
d_id = new_tree.path2id('subdir/d')
123
self.assertNotEqual(None, d_id)
124
self.assertFalse(self.base_tree.has_id(d_id))
126
def test_copy_existing_dir(self):
127
self.make_base_tree()
128
new_tree = self.make_branch_and_tree('new')
129
self.build_tree(['new/subby/', 'new/subby/a', 'new/subby/b'])
131
subdir_file_id = self.base_tree.path2id('dir/subdir')
132
new_tree.add(['subby'], [subdir_file_id])
133
self.add_helper(self.base_tree, '', new_tree, ['new'])
134
# Because 'subby' already points to subdir, we should add
135
# 'b' with the same id
136
self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
137
new_tree.path2id('subby/b'))
139
# 'subby/a' should be added with a new id because there is no
140
# matching path or child of 'subby'.
141
a_id = new_tree.path2id('subby/a')
142
self.assertNotEqual(None, a_id)
143
self.base_tree.lock_read()
144
self.addCleanup(self.base_tree.unlock)
145
self.assertFalse(self.base_tree.has_id(a_id))
148
class TestAddActions(tests.TestCase):
150
def test_quiet(self):
153
def test__print(self):
154
self.run_action("adding path\n")
156
def run_action(self, output):
157
inv = inventory.Inventory()
159
action = add.AddAction(to_file=stdout, should_print=bool(output))
161
self.apply_redirected(None, stdout, None, action, inv, None,
163
self.assertEqual(stdout.getvalue(), output)