Skip to content

Commit

Permalink
Make tests work under concurrent prove
Browse files Browse the repository at this point in the history
  • Loading branch information
exodist committed Jul 30, 2024
1 parent c05e4f3 commit b07279e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ t2/non_perl/test.binary
t2_lib
test-logs/
xxx
.immiscible-test.lock
107 changes: 107 additions & 0 deletions lib/Test2/Plugin/Immiscible.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package Test2::Plugin::Immiscible;
use strict;
use warnings;

use Test2::API qw/context/;

our $VERSION = '2.000000';

our $LOCK;

sub import {
my $class = shift;
my ($skip_cb) = @_;

my $ctx = context();

if ($skip_cb && $skip_cb->()) {
$ctx->note("Immiscibility enforcement skipped due to callback.");
}
else {
if (-w '.') {
if (open($LOCK, '>>', './.immiscible-test.lock')) {
require Fcntl;
if (flock($LOCK, Fcntl::LOCK_EX)) {
$ctx->note("Immiscibility enforcement success.");
}
else {
$ctx->plan(0, SKIP => "could not get lock '$!', cannot guarentee immiscibility.");
}
}
else {
$ctx->plan(0, SKIP => "could not get lock '$!', cannot guarentee immiscibility.");
}
}
else {
$ctx->plan(0, SKIP => "'.' is not writable, cannot guarentee immiscibility.");
}
}

$ctx->release;
}

1;

__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Plugin::Immiscible - Prevent tests with this module from running
together in the same test suite.
=head1 DESCRIPTION
Prevent any 2 tests with this module loaded from running together in the same
repo.
=head1 SYNOPSIS
use Test2::Plugin::Immiscible;
or
Test2::Plugin::Immiscible(sub { $ENV{SKIP_IMMISCIBILITY_CHECK } ? 1 : 0 );
The second form allows you to skip the protection if certain conditions are
met. The callback sub should return true if the protection should be skipped.
=over 4
=back
=head1 SOURCE
The source code repository for Test2-Harness can be found at
L<http://github.com/Test-More/Test2-Harness/>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 AUTHORS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 COPYRIGHT
Copyright Chad Granum E<lt>exodist7@gmail.comE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See L<http://dev.perl.org/licenses/>
=cut
91 changes: 91 additions & 0 deletions lib/Test2/Plugin/IsolateTemp.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package Test2::Plugin::IsolateTemp;
use strict;
use warnings;

our $VERSION = '2.000000';

use Test2::Harness::Util qw/chmod_tmp/;
use File::Temp qw/tempdir/;

our $tempdir;

if ($ENV{TEST2_HARNESS_ACTIVE}) {
# Nothing currently
}
else {
my $template = join '-' => ("T2ISO", $$, "XXXX");

$tempdir = tempdir(
$template,
TMPDIR => 1,
CLEANUP => 1,
);

chmod_tmp($tempdir);

$ENV{TMPDIR} = $tempdir;
$ENV{TEMPDIR} = $tempdir;
$ENV{TMP_DIR} = $tempdir;
$ENV{TEMP_DIR} = $tempdir;
}


1;

__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Plugin::IsolateTemp - Make sure a test uses an isolated temp dir.
=head1 DESCRIPTION
Make sure the test uses an isolated temp dir.
B<NOTE:> This is a no-op when tests are run with yath (L<App::Yath> and
L<Test2::Harness>) as yath will do this by default.
=head1 SYNOPSIS
use Test2::Plugin::IsolateTemp;
=over 4
=back
=head1 SOURCE
The source code repository for Test2-Harness can be found at
L<http://github.com/Test-More/Test2-Harness/>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 AUTHORS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 COPYRIGHT
Copyright Chad Granum E<lt>exodist7@gmail.comE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See L<http://dev.perl.org/licenses/>
=cut
3 changes: 3 additions & 0 deletions t/integration/coverage-ui.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use Test2::Plugin::IsolateTemp;
use Test2::V0;

use Test2::Harness::Util::JSON qw/encode_json decode_json/;
Expand All @@ -8,6 +9,8 @@ use App::Yath::Server;

use App::Yath::Tester qw/yath/;

use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};
Expand Down
3 changes: 3 additions & 0 deletions t/integration/coverage.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use Test2::Plugin::IsolateTemp;
use Test2::V0;
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
Expand All @@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;

use File::Temp qw/tempfile/;

use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};
Expand Down
3 changes: 3 additions & 0 deletions t/integration/coverage2.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use Test2::Plugin::IsolateTemp;
use Test2::V0;
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
Expand All @@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;

use File::Temp qw/tempfile/;

use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};
Expand Down
3 changes: 3 additions & 0 deletions t/integration/coverage3.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use Test2::Plugin::IsolateTemp;
use Test2::V0;
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
Expand All @@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;

use File::Temp qw/tempfile/;

use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};
Expand Down
3 changes: 3 additions & 0 deletions t/integration/coverage4.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use Test2::Plugin::IsolateTemp;
use Test2::V0;
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
Expand All @@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;

use File::Temp qw/tempfile/;

use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};
Expand Down
1 change: 1 addition & 0 deletions t/integration/replay.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sub clean_output {
$out->{output} =~ s/^.*duration.*$//m;
$out->{output} =~ s/^.*Wrote log file:.*$//m;
$out->{output} =~ s/^.*Symlinked to:.*$//m;
$out->{output} =~ s/^.*Linked log file:.*$//m;
$out->{output} =~ s/^\s*Wall Time:.*seconds//m;
$out->{output} =~ s/^\s*CPU Time:.*s\)//m;
$out->{output} =~ s/^\s*CPU Usage:.*%//m;
Expand Down

0 comments on commit b07279e

Please sign in to comment.