#! /usr/pkg/bin/atf-sh
# AUTOMATICALLY GENERATED FROM Makefile
# Copyright 2011 The Kyua Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Subcommand to strip out the durations and timestamps in a report.
#
# This is to make the reports deterministic and thus easily testable.  The
# time deltas are replaced by the fixed string S.UUU and the timestamps are
# replaced by the fixed strings YYYYMMDD.HHMMSS.ssssss and
# YYYY-MM-DDTHH:MM:SS.ssssssZ depending on their original format.
#
# This variable should be used as shown here:
#
#     atf_check ... -x kyua report "| ${utils_strip_times}"
#
# Use the utils_install_times_wrapper function to create a 'kyua' wrapper
# script that automatically does this.
# CHECK_STYLE_DISABLE
utils_strip_times='sed -E \
    -e "s,( |\[|\")[0-9][0-9]*\.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
    -e "s,[0-9]{8}-[0-9]{6}-[0-9]{6},YYYYMMDD-HHMMSS-ssssss,g" \
    -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"'
# CHECK_STYLE_ENABLE


# Same as utils_strip_times but avoids stripping timestamp-based report IDs.
#
# This is to make the reports deterministic and thus easily testable.  The
# time deltas are replaced by the fixed string S.UUU and the timestamps are
# replaced by the fixed string YYYY-MM-DDTHH:MM:SS.ssssssZ.
# CHECK_STYLE_DISABLE
utils_strip_times_but_not_ids='sed -E \
    -e "s,( |\[|\")[0-9][0-9]*\.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
    -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"'
# CHECK_STYLE_ENABLE


# Computes the results id for a test suite run.
#
# The computed path is "generic" in the sense that it does not include a
# real timestamp: it only includes a placeholder.  This function should be
# used along the utils_strip_times function so that the timestamps of
# the real results files are stripped out.
#
# \param path Optional path to use; if not given, use the cwd.
utils_results_id() {
    local test_suite_id="$(utils_test_suite_id "${@}")"
    echo "${test_suite_id}.YYYYMMDD-HHMMSS-ssssss"
}


# Computes the results file for a test suite run.
#
# The computed path is "generic" in the sense that it does not include a
# real timestamp: it only includes a placeholder.  This function should be
# used along the utils_strip_times function so that the timestampts of the
# real results files are stripped out.
#
# \param path Optional path to use; if not given, use the cwd.
utils_results_file() {
    echo "${HOME}/.kyua/store/results.$(utils_results_id "${@}").db"
}


# Copies a helper binary from the source directory to the work directory.
#
# \param name The name of the binary to copy.
# \param destination The target location for the binary; can be either
#     a directory name or a file name.
utils_cp_helper() {
    local name="${1}"; shift
    local destination="${1}"; shift

    ln -s "$(atf_get_srcdir)"/helpers/"${name}" "${destination}"
}


# Creates a 'kyua' binary in the path that strips timing data off the output.
#
# Call this on test cases that wish to replace timing data in the *stdout* of
# Kyua with the deterministic strings.  This is to be used by tests that
# validate the 'test' and 'report' subcommands.
utils_install_times_wrapper() {
    local DUMMY_KYUA_BINDIR=$(pwd)
    local DUMMY_KYUA_BIN="$DUMMY_KYUA_BINDIR/kyua"

    [ ! -x "$DUMMY_KYUA_BIN" ] || return

    local ORIG_KYUA_BIN=$(which kyua)
    cat >"$DUMMY_KYUA_BIN" <<EOF
#! /bin/sh

$ORIG_KYUA_BIN "\${@}" >kyua.tmpout
result=\${?}
cat kyua.tmpout | ${utils_strip_times}
exit \${result}
EOF
    chmod +x "$DUMMY_KYUA_BIN"
    export PATH="$DUMMY_KYUA_BINDIR:${PATH}"
}


# Creates a 'kyua' binary in the path that makes the output of 'test' stable.
#
# Call this on test cases that wish to replace timing data with deterministic
# strings and that need the result lines in the output to be sorted
# lexicographically.  The latter hides the indeterminism caused by parallel
# execution so that the output can be verified.  For these reasons, this is to
# be used exclusively by tests that validate the 'test' subcommand.
utils_install_stable_test_wrapper() {
    local DUMMY_KYUA_BINDIR=$(pwd)
    local DUMMY_KYUA_BIN="$DUMMY_KYUA_BINDIR/kyua"

    [ ! -x "$DUMMY_KYUA_BIN" ] || return

    local ORIG_KYUA_BIN=$(which kyua)
    cat >"$DUMMY_KYUA_BIN" <<EOF
#! /bin/sh

$ORIG_KYUA_BIN "\${@}" >kyua.tmpout
result=\${?}
cat kyua.tmpout | ${utils_strip_times} >kyua.tmpout2

# Sort the test result lines but keep the rest intact.
grep '[^ ]*:[^ ]*' kyua.tmpout2 | sort >kyua.tmpout3
grep -v '[^ ]*:[^ ]*' kyua.tmpout2 >kyua.tmpout4
cat kyua.tmpout3 kyua.tmpout4

exit \${result}
EOF
    chmod +x "$DUMMY_KYUA_BIN"
    export PATH="$DUMMY_KYUA_BINDIR:${PATH}"
}


