1 # Copyright 2011 The Kyua Authors.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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.
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.
30 # Subcommand to strip out the durations and timestamps in a report.
32 # This is to make the reports deterministic and thus easily testable. The
33 # time deltas are replaced by the fixed string S.UUU and the timestamps are
34 # replaced by the fixed strings YYYYMMDD.HHMMSS.ssssss and
35 # YYYY-MM-DDTHH:MM:SS.ssssssZ depending on their original format.
37 # This variable should be used as shown here:
39 # atf_check ... -x kyua report "| ${utils_strip_times}"
41 # Use the utils_install_times_wrapper function to create a 'kyua' wrapper
42 # script that automatically does this.
44 utils_strip_times='sed -E \
45 -e "s,( |\[|\")[0-9][0-9]*.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
46 -e "s,[0-9]{8}-[0-9]{6}-[0-9]{6},YYYYMMDD-HHMMSS-ssssss,g" \
47 -e "s,[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}Z,YYYY-MM-DDTHH:MM:SS.ssssssZ,g"'
51 # Same as utils_strip_times but avoids stripping timestamp-based report IDs.
53 # This is to make the reports deterministic and thus easily testable. The
54 # time deltas are replaced by the fixed string S.UUU and the timestamps are
55 # replaced by the fixed string YYYY-MM-DDTHH:MM:SS.ssssssZ.
57 utils_strip_times_but_not_ids='sed -E \
58 -e "s,( |\[|\")[0-9][0-9]*.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
59 -e "s,[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}Z,YYYY-MM-DDTHH:MM:SS.ssssssZ,g"'
63 # Computes the results id for a test suite run.
65 # The computed path is "generic" in the sense that it does not include a
66 # real timestamp: it only includes a placeholder. This function should be
67 # used along the utils_strip_times function so that the timestamps of
68 # the real results files are stripped out.
70 # \param path Optional path to use; if not given, use the cwd.
72 local test_suite_id="$(utils_test_suite_id "${@}")"
73 echo "${test_suite_id}.YYYYMMDD-HHMMSS-ssssss"
77 # Computes the results file for a test suite run.
79 # The computed path is "generic" in the sense that it does not include a
80 # real timestamp: it only includes a placeholder. This function should be
81 # used along the utils_strip_times function so that the timestampts of the
82 # real results files are stripped out.
84 # \param path Optional path to use; if not given, use the cwd.
85 utils_results_file() {
86 echo "${HOME}/.kyua/store/results.$(utils_results_id "${@}").db"
90 # Copies a helper binary from the source directory to the work directory.
92 # \param name The name of the binary to copy.
93 # \param destination The target location for the binary; can be either
94 # a directory name or a file name.
96 local name="${1}"; shift
97 local destination="${1}"; shift
99 ln -s "$(atf_get_srcdir)"/helpers/"${name}" "${destination}"
103 # Creates a 'kyua' binary in the path that strips timing data off the output.
105 # Call this on test cases that wish to replace timing data in the *stdout* of
106 # Kyua with the deterministic strings. This is to be used by tests that
107 # validate the 'test' and 'report' subcommands.
108 utils_install_times_wrapper() {
109 [ ! -x kyua ] || return
115 kyua "\${@}" >kyua.tmpout
117 cat kyua.tmpout | ${utils_strip_times}
121 PATH="$(pwd):${PATH}"
125 # Creates a 'kyua' binary in the path that makes the output of 'test' stable.
127 # Call this on test cases that wish to replace timing data with deterministic
128 # strings and that need the result lines in the output to be sorted
129 # lexicographically. The latter hides the indeterminism caused by parallel
130 # execution so that the output can be verified. For these reasons, this is to
131 # be used exclusively by tests that validate the 'test' subcommand.
132 utils_install_stable_test_wrapper() {
133 [ ! -x kyua ] || return
139 kyua "\${@}" >kyua.tmpout
141 cat kyua.tmpout | ${utils_strip_times} >kyua.tmpout2
143 # Sort the test result lines but keep the rest intact.
144 grep '[^ ]*:[^ ]*' kyua.tmpout2 | sort >kyua.tmpout3
145 grep -v '[^ ]*:[^ ]*' kyua.tmpout2 >kyua.tmpout4
146 cat kyua.tmpout3 kyua.tmpout4
151 PATH="$(pwd):${PATH}"
155 # Defines a test case with a default head.
157 local name="${1}"; shift
159 atf_test_case "${name}"
160 eval "${name}_head() {
161 atf_set require.progs kyua
166 # Computes the test suite identifier for results files files.
168 # \param path Optional path to use; if not given, use the cwd.
169 utils_test_suite_id() {
171 if [ ${#} -gt 0 ]; then
172 path="$(cd ${1} && pwd)"; shift
176 echo "${path}" | sed -e 's,^/,,' -e 's,/,_,g'