Re: [dev] Shell script testing functions
> 1) use shellcheck. Check out shellcheck.net or install locally. It catches the most common shell scripting problems. I already do. Thanks for the other recommendations. On Sat, Jun 9, 2018 at 6:32 PM Evan Gates wrote: > > On Sat, Jun 9, 2018, 08:20 Adrian Grigore > wrote: > > > I sometimes enjoy testing my shell scripts. Opinions? > > I'm away from my computer so I can't give full feedback but for now > I'd recommend > > 1) use shellcheck. Check out shellcheck.net or install locally. It > catches the most common shell scripting problems. > > 2) mywiki.wooledge.org has a great guide, faq, and list of pitfalls. > The pitfalls is a great place to start. It's a list of very common > problems with explanations of why they are wrong and how to fix them. > > 3) #bash on freenode. Helpful if occasionally snarky community. If you > ask them for feedback and avoid the xy problem you can learn a lot. > > emg > -- Thanks, Adi
Re: [dev] Shell script testing functions
On Sat, Jun 9, 2018, 08:20 Adrian Grigore wrote: > I sometimes enjoy testing my shell scripts. Opinions? I'm away from my computer so I can't give full feedback but for now I'd recommend 1) use shellcheck. Check out shellcheck.net or install locally. It catches the most common shell scripting problems. 2) mywiki.wooledge.org has a great guide, faq, and list of pitfalls. The pitfalls is a great place to start. It's a list of very common problems with explanations of why they are wrong and how to fix them. 3) #bash on freenode. Helpful if occasionally snarky community. If you ask them for feedback and avoid the xy problem you can learn a lot. emg
[dev] Re: Shell script testing functions
Sorry, minor bugs fixed. #!/bin/sh tap_inited=false tap_i=0 tap_exitstatus=0 tap_printline() { [ "$1" -eq 0 ] && printf "ok %d - %s\n" $tap_i "$current" && return; printf "not ok %d - %s\n" $tap_i "$current" tap_exitstatus=1 } tap_test() { lastexitstatus=$? ! $tap_inited && current="$1" && tap_inited=true && tap_pretest && return; tap_i=$((tap_i+1)) tap_printline $lastexitstatus tap_posttest current=$1 tap_pretest } tap_printplan() { printf "1..%s\n" "$tap_i" } tap_finish() { lastexitstatus=$? [ -z "$current" ] && exit tap_printline $lastexitstatus tap_printplan exit $tap_exitstatus } trap tap_finish EXIT tap_pretest() { true } tap_posttest() { true } tap_test "one plus zero is zero" sum=$((1+0)) [ $sum -eq 0 ] tap_test "one plus one is one" sum=$((1+1)) [ $sum -eq 2 ] tap_test "one plus one is three" sum=$((1+1)) [ $sum -eq 3 ] tap_test "two plus two is four" sum=$((2+2)) [ $sum -eq 4 ] On Sat, Jun 9, 2018 at 6:19 PM Adrian Grigore wrote: > > I sometimes enjoy testing my shell scripts. Opinions? > > #!/bin/sh > > tap_inited=false > tap_i=0 > tap_exitstatus=0 > > tap_printline() > { > [ "$1" -eq 0 ] && printf "ok %d - %s\n" $tap_i "$current" && return; > > printf "not ok %d - %s\n" $tap_i "$current" > exitstatus=1 > } > > tap_test() { > lastexitstatus=$? > > ! $tap_inited && current="$1" && tap_inited=true && tap_pretest && return; > tap_i=$((tap_i+1)) > > tap_printline $lastexitstatus > tap_posttest > current=$1 > tap_pretest > } > > tap_printplan() { > printf "1..%s\n" "$tap_i" > } > > tap_finish() { > lastexitstatus=$? > [ -z "$current" ] && exit > tap_printline $lastexitstatus > tap_printplan > exit $exitstatus > } > > trap tap_finish EXIT > > tap_pretest() { > true > } > > tap_posttest() { > true > } > > tap_test "one plus zero is zero" > sum=$((1+0)) > [ sum -eq 0 ] > > tap_test "one plus one is one" > sum=$((1+1)) > [ sum -eq 2 ] > > tap_test "one plus one is three" > sum=$((1+1)) > [ sum -eq 3 ] > > tap_test "two plus two is four" > sum=$((2+2)) > [ sum -eq 4 ] > > -- > Thanks, > Adi -- Thanks, Adi
[dev] Shell script testing functions
I sometimes enjoy testing my shell scripts. Opinions? #!/bin/sh tap_inited=false tap_i=0 tap_exitstatus=0 tap_printline() { [ "$1" -eq 0 ] && printf "ok %d - %s\n" $tap_i "$current" && return; printf "not ok %d - %s\n" $tap_i "$current" exitstatus=1 } tap_test() { lastexitstatus=$? ! $tap_inited && current="$1" && tap_inited=true && tap_pretest && return; tap_i=$((tap_i+1)) tap_printline $lastexitstatus tap_posttest current=$1 tap_pretest } tap_printplan() { printf "1..%s\n" "$tap_i" } tap_finish() { lastexitstatus=$? [ -z "$current" ] && exit tap_printline $lastexitstatus tap_printplan exit $exitstatus } trap tap_finish EXIT tap_pretest() { true } tap_posttest() { true } tap_test "one plus zero is zero" sum=$((1+0)) [ sum -eq 0 ] tap_test "one plus one is one" sum=$((1+1)) [ sum -eq 2 ] tap_test "one plus one is three" sum=$((1+1)) [ sum -eq 3 ] tap_test "two plus two is four" sum=$((2+2)) [ sum -eq 4 ] -- Thanks, Adi