Path: blob/main/contrib/atf/atf-sh/atf_check_test.sh
39482 views
# Copyright (c) 2007 The NetBSD Foundation, Inc.1# All rights reserved.2#3# Redistribution and use in source and binary forms, with or without4# modification, are permitted provided that the following conditions5# are met:6# 1. Redistributions of source code must retain the above copyright7# notice, this list of conditions and the following disclaimer.8# 2. Redistributions in binary form must reproduce the above copyright9# notice, this list of conditions and the following disclaimer in the10# documentation and/or other materials provided with the distribution.11#12# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425# TODO: Bring in the checks in the bootstrap testsuite for atf_check.2627atf_test_case info_ok28info_ok_head()29{30atf_set "descr" "Verifies that atf_check prints an informative" \31"message even when the command is successful"32}33info_ok_body()34{35h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"3637atf_check -s eq:0 -o save:stdout -e save:stderr -x \38"${h} atf_check_info_ok"39grep 'Executing command.*true' stdout >/dev/null || \40atf_fail "atf_check does not print an informative message"4142atf_check -s eq:0 -o save:stdout -e save:stderr -x \43"${h} atf_check_info_fail"44grep 'Executing command.*false' stdout >/dev/null || \45atf_fail "atf_check does not print an informative message"46}4748atf_test_case expout_mismatch49expout_mismatch_head()50{51atf_set "descr" "Verifies that atf_check prints a diff of the" \52"stdout and the expected stdout if the two do not" \53"match"54}55expout_mismatch_body()56{57h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"5859atf_check -s eq:1 -o save:stdout -e save:stderr -x \60"${h} atf_check_expout_mismatch"61grep 'Executing command.*echo bar' stdout >/dev/null || \62atf_fail "atf_check does not print an informative message"63grep 'stdout does not match golden output' stderr >/dev/null || \64atf_fail "atf_check does not print the stdout header"65grep 'stderr' stderr >/dev/null && \66atf_fail "atf_check prints the stderr header"67grep '^-foo' stderr >/dev/null || \68atf_fail "atf_check does not print the stdout's diff"69grep '^+bar' stderr >/dev/null || \70atf_fail "atf_check does not print the stdout's diff"71}7273atf_test_case experr_mismatch74experr_mismatch_head()75{76atf_set "descr" "Verifies that atf_check prints a diff of the" \77"stderr and the expected stderr if the two do not" \78"match"79}80experr_mismatch_body()81{82h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"8384atf_check -s eq:1 -o save:stdout -e save:stderr -x \85"${h} atf_check_experr_mismatch"86grep 'Executing command.*echo bar' stdout >/dev/null || \87atf_fail "atf_check does not print an informative message"88grep 'stdout' stderr >/dev/null && \89atf_fail "atf_check prints the stdout header"90grep 'stderr does not match golden output' stderr >/dev/null || \91atf_fail "atf_check does not print the stderr header"92grep '^-foo' stderr >/dev/null || \93atf_fail "atf_check does not print the stderr's diff"94grep '^+bar' stderr >/dev/null || \95atf_fail "atf_check does not print the stderr's diff"96}9798atf_test_case null_stdout99null_stdout_head()100{101atf_set "descr" "Verifies that atf_check prints a the stdout it got" \102"when it was supposed to be null"103}104null_stdout_body()105{106h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"107108atf_check -s eq:1 -o save:stdout -e save:stderr -x \109"${h} atf_check_null_stdout"110grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \111atf_fail "atf_check does not print an informative message"112grep 'stdout not empty' stderr >/dev/null || \113atf_fail "atf_check does not print the stdout header"114grep 'stderr' stderr >/dev/null && \115atf_fail "atf_check prints the stderr header"116grep 'These are the contents' stderr >/dev/null || \117atf_fail "atf_check does not print stdout's contents"118}119120atf_test_case null_stderr121null_stderr_head()122{123atf_set "descr" "Verifies that atf_check prints a the stderr it got" \124"when it was supposed to be null"125}126null_stderr_body()127{128h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"129130atf_check -s eq:1 -o save:stdout -e save:stderr -x \131"${h} atf_check_null_stderr"132grep 'Executing command.*echo.*These.*contents' stdout >/dev/null || \133atf_fail "atf_check does not print an informative message"134grep 'stdout' stderr >/dev/null && \135atf_fail "atf_check prints the stdout header"136grep 'stderr not empty' stderr >/dev/null || \137atf_fail "atf_check does not print the stderr header"138grep 'These are the contents' stderr >/dev/null || \139atf_fail "atf_check does not print stderr's contents"140}141142atf_test_case equal143equal_head()144{145atf_set "descr" "Verifies that atf_check_equal works"146}147equal_body()148{149h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"150151atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_ok"152153atf_check -s eq:1 -o ignore -e ignore -x \154"${h} -r resfile atf_check_equal_fail"155atf_check -s eq:0 -o ignore -e empty grep '^failed: a != b (a != b)$' \156resfile157158atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_equal_eval_ok"159160atf_check -s eq:1 -o ignore -e ignore -x \161"${h} -r resfile atf_check_equal_eval_fail"162atf_check -s eq:0 -o ignore -e empty \163grep '^failed: \${x} != \${y} (a != b)$' resfile164}165166atf_test_case not_equal167not_equal_head()168{169atf_set "descr" "Verifies that atf_check_not_equal works"170}171not_equal_body()172{173h="$(atf_get_srcdir)/misc_helpers -s $(atf_get_srcdir)"174175atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_ok"176177atf_check -s eq:1 -o ignore -e ignore -x \178"${h} -r resfile atf_check_not_equal_fail"179atf_check -s eq:0 -o ignore -e empty grep '^failed: a == b (a == b)$' \180resfile181182atf_check -s eq:0 -o ignore -e ignore -x "${h} atf_check_not_equal_eval_ok"183184atf_check -s eq:1 -o ignore -e ignore -x \185"${h} -r resfile atf_check_not_equal_eval_fail"186atf_check -s eq:0 -o ignore -e empty \187grep '^failed: \${x} == \${y} (a == b)$' resfile188}189190atf_test_case flush_stdout_on_death191flush_stdout_on_death_body()192{193CONTROL_FILE="$(pwd)/done" "$(atf_get_srcdir)/misc_helpers" \194-s "$(atf_get_srcdir)" atf_check_flush_stdout >out 2>err &195pid="${!}"196while [ ! -f ./done ]; do197echo "Still waiting for helper to create control file"198ls199sleep 1200done201kill -9 "${pid}"202203grep 'Executing command.*true' out \204|| atf_fail 'First command not in output'205grep 'Executing command.*false' out \206|| atf_fail 'Second command not in output'207}208209atf_init_test_cases()210{211atf_add_test_case info_ok212atf_add_test_case expout_mismatch213atf_add_test_case experr_mismatch214atf_add_test_case null_stdout215atf_add_test_case null_stderr216atf_add_test_case equal217atf_add_test_case flush_stdout_on_death218}219220# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4221222223