From 8f852ce613650b0cccf02adecbc18865d8e21fb6 Mon Sep 17 00:00:00 2001 From: Michał Kiedrowicz Date: Mon, 9 May 2011 23:52:07 +0200 Subject: grep: Add basic tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This modest patch adds simple tests for git grep -P/--perl-regexp and its interoperation with -i and -w. Tests are only enabled when prerequisite LIBPCRE is defined (it's automatically set based on USE_LIBPCRE in test-lib.sh). Signed-off-by: Michał Kiedrowicz Signed-off-by: Junio C Hamano --- t/README | 5 +++++ t/t7810-grep.sh | 38 ++++++++++++++++++++++++++++++++++++++ t/test-lib.sh | 1 + 3 files changed, 44 insertions(+) (limited to 't') diff --git a/t/README b/t/README index 428ee05c4a..238729c5b7 100644 --- a/t/README +++ b/t/README @@ -587,6 +587,11 @@ use these, and "test_set_prereq" for how to define your own. Test is not run by root user, and an attempt to write to an unwritable file is expected to fail correctly. + - LIBPCRE + + Git was compiled with USE_LIBPCRE=YesPlease. Wrap any tests + that use git-grep --perl-regexp or git-grep -P in these. + Tips for Writing Tests ---------------------- diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index 8184c264cf..e845218f67 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -26,6 +26,12 @@ test_expect_success setup ' echo foo mmap bar_mmap echo foo_mmap bar mmap baz } >file && + { + echo Hello world + echo HeLLo world + echo Hello_world + echo HeLLo_world + } >hello_world && echo vvv >v && echo ww w >w && echo x x xx x >x && @@ -599,4 +605,36 @@ test_expect_success 'grep -e -- -- path' ' test_cmp expected actual ' +cat >expected <actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P pattern' ' + git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -i pattern' ' + { + echo "hello.c: printf(\"Hello world.\n\");" + } >expected && + git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -w pattern' ' + { + echo "hello_world:Hello world" + echo "hello_world:HeLLo world" + } >expected && + git grep -P -w "He((?i)ll)o" hello_world >actual && + test_cmp expected actual +' + test_done diff --git a/t/test-lib.sh b/t/test-lib.sh index abc47f3abc..d3ed59803f 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -1067,6 +1067,7 @@ esac test -z "$NO_PERL" && test_set_prereq PERL test -z "$NO_PYTHON" && test_set_prereq PYTHON +test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE # Can we rely on git's output in the C locale? if test -n "$GETTEXT_POISON" -- cgit v1.3 From 258a6188496fe5131203905b6cd596af69312247 Mon Sep 17 00:00:00 2001 From: Michał Kiedrowicz Date: Mon, 9 May 2011 23:52:08 +0200 Subject: git-grep: Bail out when -P is used with -F or -E MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch makes git-grep die() when -P is used on command line together with -E/--extended-regexp or -F/--fixed-strings. This also makes it bail out when grep.extendedRegexp is enabled. But `git grep -G -P pattern` and `git grep -E -G -P pattern` still work because -G and -E set opts.regflags during parse_options() and there is no way to detect `-G` or `-E -G`. Signed-off-by: Michał Kiedrowicz Signed-off-by: Junio C Hamano --- builtin/grep.c | 4 +++- t/t7810-grep.sh | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 't') diff --git a/builtin/grep.c b/builtin/grep.c index 6831975104..8f2602653e 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -925,9 +925,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix) if (!opt.pattern_list) die(_("no pattern given.")); + if (opt.regflags != REG_NEWLINE && opt.pcre) + die(_("cannot mix --extended-regexp and --perl-regexp")); if (!opt.fixed && opt.ignore_case) opt.regflags |= REG_ICASE; - if ((opt.regflags != REG_NEWLINE) && opt.fixed) + if ((opt.regflags != REG_NEWLINE || opt.pcre) && opt.fixed) die(_("cannot mix --fixed-strings and regexp")); #ifndef NO_PTHREADS diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index e845218f67..2a31eca5f2 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -637,4 +637,20 @@ test_expect_success LIBPCRE 'grep -P -w pattern' ' test_cmp expected actual ' +test_expect_success LIBPCRE 'grep -P -F returns error' ' + test_expect_code 128 git grep -P -F main +' + +test_expect_success LIBPCRE 'grep -P -E returns error' ' + test_expect_code 128 git grep -P -E main +' + +test_expect_failure LIBPCRE 'grep -P -G returns error' ' + test_expect_code 128 git grep -P -G main +' + +test_expect_failure LIBPCRE 'grep -P -E -G returns error' ' + test_expect_code 128 git grep -P -E -G main +' + test_done -- cgit v1.3 From dd0a21ede0998ec790c37865c7dc119814ac7745 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 May 2011 00:10:14 -0700 Subject: git-grep: update tests now regexp type is "last one wins" Signed-off-by: Junio C Hamano --- t/t7810-grep.sh | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 't') diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index 2a31eca5f2..e845218f67 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -637,20 +637,4 @@ test_expect_success LIBPCRE 'grep -P -w pattern' ' test_cmp expected actual ' -test_expect_success LIBPCRE 'grep -P -F returns error' ' - test_expect_code 128 git grep -P -F main -' - -test_expect_success LIBPCRE 'grep -P -E returns error' ' - test_expect_code 128 git grep -P -E main -' - -test_expect_failure LIBPCRE 'grep -P -G returns error' ' - test_expect_code 128 git grep -P -G main -' - -test_expect_failure LIBPCRE 'grep -P -E -G returns error' ' - test_expect_code 128 git grep -P -E -G main -' - test_done -- cgit v1.3 From f556e4af2790bc8f0918093bd2b3053c7d2898dc Mon Sep 17 00:00:00 2001 From: Michał Kiedrowicz Date: Sun, 22 May 2011 13:37:28 +0200 Subject: git-grep: Update tests (mainly for -P) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add few more tests for "-P/--perl-regexp" option of "git grep". While at it, add some generic tests for grep.extendedRegexp config option, for detecting invalid regexep and check if "last one wins" rule works for selecting regexp type. Signed-off-by: Michał Kiedrowicz Signed-off-by: Junio C Hamano --- t/t7810-grep.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 't') diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index e845218f67..e061108a64 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -32,6 +32,11 @@ test_expect_success setup ' echo Hello_world echo HeLLo_world } >hello_world && + { + echo aab + echo a+b + echo a\\+b + } >ab && echo vvv >v && echo ww w >w && echo x x xx x >x && @@ -227,7 +232,17 @@ do git grep --max-depth 0 -n -e vvv $H -- t . >actual && test_cmp expected actual ' + test_expect_success "grep $L with grep.extendedRegexp=false" ' + echo "ab:a+b" >expected && + git -c grep.extendedRegexp=false grep "a+b" >actual && + test_cmp expected actual + ' + test_expect_success "grep $L with grep.extendedRegexp=true" ' + echo "ab:aab" >expected && + git -c grep.extendedRegexp=true grep "a+b" >actual && + test_cmp expected actual + ' done cat >expected <empty && + test_must_fail git -c grep.extendedregexp=true \ + grep "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp empty actual +' + +test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' ' + git -c grep.extendedregexp=true \ + grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -P -v pattern' ' + { + echo ab:a+b + echo ab:a\\+b + } >expected && + git grep -P -v "aab" ab >actual && + test_cmp expected actual +' + test_expect_success LIBPCRE 'grep -P -i pattern' ' { echo "hello.c: printf(\"Hello world.\n\");" @@ -637,4 +674,52 @@ test_expect_success LIBPCRE 'grep -P -w pattern' ' test_cmp expected actual ' +test_expect_success 'grep -G invalidpattern properly dies ' ' + test_must_fail git grep -G "a[" +' + +test_expect_success 'grep -E invalidpattern properly dies ' ' + test_must_fail git grep -E "a[" +' + +test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' ' + test_must_fail git grep -P "a[" +' + +test_expect_success 'grep -F -E -G pattern' ' + echo ab:a+b >expected && + git grep -F -E -G a+b >actual && + test_cmp expected actual +' + +test_expect_success 'grep -F -G -E pattern' ' + echo ab:aab >expected && + git grep -F -G -E a+b >actual && + test_cmp expected actual +' + +test_expect_success 'grep -E -F -G pattern' ' + echo ab:aab >expected && + git grep -E -F -G a\\+b >actual && + test_cmp expected actual +' + +test_expect_success 'grep -E -G -F pattern' ' + echo ab:a\\+b >expected && + git grep -E -G -F a\\+b >actual && + test_cmp expected actual +' + +test_expect_success 'grep -G -F -E pattern' ' + echo ab:a+b >expected && + git grep -G -F -E a\\+b >actual && + test_cmp expected actual +' + +test_expect_success LIBPCRE 'grep -E -G -F -P pattern' ' + echo ab:a+b >expected && + git grep -E -G -F -P a\\+b >actual && + test_cmp expected actual +' + test_done -- cgit v1.3 From d0042abe14b3aece87595d365d6eba84c3e53327 Mon Sep 17 00:00:00 2001 From: Michał Kiedrowicz Date: Fri, 27 May 2011 00:43:59 +0200 Subject: git-grep: Fix problems with recently added tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Brian Gernhardt reported that test 'git grep -E -F -G a\\+b' fails on OS X 10.6.7. This is because I assumed \+ is part of BRE, which isn't true on all platforms. The easiest way to make this test pass is to just update expected output, but that would make the test pointless. Its real purpose is to check whether 'git grep -E -F -G' is different from 'git grep -E -G -F'. To check that, let's change pattern to "a+b*c". This should return different match for -G, -F and -E. I also made two small tweaks to the tests. First, I added path "ab" to all calls to future-proof tests. Second, I updated last two tests to better show that 'git grep -P -E' is different from 'git grep -E -P'. Signed-off-by: Michał Kiedrowicz Signed-off-by: Junio C Hamano --- t/t7810-grep.sh | 58 ++++++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) (limited to 't') diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index e061108a64..69bd576d1c 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -33,9 +33,9 @@ test_expect_success setup ' echo HeLLo_world } >hello_world && { - echo aab - echo a+b - echo a\\+b + echo "a+b*c" + echo "a+bc" + echo "abc" } >ab && echo vvv >v && echo ww w >w && @@ -233,14 +233,14 @@ do test_cmp expected actual ' test_expect_success "grep $L with grep.extendedRegexp=false" ' - echo "ab:a+b" >expected && - git -c grep.extendedRegexp=false grep "a+b" >actual && + echo "ab:a+bc" >expected && + git -c grep.extendedRegexp=false grep "a+b*c" ab >actual && test_cmp expected actual ' test_expect_success "grep $L with grep.extendedRegexp=true" ' - echo "ab:aab" >expected && - git -c grep.extendedRegexp=true grep "a+b" >actual && + echo "ab:abc" >expected && + git -c grep.extendedRegexp=true grep "a+b*c" ab >actual && test_cmp expected actual ' done @@ -636,7 +636,7 @@ test_expect_success LIBPCRE 'grep -P pattern' ' ' test_expect_success 'grep pattern with grep.extendedRegexp=true' ' - :>empty && + >empty && test_must_fail git -c grep.extendedregexp=true \ grep "\p{Ps}.*?\p{Pe}" hello.c >actual && test_cmp empty actual @@ -650,10 +650,10 @@ test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' ' test_expect_success LIBPCRE 'grep -P -v pattern' ' { - echo ab:a+b - echo ab:a\\+b + echo "ab:a+b*c" + echo "ab:a+bc" } >expected && - git grep -P -v "aab" ab >actual && + git grep -P -v "abc" ab >actual && test_cmp expected actual ' @@ -686,39 +686,33 @@ test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' ' test_must_fail git grep -P "a[" ' -test_expect_success 'grep -F -E -G pattern' ' - echo ab:a+b >expected && - git grep -F -E -G a+b >actual && - test_cmp expected actual -' - -test_expect_success 'grep -F -G -E pattern' ' - echo ab:aab >expected && - git grep -F -G -E a+b >actual && +test_expect_success 'grep -G -E -F pattern' ' + echo "ab:a+b*c" >expected && + git grep -G -E -F "a+b*c" ab >actual && test_cmp expected actual ' test_expect_success 'grep -E -F -G pattern' ' - echo ab:aab >expected && - git grep -E -F -G a\\+b >actual && + echo "ab:a+bc" >expected && + git grep -E -F -G "a+b*c" ab >actual && test_cmp expected actual ' -test_expect_success 'grep -E -G -F pattern' ' - echo ab:a\\+b >expected && - git grep -E -G -F a\\+b >actual && +test_expect_success 'grep -F -G -E pattern' ' + echo "ab:abc" >expected && + git grep -F -G -E "a+b*c" ab >actual && test_cmp expected actual ' -test_expect_success 'grep -G -F -E pattern' ' - echo ab:a+b >expected && - git grep -G -F -E a\\+b >actual && - test_cmp expected actual +test_expect_success 'grep -G -F -P -E pattern' ' + >empty && + test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual && + test_cmp empty actual ' -test_expect_success LIBPCRE 'grep -E -G -F -P pattern' ' - echo ab:a+b >expected && - git grep -E -G -F -P a\\+b >actual && +test_expect_success LIBPCRE 'grep -G -F -E -P pattern' ' + echo "ab:a+b*c" >expected && + git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual && test_cmp expected actual ' -- cgit v1.3