xref: /minix3/external/bsd/kyua-cli/dist/integration/utils.sh (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1# Copyright 2011 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
30# Subcommand to strip the timestamps of a report.
31#
32# This is to make the reports deterministic and thus easily testable.  The
33# timestamps are replaced by the fixed string S.UUUs.
34#
35# This variable should be used as shown here:
36#
37#     atf_check ... -x kyua report "| ${uilts_strip_timestamp}"
38utils_strip_timestamp='sed -e "s,[0-9][0-9]*.[0-9][0-9][0-9]s,S.UUUs,g"'
39
40
41# Copies a helper binary from the source directory to the work directory.
42#
43# \param name The name of the binary to copy.
44# \param destination The target location for the binary; can be either
45#     a directory name or a file name.
46utils_cp_helper() {
47    local name="${1}"; shift
48    local destination="${1}"; shift
49
50    ln -s "$(atf_get_srcdir)"/helpers/"${name}" "${destination}"
51}
52
53
54# Creates a 'kyua' binary in the path that strips timestamps off the output.
55#
56# Call this on test cases that wish to replace timestamps in the *stdout* of
57# Kyua with the S.UUUs deterministic string.  This is usable for tests that
58# validate the 'test' subcommand, but also by a few specific tests for the
59# 'report' subcommand.
60utils_install_timestamp_wrapper() {
61    [ ! -x kyua ] || return
62    cat >kyua <<EOF
63#! /bin/sh
64
65PATH=${PATH}
66
67kyua "\${@}" >kyua.tmpout
68result=\${?}
69cat kyua.tmpout | ${utils_strip_timestamp}
70exit \${result}
71EOF
72    chmod +x kyua
73    PATH="$(pwd):${PATH}"
74}
75
76
77# Defines a test case with a default head.
78utils_test_case() {
79    local name="${1}"; shift
80
81    atf_test_case "${name}"
82    eval "${name}_head() {
83        atf_set require.progs kyua
84    }"
85}
86