Skip to content

Commit 2997ea9

Browse files
committed
Merge branch 'jc/test-parse-options-expect'
t0040 had too many unnecessary repetitions in its test data. Teach test-parse-options program so that a caller can tell what it expects in its output, so that these repetitions can be cleaned up. * jc/test-parse-options-expect: t0040: convert a few tests to use test-parse-options --expect t0040: remove unused test helpers test-parse-options: --expect=<string> option to simplify tests test-parse-options: fix output when callback option fails
2 parents 5d5f1c2 + 8ca65ae commit 2997ea9

File tree

2 files changed

+93
-60
lines changed

2 files changed

+93
-60
lines changed

t/helper/test-parse-options.c

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ static char *file = NULL;
1414
static int ambiguous;
1515
static struct string_list list;
1616

17+
static struct {
18+
int called;
19+
const char *arg;
20+
int unset;
21+
} length_cb;
22+
1723
static int length_callback(const struct option *opt, const char *arg, int unset)
1824
{
19-
printf("Callback: \"%s\", %d\n",
20-
(arg ? arg : "not set"), unset);
25+
length_cb.called = 1;
26+
length_cb.arg = arg;
27+
length_cb.unset = unset;
28+
2129
if (unset)
2230
return 1; /* do not support unset */
2331

@@ -31,13 +39,69 @@ static int number_callback(const struct option *opt, const char *arg, int unset)
3139
return 0;
3240
}
3341

42+
static int collect_expect(const struct option *opt, const char *arg, int unset)
43+
{
44+
struct string_list *expect;
45+
struct string_list_item *item;
46+
struct strbuf label = STRBUF_INIT;
47+
const char *colon;
48+
49+
if (!arg || unset)
50+
die("malformed --expect option");
51+
52+
expect = (struct string_list *)opt->value;
53+
colon = strchr(arg, ':');
54+
if (!colon)
55+
die("malformed --expect option, lacking a colon");
56+
strbuf_add(&label, arg, colon - arg);
57+
item = string_list_insert(expect, strbuf_detach(&label, NULL));
58+
if (item->util)
59+
die("malformed --expect option, duplicate %s", label.buf);
60+
item->util = (void *)arg;
61+
return 0;
62+
}
63+
64+
__attribute__((format (printf,3,4)))
65+
static void show(struct string_list *expect, int *status, const char *fmt, ...)
66+
{
67+
struct string_list_item *item;
68+
struct strbuf buf = STRBUF_INIT;
69+
va_list args;
70+
71+
va_start(args, fmt);
72+
strbuf_vaddf(&buf, fmt, args);
73+
va_end(args);
74+
75+
if (!expect->nr)
76+
printf("%s\n", buf.buf);
77+
else {
78+
char *colon = strchr(buf.buf, ':');
79+
if (!colon)
80+
die("malformed output format, output lacking colon: %s", fmt);
81+
*colon = '\0';
82+
item = string_list_lookup(expect, buf.buf);
83+
*colon = ':';
84+
if (!item)
85+
; /* not among entries being checked */
86+
else {
87+
if (strcmp((const char *)item->util, buf.buf)) {
88+
printf("-%s\n", (char *)item->util);
89+
printf("+%s\n", buf.buf);
90+
*status = 1;
91+
}
92+
}
93+
}
94+
strbuf_release(&buf);
95+
}
96+
3497
int main(int argc, char **argv)
3598
{
3699
const char *prefix = "prefix/";
37100
const char *usage[] = {
38101
"test-parse-options <options>",
39102
NULL
40103
};
104+
struct string_list expect = STRING_LIST_INIT_NODUP;
41105
struct option options[] = {
42106
OPT_BOOL(0, "yes", &boolean, "get a boolean"),
43107
OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
@@ -78,28 +142,38 @@ int main(int argc, char **argv)
78142
OPT__VERBOSE(&verbose, "be verbose"),
79143
OPT__DRY_RUN(&dry_run, "dry run"),
80144
OPT__QUIET(&quiet, "be quiet"),
145+
OPT_CALLBACK(0, "expect", &expect, "string",
146+
"expected output in the variable dump",
147+
collect_expect),
81148
OPT_END(),
82149
};
83150
int i;
151+
int ret = 0;
84152

85153
argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
86154

87-
printf("boolean: %d\n", boolean);
88-
printf("integer: %d\n", integer);
89-
printf("magnitude: %lu\n", magnitude);
90-
printf("timestamp: %lu\n", timestamp);
91-
printf("string: %s\n", string ? string : "(not set)");
92-
printf("abbrev: %d\n", abbrev);
93-
printf("verbose: %d\n", verbose);
94-
printf("quiet: %d\n", quiet);
95-
printf("dry run: %s\n", dry_run ? "yes" : "no");
96-
printf("file: %s\n", file ? file : "(not set)");
155+
if (length_cb.called) {
156+
const char *arg = length_cb.arg;
157+
int unset = length_cb.unset;
158+
show(&expect, &ret, "Callback: \"%s\", %d",
159+
(arg ? arg : "not set"), unset);
160+
}
161+
show(&expect, &ret, "boolean: %d", boolean);
162+
show(&expect, &ret, "integer: %d", integer);
163+
show(&expect, &ret, "magnitude: %lu", magnitude);
164+
show(&expect, &ret, "timestamp: %lu", timestamp);
165+
show(&expect, &ret, "string: %s", string ? string : "(not set)");
166+
show(&expect, &ret, "abbrev: %d", abbrev);
167+
show(&expect, &ret, "verbose: %d", verbose);
168+
show(&expect, &ret, "quiet: %d", quiet);
169+
show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
170+
show(&expect, &ret, "file: %s", file ? file : "(not set)");
97171

98172
for (i = 0; i < list.nr; i++)
99-
printf("list: %s\n", list.items[i].string);
173+
show(&expect, &ret, "list: %s", list.items[i].string);
100174

101175
for (i = 0; i < argc; i++)
102-
printf("arg %02d: %s\n", i, argv[i]);
176+
show(&expect, &ret, "arg %02d: %s", i, argv[i]);
103177

104-
return 0;
178+
return ret;
105179
}

