TestManSH Project @ SerDigital64 | Sunday, Apr 10, 2022 | 3 | Sunday, Apr 10, 2022

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

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Usage: testmansh <-b|-t|-q|-l|-i|k> [-p Project] [-c Case] [-e Image] [-r Registry] [-s BatsCore] [-u ShellCheck] [-f EnvFile] [-m Format|-j JUnitFile] [-g] [-h]

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

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
  -o           : Enable container mode (for -b and -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

Run bats-core test-cases in container images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 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

1
2
3
4
5
# Change directory to the project location
cd PROJECT_PATH

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

Deployment

Compatibility

  • AlmaLinux8
  • Alpine3
  • CentOS7
  • CentOS8
  • CentOS9
  • Debian9
  • Debian10
  • Debian11
  • Fedora33
  • Fedora34
  • Fedora35
  • MacOS12
    • Requires Bash4 (e.g. homebrew install bash)
  • OracleLinux7
  • OracleLinux8
  • RedHatLinux8
  • RockyLinux8
  • Ubuntu20
  • Ubuntu21
  • Ubuntu22

Requirements

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

Installation

Download testmansh from the source GitHub repository:

1
2
3
4
curl -O https://raw.githubusercontent.com/serdigital64/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

© 2021 - 2022 SerDigital64's Blog

Powered by Hugo with theme Dream.

Articles in this site are licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0)

avatar

SerDigital64's BlogTraveler log from my journey through the lands of the ever evolving digital world

About SerDigital64
███████╗███████╗██████╗                         
██╔════╝██╔════╝██╔══██╗                        
███████╗█████╗  ██████╔╝                        
╚════██║██╔══╝  ██╔══██╗                        
███████║███████╗██║  ██║                        
╚══════╝╚══════╝╚═╝  ╚═╝                        
                                  
██████╗ ██╗ ██████╗ ██╗████████╗ █████╗ ██╗     
██╔══██╗██║██╔════╝ ██║╚══██╔══╝██╔══██╗██║     
██║  ██║██║██║  ███╗██║   ██║   ███████║██║     
██║  ██║██║██║   ██║██║   ██║   ██╔══██║██║     
██████╔╝██║╚██████╔╝██║   ██║   ██║  ██║███████╗
╚═════╝ ╚═╝ ╚═════╝ ╚═╝   ╚═╝   ╚═╝  ╚═╝╚══════╝
                                  
██████╗  ██╗  ██╗                               
██╔════╝ ██║  ██║                               
███████╗ ███████║                               
██╔═══██╗╚════██║                               
╚██████╔╝     ██║                               
╚═════╝      ╚═╝                               


Solutions_Architect && SysAdmin && DevOpsEngineer
Developer = 'for_the_fun'
Linux && OSS_advocate
Sci_Fi = 'fan'
Photography && DIY == enthusiast()

eMail('serdigital64@gmail.com')
Articles in this site are licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0)