File Coverage

html/tool/lib/Marpa/R2/HTML/Test/Util.pm
Criterion Covered Total %
statement 56 72 77.7
branch 5 14 35.7
condition 1 3 33.3
subroutine 15 15 100.0
pod 0 4 0.0
total 77 108 71.3


line stmt bran cond sub pod time code
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 11     11   755606 use 5.010001;
  11         152  
21 11     11   66 use strict;
  11         28  
  11         248  
22 11     11   52 use warnings;
  11         22  
  11         340  
23              
24 11     11   102 use Test::More;
  11         51  
  11         69  
25 11     11   2739 use English qw( -no_match_vars );
  11         19  
  11         113  
26 11     11   4751 use File::Spec;
  11         20  
  11         380  
27 11     11   4320 use Fatal qw(unlink open close);
  11         89186  
  11         75  
28 11     11   17994 use Carp;
  11         27  
  11         617  
29 11     11   5680 use CPAN::Version;
  11         13556  
  11         337  
30 11     11   69 use Cwd;
  11         21  
  11         4659  
31              
32             sub is_win32 {
33 65     65 0 648 return $OSNAME =~ /Win32/xms;
34             }
35              
36             sub quotearg {
37 65     65 0 132 my ($arg) = @_;
38 65 50       156 return quotemeta $arg if not is_win32();
39 0         0 $arg =~ s/(\\+)$/$1$1/xms; # Double all trailing backslashes
40 0         0 $arg =~ s/"/\\"/gxms; # Backslash all quotes
41 0         0 return qq{"$arg"};
42             } ## end sub quotearg
43              
44             sub run_command {
45 15     15 0 47887 my (@args) = @_;
46              
47 15         69 my $blib = $ENV{MARPA_TEST_BLIB};
48 15         219 my $current_wd = Cwd::getcwd();
49 15   33     243 $blib //= $current_wd;
50 15         73 my $blib_arg = '-Mblib=' . quotearg($blib);
51              
52             # my $command = join q{ }, $EXECUTABLE_NAME, $blib_arg, map { quotearg($_) } @args;
53 15         64 my $command = join q{ }, $EXECUTABLE_NAME, map { quotearg($_) } @args;
  50         120  
54              
55             ## no critic (InputOutput::ProhibitBacktickOperators)
56 15         4790717 my $stdout = `$command`;
57             ## use critic
58              
59 15         768 my ( $sig, $core, $rc ) = (
60             ( $CHILD_ERROR & 127 ),
61             ( $CHILD_ERROR & 128 ),
62             ( $CHILD_ERROR >> 8 ),
63             );
64              
65 15         1344 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 12     12 0 561 my ($module_name) = @_;
72             ## no critic(BuiltinFunctions::ProhibitStringyEval)
73 12         727 my $eval_result = eval "require $module_name; '$module_name'->import; 1";
74 12 50       100 if ( !$eval_result ) {
75 0         0 my $eval_error = $EVAL_ERROR;
76 0         0 $eval_error =~ s/^/# /gxms;
77 0 0       0 print "1..0 # Skip Could not load $module_name\n", $eval_error
78             or Carp::croak("say failed: $ERRNO");
79 0         0 exit 0;
80             } ## end if ( !$eval_result )
81 11     11   100 use lib 'config';
  11         53  
  11         107  
82 12         22 $eval_result = eval { require Marpa::R2::Config; 1 };
  12         5240  
  12         52  
83 12 50       40 if ( !$eval_result ) {
84 0         0 Test::More::plan tests => 1;
85 0         0 Test::More::diag($EVAL_ERROR);
86 0         0 Test::More::fail("Could not load Marpa::R2::Config\n");
87 0         0 exit 0;
88             } ## end if ( !$eval_result )
89 12         25 my $version_wanted = $Marpa::R2::VERSION_FOR_CONFIG{$module_name};
90 12 50       33 if ( not defined $version_wanted ) {
91 0         0 Test::More::plan tests => 1;
92 0         0 Test::More::fail("$module_name is not known to Marpa::R2");
93 0         0 exit 0;
94             }
95 12         622 my $module_version = eval q{$} . $module_name . '::VERSION';
96 12 50       94 if ( CPAN::Version->vlt( $module_version, $version_wanted ) ) {
97 0 0         say
98             "1..0 # Skip $module_name version is $module_version; we wanted $version_wanted"
99             or Carp::croak("say failed: $ERRNO");
100 0           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: