File: tstlib

package info (click to toggle)
shellia 5.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792 kB
  • sloc: sh: 7,840; makefile: 34
file content (215 lines) | stat: -rw-r--r-- 6,649 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# vim: set filetype=sh :
#        file: tstlib
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007-2021)
#     license: GNU General Public License, version 3
# description: internal test library for shellia
[ -f ia.basic ] && . ./ia.basic || . /usr/share/shellia/ia.basic

LANG=C
ECHO="/bin/echo"

oneline()
{
  gawk '{if(NR!=1)printf("\\n");printf("%s",$0,NR)}END{printf("\n")}'
}

# check <comment> [-u <cmd>] [-i <input>] [-s <shell>] [--sshd2222] <cmd> <expected>
# Description: run a command and verify the result
#   <comment>   a comment that will be displayed in the result of the check
#   -s <shell>  use shell <shell>
#   --sshd2222  only run <cmd> if sshd2222 should work
#               (test before with: if ./sshd2222 should-work; then ...)
#   --sshd2222-opts like --sshd2222 but also
#               add option "--sshopts <sshopts>" to <cmd>
#               (<sshopts> will be calculated with "./sshd2222 options")
#   -u <cmd>    command <cmd> will be called
#   <expected>  the expected output of <cmd>
# If the check fails, the following messag is displayed
help="$(cat <<END
# the following diagnostic output is created (remove with "make clean")
#   exp     contains what is expected
#   res     contains the result that does not match exp
#   res.org Original result before it was uniformed to match exp
#   r4s     the result in the form used in test.* files
END
)"
check()
{
  local input
  local shell
  local unify
  local cmd
  local output
  local comment
  local sshd2222

  #${ECHO} "DEBUG check: #=<$#> 1=<$1> 2=<$2> 3=<$3> 4=<$4> 5=<$5>" >&2
  comment="$1"
  shift

  input=""
  shell=""
  unify="cat"
  sshd2222=""
  sshd2222opts=""
  while [ $# -ne 2 ]; do
    if [ "$1" = "-u" ]; then
      unify="$2"
      shift 2
    elif [ "$1" = "-i" ]; then
      input="$2"
      shift 2
    elif [ "$1" = "-s" ]; then
      shell="$2"
      shift 2
    elif [ "$1" = "--sshd2222" ]; then
      sshd2222="$1"
      shift
    elif [ "$1" = "--sshd2222-opts" ]; then
      sshd2222="$1"
      if ./sshd2222 should-work; then
        sshd2222opts="$(./sshd2222 options)"
      fi
      shift
    else
      ${ECHO} "ERROR internal error check: unknown option <$1>" >&2
      exit 1
    fi
  done

  cmd="$1"

  # change existing and empty --sshopts or add --sshopts
  if [ "$sshd2222opts" ]; then
    if [ "$(${ECHO} "$cmd" | grep -e " --sshopts \"\"")" ]; then
      cmd="$(${ECHO} "$cmd" | sed -e "s| --sshopts \"\"| --sshopts \"$sshd2222opts\"|")"
    else
      cmd="$cmd --sshopts \"$sshd2222opts\""
    fi
  elif [ "$(${ECHO} "$cmd" | grep -e " --sshopts \"\"")" ]; then
      cmd="$(${ECHO} "$cmd" | sed -e "s| --sshopts \"\"||")"
  fi

  if [ "$shell" -a -f "$(${ECHO} "$cmd" | gawk '{print $1}')" ]; then
    if [ "$shell" = "mksh" ]; then
      # special handling for mksh needed, to respect export: see shell_differences/test.export_handling
      cmd="$shell -c \"$(${ECHO} "$cmd"|ia_easy_backslash)\""
    else
      cmd="$shell $cmd"
    fi
  fi
  [ "$input" ] && cmd="$input | $cmd"

  ${ECHO} "DEBUG check: cmd=<$cmd> (see files res.cmd and res.org)"
  timeformat="\nreal=%E user=%U sys=%S exit=%x"
  # /usr/bin/time may produce the message "Command exited with non-zero status x"
  if [ "$sshd2222" ]; then
    if ! ./sshd2222 should-work; then
      ${ECHO} "WARN $comment => no test, because sshd2222 is not possible" | oneline
      return
    fi
  fi

  # create res.cmd to manually recreate output
  cat <<END >res.cmd
