t_builtin.sh revision 1.1.2.2 1 1.1.2.2 pgoyette # $NetBSD: t_builtin.sh,v 1.1.2.2 2018/09/06 06:56:49 pgoyette Exp $
2 1.1.2.2 pgoyette #
3 1.1.2.2 pgoyette # Copyright (c) 2018 The NetBSD Foundation, Inc.
4 1.1.2.2 pgoyette # All rights reserved.
5 1.1.2.2 pgoyette #
6 1.1.2.2 pgoyette # Redistribution and use in source and binary forms, with or without
7 1.1.2.2 pgoyette # modification, are permitted provided that the following conditions
8 1.1.2.2 pgoyette # are met:
9 1.1.2.2 pgoyette # 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 pgoyette # notice, this list of conditions and the following disclaimer.
11 1.1.2.2 pgoyette # 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 pgoyette # notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 pgoyette # documentation and/or other materials provided with the distribution.
14 1.1.2.2 pgoyette #
15 1.1.2.2 pgoyette # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1.2.2 pgoyette # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1.2.2 pgoyette # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1.2.2 pgoyette # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1.2.2 pgoyette # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1.2.2 pgoyette # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1.2.2 pgoyette # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1.2.2 pgoyette # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1.2.2 pgoyette # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1.2.2 pgoyette # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1.2.2 pgoyette # POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 pgoyette #
27 1.1.2.2 pgoyette
28 1.1.2.2 pgoyette # The shell to use for tests of the builtin printf (or at least try)
29 1.1.2.2 pgoyette : ${TEST_SH:=/bin/sh}
30 1.1.2.2 pgoyette
31 1.1.2.2 pgoyette # This tests the builtin printf command in ${TEST_SH}
32 1.1.2.2 pgoyette
33 1.1.2.2 pgoyette # For the actual code/tests see printf.sh
34 1.1.2.2 pgoyette # (shared with tests for the external (filesystem) command printf
35 1.1.2.2 pgoyette
36 1.1.2.2 pgoyette # These tests are designed to be able to be run by ATF, or standalone
37 1.1.2.2 pgoyette #
38 1.1.2.2 pgoyette # That is, either
39 1.1.2.2 pgoyette # atf_run t_builtin | atf-report (or whatever is needed)
40 1.1.2.2 pgoyette # or
41 1.1.2.2 pgoyette # sh t_builtin [sub_test]... (default is to run all sub_tests)
42 1.1.2.2 pgoyette #
43 1.1.2.2 pgoyette # nb: for standalone runs, do not attempt ./t_builtin as the #! line
44 1.1.2.2 pgoyette # added will force ATF which will complain about the test being run
45 1.1.2.2 pgoyette # in the wrong way. Instead use some Bourne shell compatible shell
46 1.1.2.2 pgoyette # (any will do, caveat any bugs it might have, it need not be TEST_SH,
47 1.1.2.2 pgoyette # but it can be) and run the script explicitly.
48 1.1.2.2 pgoyette
49 1.1.2.2 pgoyette do_printf()
50 1.1.2.2 pgoyette {
51 1.1.2.2 pgoyette $Running_under_ATF && atf_require_prog "${TEST_SH%% *}"
52 1.1.2.2 pgoyette
53 1.1.2.2 pgoyette unset LANG LC_ALL LC_NUMERIC LC_CTYPE LC_MESSAGES
54 1.1.2.2 pgoyette
55 1.1.2.2 pgoyette case "$*" in
56 1.1.2.2 pgoyette *\'*)
57 1.1.2.2 pgoyette $Running_under_ATF && atf_require_prog printf
58 1.1.2.2 pgoyette $Running_under_ATF && atf_require_pfog sed
59 1.1.2.2 pgoyette ;;
60 1.1.2.2 pgoyette esac
61 1.1.2.2 pgoyette
62 1.1.2.2 pgoyette COMMAND=printf
63 1.1.2.2 pgoyette for ARG
64 1.1.2.2 pgoyette do
65 1.1.2.2 pgoyette case "${ARG}" in
66 1.1.2.2 pgoyette (*\'*)
67 1.1.2.2 pgoyette # This is kind of odd, we need a working
68 1.1.2.2 pgoyette # printf in order to test printf ...
69 1.1.2.2 pgoyette # nb: do not use echo here, an arg might be "-n'x"
70 1.1.2.2 pgoyette COMMAND="${COMMAND} '$(
71 1.1.2.2 pgoyette printf '%s\n' "${ARG}" |
72 1.1.2.2 pgoyette sed "s/'/'\\\\''/g"
73 1.1.2.2 pgoyette )'"
74 1.1.2.2 pgoyette ;;
75 1.1.2.2 pgoyette (*)
76 1.1.2.2 pgoyette COMMAND="${COMMAND} '${ARG}'"
77 1.1.2.2 pgoyette ;;
78 1.1.2.2 pgoyette esac
79 1.1.2.2 pgoyette done
80 1.1.2.2 pgoyette ${TEST_SH} -c "${COMMAND}"
81 1.1.2.2 pgoyette }
82 1.1.2.2 pgoyette
83 1.1.2.2 pgoyette Not_builtin()
84 1.1.2.2 pgoyette {
85 1.1.2.2 pgoyette if $Running_under_ATF
86 1.1.2.2 pgoyette then
87 1.1.2.2 pgoyette atf_skip "${TEST_SH%% *} does not have printf built in"
88 1.1.2.2 pgoyette else
89 1.1.2.2 pgoyette echo >&2 "No builtin printf in ${TEST_SH}"
90 1.1.2.2 pgoyette exit 1
91 1.1.2.2 pgoyette fi
92 1.1.2.2 pgoyette }
93 1.1.2.2 pgoyette
94 1.1.2.2 pgoyette # See if we have a builtin "printf" command to test
95 1.1.2.2 pgoyette
96 1.1.2.2 pgoyette setup()
97 1.1.2.2 pgoyette {
98 1.1.2.2 pgoyette case "$( ${TEST_SH} -c 'type printf' 2>&1 )" in
99 1.1.2.2 pgoyette
100 1.1.2.2 pgoyette ( *[Bb]uiltin* | *[Bb]uilt[-\ ][Ii]n* )
101 1.1.2.2 pgoyette # nothing here, it all happens below.
102 1.1.2.2 pgoyette ;;
103 1.1.2.2 pgoyette
104 1.1.2.2 pgoyette (*) Tests=
105 1.1.2.2 pgoyette define Not_builtin 'Dummy test to skip when no printf builtin'
106 1.1.2.2 pgoyette return 1
107 1.1.2.2 pgoyette ;;
108 1.1.2.2 pgoyette esac
109 1.1.2.2 pgoyette
110 1.1.2.2 pgoyette return 0
111 1.1.2.2 pgoyette }
112 1.1.2.2 pgoyette
113 1.1.2.2 pgoyette setmsg()
114 1.1.2.2 pgoyette {
115 1.1.2.2 pgoyette MSG="${TEST_SH%% *} builtin printf"
116 1.1.2.2 pgoyette CurrentTest="$1"
117 1.1.2.2 pgoyette RVAL=0
118 1.1.2.2 pgoyette }
119 1.1.2.2 pgoyette
120 1.1.2.2 pgoyette BUILTIN_TEST=true
121 1.1.2.2 pgoyette
122 1.1.2.2 pgoyette # All the code to actually run the test comes from printf.sh ...
123 1.1.2.2 pgoyette
124