File: datamash-sort-errors.sh

package info (click to toggle)
datamash 1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 13,600 kB
  • sloc: ansic: 65,320; sh: 8,982; perl: 5,127; makefile: 250; sed: 16
file content (110 lines) | stat: -rwxr-xr-x 3,470 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
#!/bin/sh
#   Unit Tests for GNU Datamash - perform simple calculation on input data

#    Copyright (C) 2014-2021 Assaf Gordon <assafgordon@gmail.com>
#
#    This file is part of GNU Datamash.
#
#    GNU Datamash 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.
#
#    GNU Datamash 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 GNU Datamash.  If not, see <https://www.gnu.org/licenses/>.
#
#    Written by Assaf Gordon

##
## This script tests the sort piping code for errors
##

. "${test_dir=.}/init.sh"; path_prepend_ ./src

fail=0

require_paste_

## Ensure seq is useable
openbsd_seq_replacement_
seq 10 >/dev/null 2>/dev/null ||
  skip_ "requires a working seq"

## Cross-Compiling portability hack:
##  under qemu/binfmt, argv[0] (which is used to report errors) will contain
##  the full path of the binary, if the binary is on the $PATH.
##  So we try to detect what is the actual returned value of the program
##  in case of an error.
PROG_ARGV0=$(datamash --foobar 2>&1 | head -n 1 | cut -f1 -d:)
[ -z "$PROG_ARGV0" ] && PROG_ARGV0="datamash"

##
##
## Test preparations
##
##
GROUPPARAM=$(seq 1000 2000 | paste -d "," -s -) ||
  framework_failure_ "failed to construct too-long group parameter"

## The expected error message when 'sort' is not found
printf 'sh: sort: not found\ndatamash: read error (on close)' > exp_err2 ||
  framework_failure_ "failed to create exp_err2"

##
## Create a bad 'sort' executable, to simulate failed pipe/popen
##
BADDIR1=$(mktemp -d bad_sort.XXXXXX) ||
  framework_failure_ "Failed to create temp directory for bad-sort"
printf "#!/foo/bar/bad/interpreter" > "$BADDIR1/sort" ||
  framework_failure_ "Failed to create bad-sort: $BADDIR1/sort"
chmod a+x "$BADDIR1/sort" ||
  framework_failure_ "failed to make bad-sort executable"
ORIGPATH=$PATH

## The directory where the "datamash' executable is
DATAMASHDIR=$(dirname $(which datamash))
test -z "$DATAMASHDIR" &&
  framework_failure_ "failed to find datamash's directory"

## Create a 'sort' which will crash
BADDIR=$(mktemp -d badsort.XXXXXX) ||
  framework_failure_ "failed to create bad-sort-dir"
echo '#!/bin/sh
read A
echo "$A"
read B
echo "$B"
Z=0
C=$((1/$Z))
' > "$BADDIR/sort" || framework_failure_ "failed to create $BADDIR/sort"
chmod a+x "$BADDIR/sort" ||
  framework_failure_ "failed to make $BADDIR/sort executable"


##
## Tests start here
##

##
## Test with non-existing 'sort' executable, by giving an invalid path
##
## NOTE: This run SHOULD return an error, hence the "&&" instead of "||"
##
seq 10 | datamash --sort --sort-cmd=/not/a/sort -g 1 sum 1 &&
  { warn_ "datamash --sort with non existing 'sort' did not fail " \
          "(it should have failed)" ; fail=1 ; }

##
## Test with a 'sort' that crashes
## NOTE: This run SHOULD return an error, hence the "&&" instead of "||"
##
seq 10 | datamash --sort --sort-cmd="${BADDIR}/sort" -g 1 sum 1 &&
  { warn_ "datamash --sort with crashing 'sort' did not fail " \
          "(it should have failed)" ; fail=1 ; }

Exit $fail