~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to test_grep.py

(Martin [gz]) Add seperate output formatter
to reduce duplication of search loops, additionally
make -Fi use regexp rather than lowercasing pattern
and entirety of text for the same reason.
This also fixed bug #590589.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1936
1936
        self.assertEqual(len(out.splitlines()), 2) # finds line1 and line10
1937
1937
 
1938
1938
 
 
1939
class TestNonAscii(GrepTestBase):
 
1940
    """Tests for non-ascii filenames and file contents"""
 
1941
 
 
1942
    _test_needs_features = [tests.UnicodeFilenameFeature]
 
1943
 
 
1944
    def test_unicode_only_file(self):
 
1945
        """Test filename and contents that requires a unicode encoding"""
 
1946
        tree = self.make_branch_and_tree(".")
 
1947
        contents = [u"\u1234"]
 
1948
        self.build_tree(contents)
 
1949
        tree.add(contents)
 
1950
        tree.commit("Initial commit")
 
1951
        as_utf8 = u"\u1234".encode("UTF-8")
 
1952
 
 
1953
        # GZ 2010-06-07: Note we can't actually grep for \u1234 as the pattern
 
1954
        #                is mangled according to the user encoding.
 
1955
        streams = self.run_bzr(["grep", "--files-with-matches",
 
1956
            u"contents"], encoding="UTF-8")
 
1957
        self.assertEqual(streams, (as_utf8 + "\n", ""))
 
1958
 
 
1959
        streams = self.run_bzr(["grep", "-r", "1", "--files-with-matches",
 
1960
            u"contents"], encoding="UTF-8")
 
1961
        self.assertEqual(streams, (as_utf8 + "~1\n", ""))
 
1962
 
 
1963
        fileencoding = osutils.get_user_encoding()
 
1964
        as_mangled = as_utf8.decode(fileencoding, "replace").encode("UTF-8")
 
1965
 
 
1966
        streams = self.run_bzr(["grep", "-n",
 
1967
            u"contents"], encoding="UTF-8")
 
1968
        self.assertEqual(streams, ("%s:1:contents of %s\n" %
 
1969
            (as_utf8, as_mangled), ""))
 
1970
 
 
1971
        streams = self.run_bzr(["grep", "-n", "-r", "1",
 
1972
            u"contents"], encoding="UTF-8")
 
1973
        self.assertEqual(streams, ("%s~1:1:contents of %s\n" %
 
1974
            (as_utf8, as_mangled), ""))
 
1975
 
 
1976
 
1939
1977
class TestColorGrep(GrepTestBase):
1940
1978
    """Tests for the --color option."""
1941
1979
 
 
1980
    # GZ 2010-06-05: Does this really require the feature? Nothing prints.
1942
1981
    _test_needs_features = [features.color_feature]
1943
1982
 
1944
1983
    _rev_sep = color_string('~', fg=FG.BOLD_YELLOW)
1951
1990
        self.assertEqual(out, '')
1952
1991
        self.assertContainsRe(err, 'Valid values for --color are', flags=TestGrep._reflags)
1953
1992
 
 
1993
    def test_ver_matching_files(self):
 
1994
        """(versioned) Search for matches or no matches only"""
 
1995
        tree = self.make_branch_and_tree(".")
 
1996
        contents = ["d/", "d/aaa", "bbb"]
 
1997
        self.build_tree(contents)
 
1998
        tree.add(contents)
 
1999
        tree.commit("Initial commit")
 
2000
 
 
2001
        # GZ 2010-06-05: Maybe modify the working tree here
 
2002
 
 
2003
        streams = self.run_bzr(["grep", "--color", "always", "-r", "1",
 
2004
            "--files-with-matches", "aaa"])
 
2005
        self.assertEqual(streams, ("".join([
 
2006
            FG.MAGENTA, "d/aaa", self._rev_sep, "1", "\n"
 
2007
            ]), ""))
 
2008
 
 
2009
        streams = self.run_bzr(["grep", "--color", "always", "-r", "1",
 
2010
            "--files-without-match", "aaa"])
 
2011
        self.assertEqual(streams, ("".join([
 
2012
            FG.MAGENTA, "bbb", self._rev_sep, "1", "\n"
 
2013
            ]), ""))
 
2014
 
 
2015
    def test_wtree_matching_files(self):
 
2016
        """(wtree) Search for matches or no matches only"""
 
2017
        tree = self.make_branch_and_tree(".")
 
2018
        contents = ["d/", "d/aaa", "bbb"]
 
2019
        self.build_tree(contents)
 
2020
        tree.add(contents)
 
2021
        tree.commit("Initial commit")
 
2022
 
 
2023
        # GZ 2010-06-05: Maybe modify the working tree here
 
2024
 
 
2025
        streams = self.run_bzr(["grep", "--color", "always",
 
2026
            "--files-with-matches", "aaa"])
 
2027
        self.assertEqual(streams, ("".join([
 
2028
            FG.MAGENTA, "d/aaa", FG.NONE, "\n"
 
2029
            ]), ""))
 
2030
 
 
2031
        streams = self.run_bzr(["grep", "--color", "always",
 
2032
            "--files-without-match", "aaa"])
 
2033
        self.assertEqual(streams, ("".join([
 
2034
            FG.MAGENTA, "bbb", FG.NONE, "\n"
 
2035
            ]), ""))
 
2036
 
1954
2037
    def test_ver_basic_file(self):
1955
2038
        """(versioned) Search for pattern in specfic file.
1956
2039
        """
1962
2045
 
1963
2046
        # prepare colored result
1964
2047
        foo = color_string('foo', fg=FG.BOLD_RED)
1965
 
        res = (color_string('file0.txt', fg=FG.MAGENTA)
 
2048
        res = (FG.MAGENTA + 'file0.txt'
1966
2049
            + self._rev_sep + '1' + self._sep
1967
2050
            + foo + ' is ' + foo + 'bar1' + '\n')
1968
2051
        txt_res = 'file0.txt~1:foo is foobar1\n'
1969
2052
 
1970
 
        nres = (color_string('file0.txt', fg=FG.MAGENTA)
 
2053
        nres = (FG.MAGENTA + 'file0.txt'
1971
2054
            + self._rev_sep + '1' + self._sep + '1' + self._sep
1972
2055
            + foo + ' is ' + foo + 'bar1' + '\n')
1973
2056
 
2029
2112
 
2030
2113
        # prepare colored result
2031
2114
        foo = color_string('foo', fg=FG.BOLD_RED)
2032
 
        res = (color_string('file0.txt', fg=FG.MAGENTA)
 
2115
        res = (FG.MAGENTA + 'file0.txt'
2033
2116
            + self._sep + foo + ' is ' + foo + 'bar1' + '\n')
2034
2117
 
2035
 
        nres = (color_string('file0.txt', fg=FG.MAGENTA)
 
2118
        nres = (FG.MAGENTA + 'file0.txt'
2036
2119
            + self._sep + '1' + self._sep
2037
2120
            + foo + ' is ' + foo + 'bar1' + '\n')
2038
2121