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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests of the 'bzr add' command."""
22
from bzrlib.tests.blackbox import ExternalBase
23
from bzrlib.tests.test_win32utils import NeedsGlobExpansionFeature
26
class TestAdd(ExternalBase):
28
def test_add_reports(self):
29
"""add command prints the names of added files."""
30
tree = self.make_branch_and_tree('.')
31
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt', 'CVS'])
32
self.build_tree_contents([('.bzrignore', 'CVS\n')])
33
out = self.run_bzr('add')[0]
34
# the ordering is not defined at the moment
35
results = sorted(out.rstrip('\n').split('\n'))
36
self.assertEquals(['If you wish to add some of these files, please'\
43
'ignored 1 file(s).'],
45
out = self.run_bzr('add -v')[0]
46
results = sorted(out.rstrip('\n').split('\n'))
47
self.assertEquals(['If you wish to add some of these files, please'\
49
'ignored CVS matching "CVS"'],
52
def test_add_quiet_is(self):
53
"""add -q does not print the names of added files."""
54
tree = self.make_branch_and_tree('.')
55
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
56
out = self.run_bzr('add -q')[0]
57
# the ordering is not defined at the moment
58
results = sorted(out.rstrip('\n').split('\n'))
59
self.assertEquals([''], results)
61
def test_add_in_unversioned(self):
62
"""Try to add a file in an unversioned directory.
64
"bzr add" should add the parent(s) as necessary.
66
tree = self.make_branch_and_tree('.')
67
self.build_tree(['inertiatic/', 'inertiatic/esp'])
68
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
69
self.run_bzr('add inertiatic/esp')
70
self.assertEquals(self.run_bzr('unknowns')[0], '')
72
# Multiple unversioned parents
73
self.build_tree(['veil/', 'veil/cerpin/', 'veil/cerpin/taxt'])
74
self.assertEquals(self.run_bzr('unknowns')[0], 'veil\n')
75
self.run_bzr('add veil/cerpin/taxt')
76
self.assertEquals(self.run_bzr('unknowns')[0], '')
78
# Check whacky paths work
79
self.build_tree(['cicatriz/', 'cicatriz/esp'])
80
self.assertEquals(self.run_bzr('unknowns')[0], 'cicatriz\n')
81
self.run_bzr('add inertiatic/../cicatriz/esp')
82
self.assertEquals(self.run_bzr('unknowns')[0], '')
84
def test_add_in_versioned(self):
85
"""Try to add a file in a versioned directory.
87
"bzr add" should do this happily.
89
tree = self.make_branch_and_tree('.')
90
self.build_tree(['inertiatic/', 'inertiatic/esp'])
91
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic\n')
92
self.run_bzr('add --no-recurse inertiatic')
93
self.assertEquals(self.run_bzr('unknowns')[0], 'inertiatic/esp\n')
94
self.run_bzr('add inertiatic/esp')
95
self.assertEquals(self.run_bzr('unknowns')[0], '')
97
def test_subdir_add(self):
98
"""Add in subdirectory should add only things from there down"""
99
from bzrlib.workingtree import WorkingTree
101
eq = self.assertEqual
102
ass = self.assertTrue
105
t = self.make_branch_and_tree('.')
107
self.build_tree(['src/', 'README'])
109
eq(sorted(t.unknowns()),
112
self.run_bzr('add src')
114
self.build_tree(['src/foo.c'])
116
# add with no arguments in a subdirectory gets only files below that
120
self.assertEquals(self.run_bzr('unknowns')[0], 'README\n')
121
# reopen to see the new changes
122
t = t.bzrdir.open_workingtree()
123
versioned = [path for path, entry in t.iter_entries_by_dir()]
124
self.assertEquals(versioned,
125
['', 'src', 'src/foo.c'])
127
# add from the parent directory should pick up all file names
130
self.assertEquals(self.run_bzr('unknowns')[0], '')
131
self.run_bzr('check')
133
def test_add_missing(self):
134
"""bzr add foo where foo is missing should error."""
135
self.make_branch_and_tree('.')
136
self.run_bzr('add missing-file', retcode=3)
138
def test_add_from(self):
139
base_tree = self.make_branch_and_tree('base')
140
self.build_tree(['base/a', 'base/b/', 'base/b/c'])
141
base_tree.add(['a', 'b', 'b/c'])
142
base_tree.commit('foo')
144
new_tree = self.make_branch_and_tree('new')
145
self.build_tree(['new/a', 'new/b/', 'new/b/c', 'd'])
148
out, err = self.run_bzr('add --file-ids-from ../base')
149
self.assertEqual('', err)
150
self.assertEqualDiff('adding a w/ file id from a\n'
151
'adding b w/ file id from b\n'
152
'adding b/c w/ file id from b/c\n'
155
new_tree = new_tree.bzrdir.open_workingtree()
156
self.assertEqual(base_tree.path2id('a'), new_tree.path2id('a'))
157
self.assertEqual(base_tree.path2id('b'), new_tree.path2id('b'))
158
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('b/c'))
160
def test_add_from_subdir(self):
161
base_tree = self.make_branch_and_tree('base')
162
self.build_tree(['base/a', 'base/b/', 'base/b/c', 'base/b/d'])
163
base_tree.add(['a', 'b', 'b/c', 'b/d'])
164
base_tree.commit('foo')
166
new_tree = self.make_branch_and_tree('new')
167
self.build_tree(['new/c', 'new/d'])
170
out, err = self.run_bzr('add --file-ids-from ../base/b')
171
self.assertEqual('', err)
172
self.assertEqualDiff('adding c w/ file id from b/c\n'
173
'adding d w/ file id from b/d\n'
177
new_tree = new_tree.bzrdir.open_workingtree()
178
self.assertEqual(base_tree.path2id('b/c'), new_tree.path2id('c'))
179
self.assertEqual(base_tree.path2id('b/d'), new_tree.path2id('d'))
181
def test_add_dry_run(self):
182
"""Test a dry run add, make sure nothing is added."""
183
wt = self.make_branch_and_tree('.')
184
self.build_tree(['inertiatic/', 'inertiatic/esp'])
185
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
186
self.run_bzr('add --dry-run')
187
self.assertEqual(list(wt.unknowns()), ['inertiatic'])
189
def test_add_control_dir(self):
190
"""The control dir and its content should be refused."""
191
self.make_branch_and_tree('.')
192
err = self.run_bzr('add .bzr', retcode=3)[1]
193
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
194
err = self.run_bzr('add .bzr/README', retcode=3)[1]
195
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
196
self.build_tree(['.bzr/crescent'])
197
err = self.run_bzr('add .bzr/crescent', retcode=3)[1]
198
self.assertContainsRe(err, r'ERROR:.*\.bzr.*control file')
200
def test_add_with_wildcards(self):
201
self.requireFeature(NeedsGlobExpansionFeature)
202
self.make_branch_and_tree('.')
203
self.build_tree(['a1', 'a2', 'b', 'c33'])
204
self.run_bzr(['add', 'a?', 'c*'])
205
self.assertEquals(self.run_bzr('unknowns')[0], 'b\n')
207
def test_add_with_wildcards_unicode(self):
208
self.requireFeature(NeedsGlobExpansionFeature)
209
self.make_branch_and_tree('.')
210
self.build_tree([u'\u1234A', u'\u1235A', u'\u1235AA', 'cc'])
211
self.run_bzr(['add', u'\u1234?', u'\u1235*'])
212
self.assertEquals(self.run_bzr('unknowns')[0], 'cc\n')