2 # Copyright 2012 Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of Google Inc. nor the names of its contributors
15 # may be used to endorse or promote products derived from this software
16 # without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # Kyua-based compatibility replacement for atf-run.
34 .
"${KYUA_ATF_COMPAT_PKGDATADIR:-__PKGDATADIR__}/lib.subr"
37 # Path to ATF's configuration directory.
38 ATF_CONFDIR
="${ATF_CONFDIR:-__ATF_CONFDIR__}"
41 # Path to the bin directory where we got installed.
42 BINDIR
="${KYUA_ATF_COMPAT_BINDIR:-__BINDIR__}"
45 # Loads configuration variables from a set of files.
47 # \param dir The directory from which to load the configuration files.
48 # \param output_var The name of the variable that will accumulate all the
49 # --variable flags to represent the configuration file.
51 local dir
="${1}"; shift
52 local output_var
="${1}"; shift
55 for file in "${dir}"/*; do
56 [ "${file}" != "${dir}/*" ] ||
break
58 lib_info
"Loading configuration from ${file}"
62 *common.conf
) prefix
= ;;
63 *) prefix
="test_suites.$(basename "${file}" | sed -e 's,.conf$,,')." ;;
66 local ws
='[ ]*' # That's a space and a tab.
67 local name
='[a-zA-Z][-_a-zA-Z0-9]*'
68 local repl
="--variable='${prefix}\\1=\\2'"
69 local vars
="$(grep "^
${ws}${name}${ws}=" "${file}" | \
70 sed -e 's,#(.*)$,,;s,unprivileged-user,unprivileged_user,g' \
71 -e "s
,^
${ws}\(${name}\)${ws}=${ws}'\([^']*\)'${ws}$,${repl}," \
72 -e "s
,^
${ws}\(${name}\)${ws}=${ws}\"\([^\"]*\)\"${ws}$,${repl}," \
73 -e "s
,^
${ws}\(${name}\)${ws}=${ws}\(.*\)$,${repl},")"
75 lib_info
"Extracted arguments: ${vars}"
76 all_vars
="${all_vars} ${vars}"
78 eval ${output_var}=\'"${all_vars}"\'
82 # Transforms an atf-run -v specification to Kyua's semantics.
84 # \param raw The key=value argument to -v.
85 # \param ... The names of all the possible test suites. For test-suite specific
86 # variables, we expand them to all their possibilities as Kyua needs to know
87 # what test suite a particular variable belongs to.
89 # \post Prints one or more --variable arguments that match the input variable
92 local raw
="${1}"; shift
96 echo "--variable=${raw}" | \
97 sed -e 's,unprivileged-user,unprivileged_user,g'
100 for test_suite
in "${@}"; do
101 echo "--variable=test_suites.${test_suite}.${raw}"
108 # Collects all the test suite names defined in a subtree.
110 # \param dir The subtree to scan for Kyuafiles.
112 # \post Prints the names of all found test suites.
114 local dir
="${1}"; shift
116 find "${dir}" -name Kyuafile
-exec grep "test_suite('" "{}" \
; | \
121 # Gets the path to the compatibility Kyuafile.
123 # If a Kyuafile is found in the current directory, use that directly.
124 # Otherwise, generate a fake Kyuafile in a temporary directory and return
127 # \param [out] output_var The name of the variable to set with the path
128 # of the Kyuafile to be used.
130 local output_var
="${1}"; shift
132 if [ -f Kyuafile
]; then
133 eval ${output_var}=\'"$(pwd)/Kyuafile"\'
134 elif [ -f Atffile
]; then
135 "${BINDIR}/atf2kyua" -s "$(pwd)" -t "${Lib_TempDir}" || \
136 lib_error
"Cannot generate fake Kyuafile"
137 eval ${output_var}=\'"${Lib_TempDir}/Kyuafile"\'
139 lib_error
"Cannot find Atffile nor Kyuafile"
144 # Prints program usage to stdout.
146 # \param progname The name of the program to use for the syntax help.
148 local progname
="${1}"; shift
149 echo "Usage: ${progname} [-v var-value] [program1 [.. programN]]"
153 # Entry point for the program.
155 # \param ... The user-provided arguments.
159 while getopts ':v:' arg
"${@}"; do
162 vflags
="${OPTARG} ${cli_variables}"
165 lib_usage_error
"Unknown option -${OPTARG}"
169 shift $
((${OPTIND} - 1))
173 load_configs
"${ATF_CONFDIR}" "system_variables" # Sets system_variables.
174 load_configs
"${HOME}/.atf" "user_variables" # Sets user_variables.
177 select_kyuafile
"kyuafile_path" # Sets kyuafile_path.
179 local test_suites
="$(grab_test_suites "$
(dirname "${kyuafile_path}")")"
182 for vflag
in ${vflags}; do
183 local values
="$(convert_variable "${vflag}" ${test_suites})"
184 cli_variables
="${values} ${cli_variables}"
187 kyua
${system_variables} ${user_variables} ${cli_variables} \
188 test --kyuafile="${kyuafile_path}" --build-root="$(pwd)" "${@}"