Context
After setting up a working GitLab yaml for bats (Bash Automated Testing System) using an Alpine docker image:
unit_test:
stage: test
script:
- apk update && apk add bash
- apk add git # Default docker image uses Alpine iso Ubuntu, hence apk.
- chmod +x *.sh
- ./install-bats-libs.sh
- bash ./test.sh
I noticed it would be marvellous to include code coverage. However most of the examples are for Travis or rely on an external server. I did not yet find a GitLab example for self-hosted code coverage for Bash.
Question
Hence, I would like to ask: How can one include a self-hosted bash code coverage report in a .GitLab-ci.yml?
Attempt
The comment by Benjamin W. led me to this example. Based on that example, I tried the following BashCov in the following GitLab.yml on this bats shell testing repository:
stages:
- test
- coverage_test
unit_test:
stage: test
script:
- apk update && apk add bash
- apk add git # Default docker image uses Alpine iso Ubuntu, hence apk.
- chmod +x *.sh
- ./install-bats-libs.sh
- bash ./test.sh
test:
stage: coverage_test
image: ruby:slim
before_script:
- gem install bashcov
script:
# bashcov cannot run as root so here's a magic user
- useradd -s /bin/bash user
- su user -c "$(command -v bashcov) gem install git && ./install-bats-libs.sh && ./test.sh"
coverage: '/\(\d+\.\d+%\) covered/'
artifacts:
untracked: true
That passes the BATS tests, however the second job, with the coverage fails with output:
Output
Running with gitlab-runner 15.4.0 (43b2dc3d)
on trucolrunner Pa3Jpz_s
Preparing the "docker" executor
00:06
Using Docker executor with image ruby:slim ...
Pulling docker image ruby:slim ...
Using docker image sha256:a1ed21c1d9c7a0d7dbc9d24a25d55ee51e9d798d01073b75051dc8792e313644 for ruby:slim with digest ruby@sha256:2fd51ded2c105603ad42099bc60a057ec93667a8ebefab75b780ea1d57efa517 ...
Preparing environment
00:01
Running on runner-pa3jpzs-project-116-concurrent-0 via pcname...
Getting source from Git repository
00:00
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/root/shell_unit_testing_template/.git/
Checking out 90204537 as main...
warning: unable to rmdir 'test/libs/bats': Directory not empty
warning: unable to rmdir 'test/libs/bats-assert': Directory not empty
warning: unable to rmdir 'test/libs/bats-support': Directory not empty
Removing test/libs/
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:03
Using docker image sha256:a1ed21c1d9c7a0d7dbc9d24a25d55ee51e9d798d01073b75051dc8792e313644 for ruby:slim with digest ruby@sha256:2fd51ded2c105603ad42099bc60a057ec93667a8ebefab75b780ea1d57efa517 ...
$ gem install bashcov
Successfully installed simplecov-html-0.10.2
Successfully installed docile-1.1.5
Successfully installed simplecov-0.15.1
Successfully installed bashcov-1.8.2
4 gems installed
$ useradd -s /bin/bash user
$ su user -c "$(command -v bashcov) gem install git && ./install-bats-libs.sh && ./test.sh"
/usr/local/bin/gem: line 8: require: command not found
/usr/local/bin/gem: line 9: require: command not found
/usr/local/bin/gem: line 10: require: command not found
/usr/local/bin/gem: line 12: required_version: command not found
/usr/local/bin/gem: line 14: unless: command not found
/usr/local/bin/gem: line 15: abort: command not found
/usr/local/bin/gem: line 16: end: command not found
/usr/local/bin/gem: line 18: args: command not found
/usr/local/bin/gem: line 20: begin: command not found
/usr/local/bin/gem: line 21: Gem::GemRunner.new.run: command not found
/usr/local/bin/gem: line 22: rescue: command not found
/usr/local/bin/gem: line 23: exit: e.exit_code: numeric argument required
Run completed using bashcov 1.8.2 with Bash 5.1, Ruby 3.1.2, and SimpleCov 0.15.1.
Coverage report generated for /bin/bash gem install git to /builds/root/shell_unit_testing_template/coverage. 0 / 649 LOC (0.0%) covered.
ERROR: Job failed: exit code 1