Skip to content

Commit aba8187

Browse files
carenasgitster
authored andcommitted
t/helper: teach test-regex to report pattern errors (like REG_ILLSEQ)
7187c7b (t4210: skip i18n tests that don't work on FreeBSD, 2019-11-27) adds a REG_ILLSEQ prerequisite to avoid failures from the tests added in 4e2443b (log tests: test regex backends in "--encode=<enc>" tests, 2019-06-28), but hardcodes it to be only enabled in FreeBSD. Instead of hardcoding the affected platform, teach the test-regex helper, how to validate a pattern and report back, so it can be used to detect the same issue in other affected systems (like DragonFlyBSD or macOS). While at it, refactor the tool so it can report back the source of the errors it founds, and can be invoked also in a --silent mode, when needed, for backward compatibility. A missing flag has been added and the code reformatted, as well as updates to the way the parameters are handled, for consistency. To minimize changes, it is assumed the regcomp error is of the right type since we control the only caller, and is also assumed to affect both basic and extended syntax (only basic is tested, but both behave the same in all three affected platforms since they use the same function). Based-on-patch-by: Junio C Hamano <[email protected]> Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af6b65d commit aba8187

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

t/helper/test-regex.c

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "test-tool.h"
2-
#include "git-compat-util.h"
32
#include "gettext.h"
43

54
struct reg_flag {
@@ -8,12 +7,13 @@ struct reg_flag {
87
};
98

109
static struct reg_flag reg_flags[] = {
11-
{ "EXTENDED", REG_EXTENDED },
12-
{ "NEWLINE", REG_NEWLINE },
13-
{ "ICASE", REG_ICASE },
14-
{ "NOTBOL", REG_NOTBOL },
10+
{ "EXTENDED", REG_EXTENDED },
11+
{ "NEWLINE", REG_NEWLINE },
12+
{ "ICASE", REG_ICASE },
13+
{ "NOTBOL", REG_NOTBOL },
14+
{ "NOTEOL", REG_NOTEOL },
1515
#ifdef REG_STARTEND
16-
{ "STARTEND", REG_STARTEND },
16+
{ "STARTEND", REG_STARTEND },
1717
#endif
1818
{ NULL, 0 }
1919
};
@@ -41,36 +41,74 @@ int cmd__regex(int argc, const char **argv)
4141
{
4242
const char *pat;
4343
const char *str;
44-
int flags = 0;
44+
int ret, silent = 0, flags = 0;
4545
regex_t r;
4646
regmatch_t m[1];
47-
48-
if (argc == 2 && !strcmp(argv[1], "--bug"))
49-
return test_regex_bug();
50-
else if (argc < 3)
51-
usage("test-tool regex --bug\n"
52-
"test-tool regex <pattern> <string> [<options>]");
47+
char errbuf[64];
5348

5449
argv++;
55-
pat = *argv++;
56-
str = *argv++;
57-
while (*argv) {
58-
struct reg_flag *rf;
59-
for (rf = reg_flags; rf->name; rf++)
60-
if (!strcmp(*argv, rf->name)) {
61-
flags |= rf->flag;
62-
break;
63-
}
64-
if (!rf->name)
65-
die("do not recognize %s", *argv);
50+
argc--;
51+
52+
if (!argc)
53+
goto usage;
54+
55+
if (!strcmp(*argv, "--bug")) {
56+
if (argc == 1)
57+
return test_regex_bug();
58+
else
59+
goto usage;
60+
}
61+
if (!strcmp(*argv, "--silent")) {
62+
silent = 1;
6663
argv++;
64+
argc--;
65+
}
66+
if (!argc)
67+
goto usage;
68+
69+
pat = *argv++;
70+
if (argc == 1)
71+
str = NULL;
72+
else {
73+
str = *argv++;
74+
while (*argv) {
75+
struct reg_flag *rf;
76+
for (rf = reg_flags; rf->name; rf++)
77+
if (!strcmp(*argv, rf->name)) {
78+
flags |= rf->flag;
79+
break;
80+
}
81+
if (!rf->name)
82+
die("do not recognize flag %s", *argv);
83+
argv++;
84+
}
6785
}
6886
git_setup_gettext();
6987

70-
if (regcomp(&r, pat, flags))
71-
die("failed regcomp() for pattern '%s'", pat);
72-
if (regexec(&r, str, 1, m, 0))
73-
return 1;
88+
ret = regcomp(&r, pat, flags);
89+
if (ret) {
90+
if (silent)
91+
return ret;
92+
93+
regerror(ret, &r, errbuf, sizeof(errbuf));
94+
die("failed regcomp() for pattern '%s' (%s)", pat, errbuf);
95+
}
96+
if (!str)
97+
return 0;
98+
99+
ret = regexec(&r, str, 1, m, 0);
100+
if (ret) {
101+
if (silent || ret == REG_NOMATCH)
102+
return ret;
103+
104+
regerror(ret, &r, errbuf, sizeof(errbuf));
105+
die("failed regexec() for subject '%s' (%s)", str, errbuf);
106+
}
74107

75108
return 0;
109+
usage:
110+
usage("\ttest-tool regex --bug\n"
111+
"\ttest-tool regex [--silent] <pattern>\n"
112+
"\ttest-tool regex [--silent] <pattern> <string> [<options>]");
113+
return -1;
76114
}

0 commit comments

Comments
 (0)