#!/bin/sh
# created by tsttlib:check()
# run it from shellia/src directory
timeformat="$(${ECHO} "$timeformat"|ia_easy_backslash --quote)"
cmd="$(${ECHO} "$cmd"|ia_easy_backslash --quote)"
unify="$(${ECHO} "$unify"|ia_easy_backslash --quote)"
{ timeout 60 /usr/bin/time -f "\$timeformat" sh -c "\$cmd"; } 2>&1 | tee res.org | eval "\$unify"
END
  chmod 755 res.cmd

  output="$( { timeout 60 /usr/bin/time -f "$timeformat" sh -c "$cmd"; } 2>&1 | tee res.org | eval "$unify" )"
  if [ $(cat res.org | wc -l) -gt 5000 ]; then
    output="res.org is too long (try res.cmd)"
  else
    time="$(${ECHO} "$output" | tail -1)"
    output="$(${ECHO} "$output" | head -n -1)"
    ${ECHO} "$time $comment \"$cmd\"" | head -1 >>timelist
  fi

  ${ECHO}
  if [ "$output" = "$2" ]; then
    ${ECHO} "OK $comment \"$cmd\" => \"$output\"" | oneline
  else
    ${ECHO} "ERROR $comment \"$cmd\" => \"$output\" (expected:\"$2\")" | oneline
    ${ECHO} "$2" | sed -e "s/\\n/
/g" >exp
    ${ECHO} "$output" | sed -e "s/\\n/
/g" >res
    ${ECHO} "$output" | ia_easy_backslash |
      sed -e "1 s/^/\"/" | tac | sed -e "1 s/$/\"/" | tac >r4s
    ${ECHO} "The Test result is not what was expected. A Fix is needed."
    ${ECHO} "$help"
    exit 1
  fi
}

set_ia_x()
{
  local traceoff
  traceoff="{ set +x; } 2>/dev/null"
  sed -e "s|^\(\s*ia\>\).*|\1 -x|" -e "s|^\(\s\+\)\(eval \"\$ia_init\".*\)|\1$traceoff\n\1\2|"
}


quote="'"
SED_del_quote_in_trace="-e \"/+\+ /s/${quote}//g\""
SED_del_redirect_in_trace="-e \"/^|*+\+ \([1-9]\|\)>\s*\S\+\s*$/d\""
SED_del_ia_restore_return_in_trace="-e \"/^|*+\+ ia_restore_return$/{N;/\n|*+\+ return/{d}}\""
SED_command_not_found="sed -e \"s/.*: not:\( command\| inaccessible or\|\) not found/: not: not found/\""
GREP_pop_var_context="grep -v -e \": pop_var_context: head of shell_variables not a function context\""
# GREP_pop_var_context first seen 2022/09/18 in Bug#1020200: shellia: FTBS
UNIFY_mksh="grep -v -e \"^|*+\+ set +x$\" | \
sed -e \"s/+ typeset/+ local/\" \
-e \"s/\(+.*\) \+$/\1/\" \
$SED_del_quote_in_trace \
$SED_del_redirect_in_trace \
$SED_del_ia_restore_return_in_trace \
-e \"/+ ia_save_return/d\" \
-e \"s/^mksh: //\""
UNIFY_bash="$GREP_pop_var_context | \
sed -e \"s/+\+ /+ /\" \
$SED_del_quote_in_trace"
UNIFY_posh="grep -v -e \"^+ 2> /dev/null $\" | \
sed $SED_del_redirect_in_trace"
UNIFY_mktemp="sed -e \"s/\/tmp\/tmp\.\S\+/\/tmp\/tmp.mktemp/g\""
UNIFY_busybox="sed \
$SED_del_quote_in_trace"

# Use_locallib is needed in hello_world examples.
# To keep them easy, they read /usr/share/shellia/ia* libs.
# This libs will be available after installation of shellia.
use_locallib()
{
  cat $cmd | sed -e "s|^. /usr/share/shellia/ia|. ./ia|" > $cmd.tmp.locallib
  cmd="$cmd.tmp.locallib"
  chmod 755 $cmd
}

# temporary changes to overcome bugs
bug913718_unify()
{
  # bug#913718
  if [ "$shell" = "posh" ]; then
    if [ "$1" ]; then
      eval "$1=\"\$$1 |
sed \\\"s/.tmp.no_set-u//\\\"\""
    else
      ${ECHO} "sed \"s/.tmp.no_set-u//\""
    fi
  fi
}

bug913718_cmd()
{
  # bug#913718
  if [ "$shell" = "posh" ]; then
    cat $cmd | sed "/^set -u/ d" > $cmd.tmp.no_set-u
    cmd="$cmd.tmp.no_set-u"
    chmod 755 $cmd
  fi
}