xref: /netbsd-src/external/bsd/kyua-atf-compat/dist/atf2kyua.sh (revision b92cbc7321a4a2d13071f4a9bcf671898920c6b4)
1#! __SH__
2# Copyright 2011 Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
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.
17#
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.
29
30# \file atf2kyua.sh
31# Converts Atffiles to Kyuafiles for a particular test suite.
32
33
34. "${KYUA_ATF_COMPAT_PKGDATADIR:-__PKGDATADIR__}/lib.subr"
35
36
37# Prunes all Kyuafiles from a test suite in preparation for regeneration.
38#
39# \param target_root The path to the test suite.
40remove_kyuafiles() {
41    local target_root="${1}"; shift
42
43    if [ -d "${target_root}" ]; then
44        lib_info "Removing stale Kyuafiles from ${target_root}"
45        find "${target_root}" -name Kyuafile -exec rm -f {} \;
46    fi
47}
48
49
50# Obtains the list of test programs and subdirectories referenced by an Atffile.
51#
52# Any globs within the Atffile are expanded relative to the directory in which
53# the Atffile lives.
54#
55# \param atffile The path to the Atffile to process.
56#
57# \post Prints the list of files referenced by the Atffile on stdout.
58extract_files() {
59    local atffile="${1}"; shift
60
61    local dir="$(dirname "${atffile}")"
62
63    local globs="$(grep '^tp-glob:' "${atffile}" | cut -d ' ' -f 2-)"
64    local files="$(grep '^tp:' "${atffile}" | cut -d ' ' -f 2-)"
65
66    for file in ${files} $(cd "$(dirname "${atffile}")" && echo ${globs}); do
67        if test -d "${dir}/${file}" -o -x "${dir}/${file}"; then
68            echo "${file}"
69        fi
70    done
71}
72
73
74# Converts an Atffile to a Kyuafile.
75#
76# \param atffile The path to the Atfffile to convert.
77# \param kyuafile The path to where the Kyuafile will be written.
78convert_atffile() {
79    local atffile="${1}"; shift
80    local kyuafile="${1}"; shift
81
82    lib_info "Converting ${atffile} -> ${kyuafile}"
83
84    local test_suite="$(grep 'prop:.*test-suite.*' "${atffile}" \
85        | cut -d \" -f 2)"
86
87    local dir="$(dirname "${atffile}")"
88
89    local subdirs=
90    local test_programs=
91    for file in $(extract_files "${atffile}"); do
92        if test -f "${dir}/${file}/Atffile"; then
93            subdirs="${subdirs} ${file}"
94        elif test -x "${dir}/${file}"; then
95            test_programs="${test_programs} ${file}"
96        fi
97    done
98
99    mkdir -p "$(dirname "${kyuafile}")"
100
101    echo "syntax('kyuafile', 1)" >"${kyuafile}"
102    echo >>"${kyuafile}"
103    echo "test_suite('${test_suite}')" >>"${kyuafile}"
104    if [ -n "${subdirs}" ]; then
105        echo >>"${kyuafile}"
106        for dir in ${subdirs}; do
107            echo "include('${dir}/Kyuafile')" >>"${kyuafile}"
108        done
109    fi
110    if [ -n "${test_programs}" ]; then
111        echo >>"${kyuafile}"
112        for tp in ${test_programs}; do
113            echo "atf_test_program{name='${tp}'}" >>"${kyuafile}"
114        done
115    fi
116}
117
118
119# Adds Kyuafiles to a test suite by converting any existing Atffiles.
120#
121# \param source_root The path to the existing test suite root.  Must contain
122#     an Atffile and the test programs.
123# \param target_root The path to the directory where the Kyuafiles will be
124#     written.  The layout will mimic that of source_root.
125add_kyuafiles() {
126    local source_root="${1}"; shift
127    local target_root="${1}"; shift
128
129    for atffile in $(cd "${source_root}" && find . -name Atffile); do
130        local subdir="$(echo "${atffile}" | sed 's,Atffile$,,;s,^\./,,')"
131        convert_atffile "${source_root}/${subdir}Atffile" \
132            "${target_root}/${subdir}Kyuafile"
133    done
134}
135
136
137# Prints program usage to stdout.
138#
139# \param progname The name of the program to use for the syntax help.
140usage() {
141    local progname="${1}"; shift
142    echo "Usage: ${progname} [-s source_root] [-t target_root]"
143}
144
145
146# Entry point for the program.
147#
148# \param ... The user-provided arguments.
149main() {
150    local source_root=
151    local target_root=
152
153    while getopts ':s:t:' arg "${@}"; do
154        case "${arg}" in
155        s)
156            source_root="${OPTARG}"
157            ;;
158        t)
159            target_root="${OPTARG}"
160            ;;
161        \?)
162            lib_usage_error "Unknown option -${OPTARG}"
163            ;;
164        esac
165    done
166    shift $((${OPTIND} - 1))
167
168    [ -n "${source_root}" ] || source_root=.
169    [ -n "${target_root}" ] || target_root="${source_root}"
170
171    [ ${#} -eq 0 ] || lib_usage_error "No arguments allowed"
172
173    [ -f "${source_root}/Atffile" ] || \
174        lib_error "${source_root} is not a test suite; missing Atffile"
175
176    remove_kyuafiles "${target_root}"
177    add_kyuafiles "${source_root}" "${target_root}"
178}
179
180
181main "${@}"
182