Gentoo Archives: gentoo-dev

From: Conrad Kostecki <conikost@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] [PATCH] eclass/lua-utils.eclass: Add support for test-runners
Date: Mon, 04 Jan 2021 13:30:24
Message-Id: 20210104133009.20639-1-conikost@gentoo.org
1 During migration of dev-lua/* ebuilds to slotted lua, I noticed, that
2 many ebuilds use 'dev-lua/busted' for running tests. This change adds
3 support for running a test-runner, at first only 'busted' for now.
4 Also a non-color and plaintext output will be used for the test-runner 'busted'.
5
6 This is basically a copy of the test-runner section, written by mgorny,
7 which already exists in 'distutils-r1', but modified and adapted to lua.
8
9 In order to use this feature, you can define 'lua_enable_tests busted'
10 to setup everything needed for tests and run them. By default,
11 'dev-lua/busted' assumes, that tests are in the 'spec' folder.
12
13 If this is not the case, you can add a second argument to specify a
14 different folder. For example, if the folder is called 'foo', you can
15 just run 'lua_enable_tests busted foo'.
16
17 More test-runners can be added in future, if needed.
18
19 Signed-off-by: Conrad Kostecki <conikost@g.o>
20 ---
21 eclass/lua-utils.eclass | 73 +++++++++++++++++++++++++++++++++++++++++
22 1 file changed, 73 insertions(+)
23
24 diff --git a/eclass/lua-utils.eclass b/eclass/lua-utils.eclass
25 index 100be14cb08..b24c4a461f2 100644
26 --- a/eclass/lua-utils.eclass
27 +++ b/eclass/lua-utils.eclass
28 @@ -344,6 +344,79 @@ _lua_export() {
29 done
30 }
31
32 +# @FUNCTION: lua_enable_tests
33 +# @USAGE: <test-runner> <test-directory>
34 +# @DESCRIPTION:
35 +# Set up IUSE, RESTRICT, BDEPEND and src_test() for running tests
36 +# with the specified test runner. Also copies the current value
37 +# of RDEPEND to test?-BDEPEND. The test-runner argument must be one of:
38 +#
39 +# - busted: dev-lua/busted
40 +#
41 +# Additionally, a second argument can be passed after <test-runner>,
42 +# so <test-runner> will use that directory to search for tests.
43 +# If not passed, a default directory of <test-runner> will be used.
44 +#
45 +# - busted: spec
46 +#
47 +# This function is meant as a helper for common use cases, and it only
48 +# takes care of basic setup. You still need to list additional test
49 +# dependencies manually. If you have uncommon use case, you should
50 +# not use it and instead enable tests manually.
51 +#
52 +# This function must be called in global scope, after RDEPEND has been
53 +# declared. Take care not to overwrite the variables set by it.
54 +lua_enable_tests() {
55 + debug-print-function ${FUNCNAME} "${@}"
56 +
57 + [[ ${#} -ge 1 ]] || die "${FUNCNAME} takes at least one argument: test-runner (test-directory)"
58 + local test_pkg
59 + case ${1} in
60 + busted)
61 + test_directory="${2:-spec}"
62 + test_pkg="dev-lua/busted"
63 + if [[ ! ${_LUA_SINGLE_R0} ]]; then
64 + lua_src_test() {
65 + busted --lua="${ELUA}" --output="plainTerminal" "${test_directory}" || die "Tests fail with ${ELUA}"
66 + }
67 + src_test() {
68 + lua_foreach_impl lua_src_test
69 + }
70 + else
71 + src_test() {
72 + busted --lua="${ELUA}" --output="plainTerminal" "${test_directory}" || die "Tests fail with ${ELUA}"
73 + }
74 + fi
75 + ;;
76 + *)
77 + die "${FUNCNAME}: unsupported argument: ${1}"
78 + esac
79 +
80 + local test_deps=${RDEPEND}
81 + if [[ -n ${test_pkg} ]]; then
82 + if [[ ! ${_LUA_SINGLE_R0} ]]; then
83 + test_deps+=" ${test_pkg}[${LUA_USEDEP}]"
84 + else
85 + test_deps+=" $(lua_gen_cond_dep "
86 + ${test_pkg}[\${LUA_USEDEP}]
87 + ")"
88 + fi
89 + fi
90 + if [[ -n ${test_deps} ]]; then
91 + IUSE+=" test"
92 + RESTRICT+=" !test? ( test )"
93 + if [[ ${EAPI} == [56] ]]; then
94 + DEPEND+=" test? ( ${test_deps} )"
95 + else
96 + BDEPEND+=" test? ( ${test_deps} )"
97 + fi
98 + fi
99 +
100 + # we need to ensure successful return in case we're called last,
101 + # otherwise Portage may wrongly assume sourcing failed
102 + return 0
103 +}
104 +
105 # @FUNCTION: lua_get_CFLAGS
106 # @USAGE: [<impl>]
107 # @DESCRIPTION:
108 --
109 2.29.2