1
# Copyright (C) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from __future__ import absolute_import
21
import unicodedata as ud
23
from bzrlib import tests, osutils
24
from bzrlib._termcolor import color_string, FG
26
from bzrlib.tests.features import (
28
UnicodeFilenameFeature,
31
# NOTE: As bzr-grep optimizes PATTERN search to -F/--fixed-string
32
# for patterns that are not alphanumeric+whitespace, we test grep
33
# specfically with patterns that have special characters so that
34
# regex path is tested. alphanumeric patterns test the -F path.
36
class GrepTestBase(tests.TestCaseWithTransport):
37
"""Base class for testing grep.
39
Provides support methods for creating directory and file revisions.
41
_reflags = re.MULTILINE|re.DOTALL
43
def _mk_file(self, path, line_prefix, total_lines, versioned):
45
for i in range(total_lines):
46
text += line_prefix + str(i+1) + "\n"
48
open(path, 'w').write(text)
50
self.run_bzr(['add', path])
51
self.run_bzr(['ci', '-m', '"' + path + '"'])
53
def _update_file(self, path, text, checkin=True):
54
"""append text to file 'path' and check it in"""
55
open(path, 'a').write(text)
57
self.run_bzr(['ci', path, '-m', '"' + path + '"'])
59
def _mk_unknown_file(self, path, line_prefix='line', total_lines=10):
60
self._mk_file(path, line_prefix, total_lines, versioned=False)
62
def _mk_versioned_file(self, path, line_prefix='line', total_lines=10):
63
self._mk_file(path, line_prefix, total_lines, versioned=True)
65
def _mk_dir(self, path, versioned):
68
self.run_bzr(['add', path])
69
self.run_bzr(['ci', '-m', '"' + path + '"'])
71
def _mk_unknown_dir(self, path):
72
self._mk_dir(path, versioned=False)
74
def _mk_versioned_dir(self, path):
75
self._mk_dir(path, versioned=True)
78
class TestGrep(GrepTestBase):
79
"""Core functional tests for grep."""
81
def test_basic_unknown_file(self):
82
"""Search for pattern in specfic file.
84
If specified file is unknown, grep it anyway."""
86
self.make_branch_and_tree(wd)
88
self._mk_unknown_file('file0.txt')
90
out, err = self.run_bzr(['grep', 'line1', 'file0.txt'])
91
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
92
self.assertEqual(len(out.splitlines()), 2) # finds line1 and line10
94
out, err = self.run_bzr(['grep', 'line\d+', 'file0.txt'])
95
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
96
self.assertEqual(len(out.splitlines()), 10)
98
# unknown file is not grepped unless explicitely specified
99
out, err = self.run_bzr(['grep', 'line1'])
100
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
101
self.assertEqual(len(out.splitlines()), 0)
103
# unknown file is not grepped unless explicitely specified
104
out, err = self.run_bzr(['grep', 'line1$'])
105
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
106
self.assertEqual(len(out.splitlines()), 0)
108
def test_ver_basic_file(self):
109
"""(versioned) Search for pattern in specfic file.
112
self.make_branch_and_tree(wd)
114
self._mk_versioned_file('file0.txt')
116
out, err = self.run_bzr(['grep', '-r', '1', 'line1', 'file0.txt'])
117
self.assertContainsRe(out, "file0.txt~1:line1", flags=TestGrep._reflags)
118
self.assertEqual(len(out.splitlines()), 2) # finds line1 and line10
120
out, err = self.run_bzr(['grep', '-r', '1', 'line[0-9]$', 'file0.txt'])
121
self.assertContainsRe(out, "file0.txt~1:line1", flags=TestGrep._reflags)
122
self.assertEqual(len(out.splitlines()), 9)
124
# finds all the lines
125
out, err = self.run_bzr(['grep', '-r', '1', 'line[0-9]', 'file0.txt'])
126
self.assertContainsRe(out, "file0.txt~1:line1", flags=TestGrep._reflags)
127
self.assertEqual(len(out.splitlines()), 10)
129
def test_wtree_basic_file(self):
130
"""(wtree) Search for pattern in specfic file.
133
self.make_branch_and_tree(wd)
135
self._mk_versioned_file('file0.txt')
136
self._update_file('file0.txt', 'ABC\n', checkin=False)
138
out, err = self.run_bzr(['grep', 'ABC', 'file0.txt'])
139
self.assertContainsRe(out, "file0.txt:ABC", flags=TestGrep._reflags)
140
self.assertEqual(len(out.splitlines()), 1)
142
out, err = self.run_bzr(['grep', '[A-Z]{3}', 'file0.txt'])
143
self.assertContainsRe(out, "file0.txt:ABC", flags=TestGrep._reflags)
144
self.assertEqual(len(out.splitlines()), 1)
146
out, err = self.run_bzr(['grep', '-r', 'last:1', 'ABC', 'file0.txt'])
147
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
148
self.assertEqual(len(out.splitlines()), 0)
150
out, err = self.run_bzr(['grep', '-r', 'last:1', '[A-Z]{3}', 'file0.txt'])
151
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
152
self.assertEqual(len(out.splitlines()), 0)
154
def test_ver_basic_include(self):
155
"""(versioned) Ensure that -I flag is respected.
158
self.make_branch_and_tree(wd)
160
self._mk_versioned_file('file0.aa')
161
self._mk_versioned_file('file0.bb')
162
self._mk_versioned_file('file0.cc')
164
out, err = self.run_bzr(['grep', '-r', 'last:1',
165
'--include', '*.aa', '--include', '*.bb', 'line1'])
166
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
167
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
168
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
169
# two lines each (line1, line10) from file0.aa and file0.bb
170
self.assertEqual(len(out.splitlines()), 4)
172
out, err = self.run_bzr(['grep', '-r', 'last:1',
173
'--include', '*.aa', '--include', '*.bb', 'line1$'])
174
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
175
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
176
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
177
# one lines each (line1) from file0.aa and file0.bb
178
self.assertEqual(len(out.splitlines()), 2)
180
out, err = self.run_bzr(['grep', '-r', 'last:1',
181
'-I', '*.aa', '-I', '*.bb', 'line1'])
182
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
183
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
184
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
185
# two lines each (line1, line10) from file0.aa and file0.bb
186
self.assertEqual(len(out.splitlines()), 4)
188
out, err = self.run_bzr(['grep', '-r', 'last:1',
189
'-I', '*.aa', '-I', '*.bb', 'line1$'])
190
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
191
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
192
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
193
# one lines each (line1) from file0.aa and file0.bb
194
self.assertEqual(len(out.splitlines()), 2)
196
def test_wtree_basic_include(self):
197
"""(wtree) Ensure that --include flag is respected.
200
self.make_branch_and_tree(wd)
202
self._mk_versioned_file('file0.aa')
203
self._mk_versioned_file('file0.bb')
204
self._mk_versioned_file('file0.cc')
206
out, err = self.run_bzr(['grep', '--include', '*.aa',
207
'--include', '*.bb', 'line1'])
208
self.assertContainsRe(out, "file0.aa:line1", flags=TestGrep._reflags)
209
self.assertContainsRe(out, "file0.bb:line1", flags=TestGrep._reflags)
210
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
211
# two lines each (line1, line10) from file0.aa and file0.bb
212
self.assertEqual(len(out.splitlines()), 4)
214
out, err = self.run_bzr(['grep', '--include', '*.aa',
215
'--include', '*.bb', 'line1$'])
216
self.assertContainsRe(out, "file0.aa:line1", flags=TestGrep._reflags)
217
self.assertContainsRe(out, "file0.bb:line1", flags=TestGrep._reflags)
218
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
219
# one line each (line1) from file0.aa and file0.bb
220
self.assertEqual(len(out.splitlines()), 2)
222
def test_ver_basic_exclude(self):
223
"""(versioned) Ensure that --exclude flag is respected.
226
self.make_branch_and_tree(wd)
228
self._mk_versioned_file('file0.aa')
229
self._mk_versioned_file('file0.bb')
230
self._mk_versioned_file('file0.cc')
232
out, err = self.run_bzr(['grep', '-r', 'last:1',
233
'--exclude', '*.cc', 'line1'])
234
self.assertContainsRe(out, "file0.aa~.:line1$", flags=TestGrep._reflags)
235
self.assertContainsRe(out, "file0.bb~.:line1$", flags=TestGrep._reflags)
236
self.assertContainsRe(out, "file0.aa~.:line10", flags=TestGrep._reflags)
237
self.assertContainsRe(out, "file0.bb~.:line10", flags=TestGrep._reflags)
238
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
239
# two lines each (line1, line10) from file0.aa and file0.bb
240
self.assertEqual(len(out.splitlines()), 4)
242
out, err = self.run_bzr(['grep', '-r', 'last:1',
243
'--exclude', '*.cc', 'line1$'])
244
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
245
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
246
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
247
# one line each (line1) from file0.aa and file0.bb
248
self.assertEqual(len(out.splitlines()), 2)
250
out, err = self.run_bzr(['grep', '-r', 'last:1',
251
'-X', '*.cc', 'line1'])
252
self.assertContainsRe(out, "file0.aa~.:line1", flags=TestGrep._reflags)
253
self.assertContainsRe(out, "file0.bb~.:line1", flags=TestGrep._reflags)
254
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
255
# two lines each (line1, line10) from file0.aa and file0.bb
256
self.assertEqual(len(out.splitlines()), 4)
258
def test_wtree_basic_exclude(self):
259
"""(wtree) Ensure that --exclude flag is respected.
262
self.make_branch_and_tree(wd)
264
self._mk_versioned_file('file0.aa')
265
self._mk_versioned_file('file0.bb')
266
self._mk_versioned_file('file0.cc')
268
out, err = self.run_bzr(['grep', '--exclude', '*.cc', 'line1'])
269
self.assertContainsRe(out, "file0.aa:line1", flags=TestGrep._reflags)
270
self.assertContainsRe(out, "file0.bb:line1", flags=TestGrep._reflags)
271
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
272
# two lines each (line1, line10) from file0.aa and file0.bb
273
self.assertEqual(len(out.splitlines()), 4)
275
out, err = self.run_bzr(['grep', '--exclude', '*.cc', 'lin.1$'])
276
self.assertContainsRe(out, "file0.aa:line1", flags=TestGrep._reflags)
277
self.assertContainsRe(out, "file0.bb:line1", flags=TestGrep._reflags)
278
self.assertNotContainsRe(out, "file0.cc", flags=TestGrep._reflags)
279
# one line each (line1) from file0.aa and file0.bb
280
self.assertEqual(len(out.splitlines()), 2)
282
def test_ver_multiple_files(self):
283
"""(versioned) Search for pattern in multiple files.
286
self.make_branch_and_tree(wd)
288
self._mk_versioned_file('file0.txt', total_lines=2)
289
self._mk_versioned_file('file1.txt', total_lines=2)
290
self._mk_versioned_file('file2.txt', total_lines=2)
292
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line[1-2]$'])
293
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
294
self.assertContainsRe(out, "file0.txt~.:line2", flags=TestGrep._reflags)
295
self.assertContainsRe(out, "file1.txt~.:line1", flags=TestGrep._reflags)
296
self.assertContainsRe(out, "file1.txt~.:line2", flags=TestGrep._reflags)
297
self.assertContainsRe(out, "file2.txt~.:line1", flags=TestGrep._reflags)
298
self.assertContainsRe(out, "file2.txt~.:line2", flags=TestGrep._reflags)
299
self.assertEqual(len(out.splitlines()), 6)
301
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line'])
302
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
303
self.assertContainsRe(out, "file0.txt~.:line2", flags=TestGrep._reflags)
304
self.assertContainsRe(out, "file1.txt~.:line1", flags=TestGrep._reflags)
305
self.assertContainsRe(out, "file1.txt~.:line2", flags=TestGrep._reflags)
306
self.assertContainsRe(out, "file2.txt~.:line1", flags=TestGrep._reflags)
307
self.assertContainsRe(out, "file2.txt~.:line2", flags=TestGrep._reflags)
308
self.assertEqual(len(out.splitlines()), 6)
310
def test_multiple_wtree_files(self):
311
"""(wtree) Search for pattern in multiple files in working tree.
314
self.make_branch_and_tree(wd)
316
self._mk_versioned_file('file0.txt', total_lines=2)
317
self._mk_versioned_file('file1.txt', total_lines=2)
318
self._mk_versioned_file('file2.txt', total_lines=2)
319
self._update_file('file0.txt', 'HELLO\n', checkin=False)
320
self._update_file('file1.txt', 'HELLO\n', checkin=True)
321
self._update_file('file2.txt', 'HELLO\n', checkin=False)
323
out, err = self.run_bzr(['grep', 'HELLO',
324
'file0.txt', 'file1.txt', 'file2.txt'])
326
self.assertContainsRe(out, "file0.txt:HELLO", flags=TestGrep._reflags)
327
self.assertContainsRe(out, "file1.txt:HELLO", flags=TestGrep._reflags)
328
self.assertContainsRe(out, "file2.txt:HELLO", flags=TestGrep._reflags)
329
self.assertEqual(len(out.splitlines()), 3)
331
out, err = self.run_bzr(['grep', 'HELLO', '-r', 'last:1',
332
'file0.txt', 'file1.txt', 'file2.txt'])
334
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
335
self.assertContainsRe(out, "file1.txt~.:HELLO", flags=TestGrep._reflags)
336
self.assertNotContainsRe(out, "file2.txt", flags=TestGrep._reflags)
337
self.assertEqual(len(out.splitlines()), 1)
339
out, err = self.run_bzr(['grep', 'HE..O',
340
'file0.txt', 'file1.txt', 'file2.txt'])
342
self.assertContainsRe(out, "file0.txt:HELLO", flags=TestGrep._reflags)
343
self.assertContainsRe(out, "file1.txt:HELLO", flags=TestGrep._reflags)
344
self.assertContainsRe(out, "file2.txt:HELLO", flags=TestGrep._reflags)
345
self.assertEqual(len(out.splitlines()), 3)
347
out, err = self.run_bzr(['grep', 'HE..O', '-r', 'last:1',
348
'file0.txt', 'file1.txt', 'file2.txt'])
350
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
351
self.assertContainsRe(out, "file1.txt~.:HELLO", flags=TestGrep._reflags)
352
self.assertNotContainsRe(out, "file2.txt", flags=TestGrep._reflags)
353
self.assertEqual(len(out.splitlines()), 1)
355
def test_ver_null_option(self):
356
"""(versioned) --null option should use NUL instead of newline.
359
self.make_branch_and_tree(wd)
361
self._mk_versioned_file('file0.txt', total_lines=3)
363
nref = ud.normalize(u'NFC', u"file0.txt~1:line1\0file0.txt~1:line2\0file0.txt~1:line3\0")
365
out, err = self.run_bzr(['grep', '-r', 'last:1', '--null', 'line[1-3]'])
366
nout = ud.normalize(u'NFC', out.decode('utf-8', 'ignore'))
367
self.assertEqual(nout, nref)
368
self.assertEqual(len(out.splitlines()), 1)
370
out, err = self.run_bzr(['grep', '-r', 'last:1', '-Z', 'line[1-3]'])
371
nout = ud.normalize(u'NFC', out.decode('utf-8', 'ignore'))
372
self.assertEqual(nout, nref)
373
self.assertEqual(len(out.splitlines()), 1)
375
out, err = self.run_bzr(['grep', '-r', 'last:1', '--null', 'line'])
376
nout = ud.normalize(u'NFC', out.decode('utf-8', 'ignore'))
377
self.assertEqual(nout, nref)
378
self.assertEqual(len(out.splitlines()), 1)
380
def test_wtree_null_option(self):
381
"""(wtree) --null option should use NUL instead of newline.
384
self.make_branch_and_tree(wd)
386
self._mk_versioned_file('file0.txt', total_lines=3)
388
out, err = self.run_bzr(['grep', '--null', 'line[1-3]'])
389
self.assertEqual(out, "file0.txt:line1\0file0.txt:line2\0file0.txt:line3\0")
390
self.assertEqual(len(out.splitlines()), 1)
392
out, err = self.run_bzr(['grep', '-Z', 'line[1-3]'])
393
self.assertEqual(out, "file0.txt:line1\0file0.txt:line2\0file0.txt:line3\0")
394
self.assertEqual(len(out.splitlines()), 1)
396
out, err = self.run_bzr(['grep', '-Z', 'line'])
397
self.assertEqual(out, "file0.txt:line1\0file0.txt:line2\0file0.txt:line3\0")
398
self.assertEqual(len(out.splitlines()), 1)
400
def test_versioned_file_in_dir_no_recursive(self):
401
"""(versioned) Should not recurse with --no-recursive"""
403
self.make_branch_and_tree(wd)
405
self._mk_versioned_file('fileX.txt', line_prefix='lin')
406
self._mk_versioned_dir('dir0')
407
self._mk_versioned_file('dir0/file0.txt')
409
out, err = self.run_bzr(['grep', '-r', 'last:1', '--no-recursive', 'line1'])
410
self.assertNotContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
411
self.assertEqual(len(out.splitlines()), 0)
413
out, err = self.run_bzr(['grep', '-r', 'last:1', '--no-recursive', 'line1$'])
414
self.assertNotContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
415
self.assertEqual(len(out.splitlines()), 0)
417
def test_wtree_file_in_dir_no_recursive(self):
418
"""(wtree) Should not recurse with --no-recursive"""
420
self.make_branch_and_tree(wd)
422
self._mk_versioned_file('fileX.txt', line_prefix='lin')
423
self._mk_versioned_dir('dir0')
424
self._mk_versioned_file('dir0/file0.txt')
426
out, err = self.run_bzr(['grep', '--no-recursive', 'line1'])
427
self.assertNotContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
428
self.assertEqual(len(out.splitlines()), 0)
430
out, err = self.run_bzr(['grep', '--no-recursive', 'lin.1'])
431
self.assertNotContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
432
self.assertEqual(len(out.splitlines()), 0)
434
def test_versioned_file_in_dir_recurse(self):
435
"""(versioned) Should recurse by default.
438
self.make_branch_and_tree(wd)
440
self._mk_versioned_dir('dir0')
441
self._mk_versioned_file('dir0/file0.txt')
443
out, err = self.run_bzr(['grep', '-r', '-1', '.i.e1'])
444
self.assertContainsRe(out, "^dir0/file0.txt~.:line1", flags=TestGrep._reflags)
445
# find line1 and line10
446
self.assertEqual(len(out.splitlines()), 2)
448
out, err = self.run_bzr(['grep', '-r', '-1', 'line1'])
449
self.assertContainsRe(out, "^dir0/file0.txt~.:line1", flags=TestGrep._reflags)
450
# find line1 and line10
451
self.assertEqual(len(out.splitlines()), 2)
453
def test_wtree_file_in_dir_recurse(self):
454
"""(wtree) Should recurse by default.
457
self.make_branch_and_tree(wd)
459
self._mk_versioned_dir('dir0')
460
self._mk_versioned_file('dir0/file0.txt')
462
out, err = self.run_bzr(['grep', 'line1'])
463
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
464
# find line1 and line10
465
self.assertEqual(len(out.splitlines()), 2)
467
out, err = self.run_bzr(['grep', 'lin.1'])
468
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
469
# find line1 and line10
470
self.assertEqual(len(out.splitlines()), 2)
472
def test_versioned_file_within_dir(self):
473
"""(versioned) Search for pattern while in nested dir.
476
self.make_branch_and_tree(wd)
478
self._mk_versioned_dir('dir0')
479
self._mk_versioned_file('dir0/file0.txt')
482
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line1'])
483
self.assertContainsRe(out, "^file0.txt~.:line1", flags=TestGrep._reflags)
484
# finds line1 and line10
485
self.assertEqual(len(out.splitlines()), 2)
487
out, err = self.run_bzr(['grep', '-r', 'last:1', '.i.e1'])
488
self.assertContainsRe(out, "^file0.txt~.:line1", flags=TestGrep._reflags)
489
# finds line1 and line10
490
self.assertEqual(len(out.splitlines()), 2)
492
def test_versioned_include_file_within_dir(self):
493
"""(versioned) Ensure --include is respected with file within dir.
496
self.make_branch_and_tree(wd)
498
self._mk_versioned_dir('dir0') # revno 1
499
self._mk_versioned_file('dir0/file0.txt') # revno 2
500
self._mk_versioned_file('dir0/file1.aa') # revno 3
501
self._update_file('dir0/file1.aa', 'hello\n') # revno 4
502
self._update_file('dir0/file0.txt', 'hello\n') # revno 5
505
out, err = self.run_bzr(['grep', '-r', 'last:1',
506
'--include', '*.aa', 'line1'])
507
self.assertContainsRe(out, "^file1.aa~5:line1$", flags=TestGrep._reflags)
508
self.assertContainsRe(out, "^file1.aa~5:line10$", flags=TestGrep._reflags)
509
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
510
# finds line1 and line10
511
self.assertEqual(len(out.splitlines()), 2)
513
out, err = self.run_bzr(['grep', '-r', 'last:2..last:1',
514
'--include', '*.aa', 'line1'])
515
self.assertContainsRe(out, "^file1.aa~4:line1$", flags=TestGrep._reflags)
516
self.assertContainsRe(out, "^file1.aa~4:line10$", flags=TestGrep._reflags)
517
self.assertContainsRe(out, "^file1.aa~5:line1$", flags=TestGrep._reflags)
518
self.assertContainsRe(out, "^file1.aa~5:line10$", flags=TestGrep._reflags)
519
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
520
# finds line1 and line10 over two revisions
521
self.assertEqual(len(out.splitlines()), 4)
523
out, err = self.run_bzr(['grep', '-r', 'last:1',
524
'--include', '*.aa', 'lin.1'])
525
self.assertContainsRe(out, "^file1.aa~5:line1$", flags=TestGrep._reflags)
526
self.assertContainsRe(out, "^file1.aa~5:line10$", flags=TestGrep._reflags)
527
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
528
# finds line1 and line10
529
self.assertEqual(len(out.splitlines()), 2)
531
out, err = self.run_bzr(['grep', '-r', 'last:3..last:1',
532
'--include', '*.aa', 'lin.1'])
533
self.assertContainsRe(out, "^file1.aa~3:line1$", flags=TestGrep._reflags)
534
self.assertContainsRe(out, "^file1.aa~4:line1$", flags=TestGrep._reflags)
535
self.assertContainsRe(out, "^file1.aa~5:line1$", flags=TestGrep._reflags)
536
self.assertContainsRe(out, "^file1.aa~3:line10$", flags=TestGrep._reflags)
537
self.assertContainsRe(out, "^file1.aa~4:line10$", flags=TestGrep._reflags)
538
self.assertContainsRe(out, "^file1.aa~5:line10$", flags=TestGrep._reflags)
539
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
540
# finds line1 and line10 over 3 revisions
541
self.assertEqual(len(out.splitlines()), 6)
543
def test_versioned_exclude_file_within_dir(self):
544
"""(versioned) Ensure --exclude is respected with file within dir.
547
self.make_branch_and_tree(wd)
549
self._mk_versioned_dir('dir0')
550
self._mk_versioned_file('dir0/file0.txt')
551
self._mk_versioned_file('dir0/file1.aa')
554
out, err = self.run_bzr(['grep', '-r', 'last:1',
555
'--exclude', '*.txt', 'line1'])
556
self.assertContainsRe(out, "^file1.aa~.:line1", flags=TestGrep._reflags)
557
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
558
# finds line1 and line10
559
self.assertEqual(len(out.splitlines()), 2)
561
out, err = self.run_bzr(['grep', '-r', 'last:1',
562
'--exclude', '*.txt', 'l[a-z]ne1'])
563
self.assertContainsRe(out, "^file1.aa~.:line1", flags=TestGrep._reflags)
564
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
565
# finds line1 and line10
566
self.assertEqual(len(out.splitlines()), 2)
568
def test_wtree_file_within_dir(self):
569
"""(wtree) Search for pattern while in nested dir.
572
self.make_branch_and_tree(wd)
574
self._mk_versioned_dir('dir0')
575
self._mk_versioned_file('dir0/file0.txt')
578
out, err = self.run_bzr(['grep', 'line1'])
579
self.assertContainsRe(out, "^file0.txt:line1", flags=TestGrep._reflags)
580
# finds line1 and line10
581
self.assertEqual(len(out.splitlines()), 2)
583
out, err = self.run_bzr(['grep', 'l[aeiou]ne1'])
584
self.assertContainsRe(out, "^file0.txt:line1", flags=TestGrep._reflags)
585
# finds line1 and line10
586
self.assertEqual(len(out.splitlines()), 2)
588
def test_wtree_include_file_within_dir(self):
589
"""(wtree) Ensure --include is respected with file within dir.
592
self.make_branch_and_tree(wd)
594
self._mk_versioned_dir('dir0')
595
self._mk_versioned_file('dir0/file0.txt')
596
self._mk_versioned_file('dir0/file1.aa')
599
out, err = self.run_bzr(['grep', '--include', '*.aa', 'line1'])
600
self.assertContainsRe(out, "^file1.aa:line1", flags=TestGrep._reflags)
601
# finds line1 and line10
602
self.assertEqual(len(out.splitlines()), 2)
604
out, err = self.run_bzr(['grep', '--include', '*.aa', 'l[ixn]ne1'])
605
self.assertContainsRe(out, "^file1.aa:line1", flags=TestGrep._reflags)
606
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
607
# finds line1 and line10
608
self.assertEqual(len(out.splitlines()), 2)
610
def test_wtree_exclude_file_within_dir(self):
611
"""(wtree) Ensure --exclude is respected with file within dir.
614
self.make_branch_and_tree(wd)
616
self._mk_versioned_dir('dir0')
617
self._mk_versioned_file('dir0/file0.txt')
618
self._mk_versioned_file('dir0/file1.aa')
621
out, err = self.run_bzr(['grep', '--exclude', '*.txt', 'li.e1'])
622
self.assertContainsRe(out, "^file1.aa:line1$", flags=TestGrep._reflags)
623
self.assertContainsRe(out, "^file1.aa:line10$", flags=TestGrep._reflags)
624
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
625
# finds line1 and line10
626
self.assertEqual(len(out.splitlines()), 2)
628
out, err = self.run_bzr(['grep', '--exclude', '*.txt', 'line1'])
629
self.assertContainsRe(out, "^file1.aa:line1$", flags=TestGrep._reflags)
630
self.assertContainsRe(out, "^file1.aa:line10$", flags=TestGrep._reflags)
631
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
632
# finds line1 and line10
633
self.assertEqual(len(out.splitlines()), 2)
635
def test_versioned_include_from_outside_dir(self):
636
"""(versioned) Ensure --include is respected during recursive search.
639
self.make_branch_and_tree(wd)
642
self._mk_versioned_dir('dir0')
643
self._mk_versioned_file('dir0/file0.aa')
645
self._mk_versioned_dir('dir1')
646
self._mk_versioned_file('dir1/file1.bb')
648
self._mk_versioned_dir('dir2')
649
self._mk_versioned_file('dir2/file2.cc')
651
out, err = self.run_bzr(['grep', '-r', 'last:1',
652
'--include', '*.aa', '--include', '*.bb', 'l..e1'])
653
self.assertContainsRe(out, "^dir0/file0.aa~.:line1$", flags=TestGrep._reflags)
654
self.assertContainsRe(out, "^dir1/file1.bb~.:line1$", flags=TestGrep._reflags)
655
self.assertContainsRe(out, "^dir0/file0.aa~.:line10$", flags=TestGrep._reflags)
656
self.assertContainsRe(out, "^dir1/file1.bb~.:line10$", flags=TestGrep._reflags)
657
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
658
# finds line1 and line10
659
self.assertEqual(len(out.splitlines()), 4)
661
out, err = self.run_bzr(['grep', '-r', 'last:1',
662
'--include', '*.aa', '--include', '*.bb', 'line1'])
663
self.assertContainsRe(out, "^dir0/file0.aa~.:line1$", flags=TestGrep._reflags)
664
self.assertContainsRe(out, "^dir1/file1.bb~.:line1$", flags=TestGrep._reflags)
665
self.assertContainsRe(out, "^dir0/file0.aa~.:line10$", flags=TestGrep._reflags)
666
self.assertContainsRe(out, "^dir1/file1.bb~.:line10$", flags=TestGrep._reflags)
667
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
668
# finds line1 and line10
669
self.assertEqual(len(out.splitlines()), 4)
671
def test_wtree_include_from_outside_dir(self):
672
"""(wtree) Ensure --include is respected during recursive search.
675
self.make_branch_and_tree(wd)
678
self._mk_versioned_dir('dir0')
679
self._mk_versioned_file('dir0/file0.aa')
681
self._mk_versioned_dir('dir1')
682
self._mk_versioned_file('dir1/file1.bb')
684
self._mk_versioned_dir('dir2')
685
self._mk_versioned_file('dir2/file2.cc')
687
out, err = self.run_bzr(['grep', '--include', '*.aa',
688
'--include', '*.bb', 'l.n.1'])
689
self.assertContainsRe(out, "^dir0/file0.aa:line1$", flags=TestGrep._reflags)
690
self.assertContainsRe(out, "^dir1/file1.bb:line1$", flags=TestGrep._reflags)
691
self.assertContainsRe(out, "^dir0/file0.aa:line10$", flags=TestGrep._reflags)
692
self.assertContainsRe(out, "^dir1/file1.bb:line10$", flags=TestGrep._reflags)
693
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
694
# finds line1 and line10
695
self.assertEqual(len(out.splitlines()), 4)
697
out, err = self.run_bzr(['grep', '--include', '*.aa',
698
'--include', '*.bb', 'line1'])
699
self.assertContainsRe(out, "^dir0/file0.aa:line1$", flags=TestGrep._reflags)
700
self.assertContainsRe(out, "^dir1/file1.bb:line1$", flags=TestGrep._reflags)
701
self.assertContainsRe(out, "^dir0/file0.aa:line10$", flags=TestGrep._reflags)
702
self.assertContainsRe(out, "^dir1/file1.bb:line10$", flags=TestGrep._reflags)
703
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
704
# finds line1 and line10
705
self.assertEqual(len(out.splitlines()), 4)
707
def test_versioned_exclude_from_outside_dir(self):
708
"""(versioned) Ensure --exclude is respected during recursive search.
711
self.make_branch_and_tree(wd)
714
self._mk_versioned_dir('dir0')
715
self._mk_versioned_file('dir0/file0.aa')
717
self._mk_versioned_dir('dir1')
718
self._mk_versioned_file('dir1/file1.bb')
720
self._mk_versioned_dir('dir2')
721
self._mk_versioned_file('dir2/file2.cc')
723
out, err = self.run_bzr(['grep', '-r', 'last:1',
724
'--exclude', '*.cc', 'l..e1'])
725
self.assertContainsRe(out, "^dir0/file0.aa~.:line1", flags=TestGrep._reflags)
726
self.assertContainsRe(out, "^dir1/file1.bb~.:line1", flags=TestGrep._reflags)
727
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
729
out, err = self.run_bzr(['grep', '-r', 'last:1',
730
'--exclude', '*.cc', 'line1'])
731
self.assertContainsRe(out, "^dir0/file0.aa~.:line1", flags=TestGrep._reflags)
732
self.assertContainsRe(out, "^dir1/file1.bb~.:line1", flags=TestGrep._reflags)
733
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
735
def test_wtree_exclude_from_outside_dir(self):
736
"""(wtree) Ensure --exclude is respected during recursive search.
739
self.make_branch_and_tree(wd)
742
self._mk_versioned_dir('dir0')
743
self._mk_versioned_file('dir0/file0.aa')
745
self._mk_versioned_dir('dir1')
746
self._mk_versioned_file('dir1/file1.bb')
748
self._mk_versioned_dir('dir2')
749
self._mk_versioned_file('dir2/file2.cc')
751
out, err = self.run_bzr(['grep', '--exclude', '*.cc', 'l[hijk]ne1'])
752
self.assertContainsRe(out, "^dir0/file0.aa:line1", flags=TestGrep._reflags)
753
self.assertContainsRe(out, "^dir1/file1.bb:line1", flags=TestGrep._reflags)
754
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
756
out, err = self.run_bzr(['grep', '--exclude', '*.cc', 'line1'])
757
self.assertContainsRe(out, "^dir0/file0.aa:line1", flags=TestGrep._reflags)
758
self.assertContainsRe(out, "^dir1/file1.bb:line1", flags=TestGrep._reflags)
759
self.assertNotContainsRe(out, "file1.cc", flags=TestGrep._reflags)
761
def test_workingtree_files_from_outside_dir(self):
762
"""(wtree) Grep for pattern with dirs passed as argument.
765
self.make_branch_and_tree(wd)
768
self._mk_versioned_dir('dir0')
769
self._mk_versioned_file('dir0/file0.txt')
771
self._mk_versioned_dir('dir1')
772
self._mk_versioned_file('dir1/file1.txt')
774
out, err = self.run_bzr(['grep', 'l[aeiou]ne1', 'dir0', 'dir1'])
775
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
776
self.assertContainsRe(out, "^dir1/file1.txt:line1", flags=TestGrep._reflags)
778
out, err = self.run_bzr(['grep', 'line1', 'dir0', 'dir1'])
779
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
780
self.assertContainsRe(out, "^dir1/file1.txt:line1", flags=TestGrep._reflags)
782
def test_versioned_files_from_outside_dir(self):
783
"""(versioned) Grep for pattern with dirs passed as argument.
786
self.make_branch_and_tree(wd)
789
self._mk_versioned_dir('dir0')
790
self._mk_versioned_file('dir0/file0.txt')
792
self._mk_versioned_dir('dir1')
793
self._mk_versioned_file('dir1/file1.txt')
795
out, err = self.run_bzr(['grep', '-r', 'last:1', '.ine1', 'dir0', 'dir1'])
796
self.assertContainsRe(out, "^dir0/file0.txt~.:line1", flags=TestGrep._reflags)
797
self.assertContainsRe(out, "^dir1/file1.txt~.:line1", flags=TestGrep._reflags)
799
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line1', 'dir0', 'dir1'])
800
self.assertContainsRe(out, "^dir0/file0.txt~.:line1", flags=TestGrep._reflags)
801
self.assertContainsRe(out, "^dir1/file1.txt~.:line1", flags=TestGrep._reflags)
803
def test_wtree_files_from_outside_dir(self):
804
"""(wtree) Grep for pattern with dirs passed as argument.
807
self.make_branch_and_tree(wd)
810
self._mk_versioned_dir('dir0')
811
self._mk_versioned_file('dir0/file0.txt')
813
self._mk_versioned_dir('dir1')
814
self._mk_versioned_file('dir1/file1.txt')
816
out, err = self.run_bzr(['grep', 'li.e1', 'dir0', 'dir1'])
817
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
818
self.assertContainsRe(out, "^dir1/file1.txt:line1", flags=TestGrep._reflags)
820
out, err = self.run_bzr(['grep', 'line1', 'dir0', 'dir1'])
821
self.assertContainsRe(out, "^dir0/file0.txt:line1", flags=TestGrep._reflags)
822
self.assertContainsRe(out, "^dir1/file1.txt:line1", flags=TestGrep._reflags)
824
def test_versioned_files_from_outside_two_dirs(self):
825
"""(versioned) Grep for pattern with two levels of nested dir.
828
self.make_branch_and_tree(wd)
831
self._mk_versioned_dir('dir0')
832
self._mk_versioned_file('dir0/file0.txt')
834
self._mk_versioned_dir('dir1')
835
self._mk_versioned_file('dir1/file1.txt')
837
self._mk_versioned_dir('dir0/dir00')
838
self._mk_versioned_file('dir0/dir00/file0.txt')
840
out, err = self.run_bzr(['grep', '-r', 'last:1', 'l.ne1', 'dir0/dir00'])
841
self.assertContainsRe(out, "^dir0/dir00/file0.txt~.:line1", flags=TestGrep._reflags)
843
out, err = self.run_bzr(['grep', '-r', 'last:1', 'l.ne1'])
844
self.assertContainsRe(out, "^dir0/dir00/file0.txt~.:line1", flags=TestGrep._reflags)
846
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line1', 'dir0/dir00'])
847
self.assertContainsRe(out, "^dir0/dir00/file0.txt~.:line1", flags=TestGrep._reflags)
849
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line1'])
850
self.assertContainsRe(out, "^dir0/dir00/file0.txt~.:line1", flags=TestGrep._reflags)
852
def test_wtree_files_from_outside_two_dirs(self):
853
"""(wtree) Grep for pattern with two levels of nested dir.
856
self.make_branch_and_tree(wd)
859
self._mk_versioned_dir('dir0')
860
self._mk_versioned_file('dir0/file0.txt')
862
self._mk_versioned_dir('dir1')
863
self._mk_versioned_file('dir1/file1.txt')
865
self._mk_versioned_dir('dir0/dir00')
866
self._mk_versioned_file('dir0/dir00/file0.txt')
868
out, err = self.run_bzr(['grep', 'lin.1', 'dir0/dir00'])
869
self.assertContainsRe(out, "^dir0/dir00/file0.txt:line1", flags=TestGrep._reflags)
871
out, err = self.run_bzr(['grep', 'li.e1'])
872
self.assertContainsRe(out, "^dir0/dir00/file0.txt:line1", flags=TestGrep._reflags)
874
out, err = self.run_bzr(['grep', 'line1', 'dir0/dir00'])
875
self.assertContainsRe(out, "^dir0/dir00/file0.txt:line1", flags=TestGrep._reflags)
877
out, err = self.run_bzr(['grep', 'line1'])
878
self.assertContainsRe(out, "^dir0/dir00/file0.txt:line1", flags=TestGrep._reflags)
880
def test_versioned_file_within_dir_two_levels(self):
881
"""(versioned) Search for pattern while in nested dir (two levels).
884
self.make_branch_and_tree(wd)
886
self._mk_versioned_dir('dir0')
887
self._mk_versioned_dir('dir0/dir1')
888
self._mk_versioned_file('dir0/dir1/file0.txt')
891
out, err = self.run_bzr(['grep', '-r', 'last:1', '.ine1'])
892
self.assertContainsRe(out, "^dir1/file0.txt~.:line1", flags=TestGrep._reflags)
894
out, err = self.run_bzr(['grep', '-r', 'last:1', '--from-root', 'l.ne1'])
895
self.assertContainsRe(out, "^dir0/dir1/file0.txt~.:line1", flags=TestGrep._reflags)
897
out, err = self.run_bzr(['grep', '-r', 'last:1', '--no-recursive', 'line1'])
898
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
900
out, err = self.run_bzr(['grep', '-r', 'last:1', 'lin.1'])
901
self.assertContainsRe(out, "^dir1/file0.txt~.:line1", flags=TestGrep._reflags)
903
out, err = self.run_bzr(['grep', '-r', 'last:1', '--from-root', 'line1'])
904
self.assertContainsRe(out, "^dir0/dir1/file0.txt~.:line1", flags=TestGrep._reflags)
906
out, err = self.run_bzr(['grep', '-r', 'last:1', '--no-recursive', 'line1'])
907
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
908
self.assertEqual(len(out.splitlines()), 0)
910
def test_wtree_file_within_dir_two_levels(self):
911
"""(wtree) Search for pattern while in nested dir (two levels).
914
self.make_branch_and_tree(wd)
916
self._mk_versioned_dir('dir0')
917
self._mk_versioned_dir('dir0/dir1')
918
self._mk_versioned_file('dir0/dir1/file0.txt')
921
out, err = self.run_bzr(['grep', 'l[hij]ne1'])
922
self.assertContainsRe(out, "^dir1/file0.txt:line1", flags=TestGrep._reflags)
924
out, err = self.run_bzr(['grep', '--from-root', 'l.ne1'])
925
self.assertContainsRe(out, "^dir0/dir1/file0.txt:line1", flags=TestGrep._reflags)
927
out, err = self.run_bzr(['grep', '--no-recursive', 'lin.1'])
928
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
930
out, err = self.run_bzr(['grep', 'line1'])
931
self.assertContainsRe(out, "^dir1/file0.txt:line1", flags=TestGrep._reflags)
933
out, err = self.run_bzr(['grep', '--from-root', 'line1'])
934
self.assertContainsRe(out, "^dir0/dir1/file0.txt:line1", flags=TestGrep._reflags)
936
out, err = self.run_bzr(['grep', '--no-recursive', 'line1'])
937
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
939
def test_versioned_ignore_case_no_match(self):
940
"""(versioned) Match fails without --ignore-case.
943
self.make_branch_and_tree(wd)
945
self._mk_versioned_file('file0.txt')
947
out, err = self.run_bzr(['grep', '-r', 'last:1', 'LinE1', 'file0.txt'])
948
self.assertNotContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
950
out, err = self.run_bzr(['grep', '-r', 'last:1', 'Li.E1', 'file0.txt'])
951
self.assertNotContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
953
def test_wtree_ignore_case_no_match(self):
954
"""(wtree) Match fails without --ignore-case.
957
self.make_branch_and_tree(wd)
959
self._mk_versioned_file('file0.txt')
961
out, err = self.run_bzr(['grep', 'LinE1', 'file0.txt'])
962
self.assertNotContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
964
out, err = self.run_bzr(['grep', '.inE1', 'file0.txt'])
965
self.assertNotContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
967
def test_versioned_ignore_case_match(self):
968
"""(versioned) Match fails without --ignore-case.
971
self.make_branch_and_tree(wd)
973
self._mk_versioned_file('file0.txt')
975
out, err = self.run_bzr(['grep', '-r', 'last:1',
976
'-i', 'Li.E1', 'file0.txt'])
977
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
979
out, err = self.run_bzr(['grep', '-r', 'last:1',
980
'-i', 'LinE1', 'file0.txt'])
981
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
983
out, err = self.run_bzr(['grep', '-r', 'last:1',
984
'--ignore-case', 'LinE1', 'file0.txt'])
985
self.assertContainsRe(out, "^file0.txt~.:line1", flags=TestGrep._reflags)
987
def test_wtree_ignore_case_match(self):
988
"""(wtree) Match fails without --ignore-case.
991
self.make_branch_and_tree(wd)
993
self._mk_versioned_file('file0.txt')
995
out, err = self.run_bzr(['grep', '-i', 'LinE1', 'file0.txt'])
996
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
998
out, err = self.run_bzr(['grep', '--ignore-case', 'LinE1', 'file0.txt'])
999
self.assertContainsRe(out, "^file0.txt:line1", flags=TestGrep._reflags)
1001
out, err = self.run_bzr(['grep', '--ignore-case', 'Li.E1', 'file0.txt'])
1002
self.assertContainsRe(out, "^file0.txt:line1", flags=TestGrep._reflags)
1004
def test_versioned_from_root_fail(self):
1005
"""(versioned) Match should fail without --from-root.
1008
self.make_branch_and_tree(wd)
1010
self._mk_versioned_file('file0.txt')
1011
self._mk_versioned_dir('dir0')
1014
out, err = self.run_bzr(['grep', '-r', 'last:1', 'li.e1'])
1015
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1017
out, err = self.run_bzr(['grep', '-r', 'last:1', 'line1'])
1018
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1020
def test_wtree_from_root_fail(self):
1021
"""(wtree) Match should fail without --from-root.
1024
self.make_branch_and_tree(wd)
1026
self._mk_versioned_file('file0.txt')
1027
self._mk_versioned_dir('dir0')
1030
out, err = self.run_bzr(['grep', 'line1'])
1031
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1033
out, err = self.run_bzr(['grep', 'li.e1'])
1034
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1036
def test_versioned_from_root_pass(self):
1037
"""(versioned) Match pass with --from-root.
1040
self.make_branch_and_tree(wd)
1042
self._mk_versioned_file('file0.txt')
1043
self._mk_versioned_dir('dir0')
1046
out, err = self.run_bzr(['grep', '-r', 'last:1',
1047
'--from-root', 'l.ne1'])
1048
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
1050
out, err = self.run_bzr(['grep', '-r', 'last:1',
1051
'--from-root', 'line1'])
1052
self.assertContainsRe(out, "file0.txt~.:line1", flags=TestGrep._reflags)
1054
def test_wtree_from_root_pass(self):
1055
"""(wtree) Match pass with --from-root.
1058
self.make_branch_and_tree(wd)
1060
self._mk_versioned_file('file0.txt')
1061
self._mk_versioned_dir('dir0')
1064
out, err = self.run_bzr(['grep', '--from-root', 'lin.1'])
1065
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
1067
out, err = self.run_bzr(['grep', '--from-root', 'line1'])
1068
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
1070
def test_versioned_with_line_number(self):
1071
"""(versioned) Search for pattern with --line-number.
1074
self.make_branch_and_tree(wd)
1076
self._mk_versioned_file('file0.txt')
1078
out, err = self.run_bzr(['grep', '-r', 'last:1',
1079
'--line-number', 'li.e3', 'file0.txt'])
1080
self.assertContainsRe(out, "file0.txt~.:3:line3", flags=TestGrep._reflags)
1082
out, err = self.run_bzr(['grep', '-r', 'last:1',
1083
'--line-number', 'line3', 'file0.txt'])
1084
self.assertContainsRe(out, "file0.txt~.:3:line3", flags=TestGrep._reflags)
1086
out, err = self.run_bzr(['grep', '-r', 'last:1',
1087
'-n', 'line1', 'file0.txt'])
1088
self.assertContainsRe(out, "file0.txt~.:1:line1", flags=TestGrep._reflags)
1090
out, err = self.run_bzr(['grep', '-n', 'line[0-9]', 'file0.txt'])
1091
self.assertContainsRe(out, "file0.txt:3:line3", flags=TestGrep._reflags)
1093
def test_wtree_with_line_number(self):
1094
"""(wtree) Search for pattern with --line-number.
1097
self.make_branch_and_tree(wd)
1099
self._mk_versioned_file('file0.txt')
1101
out, err = self.run_bzr(['grep', '--line-number', 'line3', 'file0.txt'])
1102
self.assertContainsRe(out, "file0.txt:3:line3", flags=TestGrep._reflags)
1104
out, err = self.run_bzr(['grep', '-n', 'line1', 'file0.txt'])
1105
self.assertContainsRe(out, "file0.txt:1:line1", flags=TestGrep._reflags)
1107
out, err = self.run_bzr(['grep', '-n', '[hjkl]ine1', 'file0.txt'])
1108
self.assertContainsRe(out, "file0.txt:1:line1", flags=TestGrep._reflags)
1110
out, err = self.run_bzr(['grep', '-n', 'line[0-9]', 'file0.txt'])
1111
self.assertContainsRe(out, "file0.txt:3:line3", flags=TestGrep._reflags)
1113
def test_revno_basic_history_grep_file(self):
1114
"""Search for pattern in specific revision number in a file.
1118
self.make_branch_and_tree(wd)
1120
self._mk_versioned_file(fname, total_lines=0)
1121
self._update_file(fname, text="v2 text\n")
1122
self._update_file(fname, text="v3 text\n")
1123
self._update_file(fname, text="v4 text\n")
1125
# rev 2 should not have text 'v3'
1126
out, err = self.run_bzr(['grep', '-r', '2', 'v3', fname])
1127
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1129
# rev 3 should not have text 'v3'
1130
out, err = self.run_bzr(['grep', '-r', '3', 'v3', fname])
1131
self.assertContainsRe(out, "file0.txt~3:v3.*", flags=TestGrep._reflags)
1133
# rev 3 should not have text 'v3' with line number
1134
out, err = self.run_bzr(['grep', '-r', '3', '-n', 'v3', fname])
1135
self.assertContainsRe(out, "file0.txt~3:2:v3.*", flags=TestGrep._reflags)
1137
# rev 2 should not have text 'v3'
1138
out, err = self.run_bzr(['grep', '-r', '2', '[tuv]3', fname])
1139
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1141
# rev 3 should not have text 'v3'
1142
out, err = self.run_bzr(['grep', '-r', '3', '[tuv]3', fname])
1143
self.assertContainsRe(out, "file0.txt~3:v3.*", flags=TestGrep._reflags)
1145
# rev 3 should not have text 'v3' with line number
1146
out, err = self.run_bzr(['grep', '-r', '3', '-n', '[tuv]3', fname])
1147
self.assertContainsRe(out, "file0.txt~3:2:v3.*", flags=TestGrep._reflags)
1149
def test_revno_basic_history_grep_full(self):
1150
"""Search for pattern in specific revision number in a file.
1154
self.make_branch_and_tree(wd)
1156
self._mk_versioned_file(fname, total_lines=0) # rev1
1157
self._mk_versioned_file('file1.txt') # rev2
1158
self._update_file(fname, text="v3 text\n") # rev3
1159
self._update_file(fname, text="v4 text\n") # rev4
1160
self._update_file(fname, text="v5 text\n") # rev5
1162
# rev 2 should not have text 'v3'
1163
out, err = self.run_bzr(['grep', '-r', '2', 'v3'])
1164
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1166
# rev 3 should not have text 'v3'
1167
out, err = self.run_bzr(['grep', '-r', '3', 'v3'])
1168
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1170
# rev 3 should not have text 'v3' with line number
1171
out, err = self.run_bzr(['grep', '-r', '3', '-n', 'v3'])
1172
self.assertContainsRe(out, "file0.txt~3:1:v3", flags=TestGrep._reflags)
1174
# rev 2 should not have text 'v3'
1175
out, err = self.run_bzr(['grep', '-r', '2', '[tuv]3'])
1176
self.assertNotContainsRe(out, "file0.txt", flags=TestGrep._reflags)
1178
# rev 3 should not have text 'v3'
1179
out, err = self.run_bzr(['grep', '-r', '3', '[tuv]3'])
1180
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1182
# rev 3 should not have text 'v3' with line number
1183
out, err = self.run_bzr(['grep', '-r', '3', '-n', '[tuv]3'])
1184
self.assertContainsRe(out, "file0.txt~3:1:v3", flags=TestGrep._reflags)
1186
def test_revno_versioned_file_in_dir(self):
1187
"""Grep specific version of file withing dir.
1190
self.make_branch_and_tree(wd)
1192
self._mk_versioned_dir('dir0') # rev1
1193
self._mk_versioned_file('dir0/file0.txt') # rev2
1194
self._update_file('dir0/file0.txt', "v3 text\n") # rev3
1195
self._update_file('dir0/file0.txt', "v4 text\n") # rev4
1196
self._update_file('dir0/file0.txt', "v5 text\n") # rev5
1198
# v4 should not be present in revno 3
1199
out, err = self.run_bzr(['grep', '-r', 'last:3', 'v4'])
1200
self.assertNotContainsRe(out, "^dir0/file0.txt", flags=TestGrep._reflags)
1202
# v4 should be present in revno 4
1203
out, err = self.run_bzr(['grep', '-r', 'last:2', 'v4'])
1204
self.assertContainsRe(out, "^dir0/file0.txt~4:v4", flags=TestGrep._reflags)
1206
# v4 should not be present in revno 3
1207
out, err = self.run_bzr(['grep', '-r', 'last:3', '[tuv]4'])
1208
self.assertNotContainsRe(out, "^dir0/file0.txt", flags=TestGrep._reflags)
1210
# v4 should be present in revno 4
1211
out, err = self.run_bzr(['grep', '-r', 'last:2', '[tuv]4'])
1212
self.assertContainsRe(out, "^dir0/file0.txt~4:v4", flags=TestGrep._reflags)
1214
def test_revno_range_basic_history_grep(self):
1215
"""Search for pattern in revision range for file.
1219
self.make_branch_and_tree(wd)
1221
self._mk_versioned_file(fname, total_lines=0) # rev1
1222
self._mk_versioned_file('file1.txt') # rev2
1223
self._update_file(fname, text="v3 text\n") # rev3
1224
self._update_file(fname, text="v4 text\n") # rev4
1225
self._update_file(fname, text="v5 text\n") # rev5
1226
self._update_file(fname, text="v6 text\n") # rev6
1228
out, err = self.run_bzr(['grep', '-r', '1..', 'v3'])
1229
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1230
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1231
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1232
self.assertContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1233
self.assertEqual(len(out.splitlines()), 4)
1235
out, err = self.run_bzr(['grep', '-r', '..1', 'v3'])
1236
# searching only rev1 gives nothing
1237
self.assertEqual(len(out.splitlines()), 0)
1239
out, err = self.run_bzr(['grep', '-r', '..6', 'v3'])
1240
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1241
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1242
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1243
self.assertContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1244
self.assertEqual(len(out.splitlines()), 4)
1246
out, err = self.run_bzr(['grep', '-r', '..', 'v3'])
1247
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1248
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1249
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1250
self.assertContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1251
self.assertEqual(len(out.splitlines()), 4)
1253
out, err = self.run_bzr(['grep', '-r', '1..5', 'v3'])
1254
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1255
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1256
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1257
self.assertNotContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1258
self.assertEqual(len(out.splitlines()), 3)
1260
out, err = self.run_bzr(['grep', '-r', '5..1', 'v3'])
1261
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1262
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1263
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1264
self.assertNotContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1265
self.assertEqual(len(out.splitlines()), 3)
1267
out, err = self.run_bzr(['grep', '-r', '1..', '[tuv]3'])
1268
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1269
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1270
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1271
self.assertContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1272
self.assertEqual(len(out.splitlines()), 4)
1274
out, err = self.run_bzr(['grep', '-r', '1..5', '[tuv]3'])
1275
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1276
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1277
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1278
self.assertNotContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1279
self.assertEqual(len(out.splitlines()), 3)
1281
out, err = self.run_bzr(['grep', '-r', '5..1', '[tuv]3'])
1282
self.assertContainsRe(out, "file0.txt~3:v3", flags=TestGrep._reflags)
1283
self.assertContainsRe(out, "file0.txt~4:v3", flags=TestGrep._reflags)
1284
self.assertContainsRe(out, "file0.txt~5:v3", flags=TestGrep._reflags)
1285
self.assertNotContainsRe(out, "file0.txt~6:v3", flags=TestGrep._reflags)
1286
self.assertEqual(len(out.splitlines()), 3)
1288
def test_revno_range_versioned_file_in_dir(self):
1289
"""Grep rev-range for pattern for file withing a dir.
1292
self.make_branch_and_tree(wd)
1294
self._mk_versioned_dir('dir0') # rev1
1295
self._mk_versioned_file('dir0/file0.txt') # rev2
1296
self._update_file('dir0/file0.txt', "v3 text\n") # rev3
1297
self._update_file('dir0/file0.txt', "v4 text\n") # rev4
1298
self._update_file('dir0/file0.txt', "v5 text\n") # rev5
1299
self._update_file('dir0/file0.txt', "v6 text\n") # rev6
1301
out, err = self.run_bzr(['grep', '-r', '2..5', 'v3'])
1302
self.assertContainsRe(out, "^dir0/file0.txt~3:v3", flags=TestGrep._reflags)
1303
self.assertContainsRe(out, "^dir0/file0.txt~4:v3", flags=TestGrep._reflags)
1304
self.assertContainsRe(out, "^dir0/file0.txt~5:v3", flags=TestGrep._reflags)
1305
self.assertNotContainsRe(out, "^dir0/file0.txt~6:v3", flags=TestGrep._reflags)
1306
self.assertEqual(len(out.splitlines()), 3)
1308
out, err = self.run_bzr(['grep', '-r', '2..5', '[tuv]3'])
1309
self.assertContainsRe(out, "^dir0/file0.txt~3:v3", flags=TestGrep._reflags)
1310
self.assertContainsRe(out, "^dir0/file0.txt~4:v3", flags=TestGrep._reflags)
1311
self.assertContainsRe(out, "^dir0/file0.txt~5:v3", flags=TestGrep._reflags)
1312
self.assertNotContainsRe(out, "^dir0/file0.txt~6:v3", flags=TestGrep._reflags)
1313
self.assertEqual(len(out.splitlines()), 3)
1315
def test_revno_range_versioned_file_from_outside_dir(self):
1316
"""Grep rev-range for pattern from outside dir.
1319
self.make_branch_and_tree(wd)
1321
self._mk_versioned_dir('dir0') # rev1
1322
self._mk_versioned_file('dir0/file0.txt') # rev2
1323
self._update_file('dir0/file0.txt', "v3 text\n") # rev3
1324
self._update_file('dir0/file0.txt', "v4 text\n") # rev4
1325
self._update_file('dir0/file0.txt', "v5 text\n") # rev5
1326
self._update_file('dir0/file0.txt', "v6 text\n") # rev6
1328
out, err = self.run_bzr(['grep', '-r', '2..5', 'v3', 'dir0'])
1329
self.assertContainsRe(out, "^dir0/file0.txt~3:v3", flags=TestGrep._reflags)
1330
self.assertContainsRe(out, "^dir0/file0.txt~4:v3", flags=TestGrep._reflags)
1331
self.assertContainsRe(out, "^dir0/file0.txt~5:v3", flags=TestGrep._reflags)
1332
self.assertNotContainsRe(out, "^dir0/file0.txt~6:v3", flags=TestGrep._reflags)
1334
out, err = self.run_bzr(['grep', '-r', '2..5', '[tuv]3', 'dir0'])
1335
self.assertContainsRe(out, "^dir0/file0.txt~3:v3", flags=TestGrep._reflags)
1336
self.assertContainsRe(out, "^dir0/file0.txt~4:v3", flags=TestGrep._reflags)
1337
self.assertContainsRe(out, "^dir0/file0.txt~5:v3", flags=TestGrep._reflags)
1338
self.assertNotContainsRe(out, "^dir0/file0.txt~6:v3", flags=TestGrep._reflags)
1340
def test_levels(self):
1341
"""--levels=0 should show findings from merged revision.
1346
self.make_branch_and_tree(wd0)
1348
self._mk_versioned_file('file0.txt')
1351
out, err = self.run_bzr(['branch', wd0, wd1])
1353
self._mk_versioned_file('file1.txt')
1354
os.chdir(osutils.pathjoin('..', wd0))
1356
out, err = self.run_bzr(['merge', osutils.pathjoin('..', wd1)])
1357
out, err = self.run_bzr(['ci', '-m', 'merged'])
1359
out, err = self.run_bzr(['grep', 'line1'])
1360
self.assertContainsRe(out, "file0.txt:line1", flags=TestGrep._reflags)
1361
self.assertContainsRe(out, "file1.txt:line1", flags=TestGrep._reflags)
1363
# levels should be ignored by wtree grep
1364
out, err = self.run_bzr(['grep', '--levels=0', 'line1'])
1365
self.assertContainsRe(out, "^file0.txt:line1$", flags=TestGrep._reflags)
1366
self.assertContainsRe(out, "^file1.txt:line1$", flags=TestGrep._reflags)
1367
self.assertContainsRe(out, "^file0.txt:line10$", flags=TestGrep._reflags)
1368
self.assertContainsRe(out, "^file1.txt:line10$", flags=TestGrep._reflags)
1369
self.assertEqual(len(out.splitlines()), 4)
1371
out, err = self.run_bzr(['grep', '-r', 'last:1..', '--levels=0', 'line1'])
1372
self.assertContainsRe(out, "^file0.txt~2:line1$", flags=TestGrep._reflags)
1373
self.assertContainsRe(out, "^file1.txt~2:line1$", flags=TestGrep._reflags)
1374
self.assertContainsRe(out, "^file0.txt~1.1.1:line1$", flags=TestGrep._reflags)
1375
self.assertContainsRe(out, "^file1.txt~1.1.1:line1$", flags=TestGrep._reflags)
1376
self.assertContainsRe(out, "^file0.txt~2:line10$", flags=TestGrep._reflags)
1377
self.assertContainsRe(out, "^file1.txt~2:line10$", flags=TestGrep._reflags)
1378
self.assertContainsRe(out, "^file0.txt~1.1.1:line10$", flags=TestGrep._reflags)
1379
self.assertContainsRe(out, "^file1.txt~1.1.1:line10$", flags=TestGrep._reflags)
1380
self.assertEqual(len(out.splitlines()), 8)
1382
out, err = self.run_bzr(['grep', '-r', '-1..', '-n', '--levels=0', 'line1'])
1383
self.assertContainsRe(out, "^file0.txt~2:1:line1$", flags=TestGrep._reflags)
1384
self.assertContainsRe(out, "^file1.txt~2:1:line1$", flags=TestGrep._reflags)
1385
self.assertContainsRe(out, "^file0.txt~1.1.1:1:line1$", flags=TestGrep._reflags)
1386
self.assertContainsRe(out, "^file1.txt~1.1.1:1:line1$", flags=TestGrep._reflags)
1387
self.assertContainsRe(out, "^file0.txt~2:10:line10$", flags=TestGrep._reflags)
1388
self.assertContainsRe(out, "^file1.txt~2:10:line10$", flags=TestGrep._reflags)
1389
self.assertContainsRe(out, "^file0.txt~1.1.1:10:line10$", flags=TestGrep._reflags)
1390
self.assertContainsRe(out, "^file1.txt~1.1.1:10:line10$", flags=TestGrep._reflags)
1391
self.assertEqual(len(out.splitlines()), 8)
1393
# levels should be ignored by wtree grep
1394
out, err = self.run_bzr(['grep', '--levels=0', 'l.ne1'])
1395
self.assertContainsRe(out, "^file0.txt:line1$", flags=TestGrep._reflags)
1396
self.assertContainsRe(out, "^file1.txt:line1$", flags=TestGrep._reflags)
1397
self.assertContainsRe(out, "^file0.txt:line10$", flags=TestGrep._reflags)
1398
self.assertContainsRe(out, "^file1.txt:line10$", flags=TestGrep._reflags)
1399
self.assertEqual(len(out.splitlines()), 4)
1401
out, err = self.run_bzr(['grep', '-r', 'last:1..', '--levels=0', 'lin.1'])
1402
self.assertContainsRe(out, "^file0.txt~2:line1$", flags=TestGrep._reflags)
1403
self.assertContainsRe(out, "^file1.txt~2:line1$", flags=TestGrep._reflags)
1404
self.assertContainsRe(out, "^file0.txt~1.1.1:line1$", flags=TestGrep._reflags)
1405
self.assertContainsRe(out, "^file1.txt~1.1.1:line1$", flags=TestGrep._reflags)
1406
self.assertContainsRe(out, "^file0.txt~2:line10$", flags=TestGrep._reflags)
1407
self.assertContainsRe(out, "^file1.txt~2:line10$", flags=TestGrep._reflags)
1408
self.assertContainsRe(out, "^file0.txt~1.1.1:line10$", flags=TestGrep._reflags)
1409
self.assertContainsRe(out, "^file1.txt~1.1.1:line10$", flags=TestGrep._reflags)
1410
self.assertEqual(len(out.splitlines()), 8)
1412
out, err = self.run_bzr(['grep', '-r', '-1..', '-n', '--levels=0', '.ine1'])
1413
self.assertContainsRe(out, "file0.txt~2:1:line1", flags=TestGrep._reflags)
1414
self.assertContainsRe(out, "file1.txt~2:1:line1", flags=TestGrep._reflags)
1415
self.assertContainsRe(out, "file0.txt~1.1.1:1:line1", flags=TestGrep._reflags)
1416
self.assertContainsRe(out, "file1.txt~1.1.1:1:line1", flags=TestGrep._reflags)
1418
def test_dotted_rev_grep(self):
1419
"""Grep in dotted revs
1424
self.make_branch_and_tree(wd0)
1426
self._mk_versioned_file('file0.txt')
1429
out, err = self.run_bzr(['branch', wd0, wd1])
1431
self._mk_versioned_file('file1.txt') # revno 1.1.1
1432
self._update_file('file1.txt', "text 0\n") # revno 1.1.2
1433
self._update_file('file1.txt', "text 1\n") # revno 1.1.3
1434
self._update_file('file1.txt', "text 2\n") # revno 1.1.4
1435
os.chdir(osutils.pathjoin('..', wd0))
1437
out, err = self.run_bzr(['merge', osutils.pathjoin('..', wd1)])
1438
out, err = self.run_bzr(['ci', '-m', 'merged'])
1440
out, err = self.run_bzr(['grep', '-r', '1.1.1..1.1.4', 'text'])
1441
self.assertContainsRe(out, "file1.txt~1.1.2:text 0", flags=TestGrep._reflags)
1442
self.assertContainsRe(out, "file1.txt~1.1.3:text 1", flags=TestGrep._reflags)
1443
self.assertContainsRe(out, "file1.txt~1.1.3:text 1", flags=TestGrep._reflags)
1444
self.assertContainsRe(out, "file1.txt~1.1.4:text 0", flags=TestGrep._reflags)
1445
self.assertContainsRe(out, "file1.txt~1.1.4:text 1", flags=TestGrep._reflags)
1446
self.assertContainsRe(out, "file1.txt~1.1.4:text 2", flags=TestGrep._reflags)
1447
self.assertEqual(len(out.splitlines()), 6)
1449
def test_versioned_binary_file_grep(self):
1450
"""(versioned) Grep for pattern in binary file.
1453
self.make_branch_and_tree(wd)
1455
self._mk_versioned_file('file.txt')
1456
self._mk_versioned_file('file0.bin')
1457
self._update_file('file0.bin', "\x00lineNN\x00\n")
1459
# note: set --verbose/-v flag to get the skip message.
1460
out, err = self.run_bzr(['grep', '-v', '-r', 'last:1',
1461
'lineNN', 'file0.bin'])
1462
self.assertNotContainsRe(out, "file0.bin", flags=TestGrep._reflags)
1463
self.assertContainsRe(err, "Binary file.*file0.bin.*skipped", flags=TestGrep._reflags)
1464
self.assertEqual(len(out.splitlines()), 0)
1465
self.assertEqual(len(err.splitlines()), 1)
1467
out, err = self.run_bzr(['grep', '-v', '-r', 'last:1',
1468
'line.N', 'file0.bin'])
1469
self.assertNotContainsRe(out, "file0.bin", flags=TestGrep._reflags)
1470
self.assertContainsRe(err, "Binary file.*file0.bin.*skipped", flags=TestGrep._reflags)
1471
self.assertEqual(len(out.splitlines()), 0)
1472
self.assertEqual(len(err.splitlines()), 1)
1474
def test_wtree_binary_file_grep(self):
1475
"""(wtree) Grep for pattern in binary file.
1478
self.make_branch_and_tree(wd)
1480
self._mk_versioned_file('file0.bin')
1481
self._update_file('file0.bin', "\x00lineNN\x00\n")
1483
# note: set --verbose/-v flag to get the skip message.
1484
out, err = self.run_bzr(['grep', '-v', 'lineNN', 'file0.bin'])
1485
self.assertNotContainsRe(out, "file0.bin:line1", flags=TestGrep._reflags)
1486
self.assertContainsRe(err, "Binary file.*file0.bin.*skipped", flags=TestGrep._reflags)
1488
# binary warning should not be shown without --verbose
1489
out, err = self.run_bzr(['grep', 'lineNN', 'file0.bin'])
1490
self.assertNotContainsRe(out, "file0.bin:line1", flags=TestGrep._reflags)
1491
self.assertNotContainsRe(err, "Binary file", flags=TestGrep._reflags)
1493
def test_revspec(self):
1494
"""Ensure various revspecs work
1497
self.make_branch_and_tree(wd)
1499
self._mk_versioned_dir('dir0') # rev1
1500
self._mk_versioned_file('dir0/file0.txt') # rev2
1501
self._update_file('dir0/file0.txt', "v3 text\n") # rev3
1502
self._update_file('dir0/file0.txt', "v4 text\n") # rev4
1503
self._update_file('dir0/file0.txt', "v5 text\n") # rev5
1505
out, err = self.run_bzr(['grep', '-r', 'revno:1..2', 'v3'])
1506
self.assertNotContainsRe(out, "file0", flags=TestGrep._reflags)
1507
self.assertEqual(len(out.splitlines()), 0)
1509
out, err = self.run_bzr(['grep', '-r', 'revno:4..', 'v4'])
1510
self.assertContainsRe(out, "^dir0/file0.txt", flags=TestGrep._reflags)
1511
self.assertEqual(len(out.splitlines()), 2) # find v4 in rev4 and rev5
1513
out, err = self.run_bzr(['grep', '-r', '..revno:3', 'v4'])
1514
self.assertNotContainsRe(out, "file0", flags=TestGrep._reflags)
1515
self.assertEqual(len(out.splitlines()), 0)
1517
out, err = self.run_bzr(['grep', '-r', '..revno:3', 'v3'])
1518
self.assertContainsRe(out, "^dir0/file0.txt", flags=TestGrep._reflags)
1519
self.assertEqual(len(out.splitlines()), 1)
1521
def test_wtree_files_with_matches(self):
1522
"""(wtree) Ensure --files-with-matches, -l works
1525
self.make_branch_and_tree(wd)
1528
self._mk_versioned_file('file0.txt', total_lines=2)
1529
self._mk_versioned_file('file1.txt', total_lines=2)
1530
self._mk_versioned_dir('dir0')
1531
self._mk_versioned_file('dir0/file00.txt', total_lines=2)
1532
self._mk_versioned_file('dir0/file01.txt', total_lines=2)
1534
self._update_file('file0.txt', 'HELLO\n', checkin=False)
1535
self._update_file('dir0/file00.txt', 'HELLO\n', checkin=False)
1538
out, err = self.run_bzr(['grep', '--files-with-matches', 'HELLO'])
1540
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1541
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1542
self.assertEqual(len(out.splitlines()), 2)
1545
out, err = self.run_bzr(['grep', '--files-with-matches', 'HE.LO'])
1547
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1548
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1549
self.assertEqual(len(out.splitlines()), 2)
1552
out, err = self.run_bzr(['grep', '-l', 'HELLO'])
1554
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1555
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1556
self.assertEqual(len(out.splitlines()), 2)
1559
out, err = self.run_bzr(['grep', '-l', 'HE.LO'])
1561
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1562
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1563
self.assertEqual(len(out.splitlines()), 2)
1566
out, err = self.run_bzr(['grep', '-l', 'HELLO', 'dir0', 'file1.txt'])
1568
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1569
self.assertEqual(len(out.splitlines()), 1)
1572
out, err = self.run_bzr(['grep', '-l', '.ELLO', 'dir0', 'file1.txt'])
1574
self.assertContainsRe(out, "^dir0/file00.txt$", flags=TestGrep._reflags)
1575
self.assertEqual(len(out.splitlines()), 1)
1578
out, err = self.run_bzr(['grep', '-l', 'HELLO', 'file0.txt'])
1580
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1581
self.assertEqual(len(out.splitlines()), 1)
1584
out, err = self.run_bzr(['grep', '-l', '.ELLO', 'file0.txt'])
1586
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1587
self.assertEqual(len(out.splitlines()), 1)
1590
out, err = self.run_bzr(['grep', '--no-recursive', '-l', 'HELLO'])
1592
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1593
self.assertEqual(len(out.splitlines()), 1)
1596
out, err = self.run_bzr(['grep', '--no-recursive', '-l', '.ELLO'])
1598
self.assertContainsRe(out, "^file0.txt$", flags=TestGrep._reflags)
1599
self.assertEqual(len(out.splitlines()), 1)
1601
def test_ver_files_with_matches(self):
1602
"""(ver) Ensure --files-with-matches, -l works
1605
self.make_branch_and_tree(wd)
1608
self._mk_versioned_file('file0.txt', total_lines=2) # rev 1
1609
self._mk_versioned_file('file1.txt', total_lines=2) # rev 2
1610
self._mk_versioned_dir('dir0') # rev 3
1611
self._mk_versioned_file('dir0/file00.txt', total_lines=2) # rev 4
1612
self._mk_versioned_file('dir0/file01.txt', total_lines=2) # rev 5
1614
self._update_file('file0.txt', 'HELLO\n') # rev 6
1615
self._update_file('dir0/file00.txt', 'HELLO\n') # rev 7
1618
out, err = self.run_bzr(['grep', '-r', '-1', '--files-with-matches',
1621
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1622
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1623
self.assertEqual(len(out.splitlines()), 2)
1626
out, err = self.run_bzr(['grep', '-r', '-1', '--files-with-matches',
1629
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1630
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1631
self.assertEqual(len(out.splitlines()), 2)
1634
out, err = self.run_bzr(['grep', '-r', '6..7', '--files-with-matches',
1637
self.assertContainsRe(out, "^file0.txt~6$", flags=TestGrep._reflags)
1638
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1639
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1640
self.assertEqual(len(out.splitlines()), 3)
1643
out, err = self.run_bzr(['grep', '-r', '6..7', '--files-with-matches',
1646
self.assertContainsRe(out, "^file0.txt~6$", flags=TestGrep._reflags)
1647
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1648
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1649
self.assertEqual(len(out.splitlines()), 3)
1652
out, err = self.run_bzr(['grep', '-r', '-1', '-l', 'HELLO'])
1654
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1655
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1656
self.assertEqual(len(out.splitlines()), 2)
1659
out, err = self.run_bzr(['grep', '-r', '-1', '-l', 'H.LLO'])
1661
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1662
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1663
self.assertEqual(len(out.splitlines()), 2)
1666
out, err = self.run_bzr(['grep', '-l', 'HELLO', '-r', '-1',
1667
'dir0', 'file1.txt'])
1669
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1670
self.assertEqual(len(out.splitlines()), 1)
1673
out, err = self.run_bzr(['grep', '-l', 'H.LLO', '-r', '-1',
1674
'dir0', 'file1.txt'])
1676
self.assertContainsRe(out, "^dir0/file00.txt~7$", flags=TestGrep._reflags)
1677
self.assertEqual(len(out.splitlines()), 1)
1680
out, err = self.run_bzr(['grep', '-l', 'HELLO',
1681
'-r', '-2', 'file0.txt'])
1683
self.assertContainsRe(out, "^file0.txt~6$", flags=TestGrep._reflags)
1684
self.assertEqual(len(out.splitlines()), 1)
1687
out, err = self.run_bzr(['grep', '-l', 'HE.LO',
1688
'-r', '-2', 'file0.txt'])
1690
self.assertContainsRe(out, "^file0.txt~6$", flags=TestGrep._reflags)
1691
self.assertEqual(len(out.splitlines()), 1)
1694
out, err = self.run_bzr(['grep', '--no-recursive', '-r', '-1',
1697
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1698
self.assertEqual(len(out.splitlines()), 1)
1701
out, err = self.run_bzr(['grep', '--no-recursive', '-r', '-1',
1704
self.assertContainsRe(out, "^file0.txt~7$", flags=TestGrep._reflags)
1705
self.assertEqual(len(out.splitlines()), 1)
1707
def test_wtree_files_without_matches(self):
1708
"""(wtree) Ensure --files-without-match, -L works
1711
self.make_branch_and_tree(wd)
1714
self._mk_versioned_file('file0.txt', total_lines=2)
1715
self._mk_versioned_file('file1.txt', total_lines=2)
1716
self._mk_versioned_dir('dir0')
1717
self._mk_versioned_file('dir0/file00.txt', total_lines=2)
1718
self._mk_versioned_file('dir0/file01.txt', total_lines=2)
1720
self._update_file('file0.txt', 'HELLO\n', checkin=False)
1721
self._update_file('dir0/file00.txt', 'HELLO\n', checkin=False)
1724
out, err = self.run_bzr(['grep', '--files-without-match', 'HELLO'])
1726
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1727
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1728
self.assertEqual(len(out.splitlines()), 2)
1731
out, err = self.run_bzr(['grep', '--files-without-match', 'HE.LO'])
1733
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1734
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1735
self.assertEqual(len(out.splitlines()), 2)
1738
out, err = self.run_bzr(['grep', '-L', 'HELLO'])
1740
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1741
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1742
self.assertEqual(len(out.splitlines()), 2)
1745
out, err = self.run_bzr(['grep', '-L', 'HE.LO'])
1747
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1748
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1749
self.assertEqual(len(out.splitlines()), 2)
1752
out, err = self.run_bzr(['grep', '-L', 'HELLO', 'dir0', 'file1.txt'])
1754
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1755
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1756
self.assertEqual(len(out.splitlines()), 2)
1759
out, err = self.run_bzr(['grep', '-L', '.ELLO', 'dir0', 'file1.txt'])
1761
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1762
self.assertContainsRe(out, "^dir0/file01.txt$", flags=TestGrep._reflags)
1763
self.assertEqual(len(out.splitlines()), 2)
1766
out, err = self.run_bzr(['grep', '-L', 'HELLO', 'file1.txt'])
1768
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1769
self.assertEqual(len(out.splitlines()), 1)
1772
out, err = self.run_bzr(['grep', '-L', '.ELLO', 'file1.txt'])
1774
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1775
self.assertEqual(len(out.splitlines()), 1)
1778
out, err = self.run_bzr(['grep', '--no-recursive', '-L', 'HELLO'])
1780
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1781
self.assertEqual(len(out.splitlines()), 1)
1784
out, err = self.run_bzr(['grep', '--no-recursive', '-L', '.ELLO'])
1786
self.assertContainsRe(out, "^file1.txt$", flags=TestGrep._reflags)
1787
self.assertEqual(len(out.splitlines()), 1)
1789
def test_ver_files_without_matches(self):
1790
"""(ver) Ensure --files-without-match, -L works
1793
self.make_branch_and_tree(wd)
1796
self._mk_versioned_file('file0.txt', total_lines=2) # rev 1
1797
self._mk_versioned_file('file1.txt', total_lines=2) # rev 2
1798
self._mk_versioned_dir('dir0') # rev 3
1799
self._mk_versioned_file('dir0/file00.txt', total_lines=2) # rev 4
1800
self._mk_versioned_file('dir0/file01.txt', total_lines=2) # rev 5
1802
self._update_file('file0.txt', 'HELLO\n') # rev 6
1803
self._update_file('dir0/file00.txt', 'HELLO\n') # rev 7
1806
out, err = self.run_bzr(['grep', '-r', '-1', '--files-without-match',
1809
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1810
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1811
self.assertEqual(len(out.splitlines()), 2)
1814
out, err = self.run_bzr(['grep', '-r', '-1', '--files-without-match',
1817
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1818
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1819
self.assertEqual(len(out.splitlines()), 2)
1822
out, err = self.run_bzr(['grep', '-r', '6..7', '--files-without-match',
1825
self.assertContainsRe(out, "^file1.txt~6$", flags=TestGrep._reflags)
1826
self.assertContainsRe(out, "^dir0/file00.txt~6$", flags=TestGrep._reflags)
1827
self.assertContainsRe(out, "^dir0/file01.txt~6$", flags=TestGrep._reflags)
1828
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1829
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1830
self.assertEqual(len(out.splitlines()), 5)
1833
out, err = self.run_bzr(['grep', '-r', '6..7', '--files-without-match',
1836
self.assertContainsRe(out, "^file1.txt~6$", flags=TestGrep._reflags)
1837
self.assertContainsRe(out, "^dir0/file00.txt~6$", flags=TestGrep._reflags)
1838
self.assertContainsRe(out, "^dir0/file01.txt~6$", flags=TestGrep._reflags)
1839
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1840
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1841
self.assertEqual(len(out.splitlines()), 5)
1844
out, err = self.run_bzr(['grep', '-r', '-1', '-L', 'HELLO'])
1846
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1847
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1848
self.assertEqual(len(out.splitlines()), 2)
1851
out, err = self.run_bzr(['grep', '-r', '-1', '-L', 'H.LLO'])
1853
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1854
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1855
self.assertEqual(len(out.splitlines()), 2)
1858
out, err = self.run_bzr(['grep', '-L', 'HELLO', '-r', '-1',
1859
'dir0', 'file1.txt'])
1861
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1862
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1863
self.assertEqual(len(out.splitlines()), 2)
1866
out, err = self.run_bzr(['grep', '-L', 'H.LLO', '-r', '-1',
1867
'dir0', 'file1.txt'])
1869
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1870
self.assertContainsRe(out, "^dir0/file01.txt~7$", flags=TestGrep._reflags)
1871
self.assertEqual(len(out.splitlines()), 2)
1874
out, err = self.run_bzr(['grep', '-L', 'HELLO',
1875
'-r', '-2', 'file1.txt'])
1877
self.assertContainsRe(out, "^file1.txt~6$", flags=TestGrep._reflags)
1878
self.assertEqual(len(out.splitlines()), 1)
1881
out, err = self.run_bzr(['grep', '-L', 'HE.LO',
1882
'-r', '-2', 'file1.txt'])
1884
self.assertContainsRe(out, "^file1.txt~6$", flags=TestGrep._reflags)
1885
self.assertEqual(len(out.splitlines()), 1)
1888
out, err = self.run_bzr(['grep', '--no-recursive', '-r', '-1',
1891
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1892
self.assertEqual(len(out.splitlines()), 1)
1895
out, err = self.run_bzr(['grep', '--no-recursive', '-r', '-1',
1898
self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1899
self.assertEqual(len(out.splitlines()), 1)
1901
def test_no_tree(self):
1902
"""Ensure grep works without working tree.
1906
self.make_branch_and_tree(wd0)
1908
self._mk_versioned_file('file0.txt')
1910
out, err = self.run_bzr(['branch', '--no-tree', wd0, wd1])
1913
out, err = self.run_bzr(['grep', 'line1'], 3)
1914
self.assertContainsRe(err, "Cannot search working tree", flags=TestGrep._reflags)
1915
self.assertEqual(out, '')
1917
out, err = self.run_bzr(['grep', '-r', '1', 'line1'])
1918
self.assertContainsRe(out, "file0.txt~1:line1", flags=TestGrep._reflags)
1919
self.assertEqual(len(out.splitlines()), 2) # finds line1 and line10
1922
class TestNonAscii(GrepTestBase):
1923
"""Tests for non-ascii filenames and file contents"""
1925
_test_needs_features = [UnicodeFilenameFeature]
1927
def test_unicode_only_file(self):
1928
"""Test filename and contents that requires a unicode encoding"""
1929
tree = self.make_branch_and_tree(".")
1930
contents = [u"\u1234"]
1931
self.build_tree(contents)
1933
tree.commit("Initial commit")
1934
as_utf8 = u"\u1234".encode("UTF-8")
1936
# GZ 2010-06-07: Note we can't actually grep for \u1234 as the pattern
1937
# is mangled according to the user encoding.
1938
streams = self.run_bzr(["grep", "--files-with-matches",
1939
u"contents"], encoding="UTF-8")
1940
self.assertEqual(streams, (as_utf8 + "\n", ""))
1942
streams = self.run_bzr(["grep", "-r", "1", "--files-with-matches",
1943
u"contents"], encoding="UTF-8")
1944
self.assertEqual(streams, (as_utf8 + "~1\n", ""))
1946
fileencoding = osutils.get_user_encoding()
1947
as_mangled = as_utf8.decode(fileencoding, "replace").encode("UTF-8")
1949
streams = self.run_bzr(["grep", "-n",
1950
u"contents"], encoding="UTF-8")
1951
self.assertEqual(streams, ("%s:1:contents of %s\n" %
1952
(as_utf8, as_mangled), ""))
1954
streams = self.run_bzr(["grep", "-n", "-r", "1",
1955
u"contents"], encoding="UTF-8")
1956
self.assertEqual(streams, ("%s~1:1:contents of %s\n" %
1957
(as_utf8, as_mangled), ""))
1960
class TestColorGrep(GrepTestBase):
1961
"""Tests for the --color option."""
1963
# GZ 2010-06-05: Does this really require the feature? Nothing prints.
1964
_test_needs_features = [ColorFeature]
1966
_rev_sep = color_string('~', fg=FG.BOLD_YELLOW)
1967
_sep = color_string(':', fg=FG.BOLD_CYAN)
1969
def test_color_option(self):
1970
"""Ensure options for color are valid.
1972
out, err = self.run_bzr(['grep', '--color', 'foo', 'bar'], 3)
1973
self.assertEqual(out, '')
1974
self.assertContainsRe(err, 'Valid values for --color are', flags=TestGrep._reflags)
1976
def test_ver_matching_files(self):
1977
"""(versioned) Search for matches or no matches only"""
1978
tree = self.make_branch_and_tree(".")
1979
contents = ["d/", "d/aaa", "bbb"]
1980
self.build_tree(contents)
1982
tree.commit("Initial commit")
1984
# GZ 2010-06-05: Maybe modify the working tree here
1986
streams = self.run_bzr(["grep", "--color", "always", "-r", "1",
1987
"--files-with-matches", "aaa"])
1988
self.assertEqual(streams, ("".join([
1989
FG.MAGENTA, "d/aaa", self._rev_sep, "1", "\n"
1992
streams = self.run_bzr(["grep", "--color", "always", "-r", "1",
1993
"--files-without-match", "aaa"])
1994
self.assertEqual(streams, ("".join([
1995
FG.MAGENTA, "bbb", self._rev_sep, "1", "\n"
1998
def test_wtree_matching_files(self):
1999
"""(wtree) Search for matches or no matches only"""
2000
tree = self.make_branch_and_tree(".")
2001
contents = ["d/", "d/aaa", "bbb"]
2002
self.build_tree(contents)
2004
tree.commit("Initial commit")
2006
# GZ 2010-06-05: Maybe modify the working tree here
2008
streams = self.run_bzr(["grep", "--color", "always",
2009
"--files-with-matches", "aaa"])
2010
self.assertEqual(streams, ("".join([
2011
FG.MAGENTA, "d/aaa", FG.NONE, "\n"
2014
streams = self.run_bzr(["grep", "--color", "always",
2015
"--files-without-match", "aaa"])
2016
self.assertEqual(streams, ("".join([
2017
FG.MAGENTA, "bbb", FG.NONE, "\n"
2020
def test_ver_basic_file(self):
2021
"""(versioned) Search for pattern in specfic file.
2024
self.make_branch_and_tree(wd)
2026
lp = 'foo is foobar'
2027
self._mk_versioned_file('file0.txt', line_prefix=lp, total_lines=1)
2029
# prepare colored result
2030
foo = color_string('foo', fg=FG.BOLD_RED)
2031
res = (FG.MAGENTA + 'file0.txt'
2032
+ self._rev_sep + '1' + self._sep
2033
+ foo + ' is ' + foo + 'bar1' + '\n')
2034
txt_res = 'file0.txt~1:foo is foobar1\n'
2036
nres = (FG.MAGENTA + 'file0.txt'
2037
+ self._rev_sep + '1' + self._sep + '1' + self._sep
2038
+ foo + ' is ' + foo + 'bar1' + '\n')
2040
out, err = self.run_bzr(['grep', '--color',
2041
'always', '-r', '1', 'foo'])
2042
self.assertEqual(out, res)
2043
self.assertEqual(len(out.splitlines()), 1)
2045
# auto should produce plain text result
2046
# as stdout is redireched here.
2047
out, err = self.run_bzr(['grep', '--color',
2048
'auto', '-r', '1', 'foo'])
2049
self.assertEqual(out, txt_res)
2050
self.assertEqual(len(out.splitlines()), 1)
2052
out, err = self.run_bzr(['grep', '-i', '--color',
2053
'always', '-r', '1', 'FOO'])
2054
self.assertEqual(out, res)
2055
self.assertEqual(len(out.splitlines()), 1)
2057
out, err = self.run_bzr(['grep', '--color',
2058
'always', '-r', '1', 'f.o'])
2059
self.assertEqual(out, res)
2060
self.assertEqual(len(out.splitlines()), 1)
2062
out, err = self.run_bzr(['grep', '-i', '--color',
2063
'always', '-r', '1', 'F.O'])
2064
self.assertEqual(out, res)
2065
self.assertEqual(len(out.splitlines()), 1)
2067
out, err = self.run_bzr(['grep', '-n', '--color',
2068
'always', '-r', '1', 'foo'])
2069
self.assertEqual(out, nres)
2070
self.assertEqual(len(out.splitlines()), 1)
2072
out, err = self.run_bzr(['grep', '-n', '-i', '--color',
2073
'always', '-r', '1', 'FOO'])
2074
self.assertEqual(out, nres)
2075
self.assertEqual(len(out.splitlines()), 1)
2077
out, err = self.run_bzr(['grep', '-n', '--color',
2078
'always', '-r', '1', 'f.o'])
2079
self.assertEqual(out, nres)
2080
self.assertEqual(len(out.splitlines()), 1)
2082
out, err = self.run_bzr(['grep', '-n', '-i', '--color',
2083
'always', '-r', '1', 'F.O'])
2084
self.assertEqual(out, nres)
2085
self.assertEqual(len(out.splitlines()), 1)
2087
def test_wtree_basic_file(self):
2088
"""(wtree) Search for pattern in specfic file.
2091
self.make_branch_and_tree(wd)
2093
lp = 'foo is foobar'
2094
self._mk_versioned_file('file0.txt', line_prefix=lp, total_lines=1)
2096
# prepare colored result
2097
foo = color_string('foo', fg=FG.BOLD_RED)
2098
res = (FG.MAGENTA + 'file0.txt'
2099
+ self._sep + foo + ' is ' + foo + 'bar1' + '\n')
2101
nres = (FG.MAGENTA + 'file0.txt'
2102
+ self._sep + '1' + self._sep
2103
+ foo + ' is ' + foo + 'bar1' + '\n')
2105
out, err = self.run_bzr(['grep', '--color',
2107
self.assertEqual(out, res)
2108
self.assertEqual(len(out.splitlines()), 1)
2110
out, err = self.run_bzr(['grep', '-i', '--color',
2112
self.assertEqual(out, res)
2113
self.assertEqual(len(out.splitlines()), 1)
2115
out, err = self.run_bzr(['grep', '--color',
2117
self.assertEqual(out, res)
2118
self.assertEqual(len(out.splitlines()), 1)
2120
out, err = self.run_bzr(['grep', '-i', '--color',
2122
self.assertEqual(out, res)
2123
self.assertEqual(len(out.splitlines()), 1)
2125
out, err = self.run_bzr(['grep', '-n', '--color',
2127
self.assertEqual(out, nres)
2128
self.assertEqual(len(out.splitlines()), 1)
2130
out, err = self.run_bzr(['grep', '-n', '-i', '--color',
2132
self.assertEqual(out, nres)
2133
self.assertEqual(len(out.splitlines()), 1)
2135
out, err = self.run_bzr(['grep', '-n', '--color',
2137
self.assertEqual(out, nres)
2138
self.assertEqual(len(out.splitlines()), 1)
2140
out, err = self.run_bzr(['grep', '-n', '-i', '--color',
2142
self.assertEqual(out, nres)
2143
self.assertEqual(len(out.splitlines()), 1)
2146
# copied from bzrlib.tests.blackbox.test_diff
2147
def subst_dates(string):
2148
"""Replace date strings with constant values."""
2149
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
2150
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
2153
class TestGrepDiff(tests.TestCaseWithTransport):
2155
def make_example_branch(self):
2156
tree = self.make_branch_and_tree('.')
2157
self.build_tree_contents([
2159
('goodbye', 'baz\n')])
2161
tree.commit('setup')
2162
tree.add(['goodbye'])
2163
tree.commit('setup')
2166
def test_grep_diff_basic(self):
2167
"""grep -p basic test."""
2168
tree = self.make_example_branch()
2169
self.build_tree_contents([('hello', 'hello world!\n')])
2170
tree.commit('updated hello')
2171
out, err = self.run_bzr(['grep', '-p', 'hello'])
2172
self.assertEquals(err, '')
2173
self.assertEqualDiff(subst_dates(out), '''\
2175
=== modified file 'hello'
2176
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2177
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2180
=== added file 'hello'
2181
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2182
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2185
def test_grep_diff_revision(self):
2186
"""grep -p specific revision."""
2187
tree = self.make_example_branch()
2188
self.build_tree_contents([('hello', 'hello world!\n')])
2189
tree.commit('updated hello')
2190
out, err = self.run_bzr(['grep', '-p', '-r', '3', 'hello'])
2191
self.assertEquals(err, '')
2192
self.assertEqualDiff(subst_dates(out), '''\
2194
=== modified file 'hello'
2195
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2196
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2200
def test_grep_diff_revision_range(self):
2201
"""grep -p revision range."""
2202
tree = self.make_example_branch()
2203
self.build_tree_contents([('hello', 'hello world!1\n')]) # rev 3
2205
self.build_tree_contents([('blah', 'hello world!2\n')]) # rev 4
2208
open('hello', 'a').write('hello world!3\n')
2209
#self.build_tree_contents([('hello', 'hello world!3\n')]) # rev 5
2211
out, err = self.run_bzr(['grep', '-p', '-r', '2..5', 'hello'])
2212
self.assertEquals(err, '')
2213
self.assertEqualDiff(subst_dates(out), '''\
2215
=== modified file 'hello'
2216
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2217
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2220
=== added file 'blah'
2223
=== modified file 'hello'
2224
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2225
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2229
def test_grep_diff_color(self):
2230
"""grep -p color test."""
2231
tree = self.make_example_branch()
2232
self.build_tree_contents([('hello', 'hello world!\n')])
2233
tree.commit('updated hello')
2234
out, err = self.run_bzr(['grep', '--diff', '-r', '3',
2235
'--color', 'always', 'hello'])
2236
self.assertEquals(err, '')
2237
revno = color_string('=== revno:3 ===', fg=FG.BOLD_BLUE) + '\n'
2238
filename = color_string(" === modified file 'hello'", fg=FG.BOLD_MAGENTA) + '\n'
2239
redhello = color_string('hello', fg=FG.BOLD_RED)
2241
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2242
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2245
diffstr = diffstr.replace('hello', redhello)
2246
self.assertEqualDiff(subst_dates(out), revno + filename + diffstr)
2248
def test_grep_norevs(self):
2249
"""grep -p with zero revisions."""
2250
out, err = self.run_bzr(['init'])
2251
out, err = self.run_bzr(['grep', '--diff', 'foo'], 3)
2252
self.assertEquals(out, '')
2253
self.assertContainsRe(err, "ERROR:.*revision.* does not exist in branch")