1
# Copyright (C) 2006, 2007, 2009, 2010, 2011 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 (
29
from bzrlib.tests.scenarios import load_tests_apply_scenarios
32
load_tests = load_tests_apply_scenarios
35
class TestAdd(tests.TestCaseWithTransport):
38
('pre-views', {'branch_tree_format': 'pack-0.92'}),
39
('view-aware', {'branch_tree_format': '2a'}),
42
def make_branch_and_tree(self, dir):
43
return super(TestAdd, self).make_branch_and_tree(
44
dir, format=self.branch_tree_format)
46
def test_add_reports(self):
47
"""add command prints the names of added files."""
48
tree = self.make_branch_and_tree('.')
49
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt', 'CVS'])
50
self.build_tree_contents([('.bzrignore', 'CVS\n')])
51
out = self.run_bzr('add')[0]
52
# the ordering is not defined at the moment
53
results = sorted(out.rstrip('\n').split('\n'))
54
self.assertEquals(['adding .bzrignore',
59
out = self.run_bzr('add -v')[0]
60
results = sorted(out.rstrip('\n').split('\n'))
61
self.assertEquals(['ignored CVS matching "CVS"'],
64
def test_add_quiet_is(self):
65
"""add -q does not print the names of added files."""
66
tree = self.make_branch_and_tree('.')
67
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
68
out = self.run_bzr('add -q')[0]
69
# the ordering is not defined at the moment
70
results = sorted(out.rstrip('\n').split('\n'))
71
self.assertEquals([''], results)
73
def test_add_in_unversioned(self):
74
"""Try to add a file in an unversioned directory.
76
"bzr add" should add the parent(s) as necessary.
78
tree = self.make_branch_and_tree('.')
79
self.build_tree(['inertiatic/', 'inertiatic/esp'])
80
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
81
self.run_bzr('add inertiatic/esp')
82
self.assertEquals(self.run_bzr('unknowns')[0], '')
84
# Multiple unversioned parents
85
self.build_tree(['veil/', 'veil/cerpin/', 'veil/cerpin/taxt'])
86
self.assertEquals(self.run_bzr('unknowns')[0], 'veil\n')
87
self.run_bzr('add veil/cerpin/taxt')
88
self.assertEquals(self.run_bzr('unknowns')[0], '')
90
# Check whacky paths work
91
self.build_tree(['cicatriz/', 'cicatriz/esp'])
92
self.assertEquals(self.run_bzr('unknowns')[0], 'cicatriz\n')
93
self.run_bzr('add inertiatic/../cicatriz/esp')
94
self.assertEquals(self.run_bzr('unknowns')[0], '')
96
def test_add_in_versioned(self):
97
"""Try to add a file in a versioned directory.
99
"bzr add" should do this happily.
101
tree = self.make_branch_and_tree('.')
102
self.build_tree(['inertiatic/', 'inertiatic/esp'])
103
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
104
self.run_bzr('add --no-recurse inertiatic')
105
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic/esp\n')
106
self.run_bzr('add inertiatic/esp')
107
self.assertEquals(self.run_bzr('unknowns')[0], '')
109
def test_subdir_add(self):
110
"""Add in subdirectory should add only things from there down"""
111
from bzrlib.workingtree import WorkingTree
113
eq = self.assertEqual
114
ass = self.assertTrue
116
t = self.make_branch_and_tree('.')
118
self.build_tree(['src/', 'README'])
120
eq(sorted(t.unknowns()),
123
self.run_bzr('add src')
125
self.build_tree(['src/foo.c'])
127
# add with no arguments in a subdirectory gets only files below that
129
self.run_bzr('add', working_dir='src')
130
self.assertEquals('README\n',
131
self.run_bzr('unknowns', working_dir='src')[0])
132
# reopen to see the new changes
133
t = t.bzrdir.open_workingtree('src')
134
versioned = [path for path, entry in t.iter_entries_by_dir()]
135
self.assertEquals(versioned, ['', 'src', 'src/foo.c'])
137
# add from the parent directory should pick up all file names
139
self.assertEquals(self.run_bzr('unknowns')[0], '')
140
self.run_bzr('check')
142
def test_add_missing(self):
143
"""bzr add foo where foo is missing should error."""
144
self.make_branch_and_tree('.')
145
self.run_bzr('add missing-file', retcode=3)
147
def test_add_from(self):
148
base_tree = self.make_branch_and_tree('base')
149
self.build_tree(['base/a', 'base/b/', 'base/b/c'])
150
base_tree.add(['a', 'b', 'b/c'])
151
base_tree.commit('foo')
153
new_tree = self.make_branch_and_tree('new')
154
self.build_tree(['new/a', 'new/b/', 'new/b/c', 'd'])
157
out, err = self.run_bzr('add --file-ids-from ../base')
158
self.assertEqual('', err)
159
self.assertEqualDiff('adding a w/ file id from a\n'
160
'adding b w/ file id from b\n'
161
'adding b/c w/ file id from b/c\n',
163
new_tree = new_tree.bzrdir.open_workingtree()
164
self.assertEqual(base_tree.path2id('a'), new_tree.path2id('a'))
165
self.assertEqual(base_tree.path2id('b'), new_tree.path2id('b'))
166
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('b/c'))
168
def test_add_from_subdir(self):
169
base_tree = self.make_branch_and_tree('base')
170
self.build_tree(['base/a', 'base/b/', 'base/b/c', 'base/b/d'])
171
base_tree.add(['a', 'b', 'b/c', 'b/d'])
172
base_tree.commit('foo')
174
new_tree = self.make_branch_and_tree('new')
175
self.build_tree(['new/c', 'new/d'])
178
out, err = self.run_bzr('add --file-ids-from ../base/b')
179
self.assertEqual('', err)
180
self.assertEqualDiff('adding c w/ file id from b/c\n'
181
'adding d w/ file id from b/d\n',
184
new_tree = new_tree.bzrdir.open_workingtree()
185
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('c'))
186
self.assertEqual(base_tree.path2id('b/d'), new_tree.path2id('d'))
188
def test_add_dry_run(self):
189
"""Test a dry run add, make sure nothing is added."""
190
wt = self.make_branch_and_tree('.')
191
self.build_tree(['inertiatic/', 'inertiatic/esp'])
192
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
193
self.run_bzr('add --dry-run')
194
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
196
def test_add_control_dir(self):
197
"""The control dir and its content should be refused."""
198
self.make_branch_and_tree('.')
199
err = self.run_bzr('add .bzr', retcode=3)[1]
200
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
201
err = self.run_bzr('add .bzr/README', retcode=3)[1]
202
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
203
self.build_tree(['.bzr/crescent'])
204
err = self.run_bzr('add .bzr/crescent', retcode=3)[1]
205
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
207
def test_add_via_symlink(self):
208
self.requireFeature(tests.SymlinkFeature)
209
self.make_branch_and_tree('source')
210
self.build_tree(['source/top.txt'])
211
os.symlink('source', 'link')
212
out = self.run_bzr(['add', 'link/top.txt'])[0]
213
self.assertEquals(out, 'adding top.txt\n')
215
def test_add_symlink_to_abspath(self):
216
self.requireFeature(tests.SymlinkFeature)
217
self.make_branch_and_tree('tree')
218
os.symlink(osutils.abspath('target'), 'tree/link')
219
out = self.run_bzr(['add', 'tree/link'])[0]
220
self.assertEquals(out, 'adding link\n')
222
def test_add_not_child(self):
223
# https://bugs.launchpad.net/bzr/+bug/98735
224
sr = script.ScriptRunner()
225
self.make_branch_and_tree('tree1')
226
self.make_branch_and_tree('tree2')
227
self.build_tree(['tree1/a', 'tree2/b'])
228
sr.run_script(self, '''
229
$ bzr add tree1/a tree2/b
230
2>bzr: ERROR: Path "...tree2/b" is not a child of path "...tree1"
233
def test_add_multiple_files_in_unicode_cwd(self):
234
"""Adding multiple files in a non-ascii cwd, see lp:686611"""
235
self.requireFeature(tests.UnicodeFilename)
236
self.make_branch_and_tree(u"\xA7")
237
self.build_tree([u"\xA7/a", u"\xA7/b"])
238
out, err = self.run_bzr(["add", "a", "b"], working_dir=u"\xA7")
239
self.assertEquals(out, "adding a\n" "adding b\n")
240
self.assertEquals(err, "")