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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
#!/usr/bin/env perl
use strict;
use warnings;
#use Smart::Comments;
use Getopt::Std;
use PPI;
use IPC::Run3;
use File::Path;
use File::Slurp;
use List::MoreUtils qw( any );
my @dummy_comments = (
'# Create the answer to what should be produced by this Makefile',
'# The Contents of the MAKEFILE ...',
'# COMPARE RESULTS',
'# END of Contents of MAKEFILE',
);
my %opts;
getopts('o:', \%opts);
die "No output directory specified\n"
unless $opts{o};
my $outdir = $opts{o};
eval { mkpath($outdir) };
if ($@) {
print "Couldn't create $outdir: $@";
}
my @files = map glob, @ARGV;
die "No input file specified.\n" unless @files;
for my $infile (@files) {
next if -d $infile or $infile =~ /(\.swp|~)$/;
my $p4 = read_file($infile);
$p4 =~ s/<<\s*\\EOF\b/<<'EOF'/smg;
my $doc = PPI::Document->new(\$p4);
my @matched;
for my $elem ($doc->elements) {
my $content = $elem->content;
if ($elem->class =~ /Comment$/) {
chomp($content);
next if (any { $_ eq $content } @dummy_comments) or
$content =~ /-\*-perl-\*-/i or
$content =~ /^\s*#\s*$/ or
$content =~ /^\s*#\s*-+\s*$/;
push @matched, $content;
}
}
my $body = process_comments($p4, \@matched);
my $pattern = <<'_EOC_';
if\s+\(((?:!\s*)?-[a-z])\s+(\S+)\)\s+\{
\s+\$test_passed = (\d+);
\}
_EOC_
my $count = $body =~ s/$pattern/\&X::file_test('$1', $2, $3);\n/g;
$pattern = <<'_EOC_';
if \(\(-f (\S+)\)\|\|\(-f (\S+)\)\|\|\(-f (\S+)\)\|\|\(-f (\S+)\)\) \{
\s+\$test_passed = 0;
\}
_EOC_
$count += 4 * ($body =~ s/$pattern/
\&X::file_test('-f', $1, 0);
\&X::file_test('-f', $2, 0);
\&X::file_test('-f', $3, 0);
\&X::file_test('-f', $4, 0);/g);
if ($body =~ /\$test_passed\b/) {
warn "WARNING: \$test_passed involved...\n";
}
warn "info: $count file test(s) found\n" if $count;
write_file('tmp.pl', preamble(), $body);
my $stdout;
run3 [$^X, 'tmp.pl'], undef, \$stdout, undef;
#print $stdout;
if ($infile =~ /([^\/\\]+)$/) {
my $base = $1;
my $outfile = "$outdir/$base.t";
warn "Generating $outfile...\n";
write_file($outfile, $stdout);
}
}
sub process_comments {
my $p4 = shift;
my $matched = shift;
for my $match (@$matched) {
(my $value = $match) =~ s/^#\s+//g;
(my $quoted = $value) =~ s/\\/\\\\/g;
$quoted =~ s/'/\\'/g;
if ($p4 =~ s/\G(.*?)\Q$match\E/${1}\&X::comment('$quoted');/ms) {
} else {
die "Can't find matched comment '$match' in the source";
}
}
$p4;
}
sub preamble {
return <<'_EOC_';
use strict;
use warnings;
package X;
use File::Slurp;
our ($block, @blocks, %utouch);
our $count = 0;
our $extra_tests = 0;
sub file_test ($$$) {
my ($op, $file, $passed) = @_;
if ($op eq '-f') {
if ($passed) {
$X::block->{found} .= " $file";
} else {
$X::block->{not_found} .= " $file";
}
$extra_tests++;
} else {
die "Not supported yet: $op $file $passed";
}
}
sub comment ($) {
my $cmt = shift;
if ($cmt =~ /^TEST\s+#?\d+\b/i) {
$X::block->{name} = $cmt;
} else {
$X::block->{description} .= "$cmt\n"
}
}
package main;
#use Smart::Comments;
use subs qw(unlink cwd);
use Test::MockClass qw(Cwd);
my $makefile = 'test.mk';
my $mkpath = '#MAKE#';
my $make_path = 'make';
my $workdir = '.';
my $pathsep = '/';
my $description = '';
my $details = '';
my ($answer, $example);
my $delete_command = 'rm';
my $rm_command = 'rm';
my $has_POSIX = eval { require "POSIX.pm" };
my $parallel_jobs = 1;
my $make_name = '#MAKE#';
my $port_type = ($^O eq 'MSWin32' || $^O eq 'Cygwin') ? 'MSWin32' : 'UNIX';
my %extraENV = ();
my $pwd = '#PWD#';
# local vars used by the test scripts
my (@touchedfiles, $VP, $cleanit_error, $delete_error_code);
$delete_error_code = 2;
my $test_passed;
my $vos = 0;
sub cwd {
#die "Called!";
"#PWD#";
}
sub resetENV () {
%extraENV = ();
}
sub get_tmpfile {
$makefile
}
sub unlink {
#die "unlink called!";
rmfiles(@_);
}
sub rmfiles {
for my $file (@_) {
if (!exists $utouch{$file}) {
warn "WARNING: removing file $file which is not touched before\n";
} else {
delete $utouch{$file};
}
}
}
sub utouch ($@) {
my $time = shift;
for my $file (@_) {
$X::utouch{$file} = $time;
}
}
sub touch ($@) {
utouch(0, @_);
}
sub get_logfile {
1;
}
sub run_make_with_options ($$$$) {
my $infile = shift;
$X::block->{filename} = $infile;
$X::block->{options} = shift;
shift;
$X::block->{error_code} = shift;
$X::block->{source} = X::read_file($infile) if $infile;
$X::block->{utouch} = {%X::utouch};
$X::block->{env} = {%extraENV};
}
sub compare_output ($$) {
$X::block->{stdout} = shift;
push @X::blocks, $X::block;
$X::block = {};
}
sub run_make_test ($$$@) {
my $source = shift;
if (!defined $source) {
$source = $X::prev_src;
} else {
$X::prev_src = $source;
}
$X::block->{source} = $source;
$X::block->{options} = shift;
$X::block->{stdout} = shift;
my $error_code = shift;
$error_code = 0 if !defined $error_code;
$X::block->{error_code} = $error_code;
$X::block->{utouch} = {%X::utouch};
$X::block->{env} = {%extraENV};
push @X::blocks, $X::block;
$X::block = {};
}
END {
### @X::blocks;
package X;
my $use_ditto = '';
my @groups;
my $i;
my $prev_source;
my $leading_empty_lines;
for my $block (@blocks) {
$i++;
# === TEST $name
# $description
my $str = "=== " . ($block->{name} || "TEST $i:") . "\n";
$str .= $block->{description} . "\n"
if $block->{description};
# --- source
my $source = $block->{source};
if (defined $source) {
if (defined $prev_source and $source eq $prev_source) {
$use_ditto = "\nuse_source_ditto;\n";
$str .= "--- source ditto\n";
} else {
if ($source =~ /^\n+/s) {
$leading_empty_lines = length($&);
#die "LEADING: $leading_empty_lines";
}
my $opt = '';
if ($source =~ /#[A-Z]+#/) {
$opt = ' preprocess';
}
$str .= "--- source$opt\n" . $source . "\n";
}
}
$prev_source = $source;
# --- pre: $ExtraENV{$var} = $value
my %env = %{ $block->{env} };
if (%env) {
my @ln;
while (my ($k, $v) = each %env) {
$k =~ s/\\/\\\\/g;
$k =~ s/'/\\'/g;
$v =~ s/\\/\\\\/g;
$v =~ s/'/\\'/g;
push @ln, qq[\$::ExtraENV{'$k'} = '$v'];
}
if (@ln > 1) {
$str .= "\n--- pre\n" . join(";\n", @ln) . ";\n";
} else {
$str .= "\n--- pre: @ln;\n";
}
}
# --- touch
# --- utouch
my (@touch, @utouch);
my %utouch = %{ $block->{utouch} };
while (my ($file, $time) = each %utouch) {
if ($time == 0) {
push @touch, $file;
} else {
push @utouch, "$time $file";
}
}
if (@touch) {
$str .= "--- touch: " . join(" ", @touch) . "\n";
}
if (@utouch == 1) {
$str .= "--- utouch: $utouch[0]\n";
} elsif (@utouch > 1) {
$str .= "--- utouch\n" . join("\n", @utouch) . "\n";
}
# --- options
# --- goals
my $options = $block->{options};
#die $options if $options =~ /other/;
$options =~ s/\\(.)/$1/g;
if (defined $options and $options ne '') {
$options =~ s/^\s+|\s+$//g;
if ($options =~ /(?:\w+|\S+=\S+|-[\w-]+|\s+)+/) {
my @args = split /\s+/, $options;
my @goals = grep { /^\w+$/ } @args;
$options = join ' ', grep { !/^\w+$/ } @args;
if (@goals) {
$str .= "--- goals: @goals\n";
}
}
$options =~ s/\n+/ /;
my $opt = '';
if ($options =~ /#[A-Z]+#/) {
$opt = ' preprocess';
}
$str .= "--- options$opt: $options\n"
if $options !~ /^\s*$/;
}
# --- stdout
my $stdout = $block->{stdout};
my $stderr;
$stdout =~
s{^[^\n]*?(?:Error \d+|No such file or directory| Stop\.|warning)[^\n]*\n?}
{$stderr .= $&; ''}emsg;
if (defined $stdout and $stdout ne '') {
my $opt = '';
if ($stdout =~ /#[A-Z]+#/) {
$opt = ' preprocess';
}
if ($stdout =~ /^\s+$/s) {
$stdout =~ s/\n/\\n/g;
$str .= qq{--- stdout eval: "$stdout"\n};
} else {
$str .= "--- stdout$opt\n$stdout\n";
}
} else {
$str .= "--- stdout\n";
}
# --- stderr
#$stderr = $block->{stderr};
if (defined $stderr and $stderr ne '') {
if ($leading_empty_lines) {
$stderr =~ s/^(#MAKEFILE#:)(\d): /
my $n = $2 - $leading_empty_lines;
$n = 1 if $n < 1;
$1 . $n . ": "/esmg;
}
my $opt = '';
if ($stderr =~ /#[A-Z]+#/) {
$opt = ' preprocess';
}
$str .= "--- stderr$opt\n$stderr\n";
} else {
$str .= "--- stderr\n";
}
# --- error_code
my $error_code = $block->{error_code};
if (defined $error_code) {
$error_code >>= 8;
$str .= "--- error_code: $error_code\n";
} else {
$extra_tests--;
}
# --- not_found
my $not_found = $block->{not_found};
if (defined $not_found and $not_found !~ /^\s*$/) {
$str .= "--- not_found: $not_found\n";
}
# --- found
my $found = $block->{found};
if (defined $found and $found !~ /^\s*$/) {
$str .= "--- found: $found\n";
}
# --- filename
my $filename = $block->{filename};
#die $filename;
if (defined $filename && index($str, $filename) >= 0) {
$str =~ s/^--- /--- filename: $filename\n--- /ms;
}
push @groups, $str;
}
$details =~ s/^/# /gms;
$description =~ s/^/# /gms;
my $data = join "\n\n\n", @groups;
my $tests = '';
if ($extra_tests > 0) {
$tests = " + $extra_tests";
} elsif ($extra_tests < 0) {
$tests = " - " . -$extra_tests;
}
print <<_EOF_;
# Description:
$description
#
# Details:
$details
use t::Gmake;
plan tests => 3 * blocks()$tests;
$use_ditto
run_tests;
__DATA__
$data
_EOF_
}
no strict;
no warnings;
_EOC_
}
|