Skip to content

Commit b07279e

Browse files
committed
Make tests work under concurrent prove
1 parent c05e4f3 commit b07279e

File tree

9 files changed

+215
-0
lines changed

9 files changed

+215
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ t2/non_perl/test.binary
5858
t2_lib
5959
test-logs/
6060
xxx
61+
.immiscible-test.lock

lib/Test2/Plugin/Immiscible.pm

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package Test2::Plugin::Immiscible;
2+
use strict;
3+
use warnings;
4+
5+
use Test2::API qw/context/;
6+
7+
our $VERSION = '2.000000';
8+
9+
our $LOCK;
10+
11+
sub import {
12+
my $class = shift;
13+
my ($skip_cb) = @_;
14+
15+
my $ctx = context();
16+
17+
if ($skip_cb && $skip_cb->()) {
18+
$ctx->note("Immiscibility enforcement skipped due to callback.");
19+
}
20+
else {
21+
if (-w '.') {
22+
if (open($LOCK, '>>', './.immiscible-test.lock')) {
23+
require Fcntl;
24+
if (flock($LOCK, Fcntl::LOCK_EX)) {
25+
$ctx->note("Immiscibility enforcement success.");
26+
}
27+
else {
28+
$ctx->plan(0, SKIP => "could not get lock '$!', cannot guarentee immiscibility.");
29+
}
30+
}
31+
else {
32+
$ctx->plan(0, SKIP => "could not get lock '$!', cannot guarentee immiscibility.");
33+
}
34+
}
35+
else {
36+
$ctx->plan(0, SKIP => "'.' is not writable, cannot guarentee immiscibility.");
37+
}
38+
}
39+
40+
$ctx->release;
41+
}
42+
43+
1;
44+
45+
__END__
46+
47+
=pod
48+
49+
=encoding UTF-8
50+
51+
=head1 NAME
52+
53+
Test2::Plugin::Immiscible - Prevent tests with this module from running
54+
together in the same test suite.
55+
56+
=head1 DESCRIPTION
57+
58+
Prevent any 2 tests with this module loaded from running together in the same
59+
repo.
60+
61+
=head1 SYNOPSIS
62+
63+
use Test2::Plugin::Immiscible;
64+
65+
or
66+
67+
Test2::Plugin::Immiscible(sub { $ENV{SKIP_IMMISCIBILITY_CHECK } ? 1 : 0 );
68+
69+
The second form allows you to skip the protection if certain conditions are
70+
met. The callback sub should return true if the protection should be skipped.
71+
72+
=over 4
73+
74+
=back
75+
76+
=head1 SOURCE
77+
78+
The source code repository for Test2-Harness can be found at
79+
L<http://github.com/Test-More/Test2-Harness/>.
80+
81+
=head1 MAINTAINERS
82+
83+
=over 4
84+
85+
=item Chad Granum E<lt>[email protected]E<gt>
86+
87+
=back
88+
89+
=head1 AUTHORS
90+
91+
=over 4
92+
93+
=item Chad Granum E<lt>[email protected]E<gt>
94+
95+
=back
96+
97+
=head1 COPYRIGHT
98+
99+
Copyright Chad Granum E<lt>[email protected]E<gt>.
100+
101+
This program is free software; you can redistribute it and/or
102+
modify it under the same terms as Perl itself.
103+
104+
See L<http://dev.perl.org/licenses/>
105+
106+
=cut
107+

lib/Test2/Plugin/IsolateTemp.pm

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package Test2::Plugin::IsolateTemp;
2+
use strict;
3+
use warnings;
4+
5+
our $VERSION = '2.000000';
6+
7+
use Test2::Harness::Util qw/chmod_tmp/;
8+
use File::Temp qw/tempdir/;
9+
10+
our $tempdir;
11+
12+
if ($ENV{TEST2_HARNESS_ACTIVE}) {
13+
# Nothing currently
14+
}
15+
else {
16+
my $template = join '-' => ("T2ISO", $$, "XXXX");
17+
18+
$tempdir = tempdir(
19+
$template,
20+
TMPDIR => 1,
21+
CLEANUP => 1,
22+
);
23+
24+
chmod_tmp($tempdir);
25+
26+
$ENV{TMPDIR} = $tempdir;
27+
$ENV{TEMPDIR} = $tempdir;
28+
$ENV{TMP_DIR} = $tempdir;
29+
$ENV{TEMP_DIR} = $tempdir;
30+
}
31+
32+
33+
1;
34+
35+
__END__
36+
37+
=pod
38+
39+
=encoding UTF-8
40+
41+
=head1 NAME
42+
43+
Test2::Plugin::IsolateTemp - Make sure a test uses an isolated temp dir.
44+
45+
=head1 DESCRIPTION
46+
47+
Make sure the test uses an isolated temp dir.
48+
49+
B<NOTE:> This is a no-op when tests are run with yath (L<App::Yath> and
50+
L<Test2::Harness>) as yath will do this by default.
51+
52+
=head1 SYNOPSIS
53+
54+
use Test2::Plugin::IsolateTemp;
55+
56+
=over 4
57+
58+
=back
59+
60+
=head1 SOURCE
61+
62+
The source code repository for Test2-Harness can be found at
63+
L<http://github.com/Test-More/Test2-Harness/>.
64+
65+
=head1 MAINTAINERS
66+
67+
=over 4
68+
69+
=item Chad Granum E<lt>[email protected]E<gt>
70+
71+
=back
72+
73+
=head1 AUTHORS
74+
75+
=over 4
76+
77+
=item Chad Granum E<lt>[email protected]E<gt>
78+
79+
=back
80+
81+
=head1 COPYRIGHT
82+
83+
Copyright Chad Granum E<lt>[email protected]E<gt>.
84+
85+
This program is free software; you can redistribute it and/or
86+
modify it under the same terms as Perl itself.
87+
88+
See L<http://dev.perl.org/licenses/>
89+
90+
=cut
91+

t/integration/coverage-ui.t

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use Test2::Plugin::IsolateTemp;
12
use Test2::V0;
23

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

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

12+
use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });
13+
1114
my $dir = __FILE__;
1215
$dir =~ s{\.t$}{}g;
1316
$dir =~ s{^\./}{};

