logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 2b1563aa26fec9d85764ba5abc72e48d7cc53ca5
parent d32489b63acb5df00027c0d87528820b978432bc
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 15 Mar 2022 02:00:08 +0100

test-bin/lolcat: Add

Diffstat:

Mbin/lolcat.c11++++++++---
Mtest-bin/Kyuafile1+
Atest-bin/lolcat80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest-bin/outputs/lolcat/all_bytes0
4 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/bin/lolcat.c b/bin/lolcat.c @@ -41,14 +41,14 @@ concat(FILE *stream) if(fputc(c, stdout) == EOF) { - printf("\nWrite error: %s\n", strerror(errno)); + fprintf(stderr, "\nWrite error: %s\n", strerror(errno)); return 1; } } if(c == EOF && errno != 0) { - printf("\nRead error: %s\n", strerror(errno)); + fprintf(stderr, "\nRead error: %s\n", strerror(errno)); return 1; } @@ -76,13 +76,18 @@ main(int argc, char *argv[]) goto end; } } + else if(strncmp(argv[argi], "--", 3) == 0) + { + continue; + } else { FILE *file = fopen(argv[argi], "r"); if(!file) { - printf("\nError opening ‘%s’: %s\n", argv[argi], strerror(errno)); + fprintf(stderr, "\nError opening ‘%s’: %s\n", argv[argi], strerror(errno)); + ret = 1; goto end; } else diff --git a/test-bin/Kyuafile b/test-bin/Kyuafile @@ -17,6 +17,7 @@ atf_test_program{name="errno", required_files=basedir.."/bin/errno", timeout=1} atf_test_program{name="false", required_files=basedir.."/bin/false", timeout=1} atf_test_program{name="humanize", required_files=basedir.."/bin/humanize", timeout=1} atf_test_program{name="id", required_files=basedir.."/bin/id", timeout=1} +atf_test_program{name="lolcat", required_files=basedir.."/bin/lolcat", timeout=1} atf_test_program{name="mdate", required_files=basedir.."/bin/mdate", timeout=1} atf_test_program{name="pwd", required_files=basedir.."/bin/pwd", timeout=1} atf_test_program{name="seq", required_files=basedir.."/bin/seq", timeout=1} diff --git a/test-bin/lolcat b/test-bin/lolcat @@ -0,0 +1,80 @@ +#!/usr/bin/env atf-sh +atf_test_case allfile +allfile_body() { + atf_check -o file:outputs/lolcat/all_bytes ../bin/lolcat inputs/all_bytes +} + +atf_test_case allinput +allinput_body() { + atf_check -o file:outputs/lolcat/all_bytes ../bin/lolcat <inputs/all_bytes +} + +atf_test_case alldashinput +alldashinput_body() { + atf_check -o file:outputs/lolcat/all_bytes ../bin/lolcat - <inputs/all_bytes +} + +atf_test_case devnull +devnull_body() { + atf_check -o 'inline:' ../bin/lolcat /dev/null + atf_check -o 'inline:' ../bin/lolcat </dev/null + atf_check -o 'inline:' ../bin/lolcat - </dev/null +} + +atf_test_case noperm cleanup +noperm_body() { + touch inputs/chmod_000 || atf_fail "touching chmod_000" + chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000" + # shellcheck disable=SC1112 + atf_check -s exit:1 -e 'inline:\nError opening ‘inputs/chmod_000’: Permission denied\n' -o 'inline:' ../bin/lolcat inputs/chmod_000 +} +noperm_cleanup() { + chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000" + rm inputs/chmod_000 || atf_fail "rm chmod_000" +} + +atf_test_case devfull +devfull_body() { + has_glibc && atf_expect_fail "glibc ignoring write errors for fputc()" + + atf_check -s exit:1 -e 'inline:\nError writing: No space left on device\n' sh -c '../bin/lolcat inputs/all_bytes >/dev/full' + atf_check -s exit:1 -e 'inline:\nError writing: No space left on device\n' sh -c '../bin/lolcat <inputs/all_bytes >/dev/full' + atf_check -s exit:1 -e 'inline:\nError writing: No space left on device\n' sh -c '../bin/lolcat - <inputs/all_bytes >/dev/full' +} + +atf_test_case readslash +readslash_body() { + [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories" + + # shellcheck disable=SC1112 + atf_check -s exit:1 -e 'inline:\nRead error: Is a directory\n' -o 'inline:' ../bin/lolcat ./ +} + +atf_test_case enoent +enoent_body() { + # shellcheck disable=SC1112 + atf_check -s exit:1 -e 'inline:\nError opening ‘/var/empty/e/no/ent’: No such file or directory\n' -o 'inline:' ../bin/lolcat /var/empty/e/no/ent +} + +atf_test_case doubledash +doubledash_body() { + atf_check -o file:outputs/lolcat/all_bytes -- ../bin/lolcat -- inputs/all_bytes + # shellcheck disable=SC1112 + atf_check -s exit:1 -e 'inline:\nError opening ‘---’: No such file or directory\n' -o 'inline:' -- ../bin/lolcat --- inputs/all_bytes +} + +atf_init_test_cases() { + cd "$(atf_get_srcdir)" || exit 1 + + . ../test_functions.sh + + atf_add_test_case allfile + atf_add_test_case allinput + atf_add_test_case alldashinput + atf_add_test_case devnull + atf_add_test_case noperm + atf_add_test_case devfull + atf_add_test_case readslash + atf_add_test_case enoent + atf_add_test_case doubledash +} diff --git a/test-bin/outputs/lolcat/all_bytes b/test-bin/outputs/lolcat/all_bytes Binary files differ.