Eclass testing

From Gentoo Wiki
Jump to:navigation Jump to:search

This is a basic guide to write tests for Gentoo Eclasses.

Testing framework

In order to test eclasses, you need to source the eclass/tests/tests-common.sh file in the Gentoo ebuild repository. It sets up a testing environment, provides replacements for commonly used functions and adds helpers for running tests.

Tests are bash scripts that can be located in any directory, although in this guide eclass/tests will be assumed.

Sourcing

In the ::gentoo repo, it's as simple as:

CODE
source tests-common.sh || exit
Important
Always exit if sourcing failed, otherwise terrible things like bug #833342 could happen!

In other repositories, use portageq to locate the file:

CODE
GENTOO_REPO=$(portageq get_repo_path / gentoo) || exit
source "${GENTOO_REPO}"/eclass/tests/tests-common.sh || exit
TESTS_ECLASS_SEARCH_PATHS+=( "${GENTOO_REPO}"/eclass )

Note the TESTS_ECLASS_SEARCH_PATHS variable. Without it you won't be able to inherit Gentoo eclasses.

Overview of helpers

Function Description Example use
tbegin message ebegin wrapper for a test case.
CODE
tbegin "dependency strings"
t cmd Run cmd and set the suite's exit status to non-zero if it failed.
CODE
t test "${RDEPEND}" == "app-foo/bar"
tend status eend wrapper for a test case.
CODE
tend $?
texit Clean up and set the suite's exit status. Call it at the end of your test suite.
CODE
texit

External resources