Debian Codegraph @

tests/decorate-errors.pl

raw
1 #!/usr/bin/env perl
2 =pod
3 Unit Tests for decorate
4
5 Copyright (C) 2020-2021 Assaf Gordon <[email protected]>
6 Copyright (C) 2022-2025 Timothy Rice <[email protected]>
7
8 This file is part of GNU Datamash.
9
10 GNU Datamash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 GNU Datamash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>.
22
23 Written by Assaf Gordon.
24 =cut
25 use strict;
26 use warnings;
27
28 # Until a better way comes along to auto-use Coreutils Perl modules
29 # as in the coreutils' autotools system.
30 use Coreutils;
31 use CuSkip;
32 use CuTmpdir qw(decorate);
33 use MIME::Base64 ;
34
35 (my $program_name = $0) =~ s|.*/||;
36 my $prog = 'decorate';
37
38
39 # TODO: add localization tests with "grouping"
40 # Turn off localization of executable's output.
41 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
42
43
44 my $ordering_flags_error_prefix="ordering flags (b/d/i/h/n/g/M/R/V) " .
45 "cannot be combined with a conversion function: " .
46 "invalid field specification";
47
48 my @Tests =
49 (
50 ['e1','--foo-bar',
51 {ERR_SUBST=>'s/unknown/unrecognized/; s/-- foo-bar/\'--foo-bar\'/'},
52 {ERR=>"$prog: unrecognized option '--foo-bar'\n" .
53 "Try '$prog --help' for more information.\n"},
54 {EXIT=>2}],
55 ['e2','-k1,1:strlen no-such-file.txt', {EXIT=>2},
56 {ERR=>"$prog: no-such-file.txt: No such file or directory\n"}],
57 ['e3','--decorate -k1,1:strlen no-such-file.txt', {EXIT=>2},
58 {ERR=>"$prog: no-such-file.txt: No such file or directory\n"}],
59 ['e4','--undecorate 2 no-such-file.txt', {EXIT=>2},
60 {ERR=>"$prog: no-such-file.txt: No such file or directory\n"}],
61
62 ['e5','-k1,1%', {EXIT=>2},
63 {ERR=>"$prog: invalid key specification: " .
64 "invalid field specification '1,1%'\n"}],
65 ['e6','-k1,1:', {EXIT=>2},
66 {ERR=>"$prog: missing internal conversion function: " .
67 "invalid field specification '1,1:'\n"}],
68 ['e7','-k1,1@', {EXIT=>2},
69 {ERR=>"$prog: missing external conversion command: " .
70 "invalid field specification '1,1\@'\n"}],
71 ['e8','-k1,1@foobar', {EXIT=>2},
72 {ERR=>"$prog: external commands are not implemented (yet)\n"}],
73 ['e9','-k1,1:foobar', {EXIT=>2},
74 {ERR=>"$prog: invalid built-in conversion option: " .
75 "invalid field specification '1,1:foobar'\n"}],
76
77 ## Bad --key=KEYDEF syntax (should be identical to sort's behavior)
78 ['e10','-k0', {EXIT=>2},
79 {ERR=>"$prog: field number is zero: invalid field specification '0'\n"}],
80 ['e11','-k1.0', {EXIT=>2},
81 {ERR=>"$prog: character offset is zero: " .
82 "invalid field specification '1.0'\n"}],
83 ['e12','-k1.A', {EXIT=>2},
84 {ERR=>"$prog: invalid number after '.': invalid count at start of 'A'\n"}],
85 ['e13','-k1,0', {EXIT=>2},
86 {ERR=>"$prog: field number is zero: invalid field specification '1,0'\n"}],
87 ['e14','-k1,2.B', {EXIT=>2},
88 {ERR=>"$prog: invalid number after '.': invalid count at start of 'B'\n"}],
89
90
91 ## sort key ordering aren't allowed with conversion functions
92 ## (except 'r' for reverse order)
93 ['e20','-k1b,1:foobar', {EXIT=>2},
94 {ERR=>"$prog: $ordering_flags_error_prefix '1b,1:foobar'\n"}],
95 ['e21','-k1,1b:foobar', {EXIT=>2},
96 {ERR=>"$prog: $ordering_flags_error_prefix '1,1b:foobar'\n"}],
97 ['e22','-k1f,1:foobar', {EXIT=>2},
98 {ERR=>"$prog: $ordering_flags_error_prefix '1f,1:foobar'\n"}],
99 ['e23','-k1d,1:foobar', {EXIT=>2},
100 {ERR=>"$prog: $ordering_flags_error_prefix '1d,1:foobar'\n"}],
101 ['e24','-k1n,1:foobar', {EXIT=>2},
102 {ERR=>"$prog: $ordering_flags_error_prefix '1n,1:foobar'\n"}],
103 ['e25','-k1g,1:foobar', {EXIT=>2},
104 {ERR=>"$prog: $ordering_flags_error_prefix '1g,1:foobar'\n"}],
105 ['e26','-k1M,1:foobar', {EXIT=>2},
106 {ERR=>"$prog: $ordering_flags_error_prefix '1M,1:foobar'\n"}],
107 ['e27','-k1h,1:foobar', {EXIT=>2},
108 {ERR=>"$prog: $ordering_flags_error_prefix '1h,1:foobar'\n"}],
109 ['e28','-k1V,1:foobar', {EXIT=>2},
110 {ERR=>"$prog: $ordering_flags_error_prefix '1V,1:foobar'\n"}],
111 ['e29','-k1,1R:foobar', {EXIT=>2},
112 {ERR=>"$prog: $ordering_flags_error_prefix '1,1R:foobar'\n"}],
113
114
115 ['e40','-t "" -k1,1:foobar', {EXIT=>2},
116 {ERR=>"$prog: empty tab\n"}],
117 ['e41','-tab -k1,1:foobar', {EXIT=>2},
118 {ERR=>"$prog: multi-character tab 'ab'\n"}],
119 ['e42','-t: -t, -k1,1:foobar', {EXIT=>2},
120 {ERR=>"$prog: incompatible tabs\n"}],
121
122
123 ['e50','--undecorate 0', {EXIT=>2},
124 {ERR=>"$prog: invalid number of fields to undecorate '0'\n"}],
125 ['e51','--undecorate A', {EXIT=>2},
126 {ERR=>"$prog: invalid number of fields to undecorate 'A'\n"}],
127 ['e52','--undecorate -4', {EXIT=>2},
128 {ERR=>"$prog: invalid number of fields to undecorate '-4'\n"}],
129
130 ['e60','--decorate -k1,1:roman --undecorate 4', {EXIT=>2},
131 {ERR=>"$prog: --decorate and --undecorate options " .
132 "are mutually exclusive\n"}],
133 ['e61','-k1,1:roman --undecorate 4', {EXIT=>2},
134 {ERR=>"$prog: --undecorate cannot be used with --keys or --decorate\n"}],
135 ['e62','', {EXIT=>2},
136 {ERR=>"$prog: missing -k/--key decoration or --undecorate options\n"}],
137
138 ['e70', '--header X --decorate -k2,2:ipv4' , {EXIT=>2},
139 {ERR=>"$prog: invalid number of header lines 'X'\n"}],
140
141
142 # Conversion Errors
143 ['c1', '--decorate -k1,1:roman' , {IN_PIPE=>"\n"}, {OUT => " "}, {EXIT=>2},
144 {ERR=>"$prog: invalid empty roman numeral\n" .
145 "$prog: conversion failed in line 1\n" }],
146 ['c2', '--decorate -k2,2:roman' , {IN_PIPE=>"M"}, {OUT => " "}, {EXIT=>2},
147 {ERR=>"$prog: invalid empty roman numeral\n" .
148 "$prog: conversion failed in line 1\n" }],
149 ['c3', '--decorate -k1,1:ipv4' , {IN_PIPE=>"FOO\n"}, {OUT => " "}, {EXIT=>2},
150 {ERR=>"$prog: invalid dot-decimal IPv4 address 'FOO'\n" .
151 "$prog: conversion failed in line 1\n" }],
152 ['c4', '--decorate -k1,1:ipv4inet' , {IN_PIPE=>"FOO\n"}, {OUT => " "},
153 {EXIT=>2},
154 {ERR=>"$prog: invalid IPv4 address 'FOO'\n" .
155 "$prog: conversion failed in line 1\n" }],
156 ['c5', '--decorate -k1,1:ipv6' , {IN_PIPE=>"FOO\n"}, {OUT => " "}, {EXIT=>2},
157 {ERR=>"$prog: invalid IPv6 address 'FOO'\n" .
158 "$prog: conversion failed in line 1\n" }],
159 ['c6', '--decorate -k1,1:ipv6v4map', {IN_PIPE=>"FOO\n"}, {OUT => " "},
160 {EXIT=>2},
161 {ERR=>"$prog: invalid IP address 'FOO'\n" .
162 "$prog: conversion failed in line 1\n" }],
163 ['c7', '--decorate -k1,1:ipv6v4comp', {IN_PIPE=>"FOO\n"}, {OUT => " "},
164 {EXIT=>2},
165 {ERR=>"$prog: invalid IP address 'FOO'\n" .
166 "$prog: conversion failed in line 1\n" }],
167 ['c8', '--decorate -k1,1:ipv6v4map', {IN_PIPE=>"0\n"}, {OUT => " "},
168 {EXIT=>2},
169 {ERR=>"$prog: invalid IP address '0'\n" .
170 "$prog: conversion failed in line 1\n" }],
171 ['c9', '--decorate -k1,1:ipv6v4comp', {IN_PIPE=>"0\n"}, {OUT => " "},
172 {EXIT=>2},
173 {ERR=>"$prog: invalid IP address '0'\n" .
174 "$prog: conversion failed in line 1\n" }],
175 ['c10', '--decorate -k1,1:ipv6v4map', {IN_PIPE=>"\n"}, {OUT => " "},
176 {EXIT=>2},
177 {ERR=>"$prog: invalid IP address ''\n" .
178 "$prog: conversion failed in line 1\n" }],
179 ['c11', '--decorate -k1,1:ipv6v4comp', {IN_PIPE=>"\n"}, {OUT => " "},
180 {EXIT=>2},
181 {ERR=>"$prog: invalid IP address ''\n" .
182 "$prog: conversion failed in line 1\n" }],
183
184
185 # on a different architecture, would printf(%Lg) print something else?
186 # Use OUT_SUBST to trim output to 1.3 digits
187 #['b14', 'mean 1', {IN_PIPE=>$in1}, {OUT => "5.454\n"},
188 # {OUT_SUBST=>'s/^(\d\.\d{3}).*/\1/'}],
189
190 ## Some error checkings
191 #['e1', 'sum', {IN_PIPE=>""}, {EXIT=>1},
192 # {ERR=>"$prog: missing field for operation 'sum'\n"}],
193 );
194
195 # Repeat all tests with --debug option, ensure it does not cause any regression
196 my @debug_tests;
197 foreach my $t (@Tests)
198 {
199 # Skip tests with EXIT!=0 or ERR_SUBST part
200 # (as '--debug' requires its own ERR_SUBST).
201 my $exit_val;
202 my $have_err_subst;
203 foreach my $e (@$t)
204 {
205 next unless ref $e && ref $e eq 'HASH';
206 $exit_val = $e->{EXIT} if defined $e->{EXIT};
207 $have_err_subst = 1 if defined $e->{ERR_SUBST};
208 }
209 next if $exit_val || $have_err_subst;
210
211 # Duplicate the test, add '--debug' argument
212 my @newt = @$t;
213 $newt[0] = 'dbg_' . $newt[0];
214 $newt[1] = '---debug ' . $newt[1];
215
216 # Discard all debug printouts before comparing output
217 push @newt, {ERR_SUBST => q!s/.*\n//m!};
218
219 push @debug_tests, \@newt;
220 }
221 push @Tests, @debug_tests;
222
223
224 my $save_temps = $ENV{SAVE_TEMPS};
225 my $verbose = $ENV{VERBOSE};
226
227 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
228 exit $fail;