t/t0040-parse-options.sh

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Standard options
4545
-v, --verbose be verbose
4646
-n, --dry-run dry run
4747
-q, --quiet be quiet
48+
--expect <string> expected output in the variable dump
4849
4950
EOF
5051

@@ -56,52 +57,12 @@ test_expect_success 'test help' '
5657

5758
mv expect expect.err
5859

59-
cat >expect.template <<\EOF
60-
boolean: 0
61-
integer: 0
62-
magnitude: 0
63-
timestamp: 0
64-
string: (not set)
65-
abbrev: 7
66-
verbose: -1
67-
quiet: 0
68-
dry run: no
69-
file: (not set)
70-
EOF
71-
72-
check() {
60+
check () {
7361
what="$1" &&
7462
shift &&
7563
expect="$1" &&
7664
shift &&
77-
sed "s/^$what .*/$what $expect/" <expect.template >expect &&
78-
test-parse-options $* >output 2>output.err &&
79-
test_must_be_empty output.err &&
80-
test_cmp expect output
81-
}
82-
83-
check_i18n() {
84-
what="$1" &&
85-
shift &&
86-
expect="$1" &&
87-
shift &&
88-
sed "s/^$what .*/$what $expect/" <expect.template >expect &&
89-
test-parse-options $* >output 2>output.err &&
90-
test_must_be_empty output.err &&
91-
test_i18ncmp expect output
92-
}
93-
94-
check_unknown() {
95-
case "$1" in
96-
--*)
97-
echo error: unknown option \`${1#--}\' >expect ;;
98-
-*)
99-
echo error: unknown switch \`${1#-}\' >expect ;;
100-
esac &&
101-
cat expect.err >>expect &&
102-
test_must_fail test-parse-options $* >output 2>output.err &&
103-
test_must_be_empty output &&
104-
test_cmp expect output.err
65+
test-parse-options --expect="$what $expect" "$@"
10566
}
10667

10768
check_unknown_i18n() {
@@ -356,9 +317,7 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
356317
test_cmp expect output
357318
'
358319

359-
cat >expect <<\EOF
360-
Callback: "not set", 1
361-
EOF
320+
>expect
362321

363322
test_expect_success 'OPT_CALLBACK() and callback errors work' '
364323
test_must_fail test-parse-options --no-length >output 2>output.err &&

0 commit comments

Comments
 (0)