Debian Codegraph @

tests/datamash-sort-errors.sh

raw
1 #!/bin/sh
2 # Unit Tests for GNU Datamash - perform simple calculation on input data
3
4 # Copyright (C) 2014-2021 Assaf Gordon <[email protected]>
5 #
6 # This file is part of GNU Datamash.
7 #
8 # GNU Datamash is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # GNU Datamash is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>.
20 #
21 # Written by Assaf Gordon
22
23 ##
24 ## This script tests the sort piping code for errors
25 ##
26
27 . "${test_dir=.}/init.sh"; path_prepend_ ./src
28
29 fail=0
30
31 require_paste_
32
33 ## Ensure seq is useable
34 openbsd_seq_replacement_
35 seq 10 >/dev/null 2>/dev/null ||
36 skip_ "requires a working seq"
37
38 ## Cross-Compiling portability hack:
39 ## under qemu/binfmt, argv[0] (which is used to report errors) will contain
40 ## the full path of the binary, if the binary is on the $PATH.
41 ## So we try to detect what is the actual returned value of the program
42 ## in case of an error.
43 PROG_ARGV0=$(datamash --foobar 2>&1 | head -n 1 | cut -f1 -d:)
44 [ -z "$PROG_ARGV0" ] && PROG_ARGV0="datamash"
45
46 ##
47 ##
48 ## Test preparations
49 ##
50 ##
51 GROUPPARAM=$(seq 1000 2000 | paste -d "," -s -) ||
52 framework_failure_ "failed to construct too-long group parameter"
53
54 ## The expected error message when 'sort' is not found
55 printf 'sh: sort: not found\ndatamash: read error (on close)' > exp_err2 ||
56 framework_failure_ "failed to create exp_err2"
57
58 ##
59 ## Create a bad 'sort' executable, to simulate failed pipe/popen
60 ##
61 BADDIR1=$(mktemp -d bad_sort.XXXXXX) ||
62 framework_failure_ "Failed to create temp directory for bad-sort"
63 printf "#!/foo/bar/bad/interpreter" > "$BADDIR1/sort" ||
64 framework_failure_ "Failed to create bad-sort: $BADDIR1/sort"
65 chmod a+x "$BADDIR1/sort" ||
66 framework_failure_ "failed to make bad-sort executable"
67 ORIGPATH=$PATH
68
69 ## The directory where the "datamash' executable is
70 DATAMASHDIR=$(dirname $(which datamash))
71 test -z "$DATAMASHDIR" &&
72 framework_failure_ "failed to find datamash's directory"
73
74 ## Create a 'sort' which will crash
75 BADDIR=$(mktemp -d badsort.XXXXXX) ||
76 framework_failure_ "failed to create bad-sort-dir"
77 echo '#!/bin/sh
78 read A
79 echo "$A"
80 read B
81 echo "$B"
82 Z=0
83 C=$((1/$Z))
84 ' > "$BADDIR/sort" || framework_failure_ "failed to create $BADDIR/sort"
85 chmod a+x "$BADDIR/sort" ||
86 framework_failure_ "failed to make $BADDIR/sort executable"
87
88
89 ##
90 ## Tests start here
91 ##
92
93 ##
94 ## Test with non-existing 'sort' executable, by giving an invalid path
95 ##
96 ## NOTE: This run SHOULD return an error, hence the "&&" instead of "||"
97 ##
98 seq 10 | datamash --sort --sort-cmd=/not/a/sort -g 1 sum 1 &&
99 { warn_ "datamash --sort with non existing 'sort' did not fail " \
100 "(it should have failed)" ; fail=1 ; }
101
102 ##
103 ## Test with a 'sort' that crashes
104 ## NOTE: This run SHOULD return an error, hence the "&&" instead of "||"
105 ##
106 seq 10 | datamash --sort --sort-cmd="${BADDIR}/sort" -g 1 sum 1 &&
107 { warn_ "datamash --sort with crashing 'sort' did not fail " \
108 "(it should have failed)" ; fail=1 ; }
109
110 Exit $fail