t/integration/coverage.t

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use Test2::Plugin::IsolateTemp;
12
use Test2::V0;
23
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
34
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
@@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;
67

78
use File::Temp qw/tempfile/;
89

10+
use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });
11+
912
my $dir = __FILE__;
1013
$dir =~ s{\.t$}{}g;
1114
$dir =~ s{^\./}{};

t/integration/coverage2.t

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use Test2::Plugin::IsolateTemp;
12
use Test2::V0;
23
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
34
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
@@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;
67

78
use File::Temp qw/tempfile/;
89

10+
use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });
11+
912
my $dir = __FILE__;
1013
$dir =~ s{\.t$}{}g;
1114
$dir =~ s{^\./}{};

t/integration/coverage3.t

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use Test2::Plugin::IsolateTemp;
12
use Test2::V0;
23
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
34
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
@@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;
67

78
use File::Temp qw/tempfile/;
89

10+
use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });
11+
912
my $dir = __FILE__;
1013
$dir =~ s{\.t$}{}g;
1114
$dir =~ s{^\./}{};

t/integration/coverage4.t

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use Test2::Plugin::IsolateTemp;
12
use Test2::V0;
23
use Test2::Harness::Util::JSON qw/encode_json decode_json/;
34
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000022';
@@ -6,6 +7,8 @@ use App::Yath::Tester qw/yath/;
67

78
use File::Temp qw/tempfile/;
89

10+
use Test2::Plugin::Immiscible(sub { $ENV{TEST2_HARNESS_ACTIVE} ? 1 : 0 });
11+
912
my $dir = __FILE__;
1013
$dir =~ s{\.t$}{}g;
1114
$dir =~ s{^\./}{};

t/integration/replay.t

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sub clean_output {
1717
$out->{output} =~ s/^.*duration.*$//m;
1818
$out->{output} =~ s/^.*Wrote log file:.*$//m;
1919
$out->{output} =~ s/^.*Symlinked to:.*$//m;
20+
$out->{output} =~ s/^.*Linked log file:.*$//m;
2021
$out->{output} =~ s/^\s*Wall Time:.*seconds//m;
2122
$out->{output} =~ s/^\s*CPU Time:.*s\)//m;
2223
$out->{output} =~ s/^\s*CPU Usage:.*%//m;

0 commit comments

Comments
 (0)