Re: [dev] Shell script testing functions

From: Jens John <>
Date: Thu, 21 Jun 2018 14:58:37 +0200

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.
Received on Thu Jun 21 2018 - 14:58:37 CEST

This archive was generated by hypermail 2.3.0 : Thu Jun 21 2018 - 15:00:08 CEST