Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
CIP_TAG: ${{ matrix.cip_tag }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Bootstrap CIP
run: |
Expand All @@ -47,7 +47,7 @@ jobs:
cip cache-key

- name: Cache CPAN modules
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.cip
key: ${{ runner.os }}-build-${{ steps.cache-key.outputs.key }}
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Test2::Require::ProgramInPath ![static](https://github.com/uperl/Test2-Require-ProgramInPath/workflows/static/badge.svg) ![linux](https://github.com/uperl/Test2-Require-ProgramInPath/workflows/linux/badge.svg)

Skip test unless a program exists in the PATH

# SYNOPSIS

```perl
use Test2::Require::ProgramInPath 'gcc';
use Test2::V0;
use Test::Script qw( program_runs );

program_runs ['gcc', 'foo.c'];

done_testing;
```

# DESCRIPTION

This is skip unless a particular program can be found in the `PATH`. Under the covers [File::Which](https://metacpan.org/pod/File::Which) is used. This is a subclass of [Test2::Require](https://metacpan.org/pod/Test2::Require).

# METHODS

## skip

Should not be invoked directly, but returns \`undef\` if the test should not be skipped and a string containing
the reason why the test was skipped. Currently \`This test only runs if $program is in the PATH\` is returned.

# SEE ALSO

- [File::Which](https://metacpan.org/pod/File::Which)
- [Test2::Require](https://metacpan.org/pod/Test2::Require)

# AUTHOR

Graham Ollis <plicease@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
48 changes: 47 additions & 1 deletion lib/Test2/Require/ProgramInPath.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
use warnings;
use 5.020;
use experimental qw( postderef signatures );
use experimental qw( signatures );
use true;

package Test2::Require::ProgramInPath {

# ABSTRACT: Skip test unless a program exists in the PATH

=head1 SYNOPSIS

use Test2::Require::ProgramInPath 'gcc';
use Test2::V0;
use Test::Script qw( program_runs );

program_runs ['gcc', 'foo.c'];

done_testing;

=head1 DESCRIPTION

This is skip unless a particular program can be found in the C<PATH>. Under the covers L<File::Which> is used. This is a subclass of L<Test2::Require>.

=head1 METHODS

=head2 skip

Should not be invoked directly, but returns `undef` if the test should not be skipped and a string containing
the reason why the test was skipped. Currently `This test only runs if $program is in the PATH` is returned.

=cut

use File::Which ();
use Carp qw( confess );
use parent qw( Test2::Require );

sub skip ( $, $program = undef ) {
confess "no program specified" unless defined $program;
return undef if File::Which::which $program;
return "This test only runs if $program is in the PATH";
}
}


=head1 SEE ALSO

=over 4

=item L<File::Which>

=item L<Test2::Require>

=back

=cut
89 changes: 89 additions & 0 deletions t/00_diag.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use Test2::V0 -no_srand => 1;
use Config;

eval { require 'Test/More.pm' };

# This .t file is generated.
# make changes instead to dist.ini

my %modules;
my $post_diag;

$modules{$_} = $_ for qw(
ExtUtils::MakeMaker
File::Which
Test2::Require
Test2::V0
true
);



my @modules = sort keys %modules;

sub spacer ()
{
diag '';
diag '';
diag '';
}

pass 'okay';

my $max = 1;
$max = $_ > $max ? $_ : $max for map { length $_ } @modules;
our $format = "%-${max}s %s";

spacer;

my @keys = sort grep /(MOJO|PERL|\A(LC|HARNESS)_|\A(SHELL|LANG)\Z)/i, keys %ENV;

if(@keys > 0)
{
diag "$_=$ENV{$_}" for @keys;

if($ENV{PERL5LIB})
{
spacer;
diag "PERL5LIB path";
diag $_ for split $Config{path_sep}, $ENV{PERL5LIB};

}
elsif($ENV{PERLLIB})
{
spacer;
diag "PERLLIB path";
diag $_ for split $Config{path_sep}, $ENV{PERLLIB};
}

spacer;
}

diag sprintf $format, 'perl', "$] $^O $Config{archname}";

foreach my $module (sort @modules)
{
my $pm = "$module.pm";
$pm =~ s{::}{/}g;
if(eval { require $pm; 1 })
{
my $ver = eval { $module->VERSION };
$ver = 'undef' unless defined $ver;
diag sprintf $format, $module, $ver;
}
else
{
diag sprintf $format, $module, '-';
}
}

if($post_diag)
{
spacer;
$post_diag->();
}

spacer;

done_testing;

34 changes: 32 additions & 2 deletions t/test2_require_programinpath.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
use Test2::V0 -no_srand => 1;
use Test2::Require::ProgramInPath;
use Test2::Require::ProgramInPath ();

ok 1, 'todo';
subtest 'skip' => sub {

plan 2;

local *File::Which::which = sub {
is(\@_, ['foo']);
return undef;
};

is(
Test2::Require::ProgramInPath->skip('foo'),
'This test only runs if foo is in the PATH',
);

};

subtest 'no skip' => sub {

plan 2;

local *File::Which::which = sub {
is(\@_, ['foo']);
return '/bin/foo';
};

is(
Test2::Require::ProgramInPath->skip('foo'),
U(),
);

};

done_testing;
Loading