From 7a801451ba460b5a09a7b230bec8488ebca082ab Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Wed, 12 Feb 2014 21:03:03 +0100 Subject: test/run: Reorder functions Move functions before they are called, so that we no longer need forward declarations. --- quilt.changes | 1 + test/run | 257 ++++++++++++++++++++++++++++------------------------------ 2 files changed, 126 insertions(+), 132 deletions(-) diff --git a/quilt.changes b/quilt.changes index 41e9a0a..30cee3c 100644 --- a/quilt.changes +++ b/quilt.changes @@ -5,6 +5,7 @@ Wed Feb 12 20:57:35 CET 2014 - jdelvare@suse.de - test/run: Use perl module Text::ParseWords. - test/run: Drop support for su and sg. - test/run: Massive reindentation. +- test/run: Reorder functions. ------------------------------------------------------------------- Mon Feb 10 13:07:14 CET 2014 - jdelvare@suse.de diff --git a/test/run b/test/run index 9b7e998..1471bce 100755 --- a/test/run +++ b/test/run @@ -60,13 +60,6 @@ if (isatty(fileno(STDOUT))) { $FAILED = "\033[31m\033[1m" . $FAILED . "\033[m"; } -sub exec_test($$); -sub process_test($$$$); -sub print_header($); -sub print_body($); -sub print_footer($); -sub flush_output($); - my $prog; my ($in, $out) = ([], []); my $prog_line = 0; @@ -77,117 +70,49 @@ my $width = ($ENV{COLUMNS} || 80) >> 1; my $origdir = getcwd; my $workdir = "d.$$"; -# Create a dedicated working directory -mkdir $workdir or die; -chdir $workdir or die; -$ENV{PWD} = getcwd; - -if (defined $ARGV[0]) { - open(SOURCE, "$origdir/$ARGV[0]"); - print_header "[$ARGV[0]]\n"; -} else { - *SOURCE = *STDIN; -} - -# Substitute %{VAR} with environment variables -sub substitute_vars($) +sub print_header($) { - my ($line) = @_; - $line =~ s[%{(\w+)}][defined $ENV{$1} ? $ENV{$1} : ""]eg; - return $line; -} - -while (defined(my $line = )) { - $lineno++; - - # Collect input and output for the previous command - if ($line =~ s/^\s*< ?//) { - push @$in, substitute_vars($line); - next; - } - if ($line =~ s/^\s*> ?//) { - push @$out, substitute_vars($line); - next; - } - - # We have all input and output, we can execute the command - if (defined $prog) { - $last_status = process_test($prog, $prog_line, $in, $out); - $prog = undef; - last if $prog_line >= $opt_l; + if ($opt_q) { + $output{header} = $_[0]; + } else { + print $_[0]; } +} - # Parse the next command - if ($line =~ s/^\s*\$ ?//) { - # Substitute %{?} with the last command's status - $line =~ s[%{\?}][$last_status]eg; - - chomp($prog = substitute_vars($line)); - $prog_line = $lineno; - $in = []; - $out = []; +sub print_body($) +{ + if ($opt_q) { + $output{body} .= $_[0]; + } else { + print $_[0]; } } -# Execute last command if needed -process_test($prog, $prog_line, $in, $out) if defined $prog; - -close(SOURCE); -# Clean up the mess -chdir $origdir or die; -system "rm -rf $workdir"; - -my $status = sprintf("%d commands (%d passed, %d failed)", - $tests, $tests-$failed, $failed); -if (isatty(fileno(STDOUT))) { - if ($failed) { - $status = "\033[31m\033[1m" . $status . "\033[m"; +sub print_footer($) +{ + if ($opt_q) { + $output{footer} .= $_[0]; } else { - $status = "\033[32m" . $status . "\033[m"; + print $_[0]; } } -print_footer "$status\n"; -flush_output $failed; -exit $failed ? 1 : 0; - -sub process_test($$$$) { - my ($prog, $prog_line, $in, $out) = @_; - print_body "[$prog_line] \$ $prog -- "; - my ($exec_status, $result) = exec_test($prog, $in); - my @good = (); - my $nmax = (@$out > @$result) ? @$out : @$result; - for (my $n = 0; $n < $nmax; $n++) { - my $use_re; - if (defined $out->[$n] && $out->[$n] =~ /^~ /) { - $use_re = 1; - $out->[$n] =~ s/^~ //g; - } +sub flush_output($) +{ + my $failed = shift; + return unless $opt_q; - if (!defined($out->[$n]) || !defined($result->[$n]) || - (!$use_re && $result->[$n] ne $out->[$n]) || - ( $use_re && $result->[$n] !~ /^$out->[$n]/)) { - push @good, ($use_re ? '!~' : '!='); - } else { - push @good, ($use_re ? '=~' : '=='); - } - } - my $good = !grep(/!/, @good); - $tests++; - $failed++ unless $good; - print_body(($good ? $OK : $FAILED)."\n"); - if (!$good || $opt_v) { - for (my $n = 0; $n < $nmax; $n++) { - my $l = defined($out->[$n]) ? $out->[$n] : "~"; - chomp $l; - my $r = defined($result->[$n]) ? $result->[$n] : "~"; - chomp $r; - print_body sprintf("%-" . ($width - 3) . "s %s %s\n", - $r, $good[$n], $l); - } - } + print $output{header} || "", + $failed ? $output{body} : "", + $output{footer} || ""; +} - return $exec_status; +# Substitute %{VAR} with environment variables +sub substitute_vars($) +{ + my ($line) = @_; + $line =~ s[%{(\w+)}][defined $ENV{$1} ? $ENV{$1} : ""]eg; + return $line; } sub exec_test($$) { @@ -288,39 +213,107 @@ sub exec_test($$) { } } -sub print_header($) -{ - if ($opt_q) { - $output{header} = $_[0]; - } else { - print $_[0]; +sub process_test($$$$) { + my ($prog, $prog_line, $in, $out) = @_; + + print_body "[$prog_line] \$ $prog -- "; + my ($exec_status, $result) = exec_test($prog, $in); + my @good = (); + my $nmax = (@$out > @$result) ? @$out : @$result; + for (my $n = 0; $n < $nmax; $n++) { + my $use_re; + if (defined $out->[$n] && $out->[$n] =~ /^~ /) { + $use_re = 1; + $out->[$n] =~ s/^~ //g; + } + + if (!defined($out->[$n]) || !defined($result->[$n]) || + (!$use_re && $result->[$n] ne $out->[$n]) || + ( $use_re && $result->[$n] !~ /^$out->[$n]/)) { + push @good, ($use_re ? '!~' : '!='); + } else { + push @good, ($use_re ? '=~' : '=='); + } + } + my $good = !grep(/!/, @good); + $tests++; + $failed++ unless $good; + print_body(($good ? $OK : $FAILED)."\n"); + if (!$good || $opt_v) { + for (my $n = 0; $n < $nmax; $n++) { + my $l = defined($out->[$n]) ? $out->[$n] : "~"; + chomp $l; + my $r = defined($result->[$n]) ? $result->[$n] : "~"; + chomp $r; + print_body sprintf("%-" . ($width - 3) . "s %s %s\n", + $r, $good[$n], $l); + } } + + return $exec_status; } -sub print_body($) -{ - if ($opt_q) { - $output{body} .= $_[0]; - } else { - print $_[0]; - } +# Create a dedicated working directory +mkdir $workdir or die; +chdir $workdir or die; +$ENV{PWD} = getcwd; + +if (defined $ARGV[0]) { + open(SOURCE, "$origdir/$ARGV[0]"); + print_header "[$ARGV[0]]\n"; +} else { + *SOURCE = *STDIN; } -sub print_footer($) -{ - if ($opt_q) { - $output{footer} .= $_[0]; - } else { - print $_[0]; +while (defined(my $line = )) { + $lineno++; + + # Collect input and output for the previous command + if ($line =~ s/^\s*< ?//) { + push @$in, substitute_vars($line); + next; + } + if ($line =~ s/^\s*> ?//) { + push @$out, substitute_vars($line); + next; + } + + # We have all input and output, we can execute the command + if (defined $prog) { + $last_status = process_test($prog, $prog_line, $in, $out); + $prog = undef; + last if $prog_line >= $opt_l; + } + + # Parse the next command + if ($line =~ s/^\s*\$ ?//) { + # Substitute %{?} with the last command's status + $line =~ s[%{\?}][$last_status]eg; + + chomp($prog = substitute_vars($line)); + $prog_line = $lineno; + $in = []; + $out = []; } } +# Execute last command if needed +process_test($prog, $prog_line, $in, $out) if defined $prog; -sub flush_output($) -{ - my $failed = shift; - return unless $opt_q; +close(SOURCE); - print $output{header} || "", - $failed ? $output{body} : "", - $output{footer} || ""; +# Clean up the mess +chdir $origdir or die; +system "rm -rf $workdir"; + +my $status = sprintf("%d commands (%d passed, %d failed)", + $tests, $tests - $failed, $failed); +if (isatty(fileno(STDOUT))) { + if ($failed) { + $status = "\033[31m\033[1m" . $status . "\033[m"; + } else { + $status = "\033[32m" . $status . "\033[m"; + } } +print_footer "$status\n"; +flush_output $failed; +exit $failed ? 1 : 0; -- cgit