4
from bzrlib.selftest import TestCaseInTempDir, TestCase
5
from bzrlib.branch import Branch
6
from bzrlib.errors import NotBranchError, NotVersionedError
4
from bzrlib.add import smart_add, smart_add_tree
5
from bzrlib.tests import TestCaseWithTransport, TestCase
6
from bzrlib.errors import NoSuchFile
7
from bzrlib.inventory import InventoryFile, Inventory
8
from bzrlib.workingtree import WorkingTree
8
class TestSmartAdd(TestCaseInTempDir):
10
class TestSmartAdd(TestCaseWithTransport):
10
12
def test_add_dot_from_root(self):
11
13
"""Test adding . from the root of the tree."""
12
14
from bzrlib.add import smart_add
13
15
paths = ("original/", "original/file1", "original/file2")
14
16
self.build_tree(paths)
15
branch = Branch(".", init=True)
16
smart_add((".",), recurse=True)
17
wt = self.make_branch_and_tree('.')
19
smart_add_tree(wt, (u".",))
18
self.assertNotEqual(branch.inventory.path2id(path), None)
21
self.assertNotEqual(wt.path2id(path), None)
20
23
def test_add_dot_from_subdir(self):
21
24
"""Test adding . from a subdir of the tree."""
22
25
from bzrlib.add import smart_add
23
26
paths = ("original/", "original/file1", "original/file2")
24
27
self.build_tree(paths)
25
branch = Branch(".", init=True)
28
wt = self.make_branch_and_tree('.')
26
30
os.chdir("original")
27
smart_add((".",), recurse=True)
31
smart_add_tree(wt, (u".",))
29
self.assertNotEqual(branch.inventory.path2id(path), None)
33
self.assertNotEqual(wt.path2id(path), None)
31
35
def test_add_tree_from_above_tree(self):
32
36
"""Test adding a tree from above the tree."""
51
55
"original/child/", "original/child/path")
53
57
self.build_tree(build_paths)
54
branch = Branch(".", init=True)
55
child_branch = Branch("original/child", init=True)
56
smart_add((".",), True, add_reporter_null)
58
wt = self.make_branch_and_tree('.')
60
child_tree = self.make_branch_and_tree('original/child')
61
smart_add_tree(wt, (".",))
58
self.assertNotEqual((path, branch.inventory.path2id(path)),
63
self.assertNotEqual((path, wt.path2id(path)),
60
65
for path in full_child_paths:
61
self.assertEqual((path, branch.inventory.path2id(path)),
66
self.assertEqual((path, wt.path2id(path)),
63
68
for path in child_paths:
64
self.assertEqual(child_branch.inventory.path2id(path), None)
69
self.assertEqual(child_tree.path2id(path), None)
66
71
def test_add_paths(self):
67
72
"""Test smart-adding a list of paths."""
68
73
from bzrlib.add import smart_add
69
74
paths = ("file1", "file2")
70
75
self.build_tree(paths)
71
branch = Branch(".", init=True)
76
wt = self.make_branch_and_tree('.')
77
smart_add_tree(wt, paths)
74
self.assertNotEqual(branch.inventory.path2id(path), None)
76
class TestSmartAddBranch(TestCaseInTempDir):
79
self.assertNotEqual(wt.path2id(path), None)
81
def test_add_ignored_nested_paths(self):
82
"""Test smart-adding a list of paths which includes ignored ones."""
83
wt = self.make_branch_and_tree('.')
84
tree_shape = ("adir/", "adir/CVS/", "adir/CVS/afile", "adir/CVS/afile2")
85
add_paths = ("adir/CVS", "adir/CVS/afile", "adir")
86
expected_paths = ("adir", "adir/CVS", "adir/CVS/afile", "adir/CVS/afile2")
87
self.build_tree(tree_shape)
88
smart_add_tree(wt, add_paths)
89
for path in expected_paths:
90
self.assertNotEqual(wt.path2id(path), None, "No id added for %s" % path)
92
def test_save_false(self):
93
"""Test smart-adding a path with save set to false."""
94
wt = self.make_branch_and_tree('.')
95
self.build_tree(['file'])
96
smart_add_tree(wt, ['file'], save=False)
97
self.assertNotEqual(wt.path2id('file'), None, "No id added for 'file'")
98
wt.read_working_inventory()
99
self.assertEqual(wt.path2id('file'), None)
101
def test_add_dry_run(self):
102
"""Test a dry run add, make sure nothing is added."""
103
from bzrlib.commands import run_bzr
104
eq = self.assertEqual
105
wt = self.make_branch_and_tree('.')
107
self.build_tree(['inertiatic/', 'inertiatic/esp'])
108
eq(list(wt.unknowns()), ['inertiatic'])
109
self.capture('add --dry-run .')
110
eq(list(wt.unknowns()), ['inertiatic'])
112
def test_add_non_existant(self):
113
"""Test smart-adding a file that does not exist."""
114
from bzrlib.add import smart_add
115
wt = self.make_branch_and_tree('.')
117
self.assertRaises(NoSuchFile, smart_add_tree, wt, 'non-existant-file')
119
def test_returns_and_ignores(self):
120
"""Correctly returns added/ignored files"""
121
from bzrlib.commands import run_bzr
122
wt = self.make_branch_and_tree('.')
124
# no files should be ignored by default, so we need to create
125
# an ignore rule - we create one for the pyc files, which means
126
# CVS should not be ignored.
127
self.build_tree(['inertiatic/', 'inertiatic/esp', 'inertiatic/CVS',
128
'inertiatic/foo.pyc'])
129
self.build_tree_contents([('.bzrignore', '*.py[oc]\n')])
130
added, ignored = smart_add_tree(wt, u'.')
131
self.assertSubset(('inertiatic', 'inertiatic/esp', 'inertiatic/CVS'),
133
self.assertSubset(('*.py[oc]',), ignored)
134
self.assertSubset(('inertiatic/foo.pyc',), ignored['*.py[oc]'])
137
class TestSmartAddTree(TestCaseWithTransport):
77
138
"""Test smart adds with a specified branch."""
79
140
def test_add_dot_from_root(self):
80
141
"""Test adding . from the root of the tree."""
81
from bzrlib.add import smart_add_branch
82
142
paths = ("original/", "original/file1", "original/file2")
83
143
self.build_tree(paths)
84
branch = Branch(".", init=True)
85
smart_add_branch(branch, (".",))
144
wt = self.make_branch_and_tree('.')
146
smart_add_tree(wt, (u".",))
86
147
for path in paths:
87
self.assertNotEqual(branch.inventory.path2id(path), None)
148
self.assertNotEqual(wt.path2id(path), None)
89
150
def test_add_dot_from_subdir(self):
90
151
"""Test adding . from a subdir of the tree."""
91
from bzrlib.add import smart_add_branch
152
from bzrlib.add import smart_add_tree
92
153
paths = ("original/", "original/file1", "original/file2")
93
154
self.build_tree(paths)
94
branch = Branch(".", init=True)
155
wt = self.make_branch_and_tree('.')
95
157
os.chdir("original")
96
smart_add_branch(branch, (".",))
158
smart_add_tree(wt, (u".",))
97
159
for path in paths:
98
self.assertNotEqual(branch.inventory.path2id(path), None)
160
self.assertNotEqual(wt.path2id(path), None)
100
162
def test_add_tree_from_above_tree(self):
101
163
"""Test adding a tree from above the tree."""
102
from bzrlib.add import smart_add_branch
164
from bzrlib.add import smart_add_tree
103
165
paths = ("original/", "original/file1", "original/file2")
104
166
branch_paths = ("branch/", "branch/original/", "branch/original/file1",
105
167
"branch/original/file2")
106
168
self.build_tree(branch_paths)
107
branch = Branch("branch", init=True)
108
smart_add_branch(branch, ("branch",))
169
tree = self.make_branch_and_tree('branch')
171
smart_add_tree(tree, ("branch",))
109
172
for path in paths:
110
self.assertNotEqual(branch.inventory.path2id(path), None)
173
self.assertNotEqual(tree.path2id(path), None)
112
175
def test_add_above_tree_preserves_tree(self):
113
176
"""Test nested trees are not affect by an add above them."""
114
from bzrlib.add import smart_add_branch
177
from bzrlib.add import smart_add_tree
115
178
paths = ("original/", "original/file1", "original/file2")
116
179
child_paths = ("path")
117
180
full_child_paths = ("original/child", "original/child/path")
118
181
build_paths = ("original/", "original/file1", "original/file2",
119
182
"original/child/", "original/child/path")
120
183
self.build_tree(build_paths)
121
branch = Branch(".", init=True)
122
child_branch = Branch("original/child", init=True)
123
smart_add_branch(branch, (".",))
184
tree = self.make_branch_and_tree('.')
186
child_tree = self.make_branch_and_tree("original/child")
187
smart_add_tree(tree, (u".",))
124
188
for path in paths:
125
self.assertNotEqual((path, branch.inventory.path2id(path)),
189
self.assertNotEqual((path, tree.path2id(path)),
127
191
for path in full_child_paths:
128
self.assertEqual((path, branch.inventory.path2id(path)),
192
self.assertEqual((path, tree.path2id(path)),
130
194
for path in child_paths:
131
self.assertEqual(child_branch.inventory.path2id(path), None)
195
self.assertEqual(child_tree.path2id(path), None)
133
197
def test_add_paths(self):
134
198
"""Test smart-adding a list of paths."""
135
from bzrlib.add import smart_add_branch
199
from bzrlib.add import smart_add_tree
136
200
paths = ("file1", "file2")
137
201
self.build_tree(paths)
138
branch = Branch(".", init=True)
139
smart_add_branch(branch, paths)
202
wt = self.make_branch_and_tree('.')
204
smart_add_tree(wt, paths)
140
205
for path in paths:
141
self.assertNotEqual(branch.inventory.path2id(path), None)
143
class TestAddCallbacks(TestCaseInTempDir):
146
from bzrlib.inventory import InventoryEntry
147
super(TestAddCallbacks, self).setUp()
148
self.entry = InventoryEntry("id", "name", "file", None)
150
def test_null_callback(self):
151
from bzrlib.add import add_reporter_null
152
add_reporter_null('path', 'file', self.entry)
154
def test_print_callback(self):
155
from bzrlib.add import add_reporter_print
156
from StringIO import StringIO
206
self.assertNotEqual(wt.path2id(path), None)
209
class TestAddActions(TestCase):
211
def test_quiet(self):
214
def test__print(self):
215
self.run_action("added path\n")
217
def run_action(self, output):
218
from bzrlib.add import AddAction, FastPath
219
from cStringIO import StringIO
157
221
stdout = StringIO()
158
self.apply_redirected(None, stdout, None, add_reporter_print,
159
'path', 'file', self.entry)
160
self.assertEqual(stdout.getvalue(), "added path\n")
222
action = AddAction(to_file=stdout, should_print=bool(output))
224
self.apply_redirected(None, stdout, None, action, inv, None, FastPath('path'), 'file')
225
self.assertEqual(stdout.getvalue(), output)