Josef-Friedrich / test-helper.sh

A collection of helper functions for the shell-script testing.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

test-helper.sh

A collection of helper functions for shell-script testing

Testing frameworks

Blog posts

Usage

. ./test-helper.sh

mock_path

. ./test-helper.sh
mock_path /home/jf/shell/test/bin
echo $PATH

Result: /home/jf/shell/test/bin:/bin:/sbin/...

. ./test-helper.sh
PARENT_MOCK_PATH=/tests/bin
mock_path for-script1:for-script2:for-script3
echo $PATH

Result: /tests/bin/for-script1:/tests/bin/for-script2:/tests/bin/for-script3:/bin:/sbin/.....

source_exec (source executable scripts)

The function source_exec includes a file until a separator string appears. By default the separator is:

## This SEPARATOR is required for test purposes. Please don’t remove! ##

cat executable_script.sh:

#! /bin/sh

_echo_lol() {
	echo lol
}

## This SEPARATOR is required for test purposes. Please don’t remove! ##

echo "If you see this text, then the function didn’t work"

In your test environment:

. ./test-helper.sh
source_exec executable_script.sh

test_lol() {
  assert _echo_lol ....
}

Use the environment variable SOURCE_EXEC_SEPARATOR to set your own separator string.

patch

Useful to rename builtin command.

patch <file> <sed-statements>
patch test-helper.sh 's/local /superlocal /g'

or multiple sed statements

patch test-helper.sh -e 's/local /superlocal /g' -e 's/for /superfor /g'

About

A collection of helper functions for the shell-script testing.

License:MIT License


Languages

Language:Shell 98.5%Language:Makefile 1.5%