Debian Codegraph @

tests/datamash-sort-header-deprecated.pl

raw
1 #!/usr/bin/env perl
2 =pod
3 Unit Tests for GNU Datamash - perform simple calculation on input data
4
5 Copyright (C) 2022-2025 Timothy Rice <[email protected]>
6 Copyright (C) 2013-2021 Assaf Gordon <[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 and Tim Rice.
24 =cut
25 use strict;
26 use warnings;
27 use List::Util qw/max/;
28 use Data::Dumper;
29
30 # Until a better way comes along to auto-use Coreutils Perl modules
31 # as in the coreutils' autotools system.
32 use Coreutils;
33 use CuSkip;
34 use CuTmpdir qw(datamash);
35
36 (my $program_name = $0) =~ s|.*/||;
37 my $prog_bin = 'datamash';
38
39 ## Cross-Compiling portability hack:
40 ## under qemu/binfmt, argv[0] (which is used to report errors) will contain
41 ## the full path of the binary, if the binary is on the $PATH.
42 ## So we try to detect what is the actual returned value of the program
43 ## in case of an error.
44 my $prog = `$prog_bin --foobar 2>&1 | head -n 1 | cut -f1 -d:`;
45 chomp $prog if $prog;
46 $prog = $prog_bin unless $prog;
47
48 # Turn off localization of executable's output.
49 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
50
51 # An unsorted input with a header line
52 my $INFILE=<<'EOF';
53 x y z
54 A % 1
55 B ( 2
56 A & 3
57 B = 4
58 EOF
59
60 my @INFILE_lines = split /\n/, $INFILE, -1;
61 my $INFILE_NO_HEADER = join("\n", @INFILE_lines[1..$#INFILE_lines]);
62
63 my $exp_sort_in_header_full=<<'EOF';
64 A % 1 1,3
65 B ( 2 2,4
66 EOF
67
68 my $exp_sort_out_header_full=<<'EOF';
69 field-1 field-2 field-3 unique(field-3)
70 A % 1 1,3
71 B ( 2 2,4
72 EOF
73
74 my $exp_sort_headers_full=<<'EOF';
75 x y z unique(z)
76 A % 1 1,3
77 B ( 2 2,4
78 EOF
79
80 my $deprecation_notice="$prog: Using -f/--full with non-linewise " .
81 "operations is deprecated and will be disabled " .
82 "in a future release.\n";
83
84 my @Tests =
85 (
86 ['shdep01', '-t " " --sort --full --header-out -g 1 unique 3',
87 {IN_PIPE=>$INFILE_NO_HEADER}, {OUT=>$exp_sort_out_header_full},
88 {ERR=>$deprecation_notice}],
89 ['shdep02', '-t " " -g 1 --sort --full --header-in unique 3',
90 {IN_PIPE=>$INFILE}, {OUT=>$exp_sort_in_header_full},
91 {ERR=>$deprecation_notice}],
92 ['shdep03', '-t " " -g 1 --sort --full --headers unique 3',
93 {IN_PIPE=>$INFILE}, {OUT=>$exp_sort_headers_full},
94 {ERR=>$deprecation_notice}],
95
96 # Check sort-piping with empty input - should always produce empty output
97 ['shdep04', '-t " " --sort --full unique 3',
98 {IN_PIPE=>""}, {OUT=>""},
99 {ERR=>$deprecation_notice}],
100 );
101
102 my $save_temps = $ENV{SAVE_TEMPS};
103 my $verbose = $ENV{VERBOSE};
104
105 my $fail = run_tests ($program_name, $prog_bin, \@Tests, $save_temps, $verbose);
106 exit $fail;