1 | #!/usr/bin/perl
|
---|
2 | # Prior to 2.26, an invalid regexp in a -f-specified file would elicit
|
---|
3 | # a diagnostic like "Unmatched [ or [^", with no indication of the
|
---|
4 | # file or line number from which the offending regular expression came.
|
---|
5 | # With 2.26, now, each such diagnostic has a "FILENAME:LINENO: " prefix.
|
---|
6 |
|
---|
7 | # Copyright (C) 2016-2021 Free Software Foundation, Inc.
|
---|
8 |
|
---|
9 | # This program is free software: you can redistribute it and/or modify
|
---|
10 | # it under the terms of the GNU General Public License as published by
|
---|
11 | # the Free Software Foundation, either version 3 of the License, or
|
---|
12 | # (at your option) any later version.
|
---|
13 |
|
---|
14 | # This program is distributed in the hope that it will be useful,
|
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | # GNU General Public License for more details.
|
---|
18 |
|
---|
19 | # You should have received a copy of the GNU General Public License
|
---|
20 | # along with this program. If not, see <https://www.gnu.org/licenses/>.
|
---|
21 |
|
---|
22 | use strict;
|
---|
23 |
|
---|
24 | (my $program_name = $0) =~ s|.*/||;
|
---|
25 |
|
---|
26 | my $prog = 'grep';
|
---|
27 | my $full_prog_name = `$prog --no-such-option 2>&1`;
|
---|
28 | $full_prog_name =~ s/:.*//s;
|
---|
29 | $prog = $full_prog_name if $full_prog_name;
|
---|
30 |
|
---|
31 | # Turn off localization of executable's output.
|
---|
32 | @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
|
---|
33 |
|
---|
34 | # There are at least two variants of one diagnostic:
|
---|
35 | # - Unmatched [, [^, [:, [., or [=
|
---|
36 | # - Unmatched [ or [^
|
---|
37 | # Transform each to this: "Unmatched [..."
|
---|
38 | my $err_subst = {ERR_SUBST => 's/(: Unmatched \[).*/$1.../'};
|
---|
39 |
|
---|
40 | my $no_pcre = "$prog: Perl matching not supported in a --disable-perl-regexp build\n";
|
---|
41 |
|
---|
42 | my @Tests =
|
---|
43 | (
|
---|
44 | # Show that grep now includes filename:lineno in the diagnostic:
|
---|
45 | ['invalid-re', '-f g', {AUX=>{g=>"1\n2\n3\n4[[\n"}}, {EXIT=>2},
|
---|
46 | $err_subst,
|
---|
47 | {ERR => "$prog: g:4: Unmatched [...\n"},
|
---|
48 | ],
|
---|
49 |
|
---|
50 | # Show that with two or more errors, grep now prints all diagnostics:
|
---|
51 | ['invalid-re-2-files', '-f g -f h', {EXIT=>2},
|
---|
52 | {AUX=>{g=>"1\n2[[\n3\n4[[\n"}},
|
---|
53 | {AUX=>{h=>"5\n6\n7[[\n"}},
|
---|
54 | $err_subst,
|
---|
55 | {ERR => "$prog: g:2: Unmatched [...\n"
|
---|
56 | . "$prog: g:4: Unmatched [...\n"
|
---|
57 | . "$prog: h:3: Unmatched [...\n"
|
---|
58 | },
|
---|
59 | ],
|
---|
60 |
|
---|
61 | # Like the above, but on the other lines.
|
---|
62 | ['invalid-re-2-files2', '-f g -f h', {EXIT=>2},
|
---|
63 | {AUX=>{g=>"1[[\n2\n3[[\n4\n"}},
|
---|
64 | {AUX=>{h=>"5[[\n6[[\n7\n"}},
|
---|
65 | $err_subst,
|
---|
66 | {ERR => "$prog: g:1: Unmatched [...\n"
|
---|
67 | . "$prog: g:3: Unmatched [...\n"
|
---|
68 | . "$prog: h:1: Unmatched [...\n"
|
---|
69 | . "$prog: h:2: Unmatched [...\n"
|
---|
70 | },
|
---|
71 | ],
|
---|
72 |
|
---|
73 | # Make sure the line numbers are right when some regexps are duplicates.
|
---|
74 | ['invalid-re-line-numbers', '-f g -f h', {EXIT=>2},
|
---|
75 | {AUX=>{g=>"1[[\n\n3[[\n\n5[[\n"}},
|
---|
76 | {AUX=>{h=>"1[[\n\n\n4[[\n\n6[[\n"}},
|
---|
77 | $err_subst,
|
---|
78 | {ERR => "$prog: g:1: Unmatched [...\n"
|
---|
79 | . "$prog: g:3: Unmatched [...\n"
|
---|
80 | . "$prog: g:5: Unmatched [...\n"
|
---|
81 | . "$prog: h:4: Unmatched [...\n"
|
---|
82 | . "$prog: h:6: Unmatched [...\n"
|
---|
83 | },
|
---|
84 | ],
|
---|
85 |
|
---|
86 | # Show that with two '-e'-specified erroneous regexps,
|
---|
87 | # there is no file name or line number.
|
---|
88 | ['invalid-re-2e', '-e "1[[" -e "2[["', {EXIT=>2},
|
---|
89 | $err_subst,
|
---|
90 | {ERR => "$prog: Unmatched [...\n" x 2},
|
---|
91 | ],
|
---|
92 |
|
---|
93 | # Test unmatched ) as well. It is OK with -E and an error with -G and -P.
|
---|
94 | ['invalid-re-E-paren', '-E ")"', {IN=>''}, {EXIT=>1}],
|
---|
95 | ['invalid-re-E-star-paren', '-E ".*)"', {IN=>''}, {EXIT=>1}],
|
---|
96 | ['invalid-re-G-paren', '-G "\\)"', {EXIT=>2},
|
---|
97 | {ERR => "$prog: Unmatched ) or \\)\n"},
|
---|
98 | ],
|
---|
99 | ['invalid-re-G-star-paren', '-G "a.*\\)"', {EXIT=>2},
|
---|
100 | {ERR => "$prog: Unmatched ) or \\)\n"},
|
---|
101 | ],
|
---|
102 | ['invalid-re-P-paren', '-P ")"', {EXIT=>2},
|
---|
103 | {ERR => $ENV{PCRE_WORKS} == 1
|
---|
104 | ? "$prog: unmatched parentheses\n"
|
---|
105 | : $no_pcre
|
---|
106 | },
|
---|
107 | ],
|
---|
108 | ['invalid-re-P-star-paren', '-P "a.*)"', {EXIT=>2},
|
---|
109 | {ERR => $ENV{PCRE_WORKS} == 1
|
---|
110 | ? "$prog: unmatched parentheses\n"
|
---|
111 | : $no_pcre
|
---|
112 | },
|
---|
113 | ],
|
---|
114 |
|
---|
115 | # Prior to grep-3.6, the name of the offending file was not printed.
|
---|
116 | ['backtracking-with-file', '-P "((a+)*)+$"', {EXIT=>2},
|
---|
117 | {IN=>{f=>"a"x20 ."b"}},
|
---|
118 | {ERR => $ENV{PCRE_WORKS} == 1
|
---|
119 | ? "$prog: f: exceeded PCRE's backtracking limit\n"
|
---|
120 | : $no_pcre
|
---|
121 | },
|
---|
122 | ],
|
---|
123 |
|
---|
124 | );
|
---|
125 |
|
---|
126 | my $save_temps = $ENV{DEBUG};
|
---|
127 | my $verbose = $ENV{VERBOSE};
|
---|
128 |
|
---|
129 | my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
|
---|
130 | exit $fail;
|
---|