4
from bzrlib.tests import TestCaseInTempDir, TestCase
5
from bzrlib.branch import Branch
6
from bzrlib.errors import NotBranchError
7
from bzrlib.inventory import InventoryFile, Inventory
8
from bzrlib.workingtree import WorkingTree
9
from bzrlib.add import smart_add
11
class TestSmartAdd(TestCaseInTempDir):
13
def test_add_dot_from_root(self):
14
"""Test adding . from the root of the tree."""
15
from bzrlib.add import smart_add
16
paths = ("original/", "original/file1", "original/file2")
17
self.build_tree(paths)
18
branch = Branch.initialize(u".")
21
self.assertNotEqual(branch.working_tree().path2id(path), None)
23
def test_add_dot_from_subdir(self):
24
"""Test adding . from a subdir of the tree."""
25
from bzrlib.add import smart_add
26
paths = ("original/", "original/file1", "original/file2")
27
self.build_tree(paths)
28
branch = Branch.initialize(u".")
32
self.assertNotEqual(branch.working_tree().path2id(path), None)
34
def test_add_tree_from_above_tree(self):
35
"""Test adding a tree from above the tree."""
36
from bzrlib.add import smart_add
37
paths = ("original/", "original/file1", "original/file2")
38
branch_paths = ("branch/", "branch/original/", "branch/original/file1",
39
"branch/original/file2")
40
self.build_tree(branch_paths)
41
branch = Branch.initialize("branch")
42
smart_add(("branch",))
44
self.assertNotEqual(branch.working_tree().path2id(path), None)
46
def test_add_above_tree_preserves_tree(self):
47
"""Test nested trees are not affect by an add above them."""
48
from bzrlib.add import smart_add
49
paths = ("original/", "original/file1", "original/file2")
50
child_paths = ("path",)
51
full_child_paths = ("original/child", "original/child/path")
52
build_paths = ("original/", "original/file1", "original/file2",
53
"original/child/", "original/child/path")
55
self.build_tree(build_paths)
56
branch = Branch.initialize(u".")
57
child_branch = Branch.initialize("original/child")
60
self.assertNotEqual((path, branch.working_tree().path2id(path)),
62
for path in full_child_paths:
63
self.assertEqual((path, branch.working_tree().path2id(path)),
65
for path in child_paths:
66
self.assertEqual(child_branch.working_tree().path2id(path), None)
68
def test_add_paths(self):
69
"""Test smart-adding a list of paths."""
70
from bzrlib.add import smart_add
71
paths = ("file1", "file2")
72
self.build_tree(paths)
73
branch = Branch.initialize(u".")
76
self.assertNotEqual(branch.working_tree().path2id(path), None)
78
def test_add_dry_run(self):
79
"""Test a dry run add, make sure nothing is added."""
80
from bzrlib.commands import run_bzr
82
b = Branch.initialize(u'.')
84
self.build_tree(['inertiatic/', 'inertiatic/esp'])
85
eq(list(t.unknowns()), ['inertiatic'])
86
self.capture('add --dry-run .')
87
eq(list(t.unknowns()), ['inertiatic'])
90
def test_returns(self):
91
"""Correctly returns added/ignored files"""
92
from bzrlib.commands import run_bzr
93
b = Branch.initialize(u'.')
95
self.build_tree(['inertiatic/', 'inertiatic/esp', 'inertiatic/CVS',
96
'inertiatic/foo.pyc'])
97
added, ignored = smart_add(u'.')
98
self.AssertSubset(('inertiatic', 'inertiatic/esp'), added)
99
self.AssertSubset(('CVS', '*.py[oc]'), ignored)
100
self.AssertSubset(('inertiatic/CVS',), ignored['CVS'])
101
self.AssertSubset(('inertiatic/foo.pyc',), ignored['*.py[oc]'])
105
class TestSmartAddBranch(TestCaseInTempDir):
106
"""Test smart adds with a specified branch."""
108
def test_add_dot_from_root(self):
109
"""Test adding . from the root of the tree."""
110
from bzrlib.add import smart_add_tree
111
paths = ("original/", "original/file1", "original/file2")
112
self.build_tree(paths)
113
Branch.initialize(u".")
115
smart_add_tree(tree, (u".",))
117
self.assertNotEqual(tree.path2id(path), None)
119
def test_add_dot_from_subdir(self):
120
"""Test adding . from a subdir of the tree."""
121
from bzrlib.add import smart_add_tree
122
paths = ("original/", "original/file1", "original/file2")
123
self.build_tree(paths)
124
Branch.initialize(u".")
127
smart_add_tree(tree, (u".",))
129
self.assertNotEqual(tree.path2id(path), None)
131
def test_add_tree_from_above_tree(self):
132
"""Test adding a tree from above the tree."""
133
from bzrlib.add import smart_add_tree
134
paths = ("original/", "original/file1", "original/file2")
135
branch_paths = ("branch/", "branch/original/", "branch/original/file1",
136
"branch/original/file2")
137
self.build_tree(branch_paths)
138
Branch.initialize("branch")
139
tree = WorkingTree("branch")
140
smart_add_tree(tree, ("branch",))
142
self.assertNotEqual(tree.path2id(path), None)
144
def test_add_above_tree_preserves_tree(self):
145
"""Test nested trees are not affect by an add above them."""
146
from bzrlib.add import smart_add_tree
147
paths = ("original/", "original/file1", "original/file2")
148
child_paths = ("path")
149
full_child_paths = ("original/child", "original/child/path")
150
build_paths = ("original/", "original/file1", "original/file2",
151
"original/child/", "original/child/path")
152
self.build_tree(build_paths)
153
Branch.initialize(u".")
155
child_branch = Branch.initialize("original/child")
156
smart_add_tree(tree, (u".",))
158
self.assertNotEqual((path, tree.path2id(path)),
160
for path in full_child_paths:
161
self.assertEqual((path, tree.path2id(path)),
163
for path in child_paths:
164
self.assertEqual(child_branch.working_tree().path2id(path), None)
166
def test_add_paths(self):
167
"""Test smart-adding a list of paths."""
168
from bzrlib.add import smart_add_tree
169
paths = ("file1", "file2")
170
self.build_tree(paths)
171
Branch.initialize(u".")
173
smart_add_tree(tree, paths)
175
self.assertNotEqual(tree.path2id(path), None)
177
class TestAddActions(TestCaseInTempDir):
180
from bzrlib.add import add_action_null
181
self.run_action(add_action_null, "", False)
184
self.entry = InventoryFile("id", "name", None)
185
from bzrlib.add import add_action_add
186
self.run_action(add_action_add, "", True)
188
def test_add_and_print(self):
189
from bzrlib.add import add_action_add_and_print
190
self.run_action(add_action_add_and_print, "added path\n", True)
192
def test_print(self):
193
from bzrlib.add import add_action_print
194
self.run_action(add_action_print, "added path\n", False)
196
def run_action(self, action, output, should_add):
197
from StringIO import StringIO
201
self.apply_redirected(None, stdout, None, action, inv, 'path', 'file')
202
self.assertEqual(stdout.getvalue(), output)
205
self.assertNotEqual(inv.path2id('path'), None)
207
self.assertEqual(inv.path2id('path'), None)