autotest.pl 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/perl -w
  2. ## Author: Alois Mahdal at zxcvb cz
  3. # my private testing helper script
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. use v5.10;
  15. use warnings;
  16. use File::Spec;
  17. my $PLATFORM = (-d "c:\\windows" ? 'windows' : 'unix' );
  18. my $DELAY = 5;
  19. my $COLORS = { windows => { ok => 'color 0a', nok => 'color 0c' } };
  20. my $DUMPS = "dumps";
  21. my $REPEAT = 1;
  22. my $TESTS = "t";
  23. sub clear {
  24. if ($PLATFORM eq 'windows') {
  25. system "cls";
  26. } elsif ($PLATFORM eq 'unix') {
  27. system "clear";
  28. }
  29. }
  30. sub color {
  31. $mood = shift;
  32. $cmd = $COLORS->{$PLATFORM}->{$mood};
  33. system ($cmd) if $cmd;
  34. }
  35. sub cat {
  36. my $name = File::Spec->splitpath( shift );
  37. printf "$name:\n";
  38. if (open my $fh, "<", $_) {
  39. my @lines = <$fh>;
  40. print @lines, "\n";
  41. close $fh or die "cannot close $_"
  42. }
  43. else {
  44. warn "cannot open $_";
  45. next STDERR;
  46. }
  47. }
  48. sub run_tests {
  49. my $ret = 0;
  50. printf "===== STDOUT =====\n";
  51. foreach (@_) {
  52. my $name = File::Spec->splitpath( $_ );
  53. say "$name:";
  54. $cmd = sprintf 'perl %s 2>%s',
  55. File::Spec->catdir( $TESTS, $name ),
  56. File::Spec->catdir( $DUMPS, $name . '.err' );
  57. system($cmd);
  58. $ret = $? >> 8;
  59. if ($ret) {
  60. print "last test failed with exit code $ret!\n";
  61. return;
  62. }
  63. }
  64. return 1;
  65. }
  66. sub freakout {
  67. my $ret = shift;
  68. color "nok";
  69. my @paths = glob "$DUMPS/*.err";
  70. printf "\n===== STDERR =====\n";
  71. STDERR: foreach (@paths) {
  72. cat $_ and unlink $_ or warn "could not print $_";
  73. }
  74. }
  75. mkdir $DUMPS unless -d $DUMPS;
  76. do {
  77. my @paths = glob "$TESTS/*.t";
  78. &clear;
  79. color "ok";
  80. my $ok = &run_tests(@paths);
  81. if ($ok) {
  82. say "------------------";
  83. say "All tests finished.";
  84. } else {
  85. &freakout();
  86. }
  87. sleep $DELAY if $REPEAT;
  88. } while ($REPEAT);