| 1 |
# Copyright 2022 Jeffrey Kegler |
| 2 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 3 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 4 |
# General Public License as published by the Free Software Foundation, |
| 5 |
# either version 3 of the License, or (at your option) any later version. |
| 6 |
# |
| 7 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 |
# Lesser General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU Lesser |
| 13 |
# General Public License along with Marpa::R2. If not, see |
| 14 |
# http://www.gnu.org/licenses/. |
| 15 |
|
| 16 |
package Marpa::R2::HTML::Test::Util; |
| 17 |
|
| 18 |
# This code was based on study of the test suite in Andy Lester's Ack package |
| 19 |
|
| 20 |
use 5.010001; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More; |
| 25 |
use English qw( -no_match_vars ); |
| 26 |
use File::Spec; |
| 27 |
use Fatal qw(unlink open close); |
| 28 |
use Carp; |
| 29 |
use CPAN::Version; |
| 30 |
use Cwd; |
| 31 |
|
| 32 |
sub is_win32 { |
| 33 |
return $OSNAME =~ /Win32/xms; |
| 34 |
} |
| 35 |
|
| 36 |
sub quotearg { |
| 37 |
my ($arg) = @_; |
| 38 |
return quotemeta $arg if not is_win32(); |
| 39 |
$arg =~ s/(\\+)$/$1$1/xms; # Double all trailing backslashes |
| 40 |
$arg =~ s/"/\\"/gxms; # Backslash all quotes |
| 41 |
return qq{"$arg"}; |
| 42 |
} ## end sub quotearg |
| 43 |
|
| 44 |
sub run_command { |
| 45 |
my (@args) = @_; |
| 46 |
|
| 47 |
my $blib = $ENV{MARPA_TEST_BLIB}; |
| 48 |
my $current_wd = Cwd::getcwd(); |
| 49 |
$blib //= $current_wd; |
| 50 |
my $blib_arg = '-Mblib=' . quotearg($blib); |
| 51 |
|
| 52 |
# my $command = join q{ }, $EXECUTABLE_NAME, $blib_arg, map { quotearg($_) } @args; |
| 53 |
my $command = join q{ }, $EXECUTABLE_NAME, map { quotearg($_) } @args; |
| 54 |
|
| 55 |
## no critic (InputOutput::ProhibitBacktickOperators) |
| 56 |
my $stdout = `$command`; |
| 57 |
## use critic |
| 58 |
|
| 59 |
my ( $sig, $core, $rc ) = ( |
| 60 |
( $CHILD_ERROR & 127 ), |
| 61 |
( $CHILD_ERROR & 128 ), |
| 62 |
( $CHILD_ERROR >> 8 ), |
| 63 |
); |
| 64 |
|
| 65 |
return $stdout; |
| 66 |
} ## end sub run_command |
| 67 |
|
| 68 |
# This method must be called *BEFORE* any test plan is |
| 69 |
# written -- it creates its own test plan |
| 70 |
sub load_or_skip_all { |
| 71 |
my ($module_name) = @_; |
| 72 |
## no critic(BuiltinFunctions::ProhibitStringyEval) |
| 73 |
my $eval_result = eval "require $module_name; '$module_name'->import; 1"; |
| 74 |
if ( !$eval_result ) { |
| 75 |
my $eval_error = $EVAL_ERROR; |
| 76 |
$eval_error =~ s/^/# /gxms; |
| 77 |
print "1..0 # Skip Could not load $module_name\n", $eval_error |
| 78 |
or Carp::croak("say failed: $ERRNO"); |
| 79 |
exit 0; |
| 80 |
} ## end if ( !$eval_result ) |
| 81 |
use lib 'config'; |
| 82 |
$eval_result = eval { require Marpa::R2::Config; 1 }; |
| 83 |
if ( !$eval_result ) { |
| 84 |
Test::More::plan tests => 1; |
| 85 |
Test::More::diag($EVAL_ERROR); |
| 86 |
Test::More::fail("Could not load Marpa::R2::Config\n"); |
| 87 |
exit 0; |
| 88 |
} ## end if ( !$eval_result ) |
| 89 |
my $version_wanted = $Marpa::R2::VERSION_FOR_CONFIG{$module_name}; |
| 90 |
if ( not defined $version_wanted ) { |
| 91 |
Test::More::plan tests => 1; |
| 92 |
Test::More::fail("$module_name is not known to Marpa::R2"); |
| 93 |
exit 0; |
| 94 |
} |
| 95 |
my $module_version = eval q{$} . $module_name . '::VERSION'; |
| 96 |
if ( CPAN::Version->vlt( $module_version, $version_wanted ) ) { |
| 97 |
say |
| 98 |
"1..0 # Skip $module_name version is $module_version; we wanted $version_wanted" |
| 99 |
or Carp::croak("say failed: $ERRNO"); |
| 100 |
exit 0; |
| 101 |
} ## end if ( vlt( $module_version, $version_wanted ) ) |
| 102 |
} ## end sub load_or_skip_all |
| 103 |
|
| 104 |
1; |
| 105 |
|
| 106 |
# vim: set expandtab shiftwidth=4: |