tests: move tests to a directory per utility
[coreutils.git] / tests / env / env-S-script.sh
blob1d9b3e268e71bb9cf75f3c665dcfc00e7ec5823e
1 #!/bin/sh
2 # Test env -S in a #! line of a script.
4 # Copyright (C) 2018-2023 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21 print_ver_ env
22 print_ver_ printf
24 require_perl_
26 # a shortcut to avoid long lines
27 dir="$abs_top_builddir/src"
29 cat <<EOF > shebang || framework_failure_
30 #!$SHELL
31 EOF
32 cat <<\EOF >> shebang || framework_failure_
33 # Execute a script as per 3 argument shebang
34 # but without length limits (127 on Linux for example).
35 script="$1"; shift
36 shebang=$(sed -n 's/^#!//p;q' < "$script")
37 interp=$(printf '%s' "$shebang" | cut -d' ' -f1)
38 rest=$(printf '%s' "$shebang" | cut -s -d' ' -f2-)
39 test "$rest" && exec "$interp" "$rest" "$script" "$@"
40 exec "$interp" "$script" "$@"
41 EOF
42 chmod a+x shebang || framework_failure_
44 # A simple shebang program to call our new "env"
45 printf "#!$dir/env sh\necho hello\n" > env_test || framework_failure_
46 chmod a+x env_test || framework_failure_
48 # Verify we can run the shebang which is not the case if
49 # there are spaces in $abs_top_builddir.
50 ./shebang ./env_test || skip_ "Error running env_test script"
52 # env should execute 'printf' with 7 parameters:
53 # 'x%sx\n', 'A', 'B' from the "-S" argument,
54 # the name of the executed script, and its 3 parameters (C,D,'E F').
55 # Ignoring the absolute paths, the script is:
56 # #!env -S printf x%sx\n A B
57 printf "#!$dir/env -S $dir/printf "'x%%sx\\n A B\n' > env1 || framework_failure_
58 chmod a+x env1 || framework_failure_
59 cat<<\EOF>exp1 || framework_failure_
60 xAx
61 xBx
62 x./env1x
63 xCx
64 xDx
65 xE Fx
66 EOF
67 ./shebang ./env1 C D "E F" > out1 || fail=1
68 compare exp1 out1 || fail=1
71 # similar to the above test but with quotes, the first parameter should be
72 # 'A B' and not two parameters 'A','B'.
73 # Ignoring the absolute paths, the script is:
74 # #!env -S printf x%sx\n "A B"
75 printf "#!$dir/env -S $dir/printf "'x%%sx\\n "A B"\n' > env2 ||
76 framework_failure_
77 chmod a+x env2 || framework_failure_
78 cat<<\EOF>exp2 || framework_failure_
79 xA Bx
80 x./env2x
81 EOF
82 ./shebang ./env2 > out2 || fail=1
83 compare exp2 out2 || fail=1
86 # backslash-underscore instead of spaces.
87 # Ignoring the absolute paths, the script is:
88 # #!env -Sprintf\_x%sx\n\_Y
89 printf "#!$dir/env -S$dir/printf"'\\_x%%sx\\n\\_Y\n' > env3 ||
90 framework_failure_
91 chmod a+x env3 || framework_failure_
92 cat<<\EOF>exp3 || framework_failure_
93 xYx
94 x./env3x
95 xWx
96 EOF
97 ./shebang ./env3 W > out3 || fail=1
98 compare exp3 out3 || fail=1
102 # Test comments - The "#C D" should be ignored.
103 # Ignoring the absolute paths, the script is:
104 # #!env -Sprintf x%sx\n A#B #C D
105 printf "#!$dir/env -S$dir/printf"' x%%sx\\n A#B #C D\n' > env4 \
106 || framework_failure_
107 chmod a+x env4 || framework_failure_
108 cat<<\EOF>exp4 || framework_failure_
109 xA#Bx
110 x./env4x
113 ./shebang ./env4 Z > out4 || fail=1
114 compare exp4 out4 || fail=1
117 # Test with a simple Perl usage.
118 # (assume Perl is in $PATH, as it is required for the test suite).
119 # Ignoring the absolute paths, the script is:
120 # #!env -S perl -w -T
121 # print "hello\n";
122 { printf "#!$dir/env -S $PERL -w -T\n" ;
123 printf 'print "hello\\n";\n' ; } > env5 || framework_failure_
124 chmod a+x env5 || framework_failure_
125 cat<<\EOF>exp5 || framework_failure_
126 hello
128 ./shebang ./env5 > out5 || fail=1
129 compare exp5 out5 || fail=1
132 # Test with a more complex Perl usage.
133 # Ignoring the absolute paths, the script is:
134 # #!env -S perl -mFile::Basename=basename -e "print basename(\$ARGV[0]);"
135 # The backslash before the '$' is required to prevent env(1) from treating
136 # $ARGV as an (invalid syntax) envvar, and pass it as-is to Perl.
137 { printf "#!$dir/env -S " ;
138 printf "$PERL -mFile::Basename=basename -e " ;
139 printf '"print basename(\\$ARGV[0]);"\n' ; } > env6 || framework_failure_
140 chmod a+x env6 || framework_failure_
141 # Note: the perl script does not output a newline.
142 printf "env6" > exp6 || framework_failure_
143 ./shebang ./env6 > out6 || fail=1
144 compare exp6 out6 || fail=1
147 Exit $fail