Linux Test Project
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: LTP List <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH v4 2/4] tst_ansi_color.sh: Allow to run with set -e
Date: Tue, 9 Aug 2022 14:04:43 +0200	[thread overview]
Message-ID: <YvJNW4okkn2OLK8d@pevik> (raw)
In-Reply-To: <CAEemH2eZmemcmn1H3Gi6zpvr+a-tAZ38GFuGzXbWPM7uKCw-AQ@mail.gmail.com>

> Hi Petr,

> On Mon, Aug 8, 2022 at 7:38 PM Petr Vorel <pvorel@suse.cz> wrote:

> > set -e (or #!/bin/sh -e or set -o errexit) quits on any non-zero exit
> > code, therefore any && must be turned into || (or if ...; then ..; fi).
> > Fix hardens tst_res TINFO to be able to be used on scripts with errexit.

> > Signed-off-by: Petr Vorel <pvorel@suse.cz>
> > ---
> > changes v2->v3:
> > * really fix it.

> >  testcases/lib/tst_ansi_color.sh | 13 +++++++------
> >  1 file changed, 7 insertions(+), 6 deletions(-)

> > diff --git a/testcases/lib/tst_ansi_color.sh
> > b/testcases/lib/tst_ansi_color.sh
> > index 703df1eb8..517b709d0 100644
> > --- a/testcases/lib/tst_ansi_color.sh
> > +++ b/testcases/lib/tst_ansi_color.sh
> > @@ -24,18 +24,19 @@ tst_flag2color()

> >  tst_color_enabled()
> >  {
> > -       [ "$LTP_COLORIZE_OUTPUT" = "n" ] || [ "$LTP_COLORIZE_OUTPUT" = "0"
> > ] && return 0
> > -       [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1"
> > ] && return 1
> > +       [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]
> > || return 1
> > +       [ "$LTP_COLORIZE_OUTPUT" = "y" -o "$LTP_COLORIZE_OUTPUT" = "1" ]
> > || return 0

> This can work but looks a bit strange to read. I personally think
> use 'if ...; then ; fi' will be better to understand, because this is a
> simple function, no need to go with weird logic for over simplifying:).

Hi Li,

sure, I can reuse what I posted to as a suggestion to 3rd patch [1],
therefore I'll use it for these two:

if [ "$LTP_COLORIZE_OUTPUT" = "n" -o "$LTP_COLORIZE_OUTPUT" = "0" ]; then
        return 0
fi

if [ "$LTP_COLORIZE_OUTPUT" = "y" ] || [ "$LTP_COLORIZE_OUTPUT" = "1" ]; then
        return 1
fi

For the latter two I can use 'if ...' as well:

if [ "$color" = 1 ]; then
        tst_flag2color "$1"
fi
printf "$2"
if [ "$color" = 1 ]; then
        printf '\033[0m'
fi

although the original != 1 ] || is IMHO quite readable.

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/20220808113756.11582-4-pvorel@suse.cz/

> >         [ -t 1 ] || return 0
> >         return 1
> >  }

> >  tst_print_colored()
> >  {
> > -       tst_color_enabled
> > -       local color=$?
> > +       local color=0

> > -       [ "$color" = "1" ] && tst_flag2color "$1"
> > +       tst_color_enabled || color=$?
> > +
> > +       [ "$color" != 1 ] || tst_flag2color "$1"
> >         printf "$2"
> > -       [ "$color" = "1" ] && printf '\033[0m'
> > +       [ "$color" != 1 ] || printf '\033[0m'
> >  }
> > --
> > 2.37.1


> > --
> > Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2022-08-09 12:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08 11:37 [LTP] [PATCH v4 0/4] set -e and bashism fixes Petr Vorel
2022-08-08 11:37 ` [LTP] [PATCH v4 1/4] tst_test.sh: runtest.sh: Remove unused code Petr Vorel
2022-08-09  9:25   ` Li Wang
2022-08-10 15:03   ` Martin Doucha
2022-08-08 11:37 ` [LTP] [PATCH v4 2/4] tst_ansi_color.sh: Allow to run with set -e Petr Vorel
2022-08-09  9:31   ` Li Wang
2022-08-09 12:04     ` Petr Vorel [this message]
2022-08-10  2:46       ` Li Wang
2022-08-10  6:36         ` Petr Vorel
2022-08-08 11:37 ` [LTP] [PATCH v4 3/4] tst_test.sh: Fix _tst_cleanup_timer() on " Petr Vorel
2022-08-09  9:35   ` Li Wang
2022-08-09 12:13     ` Petr Vorel
2022-08-10 15:08   ` Martin Doucha
2022-08-10 16:29     ` Petr Vorel
2022-08-11  8:00       ` Martin Doucha
2022-08-11  8:08         ` Petr Vorel
2022-08-11  8:35         ` Petr Vorel
2022-08-08 11:37 ` [LTP] [PATCH v4 4/4] generate_lvm_runfile.sh: Fix bashism Petr Vorel
2022-08-09 10:33   ` Li Wang
2022-08-10 15:13   ` Martin Doucha
2022-08-10 16:38     ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YvJNW4okkn2OLK8d@pevik \
    --to=pvorel@suse.cz \
    --cc=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox