1#! __SH__
2# Copyright 2012 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 atf-report.sh
31# Kyua-based compatibility replacement for atf-report.
32
33
34. "${KYUA_ATF_COMPAT_PKGDATADIR:-__PKGDATADIR__}/lib.subr"
35
36
37# Gets the action identifier from the output of 'kyua test'.
38#
39# \param file The file that contains the output of 'kyua test'.  Can be
40#     /dev/stdout.
41#
42# \post Prints the action identifier.
43get_action() {
44    local file="${1}"; shift
45    grep '^Committed action ' "${file}" | cut -d ' ' -f 3
46}
47
48
49# Generates an HTML report.
50#
51# The original atf-report generates HTML reports that are made up of solely a
52# single HTML page.  Because of this, such reports can be written directly to
53# the file specified by the user.
54#
55# Because Kyua generates "rich" HTML reports (i.e. reports that consist of more
56# than one HTML page), we cannot perfectly emulate atf-report.  Instead, we
57# create an auxiliary directory to hold all the files, and then place a link to
58# such files in the file specified by the user.  The drawback is that HTML
59# reports sent to stdout are no longer possible.
60#
61# \param output_file The name of the file to which to write the HTML report.
62#     This file will end up being a symlink to the real report.
63report_html() {
64    local output_file="${1}"; shift
65
66    [ "${output_file}" != "/dev/stdout" ] || \
67        lib_usage_error "Cannot write HTML reports to stdout"
68
69    local dir="$(dirname "${output_file}")"
70    local index_name="${output_file##*/}"
71    local files_name="$(echo "${index_name}" | sed -e 's,\.[a-zA-Z]*$,,').files"
72
73    kyua report-html --action="$(get_action /dev/stdin)" \
74        --output="${dir}/${files_name}"
75
76    echo "Pointing ${index_name} to ${files_name}/index.html"
77    ( cd "${dir}" && ln -s "${files_name}/index.html" "${index_name}" )
78}
79
80
81# Genereates an XML report.
82#
83# For our compatibility purposes, we assume that the XML report is just an HTML
84# report.
85#
86# \param output_file The name of the file to which to write the HTML report.
87#     This file will end up being a symlink to the real report.
88report_xml() {
89    local output_file="${1}"; shift
90
91    lib_warning "XML output not supported; generating HTML instead"
92    report_html "${output_file}"
93}
94
95
96# Generates progressive textual reports.
97#
98# This wrapper attempts to emulate atf-report's ticker output by reading the
99# output of 'kyua test' progressively and sending it to the screen as soon as it
100# becomes available.  The tail of the 'kyua test' report that includes summaries
101# for the run is suppressed and is replaced with the more-detailed output of
102# 'kyua report'.
103#
104# \param output_file The name of the file to which to write the textual report.
105#     Can be /dev/stdout.
106report_ticker() {
107    local output_file="${1}"; shift
108
109    local print=yes
110    while read line; do
111        [ -n "${line}" ] || print=no
112
113        if [ "${print}" = yes ]; then
114            case "${line}" in
115            Committed*)
116                echo "${line}" >>"${Lib_TempDir}/output"
117                ;;
118            *)
119                echo "${line}"
120                ;;
121            esac
122        else
123            echo "${line}" >>"${Lib_TempDir}/output"
124        fi
125    done
126
127    kyua report --action="$(get_action "${Lib_TempDir}/output")" \
128        --output="${output_file}"
129}
130
131
132# Generates a report based on an output specification.
133#
134# \param output_spec The format:file specification of the output.
135report() {
136    local output_spec="${1}"; shift
137
138    local output_format="$(echo "${output_spec}" | cut -d : -f 1)"
139    local output_file="$(echo "${output_spec}" | cut -d : -f 2)"
140    [ "${output_file}" != - ] || output_file=/dev/stdout
141
142    case "${output_format}" in
143    html|ticker|xml)
144        "report_${output_format}" "${output_file}"
145        ;;
146
147    *)
148        lib_usage_error "Unknown output format '${output_format}'"
149        ;;
150    esac
151}
152
153
154# Prints program usage to stdout.
155#
156# \param progname The name of the program to use for the syntax help.
157usage() {
158    local progname="${1}"; shift
159    echo "Usage: ${progname} [-o output-spec]"
160}
161
162
163# Entry point for the program.
164#
165# \param ... The user-provided arguments.
166main()
167{
168    local output_spec="ticker:-"
169    while getopts ':o:' arg "${@}"; do
170        case "${arg}" in
171        o)
172            output_spec="${OPTARG}"
173            ;;
174        \?)
175            lib_usage_error "Unknown option -${OPTARG}"
176            ;;
177        esac
178    done
179    shift $((${OPTIND} - 1))
180
181    [ ${#} -eq 0 ] || lib_usage_error "No arguments allowed"
182
183    lib_init_tempdir
184
185    report "${output_spec}"
186
187    lib_cleanup
188}
189
190
191main "${@}"
192