/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to test_grep.py

grep support --diff for searching changesets. (#540705)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2165
2165
        self.assertEqual(out, nres)
2166
2166
        self.assertEqual(len(out.splitlines()), 1)
2167
2167
 
 
2168
 
 
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)
 
2174
 
 
2175
 
 
2176
class TestGrepDiff(tests.TestCaseWithTransport):
 
2177
 
 
2178
    def make_example_branch(self):
 
2179
        tree = self.make_branch_and_tree('.')
 
2180
        self.build_tree_contents([
 
2181
            ('hello', 'foo\n'),
 
2182
            ('goodbye', 'baz\n')])
 
2183
        tree.add(['hello'])
 
2184
        tree.commit('setup')
 
2185
        tree.add(['goodbye'])
 
2186
        tree.commit('setup')
 
2187
        return tree
 
2188
 
 
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), '''\
 
2197
=== revno:3 ===
 
2198
  === modified file 'hello'
 
2199
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2200
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2201
    +hello world!
 
2202
=== revno:1 ===
 
2203
  === added file 'hello'
 
2204
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2205
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2206
''')
 
2207
 
 
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), '''\
 
2216
=== revno:3 ===
 
2217
  === modified file 'hello'
 
2218
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2219
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2220
    +hello world!
 
2221
''')
 
2222
 
 
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
 
2227
        tree.commit('rev3')
 
2228
        self.build_tree_contents([('blah', 'hello world!2\n')]) # rev 4
 
2229
        tree.add('blah')
 
2230
        tree.commit('rev4')
 
2231
        open('hello', 'a').write('hello world!3\n')
 
2232
        #self.build_tree_contents([('hello', 'hello world!3\n')]) # rev 5
 
2233
        tree.commit('rev5')
 
2234
        out, err = self.run_bzr(['grep', '-p', '-r', '2..5', 'hello'])
 
2235
        self.assertEquals(err, '')
 
2236
        self.assertEqualDiff(subst_dates(out), '''\
 
2237
=== revno:5 ===
 
2238
  === modified file 'hello'
 
2239
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2240
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2241
    +hello world!3
 
2242
=== revno:4 ===
 
2243
  === added file 'blah'
 
2244
    +hello world!2
 
2245
=== revno:3 ===
 
2246
  === modified file 'hello'
 
2247
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2248
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2249
    +hello world!1
 
2250
''')
 
2251
 
 
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)
 
2263
        diffstr = '''\
 
2264
    --- hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2265
    +++ hello   YYYY-MM-DD HH:MM:SS +ZZZZ
 
2266
    +hello world!
 
2267
'''
 
2268
        diffstr = diffstr.replace('hello', redhello)
 
2269
        self.assertEqualDiff(subst_dates(out), revno + filename + diffstr)
 
2270
 
 
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")
 
2277