t7810: avoid assumption about invalid regex syntax

A few of the tests want to check that "git grep -P -E" will override -P with -E, and vice versa. To do so, we use a regex with "\x{..}", which is valid in PCRE but not defined by POSIX (for basic or extended regular expressions). However, POSIX declares quite a lot of syntax, including "\x", as "undefined". That leaves implementations free to extend the standard if they choose. At least one, musl libc, implements "\x" in the same way as PCRE. Our tests check that "-E" complains about "\x", which fails with musl. We can fix this by finding some construct which behaves reliably on both PCRE and POSIX, but differently in each system. One such construct is the use of backslash inside brackets. In PCRE, "[\d]" interprets "\d" as it would outside the brackets, matching a digit. Whereas in POSIX, the backslash must be treated literally, and we match either it or a literal "d". Moreover, implementations are not free to change this according to POSIX, so we should be able to rely on it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 11, 2017 at 06:10 UTC 7675c7bd01b8d39dc3c1e8385403f0307be31712
1 file changed +15 -11
t/t7810-grep.sh
+15 -11
@@ -37,6 +37,10 @@ test_expect_success setup '
37 echo "a+bc"
38 echo "abc"
39 } >ab &&
40 + {
41 + echo d &&
42 + echo 0
43 + } >d0 &&
44 echo vvv >v &&
45 echo ww w >w &&
46 echo x x xx x >x &&
@@ -1092,36 +1096,36 @@ test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended
1096 '
1097
1098 test_expect_success 'grep -G -F -P -E pattern' '
1095 - >empty &&
1096 - test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
1097 - test_cmp empty actual
1099 + echo "d0:d" >expected &&
1100 + git grep -G -F -P -E "[\d]" d0 >actual &&
1101 + test_cmp expected actual
1102 '
1103
1104 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1101 - >empty &&
1102 - test_must_fail git \
1105 + echo "d0:d" >expected &&
1106 + git \
1107 -c grep.patterntype=fixed \
1108 -c grep.patterntype=basic \
1109 -c grep.patterntype=perl \
1110 -c grep.patterntype=extended \
1107 - grep "a\x{2b}b\x{2a}c" ab >actual &&
1108 - test_cmp empty actual
1111 + grep "[\d]" d0 >actual &&
1112 + test_cmp expected actual
1113 '
1114
1115 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1112 - echo "ab:a+b*c" >expected &&
1113 - git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
1116 + echo "d0:0" >expected &&
1117 + git grep -G -F -E -P "[\d]" d0 >actual &&
1118 test_cmp expected actual
1119 '
1120
1121 test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1118 - echo "ab:a+b*c" >expected &&
1122 + echo "d0:0" >expected &&
1123 git \
1124 -c grep.patterntype=fixed \
1125 -c grep.patterntype=basic \
1126 -c grep.patterntype=extended \
1127 -c grep.patterntype=perl \
1124 - grep "a\x{2b}b\x{2a}c" ab >actual &&
1128 + grep "[\d]" d0 >actual &&
1129 test_cmp expected actual
1130 '
1131