run_tests.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use warnings;
  10. # Recognise VERBOSE and V which is common on other projects.
  11. BEGIN {
  12. $ENV{HARNESS_VERBOSE} = "yes" if $ENV{VERBOSE} || $ENV{V};
  13. }
  14. use File::Spec::Functions qw/catdir catfile curdir abs2rel rel2abs/;
  15. use File::Basename;
  16. use FindBin;
  17. use lib "$FindBin::Bin/../util/perl";
  18. use OpenSSL::Glob;
  19. my $TAP_Harness = eval { require TAP::Harness } ? "TAP::Harness"
  20. : "OpenSSL::TAP::Harness";
  21. my $srctop = $ENV{SRCTOP} || $ENV{TOP};
  22. my $bldtop = $ENV{BLDTOP} || $ENV{TOP};
  23. my $recipesdir = catdir($srctop, "test", "recipes");
  24. my $libdir = rel2abs(catdir($srctop, "util", "perl"));
  25. my %tapargs =
  26. ( verbosity => $ENV{VERBOSE} || $ENV{V} || $ENV{HARNESS_VERBOSE} ? 1 : 0,
  27. lib => [ $libdir ],
  28. switches => '-w',
  29. merge => 1
  30. );
  31. my @alltests = find_matching_tests("*");
  32. my %tests = ();
  33. my $initial_arg = 1;
  34. foreach my $arg (@ARGV ? @ARGV : ('alltests')) {
  35. if ($arg eq 'list') {
  36. foreach (@alltests) {
  37. (my $x = basename($_)) =~ s|^[0-9][0-9]-(.*)\.t$|$1|;
  38. print $x,"\n";
  39. }
  40. exit 0;
  41. }
  42. if ($arg eq 'alltests') {
  43. warn "'alltests' encountered, ignoring everything before that...\n"
  44. unless $initial_arg;
  45. %tests = map { $_ => 1 } @alltests;
  46. } elsif ($arg =~ m/^(-?)(.*)/) {
  47. my $sign = $1;
  48. my $test = $2;
  49. my @matches = find_matching_tests($test);
  50. # If '-foo' is the first arg, it's short for 'alltests -foo'
  51. if ($sign eq '-' && $initial_arg) {
  52. %tests = map { $_ => 1 } @alltests;
  53. }
  54. if (scalar @matches == 0) {
  55. warn "Test $test found no match, skipping ",
  56. ($sign eq '-' ? "removal" : "addition"),
  57. "...\n";
  58. } else {
  59. foreach $test (@matches) {
  60. if ($sign eq '-') {
  61. delete $tests{$test};
  62. } else {
  63. $tests{$test} = 1;
  64. }
  65. }
  66. }
  67. } else {
  68. warn "I don't know what '$arg' is about, ignoring...\n";
  69. }
  70. $initial_arg = 0;
  71. }
  72. my $harness = $TAP_Harness->new(\%tapargs);
  73. my $ret = $harness->runtests(map { abs2rel($_, rel2abs(curdir())); }
  74. sort keys %tests);
  75. # $ret->has_errors may be any number, not just 0 or 1. On VMS, numbers
  76. # from 2 and on are used as is as VMS statuses, which has severity encoded
  77. # in the lower 3 bits. 0 and 1, on the other hand, generate SUCCESS and
  78. # FAILURE, so for currect reporting on all platforms, we make sure the only
  79. # exit codes are 0 and 1. Double-bang is the trick to do so.
  80. exit !!$ret->has_errors if (ref($ret) eq "TAP::Parser::Aggregator");
  81. # If this isn't a TAP::Parser::Aggregator, it's the pre-TAP test harness,
  82. # which simply dies at the end if any test failed, so we don't need to bother
  83. # with any exit code in that case.
  84. sub find_matching_tests {
  85. my ($glob) = @_;
  86. if ($glob =~ m|^[\d\[\]\?\-]+$|) {
  87. return glob(catfile($recipesdir,"$glob-*.t"));
  88. }
  89. return glob(catfile($recipesdir,"*-$glob.t"));
  90. }
  91. # Fake TAP::Harness in case it's not loaded
  92. use Test::Harness;
  93. package OpenSSL::TAP::Harness;
  94. sub new {
  95. my $class = shift;
  96. my %args = %{ shift() };
  97. return bless { %args }, $class;
  98. }
  99. sub runtests {
  100. my $self = shift;
  101. my @switches = ();
  102. if ($self->{switches}) {
  103. push @switches, $self->{switches};
  104. }
  105. if ($self->{lib}) {
  106. foreach (@{$self->{lib}}) {
  107. my $l = $_;
  108. # It seems that $switches is getting interpreted with 'eval' or
  109. # something like that, and that we need to take care of backslashes
  110. # or they will disappear along the way.
  111. $l =~ s|\\|\\\\|g if $^O eq "MSWin32";
  112. push @switches, "-I$l";
  113. }
  114. }
  115. $Test::Harness::switches = join(' ', @switches);
  116. Test::Harness::runtests(@_);
  117. }