1
# Copyright (C) 2005, 2006, 2007 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
18
"""Tests of the 'bzr add' command."""
22
from bzrlib import osutils
23
from bzrlib.tests import (
25
split_suite_by_condition,
29
from bzrlib.tests.blackbox import ExternalBase
30
from bzrlib.tests.test_win32utils import NeedsGlobExpansionFeature
33
def load_tests(standard_tests, module, loader):
34
"""Parameterize tests for view-aware vs not."""
35
to_adapt, result = split_suite_by_condition(
36
standard_tests, condition_isinstance(TestAdd))
38
('pre-views', {'branch_tree_format': 'pack-0.92'}),
39
('view-aware', {'branch_tree_format': 'development6-rich-root'}),
41
return multiply_tests(to_adapt, scenarios, result)
44
class TestAdd(ExternalBase):
46
def make_branch_and_tree(self, dir):
47
return ExternalBase.make_branch_and_tree(self, dir,
48
format=self.branch_tree_format)
50
def test_add_reports(self):
51
"""add command prints the names of added files."""
52
tree = self.make_branch_and_tree('.')
53
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt', 'CVS'])
54
self.build_tree_contents([('.bzrignore', 'CVS\n')])
55
out = self.run_bzr('add')[0]
56
# the ordering is not defined at the moment
57
results = sorted(out.rstrip('\n').split('\n'))
58
self.assertEquals(['If you wish to add ignored files, '
59
'please add them explicitly by name. '
60
'("bzr ignored" gives a list)',
65
'ignored 1 file(s).'],
67
out = self.run_bzr('add -v')[0]
68
results = sorted(out.rstrip('\n').split('\n'))
69
self.assertEquals(['If you wish to add ignored files, '\
70
'please add them explicitly by name. ("bzr ignored" gives a list)',
71
'ignored CVS matching "CVS"'],
74
def test_add_quiet_is(self):
75
"""add -q does not print the names of added files."""
76
tree = self.make_branch_and_tree('.')
77
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
78
out = self.run_bzr('add -q')[0]
79
# the ordering is not defined at the moment
80
results = sorted(out.rstrip('\n').split('\n'))
81
self.assertEquals([''], results)
83
def test_add_in_unversioned(self):
84
"""Try to add a file in an unversioned directory.
86
"bzr add" should add the parent(s) as necessary.
88
tree = self.make_branch_and_tree('.')
89
self.build_tree(['inertiatic/', 'inertiatic/esp'])
90
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
91
self.run_bzr('add inertiatic/esp')
92
self.assertEquals(self.run_bzr('unknowns')[0], '')
94
# Multiple unversioned parents
95
self.build_tree(['veil/', 'veil/cerpin/', 'veil/cerpin/taxt'])
96
self.assertEquals(self.run_bzr('unknowns')[0], 'veil\n')
97
self.run_bzr('add veil/cerpin/taxt')
98
self.assertEquals(self.run_bzr('unknowns')[0], '')
100
# Check whacky paths work
101
self.build_tree(['cicatriz/', 'cicatriz/esp'])
102
self.assertEquals(self.run_bzr('unknowns')[0], 'cicatriz\n')
103
self.run_bzr('add inertiatic/../cicatriz/esp')
104
self.assertEquals(self.run_bzr('unknowns')[0], '')
106
def test_add_in_versioned(self):
107
"""Try to add a file in a versioned directory.
109
"bzr add" should do this happily.
111
tree = self.make_branch_and_tree('.')
112
self.build_tree(['inertiatic/', 'inertiatic/esp'])
113
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
114
self.run_bzr('add --no-recurse inertiatic')
115
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic/esp\n')
116
self.run_bzr('add inertiatic/esp')
117
self.assertEquals(self.run_bzr('unknowns')[0], '')
119
def test_subdir_add(self):
120
"""Add in subdirectory should add only things from there down"""
121
from bzrlib.workingtree import WorkingTree
123
eq = self.assertEqual
124
ass = self.assertTrue
127
t = self.make_branch_and_tree('.')
129
self.build_tree(['src/', 'README'])
131
eq(sorted(t.unknowns()),
134
self.run_bzr('add src')
136
self.build_tree(['src/foo.c'])
138
# add with no arguments in a subdirectory gets only files below that
142
self.assertEquals(self.run_bzr('unknowns')[0], 'README\n')
143
# reopen to see the new changes
144
t = t.bzrdir.open_workingtree()
145
versioned = [path for path, entry in t.iter_entries_by_dir()]
146
self.assertEquals(versioned,
147
['', 'src', 'src/foo.c'])
149
# add from the parent directory should pick up all file names
152
self.assertEquals(self.run_bzr('unknowns')[0], '')
153
self.run_bzr('check')
155
def test_add_missing(self):
156
"""bzr add foo where foo is missing should error."""
157
self.make_branch_and_tree('.')
158
self.run_bzr('add missing-file', retcode=3)
160
def test_add_from(self):
161
base_tree = self.make_branch_and_tree('base')
162
self.build_tree(['base/a', 'base/b/', 'base/b/c'])
163
base_tree.add(['a', 'b', 'b/c'])
164
base_tree.commit('foo')
166
new_tree = self.make_branch_and_tree('new')
167
self.build_tree(['new/a', 'new/b/', 'new/b/c', 'd'])
170
out, err = self.run_bzr('add --file-ids-from ../base')
171
self.assertEqual('', err)
172
self.assertEqualDiff('adding a w/ file id from a\n'
173
'adding b w/ file id from b\n'
174
'adding b/c w/ file id from b/c\n',
176
new_tree = new_tree.bzrdir.open_workingtree()
177
self.assertEqual(base_tree.path2id('a'), new_tree.path2id('a'))
178
self.assertEqual(base_tree.path2id('b'), new_tree.path2id('b'))
179
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('b/c'))
181
def test_add_from_subdir(self):
182
base_tree = self.make_branch_and_tree('base')
183
self.build_tree(['base/a', 'base/b/', 'base/b/c', 'base/b/d'])
184
base_tree.add(['a', 'b', 'b/c', 'b/d'])
185
base_tree.commit('foo')
187
new_tree = self.make_branch_and_tree('new')
188
self.build_tree(['new/c', 'new/d'])
191
out, err = self.run_bzr('add --file-ids-from ../base/b')
192
self.assertEqual('', err)
193
self.assertEqualDiff('adding c w/ file id from b/c\n'
194
'adding d w/ file id from b/d\n',
197
new_tree = new_tree.bzrdir.open_workingtree()
198
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('c'))
199
self.assertEqual(base_tree.path2id('b/d'), new_tree.path2id('d'))
201
def test_add_dry_run(self):
202
"""Test a dry run add, make sure nothing is added."""
203
wt = self.make_branch_and_tree('.')
204
self.build_tree(['inertiatic/', 'inertiatic/esp'])
205
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
206
self.run_bzr('add --dry-run')
207
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
209
def test_add_control_dir(self):
210
"""The control dir and its content should be refused."""
211
self.make_branch_and_tree('.')
212
err = self.run_bzr('add .bzr', retcode=3)[1]
213
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
214
err = self.run_bzr('add .bzr/README', retcode=3)[1]
215
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
216
self.build_tree(['.bzr/crescent'])
217
err = self.run_bzr('add .bzr/crescent', retcode=3)[1]
218
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
220
def test_add_with_wildcards(self):
221
self.requireFeature(NeedsGlobExpansionFeature)
222
self.make_branch_and_tree('.')
223
self.build_tree(['a1', 'a2', 'b', 'c33'])
224
self.run_bzr(['add', 'a?', 'c*'])
225
self.assertEquals(self.run_bzr('unknowns')[0], 'b\n')
227
def test_add_with_wildcards_unicode(self):
228
self.requireFeature(NeedsGlobExpansionFeature)
229
self.make_branch_and_tree('.')
230
self.build_tree([u'\u1234A', u'\u1235A', u'\u1235AA', 'cc'])
231
self.run_bzr(['add', u'\u1234?', u'\u1235*'])
232
self.assertEquals(self.run_bzr('unknowns')[0], 'cc\n')
234
def test_add_via_symlink(self):
235
self.requireFeature(SymlinkFeature)
236
self.make_branch_and_tree('source')
237
self.build_tree(['source/top.txt'])
238
os.symlink('source', 'link')
239
out = self.run_bzr(['add', 'link/top.txt'])[0]
240
self.assertEquals(out, 'adding top.txt\n')
242
def test_add_symlink_to_abspath(self):
243
self.requireFeature(SymlinkFeature)
244
self.make_branch_and_tree('tree')
245
os.symlink(osutils.abspath('target'), 'tree/link')
246
out = self.run_bzr(['add', 'tree/link'])[0]
247
self.assertEquals(out, 'adding link\n')