# Defines a test case with a default head.
utils_test_case() {
    local name="${1}"; shift

    atf_test_case "${name}"
    eval "${name}_head() {
        atf_set require.progs kyua
    }"
}


# Computes the test suite identifier for results files files.
#
# \param path Optional path to use; if not given, use the cwd.
utils_test_suite_id() {
    local path=
    if [ ${#} -gt 0 ]; then
        path="$(cd ${1} && pwd)"; shift
    else
        path="$(pwd)"
    fi
    echo "${path}" | sed -e 's,^/,,' -e 's,/,_,g'
}
# Copyright 2011 The Kyua Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


utils_test_case no_args
no_args_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="simple_all_pass"}
EOF
    utils_cp_helper simple_all_pass .

    cat >experr <<EOF
Usage error for command debug: Not enough arguments.
Type 'kyua help debug' for usage information.
EOF
    atf_check -s exit:3 -o empty -e file:experr kyua debug
}


utils_test_case many_args
many_args_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second

    cat >experr <<EOF
Usage error for command debug: Too many arguments.
Type 'kyua help debug' for usage information.
EOF
    atf_check -s exit:3 -o empty -e file:experr kyua debug first:pass \
        second:pass
}


utils_test_case one_arg__ok_pass
one_arg__ok_pass_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper expect_all_pass first
    utils_cp_helper simple_all_pass second

    cat >expout <<EOF
This is the stdout of pass
second:pass  ->  passed
EOF
cat >experr <<EOF
This is the stderr of pass
EOF
    atf_check -s exit:0 -o file:expout -e file:experr kyua debug second:pass
}


utils_test_case one_arg__ok_fail
one_arg__ok_fail_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
EOF
    utils_cp_helper simple_some_fail first

    cat >expout <<EOF
This is the stdout of fail
first:fail  ->  failed: This fails on purpose
EOF
    cat >experr <<EOF
This is the stderr of fail
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua debug first:fail
}


utils_test_case one_arg__no_match
one_arg__no_match_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper expect_all_pass first
    utils_cp_helper simple_all_pass second

    cat >experr <<EOF
kyua: E: Unknown test case 'second:die'.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua debug second:die
}


utils_test_case one_arg__no_test_case
one_arg__no_test_case_body() {
    # CHECK_STYLE_DISABLE
    cat >experr <<EOF
Usage error for command debug: 'foo' is not a test case identifier (missing ':'?).
Type 'kyua help debug' for usage information.
EOF
    # CHECK_STYLE_ENABLE
    atf_check -s exit:3 -o empty -e file:experr kyua debug foo
}


utils_test_case one_arg__bad_filter
one_arg__bad_filter_body() {
    cat >experr <<EOF
kyua: E: Test case component in 'foo:' is empty.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua debug foo:
}


utils_test_case body_and_cleanup
body_and_cleanup_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="single"}
EOF
    utils_cp_helper metadata single

    cat >expout <<EOF
single:with_cleanup  ->  passed
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua debug \
        --stdout=saved.out --stderr=saved.err single:with_cleanup

    cat >expout <<EOF
Body message to stdout
Cleanup message to stdout
EOF
    atf_check -s exit:0 -o file:expout -e empty cat saved.out

    cat >experr <<EOF
Body message to stderr
Cleanup message to stderr
EOF
    atf_check -s exit:0 -o file:experr -e empty cat saved.err
}


utils_test_case stdout_stderr_flags
stdout_stderr_flags_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper expect_all_pass first
    utils_cp_helper simple_all_pass second

    cat >expout <<EOF
second:pass  ->  passed
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua debug \
        --stdout=saved.out --stderr=saved.err second:pass

    cat >expout <<EOF
This is the stdout of pass
EOF
    cmp -s saved.out expout || atf_fail "--stdout did not redirect the" \
        "standard output to the desired file"

    cat >experr <<EOF
This is the stderr of pass
EOF
    cmp -s saved.err experr || atf_fail "--stderr did not redirect the" \
        "standard error to the desired file"
}


utils_test_case args_are_relative
args_are_relative_body() {
    mkdir root
    cat >root/Kyuafile <<EOF
syntax(2)
test_suite("integration")
include("subdir/Kyuafile")
atf_test_program{name="prog"}
EOF
    utils_cp_helper simple_all_pass root/prog

    mkdir root/subdir
    cat >root/subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="prog"}
EOF
    utils_cp_helper simple_some_fail root/subdir/prog

    cat >expout <<EOF
This is the stdout of fail
subdir/prog:fail  ->  failed: This fails on purpose
EOF
    cat >experr <<EOF
