Skip to content

Commit 094d52f

Browse files
committed
Run tests serially if MCE is not present.
This is the first step towards making MCE optional for GH #1.
1 parent 854f789 commit 094d52f

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

lib/Test/Perl/Critic.pm

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use warnings;
77

88
use Carp qw(croak);
99
use English qw(-no_match_vars);
10-
use MCE::Grep;
1110

1211
use Test::Builder qw();
1312
use Perl::Critic qw();
@@ -93,23 +92,41 @@ sub all_critic_ok {
9392
my @files = Perl::Critic::Utils::all_perl_files(@dirs_or_files);
9493
croak 'Nothing to critique' if not @files;
9594

96-
# Since tests are running in forked MCE workers, test results could arrive
97-
# in any order. The test numbers will be meaningless, so turn them off.
98-
$TEST->use_numbers(0);
95+
my $have_mce = eval {require MCE::Grep};
96+
return $have_mce ? _test_parallel(@files) : _test_serial(@files);
97+
}
98+
99+
#---------------------------------------------------------------------------
100+
101+
sub _test_parallel {
102+
my @files = @_;
103+
104+
# Since tests are running in forked MCE workers, test results could arrive
105+
# in any order. The test numbers will be meaningless, so turn them off.
106+
$TEST->use_numbers(0);
99107

100-
# The parent won't know about any of the tests that were run by the forked
101-
# workers. So we disable the T::B sanity checks at the end of its life.
102-
$TEST->no_ending(1);
108+
# The parent won't know about any of the tests that were run by the forked
109+
# workers. So we disable the T::B sanity checks at the end of its life.
110+
$TEST->no_ending(1);
111+
112+
my $okays = MCE::Grep::mce_grep { critic_ok($_) } @files;
113+
my $pass = $okays == @files;
114+
115+
# To make Test::Harness happy, we must emit a test plan and a sensible exit
116+
# status. Usually, T::B does this for us, but we disabled the ending above.
117+
$pass || eval 'END { $? = 1 }'; ## no critic qw(Eval Interpolation)
118+
$TEST->done_testing(scalar @files);
119+
}
120+
121+
#---------------------------------------------------------------------------
103122

104-
my $okays = mce_grep { critic_ok($_) } @files;
105-
my $pass = $okays == @files;
123+
sub _test_serial {
124+
my @files = @_;
106125

107-
# To make Test::Harness happy, we must emit a test plan and a sensible exit
108-
# status. Usually, T::B does this for us, but we disabled the ending above.
109-
$pass || eval 'END { $? = 1 }'; ## no critic qw(Eval Interpolation)
110-
$TEST->done_testing(scalar @files);
126+
my $okays = grep {critic_ok($_)} @files;
127+
my $pass = $okays == @files;
111128

112-
return $pass;
129+
return $pass;
113130
}
114131

115132
#---------------------------------------------------------------------------

0 commit comments

Comments
 (0)