~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to test_grep.py

  • Committer: Parth Malwankar
  • Date: 2010-05-22 12:27:08 UTC
  • mto: (0.40.129 grep)
  • mto: This revision was merged to the branch mainline in revision 6555.
  • Revision ID: parth.malwankar@gmail.com-20100522122708-eszm3wzmhn4122d8
test cases for colored results.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import re
19
19
import unicodedata as ud
20
20
 
 
21
from termcolor import color_string, FG
21
22
from bzrlib import tests, osutils
22
23
 
23
24
# NOTE: As bzr-grep optimizes PATTERN search to -F/--fixed-string
25
26
# specfically with patterns that have special characters so that
26
27
# regex path is tested. alphanumeric patterns test the -F path.
27
28
 
28
 
class TestGrep(tests.TestCaseWithTransport):
 
29
class GrepTestBase(tests.TestCaseWithTransport):
 
30
    """Base class for testing grep.
 
31
 
 
32
    Provides support methods for creating directory and file revisions.
 
33
    """
29
34
    _reflags = re.MULTILINE|re.DOTALL
30
35
 
31
36
    def _mk_file(self, path, line_prefix, total_lines, versioned):
62
67
    def _mk_versioned_dir(self, path):
63
68
        self._mk_dir(path, versioned=True)
64
69
 
 
70
class TestGrep(GrepTestBase):
 
71
    """Core functional tests for grep."""
 
72
 
65
73
    def test_basic_unknown_file(self):
66
74
        """Search for pattern in specfic file.
67
75
 
1904
1912
        self.assertContainsRe(out, "^file1.txt~7$", flags=TestGrep._reflags)
1905
1913
        self.assertEqual(len(out.splitlines()), 1)
1906
1914
 
 
1915
 
 
1916
class TestColorGrep(GrepTestBase):
 
1917
    """Tests for the --color option."""
 
1918
    _rev_sep = color_string('~', fg=FG.BOLD_YELLOW)
 
1919
    _sep = color_string(':', fg=FG.BOLD_CYAN)
 
1920
 
 
1921
    def test_color_option(self):
 
1922
        """Ensure options for color are valid.
 
1923
        """
 
1924
        out, err = self.run_bzr(['grep', '--color', 'foo', 'bar'], 3)
 
1925
        self.assertEqual(out, '')
 
1926
        self.assertContainsRe(err, 'Valid values for --color are', flags=TestGrep._reflags)
 
1927
 
 
1928
    def test_ver_basic_file(self):
 
1929
        """(versioned) Search for pattern in specfic file.
 
1930
        """
 
1931
        wd = 'foobar0'
 
1932
        self.make_branch_and_tree(wd)
 
1933
        os.chdir(wd)
 
1934
        lp = 'foo is foobar'
 
1935
        self._mk_versioned_file('file0.txt', line_prefix=lp, total_lines=1)
 
1936
 
 
1937
        # prepare colored result
 
1938
        foo = color_string('foo', fg=FG.BOLD_RED)
 
1939
        res = (color_string('file0.txt', fg=FG.MAGENTA)
 
1940
            + self._rev_sep + '1' + self._sep
 
1941
            + foo + ' is ' + foo + 'bar1' + '\n')
 
1942
 
 
1943
        nres = (color_string('file0.txt', fg=FG.MAGENTA)
 
1944
            + self._rev_sep + '1' + self._sep + '1' + self._sep
 
1945
            + foo + ' is ' + foo + 'bar1' + '\n')
 
1946
 
 
1947
        out, err = self.run_bzr(['grep', '--color',
 
1948
            'always', '-r', '1', 'foo'])
 
1949
        self.assertEqual(out, res)
 
1950
        self.assertEqual(len(out.splitlines()), 1)
 
1951
 
 
1952
        out, err = self.run_bzr(['grep', '-i', '--color',
 
1953
            'always', '-r', '1', 'FOO'])
 
1954
        self.assertEqual(out, res)
 
1955
        self.assertEqual(len(out.splitlines()), 1)
 
1956
 
 
1957
        out, err = self.run_bzr(['grep', '--color',
 
1958
            'always', '-r', '1', 'f.o'])
 
1959
        self.assertEqual(out, res)
 
1960
        self.assertEqual(len(out.splitlines()), 1)
 
1961
 
 
1962
        out, err = self.run_bzr(['grep', '-i', '--color',
 
1963
            'always', '-r', '1', 'F.O'])
 
1964
        self.assertEqual(out, res)
 
1965
        self.assertEqual(len(out.splitlines()), 1)
 
1966
 
 
1967
        out, err = self.run_bzr(['grep', '-n', '--color',
 
1968
            'always', '-r', '1', 'foo'])
 
1969
        self.assertEqual(out, nres)
 
1970
        self.assertEqual(len(out.splitlines()), 1)
 
1971
 
 
1972
        out, err = self.run_bzr(['grep', '-n', '-i', '--color',
 
1973
            'always', '-r', '1', 'FOO'])
 
1974
        self.assertEqual(out, nres)
 
1975
        self.assertEqual(len(out.splitlines()), 1)
 
1976
 
 
1977
        out, err = self.run_bzr(['grep', '-n', '--color',
 
1978
            'always', '-r', '1', 'f.o'])
 
1979
        self.assertEqual(out, nres)
 
1980
        self.assertEqual(len(out.splitlines()), 1)
 
1981
 
 
1982
        out, err = self.run_bzr(['grep', '-n', '-i', '--color',
 
1983
            'always', '-r', '1', 'F.O'])
 
1984
        self.assertEqual(out, nres)
 
1985
        self.assertEqual(len(out.splitlines()), 1)
 
1986
 
 
1987
    def test_wtree_basic_file(self):
 
1988
        """(wtree) Search for pattern in specfic file.
 
1989
        """
 
1990
        wd = 'foobar0'
 
1991
        self.make_branch_and_tree(wd)
 
1992
        os.chdir(wd)
 
1993
        lp = 'foo is foobar'
 
1994
        self._mk_versioned_file('file0.txt', line_prefix=lp, total_lines=1)
 
1995
 
 
1996
        # prepare colored result
 
1997
        foo = color_string('foo', fg=FG.BOLD_RED)
 
1998
        res = (color_string('file0.txt', fg=FG.MAGENTA)
 
1999
            + self._sep + foo + ' is ' + foo + 'bar1' + '\n')
 
2000
 
 
2001
        nres = (color_string('file0.txt', fg=FG.MAGENTA)
 
2002
            + self._sep + '1' + self._sep
 
2003
            + foo + ' is ' + foo + 'bar1' + '\n')
 
2004
 
 
2005
        out, err = self.run_bzr(['grep', '--color',
 
2006
            'always', 'foo'])
 
2007
        self.assertEqual(out, res)
 
2008
        self.assertEqual(len(out.splitlines()), 1)
 
2009
 
 
2010
        out, err = self.run_bzr(['grep', '-i', '--color',
 
2011
            'always', 'FOO'])
 
2012
        self.assertEqual(out, res)
 
2013
        self.assertEqual(len(out.splitlines()), 1)
 
2014
 
 
2015
        out, err = self.run_bzr(['grep', '--color',
 
2016
            'always', 'f.o'])
 
2017
        self.assertEqual(out, res)
 
2018
        self.assertEqual(len(out.splitlines()), 1)
 
2019
 
 
2020
        out, err = self.run_bzr(['grep', '-i', '--color',
 
2021
            'always', 'F.O'])
 
2022
        self.assertEqual(out, res)
 
2023
        self.assertEqual(len(out.splitlines()), 1)
 
2024
 
 
2025
        out, err = self.run_bzr(['grep', '-n', '--color',
 
2026
            'always', 'foo'])
 
2027
        self.assertEqual(out, nres)
 
2028
        self.assertEqual(len(out.splitlines()), 1)
 
2029
 
 
2030
        out, err = self.run_bzr(['grep', '-n', '-i', '--color',
 
2031
            'always', 'FOO'])
 
2032
        self.assertEqual(out, nres)
 
2033
        self.assertEqual(len(out.splitlines()), 1)
 
2034
 
 
2035
        out, err = self.run_bzr(['grep', '-n', '--color',
 
2036
            'always', 'f.o'])
 
2037
        self.assertEqual(out, nres)
 
2038
        self.assertEqual(len(out.splitlines()), 1)
 
2039
 
 
2040
        out, err = self.run_bzr(['grep', '-n', '-i', '--color',
 
2041
            'always', 'F.O'])
 
2042
        self.assertEqual(out, nres)
 
2043
        self.assertEqual(len(out.splitlines()), 1)
 
2044