Skip to content

lackhove/coverage-sh

Repository files navigation

Coverage.sh

PyPI - Version PyPI - Status PyPI - Python Version PyPI - License Coverage Ruff Rye

A Coverage.py plugin to measure code coverage of shell (sh or bash) scripts executed from python.

Installation

pip install coverage-sh

Usage

In your pyproject.toml, set

[tool.coverage.run]
plugins = ["coverage_sh"]

and run

coverage run main.py
coverage combine
coverage html

to measure coverage of all shell scripts executed via the subprocess module, e.g.:

subprocess.run(["bash", "test.sh"])

The resulting coverage is then displayed alongside the coverage of the python files:

coverage.sh report screenshot

Caveats

The plugin works by patching the subprocess.Popen class to set the "ENV" and "BASH_ENV" environment variables before execution, to source a helper script which enables tracing. This approach comes with a few caveats:

  • It will only cover shell scripts that are executed via the subprocess module.
  • Only bash and sh are supported

Cover-Always Mode

When using the subprocess modue is not an option, coverage-sh can operate in "cover-always-mode", which is activated by setting

[tool.coverage.coverage_sh]
cover_always = true

in the pyproject.toml. In this mode, Coverage.sh will not respect the coverage.start() and coverage.stop() calls and instead cover every shell script executed after the plugin gets loaded until the main process is finished. This mode is also incompatible with the popular pytest-cov but works with starting pytest from coverage , e.g.:

coverage run -m pytest arg1 arg2 arg3

License

Licensed under the MIT License.

About

A Coverage.py plugin to measure code coverage of shell (sh or bash) scripts executed from python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published