1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
#!/usr/bin/env perl
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2014-2018, Intel Corporation
#
#
# match -- compare an output file with expected results
#
# usage: match [-adoqv] [match-file]...
#
# this script compares the output from a test run, stored in a file, with
# the expected output. comparison is done line-by-line until either all
# lines compare correctly (exit code 0) or a miscompare is found (exit
# code nonzero).
#
# expected output is stored in a ".match" file, which contains a copy of
# the expected output with embedded tokens for things that should not be
# exact matches. the supported tokens are:
#
# $(N) an integer (i.e. one or more decimal digits)
# $(NC) one or more decimal digits with comma separators
# $(FP) a floating point number
# $(S) ascii string
# $(X) hex number
# $(XX) hex number prefixed with 0x
# $(W) whitespace
# $(nW) non-whitespace
# $(*) any string
# $(DD) output of a "dd" run
# $(OPT) line is optional (may be missing, matched if found)
# $(OPX) ends a contiguous list of $(OPT)...$(OPX) lines, at least
# one of which must match
# ${string1|string2} string1 OR string2
#
# Additionally, if any "X.ignore" file exists, strings or phrases found per
# line in the file will be ignored if found as a substring in the
# corresponding output file (making it easy to skip entire output lines).
#
# arguments are:
#
# -a find all files of the form "X.match" in the current
# directory and match them again the corresponding file "X".
#
# -o custom output filename - only one match file can be given
#
# -d debug -- show lots of debug output
#
# -q don't print log files on mismatch
#
# -v verbose -- show every line as it is being matched
#
use strict;
use Getopt::Std;
use Encode;
use v5.16;
select STDERR;
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
my $Me = $0;
$Me =~ s,.*/,,;
our ($opt_a, $opt_d, $opt_q, $opt_v, $opt_o);
$SIG{HUP} = $SIG{INT} = $SIG{TERM} = $SIG{__DIE__} = sub {
die @_ if $^S;
my $errstr = shift;
die "FAIL: $Me: $errstr";
};
sub usage {
my $msg = shift;
warn "$Me: $msg\n" if $msg;
warn "Usage: $Me [-adqv] [match-file]...\n";
warn " or: $Me [-dqv] -o output-file match-file...\n";
exit 1;
}
getopts('adoqv') or usage;
my %match2file;
if ($opt_a) {
usage("-a and filename arguments are mutually exclusive")
if $#ARGV != -1;
opendir(DIR, '.') or die "opendir: .: $!\n";
my @matchfiles = grep { /(.*)\.match$/ && -f $1 } readdir(DIR);
closedir(DIR);
die "no files found to process\n" unless @matchfiles;
foreach my $mfile (@matchfiles) {
die "$mfile: $!\n" unless open(F, $mfile);
close(F);
my $ofile = $mfile;
$ofile =~ s/\.match$//;
die "$mfile found but cannot open $ofile: $!\n"
unless open(F, $ofile);
close(F);
$match2file{$mfile} = $ofile;
}
} elsif ($opt_o) {
usage("-o argument requires two paths") if $#ARGV != 1;
$match2file{$ARGV[1]} = $ARGV[0];
} else {
usage("no match-file arguments found") if $#ARGV == -1;
# to improve the failure case, check all filename args exist and
# are provided in pairs now, before going through and processing them
foreach my $mfile (@ARGV) {
my $ofile = $mfile;
usage("$mfile: not a .match file") unless
$ofile =~ s/\.match$//;
usage("$mfile: $!") unless open(F, $mfile);
close(F);
usage("$ofile: $!") unless open(F, $ofile);
close(F);
$match2file{$mfile} = $ofile;
}
}
my $mfile;
my $ofile;
my $ifile;
print "Files to be processed:\n" if $opt_v;
foreach $mfile (sort keys %match2file) {
$ofile = $match2file{$mfile};
$ifile = $ofile . ".ignore";
$ifile = undef unless (-f $ifile);
if ($opt_v) {
print " match-file \"$mfile\" output-file \"$ofile\"";
if ($ifile) {
print " ignore-file $ifile\n";
} else {
print "\n";
}
}
match($mfile, $ofile, $ifile);
}
exit 0;
#
# strip_it - user can optionally ignore lines from files that contain
# any number of substrings listed in a file called "X.ignore" where X
# is the name of the output file.
#
sub strip_it {
my ($ifile, $file, $input) = @_;
# if there is no ignore file just return unaltered input
return $input unless $ifile;
my @lines_in = split /^/, $input;
my $output;
my $line_in;
my @i_file = split /^/, snarf($ifile);
my $i_line;
my $ignore_it = 0;
foreach $line_in (@lines_in) {
my @i_lines = @i_file;
foreach $i_line (@i_lines) {
chop($i_line);
if (index($line_in, $i_line) != -1) {
$ignore_it = 1;
if ($opt_v) {
print "Ignoring (from $file): $line_in";
}
}
}
if ($ignore_it == 0) {
$output .= $line_in;
}
$ignore_it = 0;
}
return $output;
}
#
# match -- process a match-file, output-file pair
#
sub match {
my ($mfile, $ofile, $ifile) = @_;
my $pat;
my $output = snarf($ofile);
$output = strip_it($ifile, $ofile, $output);
my $all_lines = $output;
my $line_pat = 0;
my $line_out = 0;
my $opt = 0;
my $opx = 0;
my $opt_found = 0;
my $fstr = snarf($mfile);
$fstr = strip_it($ifile, $mfile, $fstr);
for (split /^/, $fstr) {
$pat = $_;
$line_pat++;
$line_out++;
s/([*+?|{}.\\^\$\[()])/\\$1/g;
s/\\\$\\\(FP\\\)/[-+]?\\d*\\.?\\d+([eE][-+]?\\d+)?/g;
s/\\\$\\\(N\\\)/[-+]?\\d+/g;
s/\\\$\\\(NC\\\)/[-+]?\\d+(,[0-9]+)*/g;
s/\\\$\\\(\\\*\\\)/\\p{Print}*/g;
s/\\\$\\\(S\\\)/\\P{IsC}+/g;
s/\\\$\\\(X\\\)/\\p{XPosixXDigit}+/g;
s/\\\$\\\(XX\\\)/0x\\p{XPosixXDigit}+/g;
s/\\\$\\\(W\\\)/\\p{Blank}*/g;
s/\\\$\\\(nW\\\)/\\p{Graph}*/g;
s/\\\$\\\{([^|]*)\\\|([^|]*)\\\}/($1|$2)/g;
s/\\\$\\\(DD\\\)/\\d+\\+\\d+ records in\n\\d+\\+\\d+ records out\n\\d+ bytes \\\(\\d+ .B\\\) copied, [.0-9e-]+[^,]*, [.0-9]+ .B.s/g;
if (s/\\\$\\\(OPT\\\)//) {
$opt = 1;
} elsif (s/\\\$\\\(OPX\\\)//) {
$opx = 1;
} else {
$opt_found = 0;
}
if ($opt_v) {
my @lines = split /\n/, $output;
my $line;
if (@lines) {
$line = $lines[0];
} else {
$line = "[EOF]";
}
printf("%s:%-3d %s%s:%-3d %s\n", $mfile, $line_pat, $pat, $ofile, $line_out, $line);
}
print " => /$_/\n" if $opt_d;
print " [$output]\n" if $opt_d;
unless ($output =~ s/^$_//) {
if ($opt || ($opx && $opt_found)) {
printf("%s:%-3d [skipping optional line]\n", $ofile, $line_out) if $opt_v;
$line_out--;
$opt = 0;
} else {
if (!$opt_v) {
if ($opt_q) {
print "[MATCHING FAILED]\n";
} else {
print "[MATCHING FAILED, COMPLETE FILE ($ofile) BELOW]\n$all_lines\n[EOF]\n";
}
$opt_v = 1;
match($mfile, $ofile);
}
die "$mfile:$line_pat did not match pattern\n";
}
} elsif ($opt) {
$opt_found = 1;
}
$opx = 0;
}
if ($output ne '') {
if (!$opt_v) {
if ($opt_q) {
print "[MATCHING FAILED]\n";
} else {
print "[MATCHING FAILED, COMPLETE FILE ($ofile) BELOW]\n$all_lines\n[EOF]\n";
}
}
# make it a little more print-friendly...
$output =~ s/\n/\\n/g;
die "line $line_pat: unexpected output: \"$output\"\n";
}
}
#
# snarf -- slurp an entire file into memory
#
sub snarf {
my ($file) = @_;
my $fh;
open($fh, '<', $file) or die "$file $!\n";
local $/;
$_ = <$fh>;
close $fh;
# check known encodings or die
my $decoded;
my @encodings = ("UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE");
foreach my $enc (@encodings) {
eval { $decoded = decode( $enc, $_, Encode::FB_CROAK ) };
if (!$@) {
$decoded =~ s/\R/\n/g;
return $decoded;
}
}
die "$Me: ERROR: Unknown file encoding";
}
|