1
# Copyright (C) 2005, 2006, 2007, 2009, 2010 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from cStringIO import StringIO
19
from bzrlib import osutils
20
from bzrlib.add import (
27
class AddCustomIDAction(add.AddAction):
24
from bzrlib.tests import TestCase, TestCaseWithTransport
25
from bzrlib.inventory import Inventory
28
class AddCustomIDAction(AddAction):
29
30
def __call__(self, inv, parent_ie, path, kind):
30
31
# The first part just logs if appropriate
31
32
# Now generate a custom id
32
33
file_id = osutils.safe_file_id(kind + '-'
33
+ path.replace('/', '%'),
34
+ path.raw_path.replace('/', '%'),
35
36
if self.should_print:
36
37
self._to_file.write('added %s with id %s\n'
38
% (path.raw_path, file_id))
41
class TestAddFrom(tests.TestCaseWithTransport):
42
class TestAddFrom(TestCaseWithTransport):
42
43
"""Tests for AddFromBaseAction"""
44
45
def make_base_tree(self):
60
61
new_tree.lock_write()
62
action = add.AddFromBaseAction(base_tree, base_path,
64
should_print=should_print)
63
action = AddFromBaseAction(base_tree, base_path,
65
should_print=should_print)
65
66
new_tree.smart_add(file_list, action=action)
115
116
self.assertNotEqual(None, c_id)
116
117
self.base_tree.lock_read()
117
118
self.addCleanup(self.base_tree.unlock)
118
self.assertFalse(self.base_tree.has_id(c_id))
119
self.failIf(c_id in self.base_tree)
120
121
d_id = new_tree.path2id('subdir/d')
121
122
self.assertNotEqual(None, d_id)
122
self.assertFalse(self.base_tree.has_id(d_id))
123
self.failIf(d_id in self.base_tree)
124
125
def test_copy_existing_dir(self):
125
126
self.make_base_tree()
140
141
self.assertNotEqual(None, a_id)
141
142
self.base_tree.lock_read()
142
143
self.addCleanup(self.base_tree.unlock)
143
self.assertFalse(self.base_tree.has_id(a_id))
146
class TestAddActions(tests.TestCase):
144
self.failIf(a_id in self.base_tree)
147
class TestAddActions(TestCase):
148
149
def test_quiet(self):
149
150
self.run_action("")
151
152
def test__print(self):
152
self.run_action("adding path\n")
153
self.run_action("added path\n")
154
155
def run_action(self, output):
155
inv = inventory.Inventory()
156
from bzrlib.add import AddAction
157
from bzrlib.mutabletree import _FastPath
156
159
stdout = StringIO()
157
action = add.AddAction(to_file=stdout, should_print=bool(output))
160
action = AddAction(to_file=stdout, should_print=bool(output))
159
162
self.apply_redirected(None, stdout, None, action, inv, None,
163
_FastPath('path'), 'file')
161
164
self.assertEqual(stdout.getvalue(), output)