Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,72 @@
package Test2::Require::AuthorTesting;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
sub skip {
my $class = shift;
return undef if $ENV{'AUTHOR_TESTING'};
return 'Author test, set the $AUTHOR_TESTING environment variable to run it';
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::AuthorTesting - Only run a test when the AUTHOR_TESTING
environment variable is set.
=head1 DESCRIPTION
It is common practice to write tests that are only run when the AUTHOR_TESTING
environment variable is set. This module automates the (admittedly trivial) work
of creating such a test.
=head1 SYNOPSIS
use Test2::Require::AuthorTesting;
...
done_testing;
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,75 @@
package Test2::Require::EnvVar;
use strict;
use warnings;
use Carp qw/confess/;
use base 'Test2::Require';
our $VERSION = '0.000139';
sub skip {
my $class = shift;
my ($var) = @_;
confess "no environment variable specified" unless $var;
return undef if $ENV{$var};
return "This test only runs if the \$$var environment variable is set";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::EnvVar - Only run a test when a specific environment variable
is set.
=head1 DESCRIPTION
It is common practice to write tests that are only run when an environment
variable is set. This module automates the (admittedly trivial) work of creating
such a test.
=head1 SYNOPSIS
use Test2::Require::EnvVar 'SOME_VAR';
...
done_testing;
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,112 @@
package Test2::Require::Fork;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
use Test2::Util qw/CAN_FORK/;
sub skip {
return undef if CAN_FORK;
return "This test requires a perl capable of forking.";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::Fork - Skip a test file unless the system supports forking
=head1 DESCRIPTION
It is fairly common to write tests that need to fork. Not all systems support
forking. This library does the hard work of checking if forking is supported on
the current system. If forking is not supported then this will skip all tests
and exit true.
=head1 SYNOPSIS
use Test2::Require::Fork;
... Code that forks ...
=head1 EXPLANATION
Checking if the current system supports forking is not simple. Here is an
example of how to do it:
use Config;
sub CAN_FORK {
return 1 if $Config{d_fork};
# Some platforms use ithreads to mimic forking
return 0 unless $^O eq 'MSWin32' || $^O eq 'NetWare';
return 0 unless $Config{useithreads};
return 0 unless $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/;
# Threads are not reliable before 5.008001
return 0 unless $] >= 5.008001;
# Devel::Cover currently breaks with threads
return 0 if $INC{'Devel/Cover.pm'};
return 1;
}
Duplicating this non-trivial code in all tests that need to fork is error-prone. It is
easy to forget bits, or get it wrong. On top of these checks, you also need to
tell the harness that no tests should run and why.
=head1 SEE ALSO
=over 4
=item L<Test2::Require::CanReallyfork>
Similar to this module, but will skip on any perl that only has fork emulation.
=item L<Test2::Require::CanThread>
Skip the test file if the system does not support threads.
=back
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,113 @@
package Test2::Require::Module;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
use Test2::Util qw/pkg_to_file/;
sub skip {
my $class = shift;
my ($module, $ver) = @_;
return "Module '$module' is not installed"
unless check_installed($module);
return undef unless defined $ver;
return check_version($module, $ver);
}
sub check_installed {
my ($mod) = @_;
my $file = pkg_to_file($mod);
return 1 if eval { require $file; 1 };
my $error = $@;
return 0 if $error =~ m/Can't locate \Q$file\E in \@INC/;
# Some other error, rethrow it.
die $error;
}
sub check_version {
my ($mod, $ver) = @_;
return undef if eval { $mod->VERSION($ver); 1 };
my $have = $mod->VERSION;
return "Need '$mod' version $ver, have $have.";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::Module - Skip tests if certain packages are not installed, or
insufficient versions.
=head1 DESCRIPTION
Sometimes you have tests that are nice to run, but depend on tools that may not
be available. Instead of adding the tool as a dep, or making the test always
skip, it is common to make the test run conditionally. This package helps make
that possible.
This module is modeled after L<Test::Requires>. The difference is that this
module is based on L<Test2> directly, and does not go through L<Test::Builder>.
Another difference is that the packages you check for are not imported into
your namespace for you. This is intentional.
=head1 SYNOPSIS
# The test will be skipped unless Some::Module is installed, any version.
use Test2::Require::Module 'Some::Module';
# The test will be skipped unless 'Other::Module' is installed and at
# version '5.555' or greater.
use Test2::Require::Module 'Other::Module' => '5.555';
# We now need to use them directly, Test2::Require::Module does not import
# them for us.
use Some::Module;
use Other::Module;
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,79 @@
package Test2::Require::Perl;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
use Test2::Util qw/pkg_to_file/;
use Scalar::Util qw/reftype/;
sub skip {
my $class = shift;
my ($ver) = @_;
return undef if eval "no warnings 'portable'; require $ver; 1";
my $error = $@;
return $1 if $error =~ m/^(Perl \S* required)/i;
die $error;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::Perl - Skip the test unless the necessary version of Perl is
installed.
=head1 DESCRIPTION
Sometimes you have tests that are nice to run, but depend on a certain version
of Perl. This package lets you run the test conditionally, depending on if the
correct version of Perl is available.
=head1 SYNOPSIS
# Skip the test unless perl 5.10 or greater is installed.
use Test2::Require::Perl 'v5.10';
# Enable 5.10 features.
use v5.10;
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,86 @@
package Test2::Require::RealFork;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
use Test2::Util qw/CAN_REALLY_FORK/;
sub skip {
return undef if CAN_REALLY_FORK;
return "This test requires a perl capable of true forking.";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::RealFork - Skip a test file unless the system supports true
forking
=head1 DESCRIPTION
It is fairly common to write tests that need to fork. Not all systems support
forking. This library does the hard work of checking if forking is supported on
the current system. If forking is not supported then this will skip all tests
and exit true.
=head1 SYNOPSIS
use Test2::Require::RealFork;
... Code that forks ...
=head1 SEE ALSO
=over 4
=item L<Test2::Require::Canfork>
Similar to this module, but will allow fork emulation.
=item L<Test2::Require::CanThread>
Skip the test file if the system does not support threads.
=back
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut

View File

@@ -0,0 +1,106 @@
package Test2::Require::Threads;
use strict;
use warnings;
use base 'Test2::Require';
our $VERSION = '0.000139';
use Test2::Util qw/CAN_THREAD/;
sub skip {
return undef if CAN_THREAD;
return "This test requires a perl capable of threading.";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Require::Threads - Skip a test file unless the system supports threading
=head1 DESCRIPTION
It is fairly common to write tests that need to use threads. Not all systems
support threads. This library does the hard work of checking if threading is
supported on the current system. If threading is not supported then this will
skip all tests and exit true.
=head1 SYNOPSIS
use Test2::Require::Threads;
... Code that uses threads ...
=head1 EXPLANATION
Checking if the current system supports threading is not simple, here is an
example of how to do it:
use Config;
sub CAN_THREAD {
# Threads are not reliable before 5.008001
return 0 unless $] >= 5.008001;
return 0 unless $Config{'useithreads'};
# Devel::Cover currently breaks with threads
return 0 if $INC{'Devel/Cover.pm'};
return 1;
}
Duplicating this non-trivial code in all tests that need to use threads is
error-prone. It is easy to forget bits, or get it wrong. On top of these checks you
also need to tell the harness that no tests should run and why.
=head1 SEE ALSO
=over 4
=item L<Test2::Require::CanFork>
Skip the test file if the system does not support forking.
=item L<Test2>
Test2::Require::Threads uses L<Test2> under the hood.
=back
=head1 SOURCE
The source code repository for Test2-Suite can be found at
F<https://github.com/Test-More/Test2-Suite/>.
=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 2018 Chad Granum E<lt>exodist@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut