1
# Copyright (C) 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
18
"""Tests of the 'bzr add' command."""
26
from bzrlib.tests import (
31
def load_tests(standard_tests, module, loader):
32
"""Parameterize tests for view-aware vs not."""
33
to_adapt, result = tests.split_suite_by_condition(
34
standard_tests, tests.condition_isinstance(TestAdd))
36
('pre-views', {'branch_tree_format': 'pack-0.92'}),
37
('view-aware', {'branch_tree_format': 'development6-rich-root'}),
39
return tests.multiply_tests(to_adapt, scenarios, result)
42
class TestAdd(tests.TestCaseWithTransport):
44
def make_branch_and_tree(self, dir):
45
return super(TestAdd, self).make_branch_and_tree(
46
dir, format=self.branch_tree_format)
48
def test_add_reports(self):
49
"""add command prints the names of added files."""
50
tree = self.make_branch_and_tree('.')
51
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt', 'CVS'])
52
self.build_tree_contents([('.bzrignore', 'CVS\n')])
53
out = self.run_bzr('add')[0]
54
# the ordering is not defined at the moment
55
results = sorted(out.rstrip('\n').split('\n'))
56
self.assertEquals(['adding .bzrignore',
61
out = self.run_bzr('add -v')[0]
62
results = sorted(out.rstrip('\n').split('\n'))
63
self.assertEquals(['ignored CVS matching "CVS"'],
66
def test_add_quiet_is(self):
67
"""add -q does not print the names of added files."""
68
tree = self.make_branch_and_tree('.')
69
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
70
out = self.run_bzr('add -q')[0]
71
# the ordering is not defined at the moment
72
results = sorted(out.rstrip('\n').split('\n'))
73
self.assertEquals([''], results)
75
def test_add_in_unversioned(self):
76
"""Try to add a file in an unversioned directory.
78
"bzr add" should add the parent(s) as necessary.
80
tree = self.make_branch_and_tree('.')
81
self.build_tree(['inertiatic/', 'inertiatic/esp'])
82
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
83
self.run_bzr('add inertiatic/esp')
84
self.assertEquals(self.run_bzr('unknowns')[0], '')
86
# Multiple unversioned parents
87
self.build_tree(['veil/', 'veil/cerpin/', 'veil/cerpin/taxt'])
88
self.assertEquals(self.run_bzr('unknowns')[0], 'veil\n')
89
self.run_bzr('add veil/cerpin/taxt')
90
self.assertEquals(self.run_bzr('unknowns')[0], '')
92
# Check whacky paths work
93
self.build_tree(['cicatriz/', 'cicatriz/esp'])
94
self.assertEquals(self.run_bzr('unknowns')[0], 'cicatriz\n')
95
self.run_bzr('add inertiatic/../cicatriz/esp')
96
self.assertEquals(self.run_bzr('unknowns')[0], '')
98
def test_add_in_versioned(self):
99
"""Try to add a file in a versioned directory.
101
"bzr add" should do this happily.
103
tree = self.make_branch_and_tree('.')
104
self.build_tree(['inertiatic/', 'inertiatic/esp'])
105
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
106
self.run_bzr('add --no-recurse inertiatic')
107
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic/esp\n')
108
self.run_bzr('add inertiatic/esp')
109
self.assertEquals(self.run_bzr('unknowns')[0], '')
111
def test_subdir_add(self):
112
"""Add in subdirectory should add only things from there down"""
113
from bzrlib.workingtree import WorkingTree
115
eq = self.assertEqual
116
ass = self.assertTrue
118
t = self.make_branch_and_tree('.')
120
self.build_tree(['src/', 'README'])
122
eq(sorted(t.unknowns()),
125
self.run_bzr('add src')
127
self.build_tree(['src/foo.c'])
129
# add with no arguments in a subdirectory gets only files below that
131
self.run_bzr('add', working_dir='src')
132
self.assertEquals('README\n',
133
self.run_bzr('unknowns', working_dir='src')[0])
134
# reopen to see the new changes
135
t = t.bzrdir.open_workingtree('src')
136
versioned = [path for path, entry in t.iter_entries_by_dir()]
137
self.assertEquals(versioned, ['', 'src', 'src/foo.c'])
139
# add from the parent directory should pick up all file names
141
self.assertEquals(self.run_bzr('unknowns')[0], '')
142
self.run_bzr('check')
144
def test_add_missing(self):
145
"""bzr add foo where foo is missing should error."""
146
self.make_branch_and_tree('.')
147
self.run_bzr('add missing-file', retcode=3)
149
def test_add_from(self):
150
base_tree = self.make_branch_and_tree('base')
151
self.build_tree(['base/a', 'base/b/', 'base/b/c'])
152
base_tree.add(['a', 'b', 'b/c'])
153
base_tree.commit('foo')
155
new_tree = self.make_branch_and_tree('new')
156
self.build_tree(['new/a', 'new/b/', 'new/b/c', 'd'])
159
out, err = self.run_bzr('add --file-ids-from ../base')
160
self.assertEqual('', err)
161
self.assertEqualDiff('adding a w/ file id from a\n'
162
'adding b w/ file id from b\n'
163
'adding b/c w/ file id from b/c\n',
165
new_tree = new_tree.bzrdir.open_workingtree()
166
self.assertEqual(base_tree.path2id('a'), new_tree.path2id('a'))
167
self.assertEqual(base_tree.path2id('b'), new_tree.path2id('b'))
168
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('b/c'))
170
def test_add_from_subdir(self):
171
base_tree = self.make_branch_and_tree('base')
172
self.build_tree(['base/a', 'base/b/', 'base/b/c', 'base/b/d'])
173
base_tree.add(['a', 'b', 'b/c', 'b/d'])
174
base_tree.commit('foo')
176
new_tree = self.make_branch_and_tree('new')
177
self.build_tree(['new/c', 'new/d'])
180
out, err = self.run_bzr('add --file-ids-from ../base/b')
181
self.assertEqual('', err)
182
self.assertEqualDiff('adding c w/ file id from b/c\n'
183
'adding d w/ file id from b/d\n',
186
new_tree = new_tree.bzrdir.open_workingtree()
187
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('c'))
188
self.assertEqual(base_tree.path2id('b/d'), new_tree.path2id('d'))
190
def test_add_dry_run(self):
191
"""Test a dry run add, make sure nothing is added."""
192
wt = self.make_branch_and_tree('.')
193
self.build_tree(['inertiatic/', 'inertiatic/esp'])
194
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
195
self.run_bzr('add --dry-run')
196
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
198
def test_add_control_dir(self):
199
"""The control dir and its content should be refused."""
200
self.make_branch_and_tree('.')
201
err = self.run_bzr('add .bzr', retcode=3)[1]
202
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
203
err = self.run_bzr('add .bzr/README', retcode=3)[1]
204
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
205
self.build_tree(['.bzr/crescent'])
206
err = self.run_bzr('add .bzr/crescent', retcode=3)[1]
207
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
209
def test_add_via_symlink(self):
210
self.requireFeature(tests.SymlinkFeature)
211
self.make_branch_and_tree('source')
212
self.build_tree(['source/top.txt'])
213
os.symlink('source', 'link')
214
out = self.run_bzr(['add', 'link/top.txt'])[0]
215
self.assertEquals(out, 'adding top.txt\n')
217
def test_add_symlink_to_abspath(self):
218
self.requireFeature(tests.SymlinkFeature)
219
self.make_branch_and_tree('tree')
220
os.symlink(osutils.abspath('target'), 'tree/link')
221
out = self.run_bzr(['add', 'tree/link'])[0]
222
self.assertEquals(out, 'adding link\n')
224
def test_add_not_child(self):
225
# https://bugs.launchpad.net/bzr/+bug/98735
226
sr = script.ScriptRunner()
227
self.make_branch_and_tree('tree1')
228
self.make_branch_and_tree('tree2')
229
self.build_tree(['tree1/a', 'tree2/b'])
230
sr.run_script(self, '''
231
$ bzr add tree1/a tree2/b
232
2>bzr: ERROR: Path "...tree2/b" is not a child of path "...tree1"