xref: /netbsd-src/external/bsd/kyua-atf-compat/dist/tests_lib.subr (revision b92cbc7321a4a2d13071f4a9bcf671898920c6b4)
1# Copyright 2012 Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9#   notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright
11#   notice, this list of conditions and the following disclaimer in the
12#   documentation and/or other materials provided with the distribution.
13# * Neither the name of Google Inc. nor the names of its contributors
14#   may be used to endorse or promote products derived from this software
15#   without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# \file tests_lib.subr
30# Common routines for test programs.
31
32
33# Creates an Atffile file.
34#
35# \param destfile The path to the file to be created.
36# \param ... The lines to append to the Atffile.
37create_atffile() {
38    local destfile="${1}"; shift
39
40    cat >"${destfile}" <<EOF
41Content-type: application/X-atf-atffile, version="1"
42
43EOF
44    for line in "${@}"; do
45        echo "${line}" >>"${destfile}"
46    done
47}
48
49
50# Creates an ATF configuration file.
51#
52# \param destfile The path to the file to be created.
53# \param ... The lines to append to the configuration file.
54create_config() {
55    local destfile="${1}"; shift
56
57    cat >"${destfile}" <<EOF
58Content-type: application/X-atf-config, version="1"
59
60# This is a comment that should be ignored.
61  # And this is another one.
62
63EOF
64    for line in "${@}"; do
65        echo "${line}" >>"${destfile}"
66    done
67}
68
69
70# Creates a fake test program.
71#
72# \param destfile The path to the test program to be created.
73# \param ... List of the test cases to include in the fake test program.
74create_test_program() {
75    local destfile="${1}"; shift
76
77    sed -e "s,@CONTROL_DIR@,$(pwd),g" \
78        -e "s,@ENABLED_TESTS@,${*},g" \
79        <"$(atf_get_srcdir)/helpers" >"${destfile}"
80    chmod +x "${destfile}"
81}
82
83
84# Strips timestamps from the output of 'kyua test' or 'kyua report'.
85#
86# The timestamps are all replaced with X.XXXs so that they can later be compared
87# against prerecorded golden output.
88#
89# \param file The file in which to replace the timestamps.  The replacement is
90#     performed in place.  The reason is that the calls to atf-run and
91#     atf-report should be validated on their own WITHOUT piping them through
92#     any manipulation function.
93strip_timestamps() {
94    local file="${1}"; shift
95
96    sed -e 's,[0-9][0-9]*\.[0-9][0-9]*s,X.XXXs,g' <"${file}" >"${file}.new"
97    mv "${file}.new" "${file}"
98}
99