Re: [dev] Shell script testing functions

2018-06-21 Thread Jens John
On Sat, 9 Jun 2018, at 17:19, Adrian Grigore wrote:
> I sometimes enjoy testing my shell scripts. Opinions?

If you're looking into writing effective unit tests for your shell projects, I 
highly recommend to run the script itself as well as the test in a controlled 
environment. Treat the environment as input to your program:

  * Clear all environment variables and (re-) set only those which are needed, 
and
  * When writing the script per se, check that the environment fits your 
requirements and if not, provide appropriate settings.

This is necessary since shell scripts may call out to tools which make certain 
assumptions about the environment and may behave incorrectly if assumptions are 
not met:

  * Locale-related environment variables control a lot of text processing 
aspects in the libc and other libraries and tools like ncurses. If you get 
UTF-8 input and run with noncompatible locale settings, you'll be in for a bad 
time. Some tools may fail to initialize correctly. For example, $HOME is not 
guaranteed to be set in Bash and tools that depend on it may fail while tilde 
home directory expansion uses passwd and will continue to work.

  * The environment changes depending on the execution context: Invoking a 
program from your current shell will produce a wildly different environement 
than running during system start under sysvinit, which again will be different 
from running a program in a separate cgroup with cleansed environment, which 
again will be different when just invoking /etc/init.d/program from your 
current shell.

The environment can make or break your script/program.



Re: [dev] Shell script testing functions

2018-06-09 Thread Adrian Grigore
> 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

2018-06-09 Thread Evan Gates
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] Shell script testing functions

2018-06-09 Thread Adrian Grigore
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