Gentoo Archives: gentoo-dev

From: Conrad Kostecki <conikost@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] [PATCH v3] eclass/lua-utils.eclass: Add support for test-runners
Date: Fri, 08 Jan 2021 00:14:30
Message-Id: 20210108001415.3124-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 PATCH v2 has two changes:
20 - removed EAPI condition, as lua-utils is EAPI=7 only.
21 - make test_directoy as a local variable and use eval in src_test to
22 read it.
23
24 PATCH v3 has the right patch, as v2 was wrong.
25
26 Signed-off-by: Conrad Kostecki <conikost@g.o>
27 ---
28 eclass/lua-utils.eclass | 70 +++++++++++++++++++++++++++++++++++++++++
29 1 file changed, 70 insertions(+)
30
31 diff --git a/eclass/lua-utils.eclass b/eclass/lua-utils.eclass
32 index 100be14cb08..0589318ef51 100644
33 --- a/eclass/lua-utils.eclass
34 +++ b/eclass/lua-utils.eclass
35 @@ -344,6 +344,76 @@ _lua_export() {
36 done
37 }
38
39 +# @FUNCTION: lua_enable_tests
40 +# @USAGE: <test-runner> <test-directory>
41 +# @DESCRIPTION:
42 +# Set up IUSE, RESTRICT, BDEPEND and src_test() for running tests
43 +# with the specified test runner. Also copies the current value
44 +# of RDEPEND to test?-BDEPEND. The test-runner argument must be one of:
45 +#
46 +# - busted: dev-lua/busted
47 +#
48 +# Additionally, a second argument can be passed after <test-runner>,
49 +# so <test-runner> will use that directory to search for tests.
50 +# If not passed, a default directory of <test-runner> will be used.
51 +#
52 +# - busted: spec
53 +#
54 +# This function is meant as a helper for common use cases, and it only
55 +# takes care of basic setup. You still need to list additional test
56 +# dependencies manually. If you have uncommon use case, you should
57 +# not use it and instead enable tests manually.
58 +#
59 +# This function must be called in global scope, after RDEPEND has been
60 +# declared. Take care not to overwrite the variables set by it.
61 +lua_enable_tests() {
62 + debug-print-function ${FUNCNAME} "${@}"
63 +
64 + [[ ${#} -ge 1 ]] || die "${FUNCNAME} takes at least one argument: test-runner (test-directory)"
65 + local test_directory
66 + local test_pkg
67 + case ${1} in
68 + busted)
69 + test_directory="${2:-spec}"
70 + test_pkg="dev-lua/busted"
71 + if [[ ! ${_LUA_SINGLE_R0} ]]; then
72 + eval "lua_src_test() {
73 + busted --lua=\"\${ELUA}\" --output=\"plainTerminal\" \"${test_directory}\" || die \"Tests fail with \${ELUA}\"
74 + }"
75 + src_test() {
76 + lua_foreach_impl lua_src_test
77 + }
78 + else
79 + eval "src_test() {
80 + busted --lua=\"\${ELUA}\" --output=\"plainTerminal\" \"${test_directory}\" || die \"Tests fail with \${ELUA}\"
81 + }"
82 + fi
83 + ;;
84 + *)
85 + die "${FUNCNAME}: unsupported argument: ${1}"
86 + esac
87 +
88 + local test_deps=${RDEPEND}
89 + if [[ -n ${test_pkg} ]]; then
90 + if [[ ! ${_LUA_SINGLE_R0} ]]; then
91 + test_deps+=" ${test_pkg}[${LUA_USEDEP}]"
92 + else
93 + test_deps+=" $(lua_gen_cond_dep "
94 + ${test_pkg}[\${LUA_USEDEP}]
95 + ")"
96 + fi
97 + fi
98 + if [[ -n ${test_deps} ]]; then
99 + IUSE+=" test"
100 + RESTRICT+=" !test? ( test )"
101 + BDEPEND+=" test? ( ${test_deps} )"
102 + fi
103 +
104 + # we need to ensure successful return in case we're called last,
105 + # otherwise Portage may wrongly assume sourcing failed
106 + return 0
107 +}
108 +
109 # @FUNCTION: lua_get_CFLAGS
110 # @USAGE: [<impl>]
111 # @DESCRIPTION:
112 --
113 2.30.0

Replies