From 29d92d40911aeb517c044d733fc652e7097294e8 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sat, 27 Nov 2021 14:03:44 -0500 Subject: [PATCH] tests: Enhance output to contain filename/line number info * tests/scripts/options/dash-I: Use string comparison operator. * tests/test_driver.pl (cmd2str): New method to convert a command line to a string with proper quoting. * tests/run_make_tests.pl (create_command): Call cmd2str() to format the command. Add the filename/line number to the output. --- tests/run_make_tests.pl | 15 +++++---------- tests/scripts/options/dash-I | 2 +- tests/test_driver.pl | 31 ++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/tests/run_make_tests.pl b/tests/run_make_tests.pl index d248ec97..cc46af1f 100644 --- a/tests/run_make_tests.pl +++ b/tests/run_make_tests.pl @@ -310,7 +310,7 @@ sub create_command { # Using a ref should be preferred as it's more portable but all the older # invocations use strings. sub run_make_with_options { - my ($filename,$options,$logname,$expected_code,$timeout,@call) = @_; + my ($filename, $options, $logname, $expected_code, $timeout, @call) = @_; @call = caller unless @call; my $code; my $command = create_command($options); @@ -364,13 +364,8 @@ sub run_make_with_options { $command = add_options($command, $options); } - my $cmdstr = ref($command) ? "'".join("' '", @$command)."'" : $command; - - if (@call) { - $command_string = "#$call[1]:$call[2]\n$cmdstr\n"; - } else { - $command_string = $cmdstr; - } + my $cmdstr = cmd2str($command); + $command_string = "# $call[1]:$call[2]:\n$cmdstr\n"; if ($valgrind) { print VALGRIND "\n\nExecuting: $cmdstr\n"; @@ -409,9 +404,9 @@ sub run_make_with_options { } if ($code != $expected_code) { - print "Error running @make_command (expected $expected_code; got $code): $cmdstr\n"; + print "Error running @make_command (expected $expected_code; got $code)\n$call[1]:$call[2]: $cmdstr\n"; $test_passed = 0; - &create_file (&get_runfile, $command_string); + &create_file(get_runfile(), $command_string); # If it's a SIGINT, stop here if ($code & 127) { print STDERR "\nCaught signal ".($code & 127)."!\n"; diff --git a/tests/scripts/options/dash-I b/tests/scripts/options/dash-I index e4646014..64ee7c58 100644 --- a/tests/scripts/options/dash-I +++ b/tests/scripts/options/dash-I @@ -92,7 +92,7 @@ unlink('defaultdirs.mk'); my $fn = undef; foreach my $dn (split ' ', $dirs) { # On Windows the default is "." which is bogus! - if ($dn != '.') { + if ($dn ne '.') { my @files = glob(File::Spec->catfile($dn, "*")); if (@files) { (undef, undef, $fn) = File::Spec->splitpath($files[0]); diff --git a/tests/test_driver.pl b/tests/test_driver.pl index ec17be1f..686f5239 100644 --- a/tests/test_driver.pl +++ b/tests/test_driver.pl @@ -148,6 +148,27 @@ sub resetENV } } +# Returns a string-ified version of cmd which is a value provided to exec() +# so it can either be a ref of a list or a string. +sub cmd2str +{ + my $cmd = $_[0]; + if (!ref($cmd)) { + return $cmd; + } + + my @c; + foreach (@$cmd) { + if (/[][#;"*?&|<>(){}\$`^~!]/) { + s/\'/\'\\'\'/g; + push @c, "'$_'"; + } else { + push @c, $_; + } + } + return join(' ', @c); +} + sub toplevel { # Pull in benign variables from the user's environment @@ -733,7 +754,7 @@ sub error sub compare_output { - my ($answer,$logfile) = @_; + my ($answer, $logfile) = @_; my ($slurp, $answer_matched) = ('', 0); ++$tests_run; @@ -899,15 +920,15 @@ sub compare_output if (! $answer_matched) { print "DIFFERENT OUTPUT\n" if $debug; - &create_file (&get_basefile, $answer); - &create_file (&get_runfile, $command_string); + &create_file(&get_basefile, $answer); + &create_file(&get_runfile, $command_string); print "\nCreating Difference File ...\n" if $debug; # Create the difference file my $command = "diff -c " . &get_basefile . " " . $logfile; - &run_command_with_output(&get_difffile,$command); + &run_command_with_output(get_difffile(), $command); } return 0; @@ -938,7 +959,7 @@ sub attach_default_output if ($vos) { my $code = system "++attach_default_output_hack $filename"; - $code == -2 or &error ("adoh death\n", 1); + $code == -2 or &error ("ado death\n", 1); return 1; }