Skip to content

Commit 32331ed

Browse files
authored
Merge pull request #1 from uperl/graham/init
initial implementation
2 parents 9fda6a9 + 7df6220 commit 32331ed

File tree

5 files changed

+212
-5
lines changed

5 files changed

+212
-5
lines changed

.github/workflows/linux.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
CIP_TAG: ${{ matrix.cip_tag }}
3535

3636
steps:
37-
- uses: actions/checkout@v2
37+
- uses: actions/checkout@v4
3838

3939
- name: Bootstrap CIP
4040
run: |
@@ -47,7 +47,7 @@ jobs:
4747
cip cache-key
4848
4949
- name: Cache CPAN modules
50-
uses: actions/cache@v2
50+
uses: actions/cache@v4
5151
with:
5252
path: ~/.cip
5353
key: ${{ runner.os }}-build-${{ steps.cache-key.outputs.key }}

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 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)
2+
3+
Skip test unless a program exists in the PATH
4+
5+
# SYNOPSIS
6+
7+
```perl
8+
use Test2::Require::ProgramInPath 'gcc';
9+
use Test2::V0;
10+
use Test::Script qw( program_runs );
11+
12+
program_runs ['gcc', 'foo.c'];
13+
14+
done_testing;
15+
```
16+
17+
# DESCRIPTION
18+
19+
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).
20+
21+
# METHODS
22+
23+
## skip
24+
25+
Should not be invoked directly, but returns \`undef\` if the test should not be skipped and a string containing
26+
the reason why the test was skipped. Currently \`This test only runs if $program is in the PATH\` is returned.
27+
28+
# SEE ALSO
29+
30+
- [File::Which](https://metacpan.org/pod/File::Which)
31+
- [Test2::Require](https://metacpan.org/pod/Test2::Require)
32+
33+
# AUTHOR
34+
35+
Graham Ollis <[email protected]>
36+
37+
# COPYRIGHT AND LICENSE
38+
39+
This software is copyright (c) 2025 by Graham Ollis.
40+
41+
This is free software; you can redistribute it and/or modify it under
42+
the same terms as the Perl 5 programming language system itself.

lib/Test2/Require/ProgramInPath.pm

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
use warnings;
22
use 5.020;
3-
use experimental qw( postderef signatures );
3+
use experimental qw( signatures );
44
use true;
55

66
package Test2::Require::ProgramInPath {
77

88
# ABSTRACT: Skip test unless a program exists in the PATH
9+
10+
=head1 SYNOPSIS
11+
12+
use Test2::Require::ProgramInPath 'gcc';
13+
use Test2::V0;
14+
use Test::Script qw( program_runs );
15+
16+
program_runs ['gcc', 'foo.c'];
17+
18+
done_testing;
19+
20+
=head1 DESCRIPTION
21+
22+
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>.
23+
24+
=head1 METHODS
25+
26+
=head2 skip
27+
28+
Should not be invoked directly, but returns `undef` if the test should not be skipped and a string containing
29+
the reason why the test was skipped. Currently `This test only runs if $program is in the PATH` is returned.
30+
31+
=cut
32+
33+
use File::Which ();
34+
use Carp qw( confess );
35+
use parent qw( Test2::Require );
36+
37+
sub skip ( $, $program = undef ) {
38+
confess "no program specified" unless defined $program;
39+
return undef if File::Which::which $program;
40+
return "This test only runs if $program is in the PATH";
41+
}
942
}
43+
44+
45+
=head1 SEE ALSO
46+
47+
=over 4
48+
49+
=item L<File::Which>
50+
51+
=item L<Test2::Require>
52+
53+
=back
54+
55+
=cut

t/00_diag.t

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use Test2::V0 -no_srand => 1;
2+
use Config;
3+
4+
eval { require 'Test/More.pm' };
5+
6+
# This .t file is generated.
7+
# make changes instead to dist.ini
8+
9+
my %modules;
10+
my $post_diag;
11+
12+
$modules{$_} = $_ for qw(
13+
ExtUtils::MakeMaker
14+
File::Which
15+
Test2::Require
16+
Test2::V0
17+
true
18+
);
19+
20+
21+
22+
my @modules = sort keys %modules;
23+
24+
sub spacer ()
25+
{
26+
diag '';
27+
diag '';
28+
diag '';
29+
}
30+
31+
pass 'okay';
32+
33+
my $max = 1;
34+
$max = $_ > $max ? $_ : $max for map { length $_ } @modules;
35+
our $format = "%-${max}s %s";
36+
37+
spacer;
38+
39+
my @keys = sort grep /(MOJO|PERL|\A(LC|HARNESS)_|\A(SHELL|LANG)\Z)/i, keys %ENV;
40+
41+
if(@keys > 0)
42+
{
43+
diag "$_=$ENV{$_}" for @keys;
44+
45+
if($ENV{PERL5LIB})
46+
{
47+
spacer;
48+
diag "PERL5LIB path";
49+
diag $_ for split $Config{path_sep}, $ENV{PERL5LIB};
50+
51+
}
52+
elsif($ENV{PERLLIB})
53+
{
54+
spacer;
55+
diag "PERLLIB path";
56+
diag $_ for split $Config{path_sep}, $ENV{PERLLIB};
57+
}
58+
59+
spacer;
60+
}
61+
62+
diag sprintf $format, 'perl', "$] $^O $Config{archname}";
63+
64+
foreach my $module (sort @modules)
65+
{
66+
my $pm = "$module.pm";
67+
$pm =~ s{::}{/}g;
68+
if(eval { require $pm; 1 })
69+
{
70+
my $ver = eval { $module->VERSION };
71+
$ver = 'undef' unless defined $ver;
72+
diag sprintf $format, $module, $ver;
73+
}
74+
else
75+
{
76+
diag sprintf $format, $module, '-';
77+
}
78+
}
79+
80+
if($post_diag)
81+
{
82+
spacer;
83+
$post_diag->();
84+
}
85+
86+
spacer;
87+
88+
done_testing;
89+

t/test2_require_programinpath.t

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
use Test2::V0 -no_srand => 1;
2-
use Test2::Require::ProgramInPath;
2+
use Test2::Require::ProgramInPath ();
33

4-
ok 1, 'todo';
4+
subtest 'skip' => sub {
5+
6+
plan 2;
7+
8+
local *File::Which::which = sub {
9+
is(\@_, ['foo']);
10+
return undef;
11+
};
12+
13+
is(
14+
Test2::Require::ProgramInPath->skip('foo'),
15+
'This test only runs if foo is in the PATH',
16+
);
17+
18+
};
19+
20+
subtest 'no skip' => sub {
21+
22+
plan 2;
23+
24+
local *File::Which::which = sub {
25+
is(\@_, ['foo']);
26+
return '/bin/foo';
27+
};
28+
29+
is(
30+
Test2::Require::ProgramInPath->skip('foo'),
31+
U(),
32+
);
33+
34+
};
535

636
done_testing;

0 commit comments

Comments
 (0)