This is the stderr of fail
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua debug \
        -k "$(pwd)/root/Kyuafile" subdir/prog:fail
}


utils_test_case only_load_used_test_programs
only_load_used_test_programs_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper bad_test_program second

    cat >expout <<EOF
This is the stdout of pass
first:pass  ->  passed
EOF
    cat >experr <<EOF
This is the stderr of pass
EOF
    CREATE_COOKIE="$(pwd)/cookie"; export CREATE_COOKIE
    atf_check -s exit:0 -o file:expout -e file:experr kyua debug first:pass
    if [ -f "${CREATE_COOKIE}" ]; then
        atf_fail "An unmatched test case has been executed, which harms" \
            "performance"
    fi
}


utils_test_case config_behavior
config_behavior_body() {
    cat >"my-config" <<EOF
syntax(2)
test_suites.suite1["the-variable"] = "value1"
test_suites.suite2["the-variable"] = "override me"
EOF

    cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="config1", test_suite="suite1"}
atf_test_program{name="config2", test_suite="suite2"}
atf_test_program{name="config3", test_suite="suite3"}
EOF
    utils_cp_helper config config1
    utils_cp_helper config config2
    utils_cp_helper config config3

    atf_check -s exit:1 -o match:'failed' -e empty \
        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
        debug config1:get_variable
    atf_check -s exit:0 -o match:'passed' -e empty \
        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
        debug config2:get_variable
    atf_check -s exit:0 -o match:'skipped' -e empty \
        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
        debug config3:get_variable
}


utils_test_case build_root_flag
build_root_flag_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    mkdir build
    utils_cp_helper expect_all_pass build/first
    utils_cp_helper simple_all_pass build/second

    cat >expout <<EOF
This is the stdout of pass
second:pass  ->  passed
EOF
cat >experr <<EOF
This is the stderr of pass
EOF
    atf_check -s exit:0 -o file:expout -e file:experr \
        kyua debug --build-root=build second:pass
}


utils_test_case kyuafile_flag__ok
kyuafile_flag__ok_body() {
    cat >Kyuafile <<EOF
This file is bogus but it is not loaded.
EOF

    cat >myfile <<EOF
syntax(2)
test_suite("hello-world")
atf_test_program{name="sometest"}
EOF
    utils_cp_helper simple_all_pass sometest

    atf_check -s exit:0 -o match:passed -e empty kyua test -k myfile sometest
    atf_check -s exit:0 -o match:passed -e empty kyua test --kyuafile=myfile \
        sometest
}


utils_test_case missing_kyuafile
missing_kyuafile_body() {
    cat >experr <<EOF
kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua debug foo:bar
}


utils_test_case bogus_kyuafile
bogus_kyuafile_body() {
    cat >Kyuafile <<EOF
Hello, world.
EOF
    atf_check -s exit:2 -o empty \
        -e match:"Load of 'Kyuafile' failed: .* Kyuafile:2:" kyua list
}


utils_test_case bogus_test_program
bogus_test_program_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="crash_on_list"}
atf_test_program{name="non_executable"}
EOF
    utils_cp_helper bad_test_program crash_on_list
    echo 'I am not executable' >non_executable

    cat >experr <<EOF
kyua: E: Unknown test case 'crash_on_list:a'.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua debug crash_on_list:a

    cat >experr <<EOF
kyua: E: Unknown test case 'non_executable:a'.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua debug non_executable:a

    # CHECK_STYLE_DISABLE
    cat >expout <<EOF
crash_on_list:__test_cases_list__  ->  broken: Invalid header for test case list; expecting Content-Type for application/X-atf-tp version 1, got ''
EOF
    # CHECK_STYLE_ENABLE
    atf_check -s exit:1 -o file:expout -e empty kyua debug \
        crash_on_list:__test_cases_list__

    # CHECK_STYLE_DISABLE
    cat >expout <<EOF
non_executable:__test_cases_list__  ->  broken: Permission denied to run test program
EOF
    # CHECK_STYLE_ENABLE
    atf_check -s exit:1 -o file:expout -e empty kyua debug \
        non_executable:__test_cases_list__
}


atf_init_test_cases() {
    atf_add_test_case no_args
    atf_add_test_case many_args
    atf_add_test_case one_arg__ok_pass
    atf_add_test_case one_arg__ok_fail
    atf_add_test_case one_arg__no_match
    atf_add_test_case one_arg__no_test_case
    atf_add_test_case one_arg__bad_filter

    atf_add_test_case body_and_cleanup

    atf_add_test_case stdout_stderr_flags

    atf_add_test_case args_are_relative

    atf_add_test_case only_load_used_test_programs

    atf_add_test_case config_behavior

    atf_add_test_case build_root_flag
    atf_add_test_case kyuafile_flag__ok
    atf_add_test_case missing_kyuafile
    atf_add_test_case bogus_kyuafile
    atf_add_test_case bogus_test_program
}
