dnl
dnl Automated Testing Framework (atf)
dnl
dnl Copyright (c) 2007 The NetBSD Foundation, Inc.
dnl All rights reserved.
dnl
dnl Redistribution and use in source and binary forms, with or without
dnl modification, are permitted provided that the following conditions
dnl are met:
dnl 1. Redistributions of source code must retain the above copyright
dnl    notice, this list of conditions and the following disclaimer.
dnl 2. Redistributions in binary form must reproduce the above copyright
dnl    notice, this list of conditions and the following disclaimer in the
dnl    documentation and/or other materials provided with the distribution.
dnl
dnl THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
dnl CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
dnl INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dnl MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
dnl IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
dnl DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dnl DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
dnl GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dnl INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
dnl IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
dnl OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
dnl IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dnl

AT_SETUP([atf-config: querying of build-time variables])
AT_TESTED(atf-config)

all_vars="atf_arch \
          atf_build_cc \
          atf_build_cflags \
          atf_build_cpp \
          atf_build_cppflags \
          atf_build_cxx \
          atf_build_cxxflags \
          atf_confdir \
          atf_includedir \
          atf_libdir \
          atf_libexecdir \
          atf_machine \
          atf_pkgdatadir \
          atf_shell \
          atf_workdir"
all_vars_no=15

dnl List all variables.
AT_CHECK([atf-config], [0], [stdout], [])
AT_CHECK([COUNT_LINES(stdout, ${all_vars_no})], [0], [], [])
for v in ${all_vars}; do
    AT_CHECK([grep "${v}" stdout], [0], [ignore], [])
done

dnl Query a single variable and test terse mode.
for v in ${all_vars}; do
    AT_CHECK([atf-config ${v}], [0], [stdout], [])
    AT_CHECK([COUNT_LINES(stdout, 1)], [0], [], [])
    AT_CHECK([grep "${v}" stdout], [0], [ignore], [])
    AT_CHECK([cut -d ' ' -f 3- stdout], [0], [stdout], [])
    AT_CHECK([mv stdout expout], [0], [], [])
    AT_CHECK([atf-config -t ${v}], [0], [expout], [])
done

dnl Query several variables.
AT_CHECK([atf-config atf_libexecdir atf_pkgdatadir], [0], [stdout], [])
AT_CHECK([grep 'atf_libexecdir' stdout], [0], [ignore], [])
AT_CHECK([grep 'atf_pkgdatadir' stdout], [0], [ignore], [])
AT_CHECK([COUNT_LINES(stdout, 2)], [0], [ignore], [])

dnl Query a non-existent variable.
AT_CHECK([atf-config non_existent], [1], [], [stderr])
AT_CHECK([grep 'Unknown variable.*non_existent' stderr], [0], [ignore], [])

dnl Query an existent and non-existent variable.
for v in ${all_vars}; do
    AT_CHECK([atf-config ${v} non_existent], [1], [], [stderr])
    AT_CHECK([grep 'Unknown variable.*non_existent' stderr],
             [0], [ignore], [])
    AT_CHECK([atf-config non_existent ${v}], [1], [], [stderr])
    AT_CHECK([grep 'Unknown variable.*non_existent' stderr],
             [0], [ignore], [])
done

dnl Override every variable through the environment.
for v in ${all_vars}; do
    V=$(echo ${v} | tr '@<:@a-z@:>@' '@<:@A-Z@:>@')
    AT_CHECK([env ${V}=testval atf-config], [0], [stdout], [])
    AT_CHECK([mv stdout all], [0], [], [])

    AT_CHECK([grep "^${v} : " all], [0], [stdout], [])
    AT_CHECK([mv stdout affected], [0], [], [])
    AT_CHECK([grep -v "^${v} : " all], [0], [stdout], [])
    AT_CHECK([mv stdout unaffected], [0], [], [])

    AT_CHECK([COUNT_LINES(affected, 1)], [0], [ignore], [])
    AT_CHECK([COUNT_LINES(unaffected, $((${all_vars_no} - 1)))],
             [0], [ignore], [])

    AT_CHECK([grep "^${v} : testval$" affected], [0], [ignore], [])
    AT_CHECK([grep ' : testval$' unaffected], [1], [], [])
done

AT_CLEANUP()

dnl vim: syntax=m4:expandtab:shiftwidth=4:softtabstop=4
