| 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 |
$prog .= " --sort-cmd=/usr/bin/sort" if $^O eq "netbsd"; |
| 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 $in1=<<'EOF'; |
| 45 |
I 1.20.30.41 |
| 46 |
II 1.20.30.1 |
| 47 |
IV 1.10.30.14 |
| 48 |
M 1.2.10.3 |
| 49 |
M 192.168.43.1 |
| 50 |
II 1.20.30.41 |
| 51 |
D 192.168.17.8 |
| 52 |
C 1.2.10.3 |
| 53 |
L 192.168.17.10 |
| 54 |
EOF |
| 55 |
|
| 56 |
|
| 57 |
my $out1_dec_roman=<<'EOF'; |
| 58 |
I 1.20.30.41 |
| 59 |
II 1.20.30.1 |
| 60 |
II 1.20.30.41 |
| 61 |
IV 1.10.30.14 |
| 62 |
L 192.168.17.10 |
| 63 |
C 1.2.10.3 |
| 64 |
D 192.168.17.8 |
| 65 |
M 1.2.10.3 |
| 66 |
M 192.168.43.1 |
| 67 |
EOF |
| 68 |
|
| 69 |
my $out1_dec_ipv4=<<'EOF'; |
| 70 |
C 1.2.10.3 |
| 71 |
M 1.2.10.3 |
| 72 |
IV 1.10.30.14 |
| 73 |
II 1.20.30.1 |
| 74 |
I 1.20.30.41 |
| 75 |
II 1.20.30.41 |
| 76 |
D 192.168.17.8 |
| 77 |
L 192.168.17.10 |
| 78 |
M 192.168.43.1 |
| 79 |
EOF |
| 80 |
|
| 81 |
my $out1_dec_ipv4_stable=<<'EOF'; |
| 82 |
M 1.2.10.3 |
| 83 |
C 1.2.10.3 |
| 84 |
IV 1.10.30.14 |
| 85 |
II 1.20.30.1 |
| 86 |
I 1.20.30.41 |
| 87 |
II 1.20.30.41 |
| 88 |
D 192.168.17.8 |
| 89 |
L 192.168.17.10 |
| 90 |
M 192.168.43.1 |
| 91 |
EOF |
| 92 |
|
| 93 |
|
| 94 |
my $out1_dec_ipv4_rev=<<'EOF'; |
| 95 |
M 192.168.43.1 |
| 96 |
L 192.168.17.10 |
| 97 |
D 192.168.17.8 |
| 98 |
I 1.20.30.41 |
| 99 |
II 1.20.30.41 |
| 100 |
II 1.20.30.1 |
| 101 |
IV 1.10.30.14 |
| 102 |
C 1.2.10.3 |
| 103 |
M 1.2.10.3 |
| 104 |
EOF |
| 105 |
|
| 106 |
my $out1_dec_ipv4_rev_header1=<<'EOF'; |
| 107 |
I 1.20.30.41 |
| 108 |
M 192.168.43.1 |
| 109 |
L 192.168.17.10 |
| 110 |
D 192.168.17.8 |
| 111 |
II 1.20.30.41 |
| 112 |
II 1.20.30.1 |
| 113 |
IV 1.10.30.14 |
| 114 |
C 1.2.10.3 |
| 115 |
M 1.2.10.3 |
| 116 |
EOF |
| 117 |
|
| 118 |
my $out1_dec_ipv4_rev_header2=<<'EOF'; |
| 119 |
I 1.20.30.41 |
| 120 |
II 1.20.30.1 |
| 121 |
M 192.168.43.1 |
| 122 |
L 192.168.17.10 |
| 123 |
D 192.168.17.8 |
| 124 |
II 1.20.30.41 |
| 125 |
IV 1.10.30.14 |
| 126 |
C 1.2.10.3 |
| 127 |
M 1.2.10.3 |
| 128 |
EOF |
| 129 |
|
| 130 |
|
| 131 |
my $out1_dec_roman_ipv4rev=<<'EOF'; |
| 132 |
I 1.20.30.41 |
| 133 |
II 1.20.30.41 |
| 134 |
II 1.20.30.1 |
| 135 |
IV 1.10.30.14 |
| 136 |
L 192.168.17.10 |
| 137 |
C 1.2.10.3 |
| 138 |
D 192.168.17.8 |
| 139 |
M 192.168.43.1 |
| 140 |
M 1.2.10.3 |
| 141 |
EOF |
| 142 |
|
| 143 |
my $out1_dec_ipv4_romanrev=<<'EOF'; |
| 144 |
M 1.2.10.3 |
| 145 |
C 1.2.10.3 |
| 146 |
IV 1.10.30.14 |
| 147 |
II 1.20.30.1 |
| 148 |
II 1.20.30.41 |
| 149 |
I 1.20.30.41 |
| 150 |
D 192.168.17.8 |
| 151 |
L 192.168.17.10 |
| 152 |
M 192.168.43.1 |
| 153 |
EOF |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
my $out1_dec_roman_k2=<<'EOF'; |
| 158 |
I 1.20.30.41 |
| 159 |
II 1.20.30.1 |
| 160 |
II 1.20.30.41 |
| 161 |
IV 1.10.30.14 |
| 162 |
L 192.168.17.10 |
| 163 |
C 1.2.10.3 |
| 164 |
D 192.168.17.8 |
| 165 |
M 1.2.10.3 |
| 166 |
M 192.168.43.1 |
| 167 |
EOF |
| 168 |
|
| 169 |
my $out1_dec_k2n_roman=<<'EOF'; |
| 170 |
IV 1.10.30.14 |
| 171 |
I 1.20.30.41 |
| 172 |
II 1.20.30.1 |
| 173 |
II 1.20.30.41 |
| 174 |
C 1.2.10.3 |
| 175 |
M 1.2.10.3 |
| 176 |
L 192.168.17.10 |
| 177 |
D 192.168.17.8 |
| 178 |
M 192.168.43.1 |
| 179 |
EOF |
| 180 |
|
| 181 |
my $in2=<<'EOF'; |
| 182 |
203.0.113.47 |
| 183 |
192.0.2.33 |
| 184 |
203.0.113.0 |
| 185 |
192.0.2.3 |
| 186 |
0.0.0.0 |
| 187 |
::ffff:192.0.2.42 |
| 188 |
2001:db8:6:5:4:3:2:1 |
| 189 |
2001:Db8:: |
| 190 |
::192.0.2.41 |
| 191 |
::1 |
| 192 |
EOF |
| 193 |
|
| 194 |
my $out2_ipv6v4map=<<'EOF'; |
| 195 |
::1 |
| 196 |
::192.0.2.41 |
| 197 |
0.0.0.0 |
| 198 |
192.0.2.3 |
| 199 |
192.0.2.33 |
| 200 |
::ffff:192.0.2.42 |
| 201 |
203.0.113.0 |
| 202 |
203.0.113.47 |
| 203 |
2001:Db8:: |
| 204 |
2001:db8:6:5:4:3:2:1 |
| 205 |
EOF |
| 206 |
|
| 207 |
my $out2_ipv6v4comp=<<'EOF'; |
| 208 |
0.0.0.0 |
| 209 |
::1 |
| 210 |
192.0.2.3 |
| 211 |
192.0.2.33 |
| 212 |
::192.0.2.41 |
| 213 |
203.0.113.0 |
| 214 |
203.0.113.47 |
| 215 |
::ffff:192.0.2.42 |
| 216 |
2001:Db8:: |
| 217 |
2001:db8:6:5:4:3:2:1 |
| 218 |
EOF |
| 219 |
|
| 220 |
|
| 221 |
my @Tests = |
| 222 |
( |
| 223 |
['s1', '-k1,1:roman', {IN_PIPE=>$in1}, {OUT => $out1_dec_roman}], |
| 224 |
['s2', '-k2,2:ipv4', {IN_PIPE=>$in1}, {OUT => $out1_dec_ipv4}], |
| 225 |
['s3', '-k2,2:ipv4 --stable', {IN_PIPE=>$in1}, |
| 226 |
{OUT => $out1_dec_ipv4_stable}], |
| 227 |
['s4', '-k2,2r:ipv4', {IN_PIPE=>$in1}, {OUT => $out1_dec_ipv4_rev}], |
| 228 |
|
| 229 |
['s5', '-k1,1:roman -k2,2r:ipv4' , {IN_PIPE=>$in1}, |
| 230 |
{OUT => $out1_dec_roman_ipv4rev}], |
| 231 |
['s6', '-k2,2:ipv4 -k1r,1:roman' , {IN_PIPE=>$in1}, |
| 232 |
{OUT => $out1_dec_ipv4_romanrev}], |
| 233 |
|
| 234 |
['s10', '-k1,1:roman -k2,2' , {IN_PIPE=>$in1}, |
| 235 |
{OUT => $out1_dec_roman_k2}], |
| 236 |
['s11', '-k2n,2 -k1,1:roman' , {IN_PIPE=>$in1}, |
| 237 |
{OUT => $out1_dec_k2n_roman}], |
| 238 |
|
| 239 |
['s12', '-k1,1:ipv6v4map', {IN_PIPE=>$in2}, {OUT => $out2_ipv6v4map}], |
| 240 |
['s13', '-k1,1:ipv6v4comp', {IN_PIPE=>$in2}, {OUT => $out2_ipv6v4comp}], |
| 241 |
|
| 242 |
|
| 243 |
# Sort with header lines |
| 244 |
['sh1', '-H -k2,2r:ipv4', {IN_PIPE=>$in1}, {OUT=>$out1_dec_ipv4_rev_header1}], |
| 245 |
['sh2', '--header=2 -k2,2r:ipv4', {IN_PIPE=>$in1}, |
| 246 |
{OUT => $out1_dec_ipv4_rev_header2}], |
| 247 |
# More header lines than in the input |
| 248 |
['sh3', '--header=9 -k2,2r:ipv4', {IN_PIPE=>$in1}, {OUT => $in1}], |
| 249 |
['sh4', '--header=10 -k2,2r:ipv4', {IN_PIPE=>$in1}, {OUT => $in1}], |
| 250 |
|
| 251 |
); |
| 252 |
|
| 253 |
# Repeat all tests with --debug option, ensure it does not cause any regression |
| 254 |
my @debug_tests; |
| 255 |
foreach my $t (@Tests) |
| 256 |
{ |
| 257 |
# Skip tests with EXIT!=0 or ERR_SUBST part |
| 258 |
# (as '--debug' requires its own ERR_SUBST). |
| 259 |
my $exit_val; |
| 260 |
my $have_err_subst; |
| 261 |
foreach my $e (@$t) |
| 262 |
{ |
| 263 |
next unless ref $e && ref $e eq 'HASH'; |
| 264 |
$exit_val = $e->{EXIT} if defined $e->{EXIT}; |
| 265 |
$have_err_subst = 1 if defined $e->{ERR_SUBST}; |
| 266 |
} |
| 267 |
next if $exit_val || $have_err_subst; |
| 268 |
|
| 269 |
# Duplicate the test, add '--debug' argument |
| 270 |
my @newt = @$t; |
| 271 |
$newt[0] = 'dbg_' . $newt[0]; |
| 272 |
$newt[1] = '---debug ' . $newt[1]; |
| 273 |
|
| 274 |
# Discard all debug printouts before comparing output |
| 275 |
push @newt, {ERR_SUBST => q!s/.*\n//m!}; |
| 276 |
|
| 277 |
push @debug_tests, \@newt; |
| 278 |
} |
| 279 |
push @Tests, @debug_tests; |
| 280 |
|
| 281 |
|
| 282 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 283 |
my $verbose = $ENV{VERBOSE}; |
| 284 |
|
| 285 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 286 |
exit $fail; |