abuild-tar: fix --help and add test for usage

This commit is contained in:
Natanael Copa 2022-11-30 11:59:30 +01:00
parent 94122d7bec
commit 8a16229d3e
2 changed files with 30 additions and 6 deletions

View File

@ -104,9 +104,9 @@ static void tarhdr_checksum(struct tar_header *hdr)
put_octal(hdr->chksum, sizeof(hdr->chksum)-1, chksum); put_octal(hdr->chksum, sizeof(hdr->chksum)-1, chksum);
} }
static int usage(void) static int usage(FILE *out)
{ {
fprintf(stderr, fprintf(out,
"abuild-tar " VERSION "\n" "abuild-tar " VERSION "\n"
"\n" "\n"
"usage: abuild-tar [--hash[=<algorithm>]] [--cut]\n" "usage: abuild-tar [--hash[=<algorithm>]] [--cut]\n"
@ -367,9 +367,10 @@ err:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
static int cut = 0; static int cut = 0, help = 0;
static const struct option options[] = { static const struct option options[] = {
{ "hash", optional_argument }, { "hash", optional_argument },
{ "help", no_argument, &help, 1 },
{ "cut", no_argument, &cut, 1 }, { "cut", no_argument, &cut, 1 },
{ NULL } { NULL }
}; };
@ -388,15 +389,18 @@ int main(int argc, char **argv)
digest = optarg ? optarg : "sha1"; digest = optarg ? optarg : "sha1";
} }
if (help)
return usage(stdout) && 0;
if (digest == NULL && cut == 0) if (digest == NULL && cut == 0)
return usage(); return usage(stderr);
if (isatty(STDIN_FILENO)) if (isatty(STDIN_FILENO))
return usage(); return usage(stderr);
if (digest != NULL) { if (digest != NULL) {
md = EVP_get_digestbyname(digest); md = EVP_get_digestbyname(digest);
if (md == NULL) if (md == NULL)
return usage(); return usage(stderr);
} }
return do_it(md, cut); return do_it(md, cut);

20
tests/abuild_tar_test Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env atf-sh
. $(atf_get_srcdir)/test_env.sh
init_tests \
abuild_tar_help \
abuild_tar_invalid_opt
DATADIR=$(atf_get_srcdir)/testdata
abuild_tar_help_body() {
atf_check -s exit:0 \
-o match:"usage:" \
abuild-tar --help
}
abuild_tar_invalid_opt_body() {
atf_check -s not-exit:0 \
-e match:"usage:" \
abuild-tar --invalid
}