summaryrefslogtreecommitdiffstats
path: root/tests/misc/env-S-script.sh
blob: 0fd43d32f949f01eb8cd4b49f560ba3c83afc10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# Test env -S in a #! line of a script.

# Copyright (C) 2018-2022 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ env
print_ver_ printf

require_perl_

# a shortcut to avoid long lines
dir="$abs_top_builddir/src"

cat <<EOF > shebang || framework_failure_
#!$SHELL
EOF
cat <<\EOF >> shebang || framework_failure_
# Execute a script as per 3 argument shebang
# but without length limits (127 on Linux for example).
script="$1"; shift
shebang=$(sed -n 's/^#!//p;q' < "$script")
interp=$(printf '%s' "$shebang" | cut -d' ' -f1)
rest=$(printf '%s' "$shebang" | cut -s -d' ' -f2-)
test "$rest" && exec "$interp" "$rest" "$script" "$@"
exec "$interp" "$script" "$@"
EOF
chmod a+x shebang || framework_failure_

# A simple shebang program to call our new "env"
printf "#!$dir/env sh\necho hello\n" > env_test || framework_failure_
chmod a+x env_test || framework_failure_

# Verify we can run the shebang which is not the case if
# there are spaces in $abs_top_builddir.
./shebang ./env_test || skip_ "Error running env_test script"

# env should execute 'printf' with 7 parameters:
# 'x%sx\n', 'A', 'B' from the "-S" argument,
# the name of the executed script, and its 3 parameters (C,D,'E F').
# Ignoring the absolute paths, the script is:
#     #!env -S printf x%sx\n A B
printf "#!$dir/env -S $dir/printf "'x%%sx\\n A B\n' > env1 || framework_failure_
chmod a+x env1 || framework_failure_
cat<<\EOF>exp1 || framework_failure_
xAx
xBx
x./env1x
xCx
xDx
xE Fx
EOF
./shebang ./env1 C D "E F" > out1 || fail=1
compare exp1 out1 || fail=1


# similar to the above test but with quotes, the first parameter should be
# 'A B' and not two paramaters 'A','B'.
# Ignoring the absolute paths, the script is:
#     #!env -S printf x%sx\n "A B"
printf "#!$dir/env -S $dir/printf "'x%%sx\\n "A B"\n' > env2 ||
  framework_failure_
chmod a+x env2 || framework_failure_
cat<<\EOF>exp2 || framework_failure_
xA Bx
x./env2x
EOF
./shebang ./env2 > out2 || fail=1
compare exp2 out2 || fail=1


# backslash-underscore instead of spaces.
# Ignoring the absolute paths, the script is:
#     #!env -Sprintf\_x%sx\n\_Y
printf "#!$dir/env -S$dir/printf"'\\_x%%sx\\n\\_Y\n' > env3 ||
  framework_failure_
chmod a+x env3 || framework_failure_
cat<<\EOF>exp3 || framework_failure_
xYx
x./env3x
xWx
EOF
./shebang ./env3 W > out3 || fail=1
compare exp3 out3 || fail=1



# Test comments - The "#C D" should be ignored.
# Ignoring the absolute paths, the script is:
#     #!env -Sprintf x%sx\n A#B #C D
printf "#!$dir/env -S$dir/printf"' x%%sx\\n A#B #C D\n' > env4 \
    || framework_failure_
chmod a+x env4 || framework_failure_
cat<<\EOF>exp4 || framework_failure_
xA#Bx
x./env4x
xZx
EOF
./shebang ./env4 Z > out4 || fail=1
compare exp4 out4 || fail=1


# Test with a simple Perl usage.
# (assume Perl is in $PATH, as it is required for the test suite).
# Ignoring the absolute paths, the script is:
#     #!env -S perl -w -T
#     print "hello\n";
{ printf "#!$dir/env -S $PERL -w -T\n" ;
  printf 'print "hello\\n";\n' ; } > env5 || framework_failure_
chmod a+x env5 || framework_failure_
cat<<\EOF>exp5 || framework_failure_
hello
EOF
./shebang ./env5 > out5 || fail=1
compare exp5 out5 || fail=1


# Test with a more complex Perl usage.
# Ignoring the absolute paths, the script is:
#     #!env -S perl -mFile::Basename=basename -e "print basename(\$ARGV[0]);"
# The backslash before the '$' is required to prevent env(1) from treating
# $ARGV as an (invalid syntax) envvar, and pass it as-is to Perl.
{ printf "#!$dir/env -S " ;
  printf "$PERL -mFile::Basename=basename -e " ;
  printf '"print basename(\\$ARGV[0]);"\n' ; } > env6 || framework_failure_
chmod a+x env6 || framework_failure_
# Note: the perl script does not output a newline.
printf "env6" > exp6 || framework_failure_
./shebang ./env6 > out6 || fail=1
compare exp6 out6 || fail=1


Exit $fail