Skip to content

Simple tool for testing Bash scripts in either native environment or purpose-build container images

License

Notifications You must be signed in to change notification settings

automation64/testmansh

Repository files navigation

Script: testmansh

Overview

Simple tool for testing Bash scripts in native environment or purpose-build container images.

Supported features:

  • Run tests inside container using docker or podman container engines
  • Run tests in native environment (localhost)

Supported tools

  • code linting: shell check
  • code testing: bats-core, bats-helpers file, asset, support

Usage

Usage: testmansh <-b|-t|-q|-l|-i|k> [-p Project] [-c Case] [-e Image] [-o|-a] [-r Registry] [-s BatsCore] [-u ShellCheck] [-f EnvFile] [-m Format|-j JUnitFile] [-g] [-V Verbose] [-D Debug] [-h]

Simple tool for testing Bash scripts in native environment or purpose-build container images.

By default testmansh assumes that scripts are organized using the following directory structure:

  - project_path/: project base path
  - project_path/src/: bash scripts
  - project_path/test/: test-cases repository
  - project_path/test/batscore/: test cases in bats-core format
  - project_path/test/samples/: test samples
  - project_path/test/lib/: shared code for test cases

The tool also sets and exports shell environment variables that can be used directly in test cases. The content is automatically adjusted to native or container execution:

  - TESTMANSH_PROJECT_ROOT: project base path (project/)
  - TESTMANSH_PROJECT_BIN: dev time scripts (project/bin)
  - TESTMANSH_PROJECT_SRC: source code (project/src)
  - TESTMANSH_PROJECT_LIB: dev time libs (project/lib)
  - TESTMANSH_PROJECT_BUILD: location for dev,test builds (project/build)
  - TESTMANSH_TEST: tests base path (project/test)
  - TESTMANSH_TEST_SAMPLES: test samples (project/test/samples)
  - TESTMANSH_TEST_LIB: test shared libs (project/test/lib)
  - TESTMANSH_TEST_BATSCORE_SETUP: batscore setup script (project/test/lib/batscore-setup.bash)
  - TESTMANSH_CMD_BATS_HELPER_SUPPORT: full path to the bats-core support helper
  - TESTMANSH_CMD_BATS_HELPER_ASSERT: full path to the bats-core assert helper
  - TESTMANSH_CMD_BATS_HELPER_FILE: full path to the bats-core file helper

Commands

  -b           : Run bats-core tests
  -t           : Run shellcheck linter
  -q           : Open bats-core container
  -l           : List bats-core test cases
  -i           : List bats-core container images
  -k           : List shellcheck targets

Flags

  -g           : Enable debug mode in test cases
  -o           : Enable container mode (for -b and -t)
  -a           : Autodetect best mode (for -t)
  -h           : Show Help

Parameters

  -p Project   : Full path to the project location. Alternative: exported shell variable TESTMANSH_PROJECT. Default: current location
  -c Case      : Test case name (for -b) or source code (for -t). Default: all. Format: path/file or path/ (relative to TESTMANSH_PROJECT)
  -e Image     : Image name to use for running the container test (-b) or open (-q). Alternative: exported shell variable TESTMANSH_IMAGES_TEST. Default: predefined list
  -r Registry  : Container registry URL. Alternative: exported shell variable TESTMANSH_REGISTRY
  -f EnvFile   : Full path to the container environment file. Default: TESTMANSH_PROJECT/test/container.env
  -s BatsCore  : Full path to the bats-core shell script. Alternative: exported shell variable TESTMANSH_CMD_BATS
  -u ShellCheck: Full path to the bats-core shell script. Alternative: exported shell variable TESTMANSH_CMD_SHELLCHECK
  -m Format    : Show test results on STDOUT using the Format type. Format: shellcheck and bats-core dependant
  -j JUnitFile : Save test results in JUnit format to the file JUnitFile. Format: full path
  -V Verbose   : Set verbosity level. Format: one of BL64_MSG_VERBOSE_*
  -D Debug     : Enable debugging mode. Format: one of BL64_DBG_TARGET_*```

### Run bats-core test-cases in container images

```shell
# Change directory to the project location
cd PROJECT_PATH

# (Optional) List available containers and select one. All containers are included by default
testmansh -i

# (Optional) List available test-cases using default location and select one. All test-cases are included by default
testmansh -l

# Run test-cases (with optional container and test case selection)
testmansh -b -o [-c CASE_PATH] [-e IMAGE_NAME]

Run code linter

# Change directory to the project location
cd PROJECT_PATH

# Run linter using default code location src/
testmansh -t -o

Deployment

Compatibility

Same as bashlib64

Requirements

  • Bash >= v4
  • Container test
    • Docker or Podman

Installation

Download testmansh from the source GitHub repository:

curl -O https://raw.githubusercontent.com/automation64/testmansh/main/testmansh
chmod 0755 testmansh
# Optional: move to searchable path
mv testmansh ~/.local/bin

Contributing

Help on implementing new features and maintaining the code base is welcomed.

License

GPL-3.0-or-later

Author

About

Simple tool for testing Bash scripts in either native environment or purpose-build container images

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages