[Toybox] Testing: how to check return code
Jarno Mäkipää
jmakip87 at gmail.com
Wed Oct 30 09:08:05 PDT 2019
Hi
I think some other tests use this kind of execution to test commands
that should fail
cmd1 && cmd2 && echo success || echo epic fail
which is same as
if cmd1 && cmd2; then
echo success
else
echo epic fail
fi
So maybe your test could be written something like this,
where stderr is piped to null and when failing command we should get "yes\n"
testing "with -w fail negative" "ls -w -5 2> /dev/null && echo no ||
echo yes" "yes\n" "" ""
try check other tests that has echo yes in them for reference.
-Jarno
On Wed, Oct 30, 2019 at 3:44 PM Andrew Ilijic <ilijic.andrew at gmail.com> wrote:
>
> While implementing `-w` for the `ls` command, I coded it so that the
> command would fail if the user tried to pass a negative value. I
> wanted to test the return code with a negative argument but could not
> make it work. I am not great at Bash. If anyone can tell me what I was
> doing wrong, I would appreciate it.
>
> ```
> testing "with -w - Fail on negative" \
> "$IN && ls -w -5 > /dev/null; echo $? && $OUT" \
> "1" "" ""
> ```
> The command would fail and return 1 but, in testing it would always
> write zero to the file.
>
> ~Andrew
> _______________________________________________
> Toybox mailing list
> Toybox at lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
More information about the Toybox
mailing list