| 1 |
#!/usr/bin/env perl |
| 2 |
=pod |
| 3 |
Unit Tests for GNU Datamash - perform simple calculation on input data |
| 4 |
|
| 5 |
Copyright (C) 2013-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(datamash); |
| 33 |
use MIME::Base64 ; |
| 34 |
|
| 35 |
(my $program_name = $0) =~ s|.*/||; |
| 36 |
my $prog_bin = 'datamash'; |
| 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 |
my $prog = `$prog_bin ---print-progname`; |
| 44 |
$prog = $prog_bin unless $prog; |
| 45 |
|
| 46 |
## Portability hack |
| 47 |
## Check if the system's sort supports stable sorting ('-s'). |
| 48 |
## If it doesn't - skip some tests |
| 49 |
my $rc = system("sort -s < /dev/null > /dev/null 2>/dev/null"); |
| 50 |
die "testing framework failure: failed to execute sort -s" |
| 51 |
if ( ($rc == -1) || ($rc & 127) ); |
| 52 |
my $sort_exit_code = ($rc >> 8); |
| 53 |
my $have_stable_sort = ($sort_exit_code==0); |
| 54 |
|
| 55 |
|
| 56 |
# TODO: add localization tests with "grouping" |
| 57 |
# Turn off localization of executable's output. |
| 58 |
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; |
| 59 |
|
| 60 |
my $in_g3=<<'EOF'; |
| 61 |
A 3 W |
| 62 |
A 5 W |
| 63 |
A 7 W |
| 64 |
A 11 X |
| 65 |
A 13 X |
| 66 |
B 17 Y |
| 67 |
B 19 Z |
| 68 |
C 23 Z |
| 69 |
EOF |
| 70 |
|
| 71 |
my $in_g4=<<'EOF'; |
| 72 |
A 5 |
| 73 |
K 6 |
| 74 |
P 2 |
| 75 |
EOF |
| 76 |
|
| 77 |
my $in_hdr1=<<'EOF'; |
| 78 |
x y z |
| 79 |
A 1 10 |
| 80 |
A 2 10 |
| 81 |
A 3 10 |
| 82 |
A 4 10 |
| 83 |
A 4 10 |
| 84 |
B 5 10 |
| 85 |
B 6 20 |
| 86 |
B 7 30 |
| 87 |
C 8 11 |
| 88 |
C 9 22 |
| 89 |
C 1 33 |
| 90 |
C 2 44 |
| 91 |
EOF |
| 92 |
|
| 93 |
my $in_hdr_only=<<'EOF'; |
| 94 |
X:Y:Z |
| 95 |
EOF |
| 96 |
|
| 97 |
my $full_deprecation = "$prog: Using -f/--full with non-linewise operations " . |
| 98 |
"is deprecated and will be disabled in a future release.\n"; |
| 99 |
|
| 100 |
my @Tests = |
| 101 |
( |
| 102 |
# empty input = empty output, regardless of options |
| 103 |
['emp2dep', '--full count 2', {IN_PIPE=>""},{OUT=>""}, |
| 104 |
{ERR=>"$full_deprecation"}], |
| 105 |
['emp5dep', '--full --header-in count 2', {IN_PIPE=>""},{OUT=>""}, |
| 106 |
{ERR=>"$full_deprecation"}], |
| 107 |
['emp6dep', '--full --header-out count 2', {IN_PIPE=>""},{OUT=>""}, |
| 108 |
{ERR=>"$full_deprecation"}], |
| 109 |
['emp7dep', '--full --header-in --header-out count 2', |
| 110 |
{IN_PIPE=>""},{OUT=>""}, |
| 111 |
{ERR=>"$full_deprecation"}], |
| 112 |
['emp8dep', '-g3,4 --full --header-in --header-out count 2', |
| 113 |
{IN_PIPE=>""},{OUT=>""}, |
| 114 |
{ERR=>"$full_deprecation"}], |
| 115 |
|
| 116 |
# --full option - without grouping, returns the first line |
| 117 |
['fl1dep', '-t" " --full sum 2', {IN_PIPE=>$in_g3}, |
| 118 |
{OUT=>"A 3 W 98\n"}, |
| 119 |
{ERR=>"$full_deprecation"}], |
| 120 |
# --full with grouping - print entire line of each group |
| 121 |
['fl2dep', '-t" " --full -g3 sum 2', {IN_PIPE=>$in_g3}, |
| 122 |
{OUT=>"A 3 W 15\nA 11 X 24\nB 17 Y 17\nB 19 Z 42\n"}, |
| 123 |
{ERR=>"$full_deprecation"}], |
| 124 |
|
| 125 |
# Input and output header, with full line |
| 126 |
['hdr3dep', '-t" " -g 1 --full --header-in --header-out count 2', |
| 127 |
{IN_PIPE=>$in_hdr1}, |
| 128 |
{OUT=>"x y z count(y)\nA 1 10 5\nB 5 10 3\nC 8 11 4\n"}, |
| 129 |
{ERR=>"$full_deprecation"}], |
| 130 |
|
| 131 |
# Output Header with --full |
| 132 |
['hdr5dep', '-t" " -g 1 --full --header-out count 2', {IN_PIPE=>$in_g3}, |
| 133 |
{OUT=>"field-1 field-2 field-3 count(field-2)\n" . |
| 134 |
"A 3 W 5\nB 17 Y 2\nC 23 Z 1\n"}, |
| 135 |
{ERR=>"$full_deprecation"}], |
| 136 |
|
| 137 |
# Input has only one header line (no data lines), and the user requested |
| 138 |
# header-in and header-out => header line should be printed |
| 139 |
['hdr15dep', '-t: --full -H sum 1', {IN_PIPE=>$in_hdr_only}, |
| 140 |
{OUT=>"X:Y:Z:sum(X)\n"}, |
| 141 |
{ERR=>"$full_deprecation"}], |
| 142 |
['hdr17dep', '-t: --full -s -g1 -H sum 2', {IN_PIPE=>$in_hdr_only}, |
| 143 |
{OUT=>"X:Y:Z:sum(Y)\n"}, |
| 144 |
{ERR=>"$full_deprecation"}], |
| 145 |
|
| 146 |
# Test single line per group |
| 147 |
['sl2dep', '-t" " --full -g 1 mean 2', {IN_PIPE=>$in_g4}, |
| 148 |
{OUT=>"A 5 5\nK 6 6\nP 2 2\n"}, |
| 149 |
{ERR=>"$full_deprecation"}], |
| 150 |
); |
| 151 |
|
| 152 |
my $save_temps = $ENV{SAVE_TEMPS}; |
| 153 |
my $verbose = $ENV{VERBOSE}; |
| 154 |
|
| 155 |
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); |
| 156 |
exit $fail; |