Skip to content

Commit b77651b

Browse files
matheustavaresgitster
authored andcommitted
t/helper/test-config: return exit codes consistently
The test-config helper may exit with a variety of at least four different codes, to reflect the status of the requested operations. These codes are sometimes checked in the tests, but not all of the codes are returned consistently by the helper: 1 will usually refer to a "value not found", but usage errors can also return 1 or 128. Moreover, 128 is also expected on errors within the configset functions. These inconsistent uses of the exit codes can lead to false positives in the tests. Although all tests which expect errors and check the helper's exit code currently also check the output, it's still better to standardize the exit codes and avoid future problems in new tests. While we are here, let's also check that we have the expected argc for configset_get_value and configset_get_value_multi, before trying to use argv. Note: this change is implemented with the unification of the exit labels. This might seem unnecessary, for now, but it will benefit the next patch, which will increase the cleanup section. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42811db commit b77651b

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

t/helper/test-config.c

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
* iterate -> iterate over all values using git_config(), and print some
3131
* data for each
3232
*
33+
* Exit codes:
34+
* 0: success
35+
* 1: value not found for the given config key
36+
* 2: config file path given as argument is inaccessible or doesn't exist
37+
* 129: test-config usage error
38+
*
39+
* Note: tests may also expect 128 for die() calls in the config machinery.
40+
*
3341
* Examples:
3442
*
3543
* To print the value with highest priority for key "foo.bAr Baz.rock":
@@ -64,35 +72,42 @@ static int early_config_cb(const char *var, const char *value, void *vdata)
6472
return 0;
6573
}
6674

75+
enum test_config_exit_code {
76+
TC_SUCCESS = 0,
77+
TC_VALUE_NOT_FOUND = 1,
78+
TC_CONFIG_FILE_ERROR = 2,
79+
TC_USAGE_ERROR = 129,
80+
};
81+
6782
int cmd__config(int argc, const char **argv)
6883
{
6984
int i, val;
7085
const char *v;
7186
const struct string_list *strptr;
7287
struct config_set cs;
88+
enum test_config_exit_code ret = TC_SUCCESS;
7389

7490
if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
7591
read_early_config(early_config_cb, (void *)argv[2]);
76-
return 0;
92+
return TC_SUCCESS;
7793
}
7894

7995
setup_git_directory();
8096

8197
git_configset_init(&cs);
8298

83-
if (argc < 2) {
84-
fprintf(stderr, "Please, provide a command name on the command-line\n");
85-
goto exit1;
86-
} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
99+
if (argc < 2)
100+
goto print_usage_error;
101+
102+
if (argc == 3 && !strcmp(argv[1], "get_value")) {
87103
if (!git_config_get_value(argv[2], &v)) {
88104
if (!v)
89105
printf("(NULL)\n");
90106
else
91107
printf("%s\n", v);
92-
goto exit0;
93108
} else {
94109
printf("Value not found for \"%s\"\n", argv[2]);
95-
goto exit1;
110+
ret = TC_VALUE_NOT_FOUND;
96111
}
97112
} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
98113
strptr = git_config_get_value_multi(argv[2]);
@@ -104,59 +119,56 @@ int cmd__config(int argc, const char **argv)
104119
else
105120
printf("%s\n", v);
106121
}
107-
goto exit0;
108122
} else {
109123
printf("Value not found for \"%s\"\n", argv[2]);
110-
goto exit1;
124+
ret = TC_VALUE_NOT_FOUND;
111125
}
112126
} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
113127
if (!git_config_get_int(argv[2], &val)) {
114128
printf("%d\n", val);
115-
goto exit0;
116129
} else {
117130
printf("Value not found for \"%s\"\n", argv[2]);
118-
goto exit1;
131+
ret = TC_VALUE_NOT_FOUND;
119132
}
120133
} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
121134
if (!git_config_get_bool(argv[2], &val)) {
122135
printf("%d\n", val);
123-
goto exit0;
124136
} else {
125137
printf("Value not found for \"%s\"\n", argv[2]);
126-
goto exit1;
138+
ret = TC_VALUE_NOT_FOUND;
127139
}
128140
} else if (argc == 3 && !strcmp(argv[1], "get_string")) {
129141
if (!git_config_get_string_const(argv[2], &v)) {
130142
printf("%s\n", v);
131-
goto exit0;
132143
} else {
133144
printf("Value not found for \"%s\"\n", argv[2]);
134-
goto exit1;
145+
ret = TC_VALUE_NOT_FOUND;
135146
}
136-
} else if (!strcmp(argv[1], "configset_get_value")) {
147+
} else if (argc >= 3 && !strcmp(argv[1], "configset_get_value")) {
137148
for (i = 3; i < argc; i++) {
138149
int err;
139150
if ((err = git_configset_add_file(&cs, argv[i]))) {
140151
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
141-
goto exit2;
152+
ret = TC_CONFIG_FILE_ERROR;
153+
goto out;
142154
}
143155
}
144156
if (!git_configset_get_value(&cs, argv[2], &v)) {
145157
if (!v)
146158
printf("(NULL)\n");
147159
else
148160
printf("%s\n", v);
149-
goto exit0;
150161
} else {
151162
printf("Value not found for \"%s\"\n", argv[2]);
152-
goto exit1;
163+
ret = TC_VALUE_NOT_FOUND;
153164
}
154-
} else if (!strcmp(argv[1], "configset_get_value_multi")) {
165+
} else if (argc >= 3 && !strcmp(argv[1], "configset_get_value_multi")) {
155166
for (i = 3; i < argc; i++) {
156167
int err;
157168
if ((err = git_configset_add_file(&cs, argv[i]))) {
158169
fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
159-
goto exit2;
170+
ret = TC_CONFIG_FILE_ERROR;
171+
goto out;
160172
}
161173
}
162174
strptr = git_configset_get_value_multi(&cs, argv[2]);
@@ -168,27 +180,19 @@ int cmd__config(int argc, const char **argv)
168180
else
169181
printf("%s\n", v);
170182
}
171-
goto exit0;
172183
} else {
173184
printf("Value not found for \"%s\"\n", argv[2]);
174-
goto exit1;
185+
ret = TC_VALUE_NOT_FOUND;
175186
}
176187
} else if (!strcmp(argv[1], "iterate")) {
177188
git_config(iterate_cb, NULL);
178-
goto exit0;
189+
} else {
190+
print_usage_error:
191+
fprintf(stderr, "Invalid syntax. Usage: test-tool config <cmd> [args]\n");
192+
ret = TC_USAGE_ERROR;
179193
}
180194

181-
die("%s: Please check the syntax and the function name", argv[0]);
182-
183-
exit0:
184-
git_configset_clear(&cs);
185-
return 0;
186-
187-
exit1:
188-
git_configset_clear(&cs);
189-
return 1;
190-
191-
exit2:
195+
out:
192196
git_configset_clear(&cs);
193-
return 2;
197+
return ret;
194198
}

0 commit comments

Comments
 (0)