Skip to content

Commit

Permalink
test: Handle --color option in unittest
Browse files Browse the repository at this point in the history
The unittest doesn't use getopt_long() so let's implement it for the
color option manually.  Maybe we can add the long option support if we
want to add more options later.

Signed-off-by: Namhyung Kim <[email protected]>
  • Loading branch information
namhyung committed Jun 11, 2023
1 parent 4491ee8 commit 82d8624
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions tests/unittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,32 @@ int main(int argc, char *argv[])
char *filter = NULL;
char *term;
int c;
bool color_set = false;

while ((c = getopt(argc, argv, "dvnpiO:f:")) != -1) {
while ((c = getopt(argc, argv, "dvnpiO:f:-:")) != -1) {
switch (c) {
case 'd':
case 'v':
debug = 1;
break;
case 'n':
color = false;
color_set = true;
break;
case '-':
/* poor man's getopt_long() */
if (!strncmp(optarg, "color", 5)) {
char *arg;
if (optarg[5] == '=')
arg = optarg + 6;
else
arg = argv[optind++];
if (!strcmp(arg, "on") || !strcmp(arg, "1"))
color = true;
if (!strcmp(arg, "off") || !strcmp(arg, "0"))
color = false;
color_set = true;
}
break;
default:
break;
Expand All @@ -314,11 +331,13 @@ int main(int argc, char *argv[])
return -1;
}

term = getenv("TERM");
if (term && !strcmp(term, "dumb"))
color = false;
if (!isatty(STDIN_FILENO))
color = false;
if (!color_set) {
term = getenv("TERM");
if (term && !strcmp(term, "dumb"))
color = false;
if (!isatty(STDIN_FILENO))
color = false;
}

for (i = 0; i < test_num; i++)
run_unit_test(&test_cases[i], test_stats, filter);
Expand Down

0 comments on commit 82d8624

Please sign in to comment.