2165
2165
self.assertEqual(out, nres)
2166
2166
self.assertEqual(len(out.splitlines()), 1)
2169
# copied from bzrlib.tests.blackbox.test_diff
2170
def subst_dates(string):
2171
"""Replace date strings with constant values."""
2172
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
2173
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
2176
class TestGrepDiff(tests.TestCaseWithTransport):
2178
def make_example_branch(self):
2179
tree = self.make_branch_and_tree('.')
2180
self.build_tree_contents([
2182
('goodbye', 'baz\n')])
2184
tree.commit('setup')
2185
tree.add(['goodbye'])
2186
tree.commit('setup')
2189
def test_grep_diff_basic(self):
2190
"""grep -p basic test."""
2191
tree = self.make_example_branch()
2192
self.build_tree_contents([('hello', 'hello world!\n')])
2193
tree.commit('updated hello')
2194
out, err = self.run_bzr(['grep', '-p', 'hello'])
2195
self.assertEquals(err, '')
2196
self.assertEqualDiff(subst_dates(out), '''\
2198
=== modified file 'hello'
2199
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2200
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2203
=== added file 'hello'
2204
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2205
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2208
def test_grep_diff_revision(self):
2209
"""grep -p specific revision."""
2210
tree = self.make_example_branch()
2211
self.build_tree_contents([('hello', 'hello world!\n')])
2212
tree.commit('updated hello')
2213
out, err = self.run_bzr(['grep', '-p', '-r', '3', 'hello'])
2214
self.assertEquals(err, '')
2215
self.assertEqualDiff(subst_dates(out), '''\
2217
=== modified file 'hello'
2218
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2219
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2223
def test_grep_diff_revision_range(self):
2224
"""grep -p revision range."""
2225
tree = self.make_example_branch()
2226
self.build_tree_contents([('hello', 'hello world!1\n')]) # rev 3
2228
self.build_tree_contents([('blah', 'hello world!2\n')]) # rev 4
2231
open('hello', 'a').write('hello world!3\n')
2232
#self.build_tree_contents([('hello', 'hello world!3\n')]) # rev 5
2234
out, err = self.run_bzr(['grep', '-p', '-r', '2..5', 'hello'])
2235
self.assertEquals(err, '')
2236
self.assertEqualDiff(subst_dates(out), '''\
2238
=== modified file 'hello'
2239
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2240
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2243
=== added file 'blah'
2246
=== modified file 'hello'
2247
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2248
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2252
def test_grep_diff_color(self):
2253
"""grep -p color test."""
2254
tree = self.make_example_branch()
2255
self.build_tree_contents([('hello', 'hello world!\n')])
2256
tree.commit('updated hello')
2257
out, err = self.run_bzr(['grep', '--diff', '-r', '3',
2258
'--color', 'always', 'hello'])
2259
self.assertEquals(err, '')
2260
revno = color_string('=== revno:3 ===', fg=FG.BOLD_BLUE) + '\n'
2261
filename = color_string(" === modified file 'hello'", fg=FG.BOLD_MAGENTA) + '\n'
2262
redhello = color_string('hello', fg=FG.BOLD_RED)
2264
--- hello YYYY-MM-DD HH:MM:SS +ZZZZ
2265
+++ hello YYYY-MM-DD HH:MM:SS +ZZZZ
2268
diffstr = diffstr.replace('hello', redhello)
2269
self.assertEqualDiff(subst_dates(out), revno + filename + diffstr)
2271
def test_grep_norevs(self):
2272
"""grep -p with zero revisions."""
2273
out, err = self.run_bzr(['init'])
2274
out, err = self.run_bzr(['grep', '--diff', 'foo'], 3)
2275
self.assertEquals(out, '')
2276
self.assertContainsRe(err, "ERROR:.*revision.* does not exist in branch")