Skip to content

Commit 9d931c5

Browse files
committed
test cases for the maybe_patchperl() function.
1 parent e5c4524 commit 9d931c5

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

lib/App/Perlbrew/Patchperl.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ sub maybe_patchperl_in_app_root {
2828
}
2929

3030
sub maybe_patchperl_in_system {
31-
my $code = system("patchperl --version") >> 8;
31+
my $code = system("patchperl --version");
3232

33-
if ($code != 127) {
33+
if ($code == 0) {
3434
return "patchperl"
3535
} else {
3636
return undef;

t/20.patchperl.t

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env perl
2+
use Test2::V0;
3+
use Test2::Tools::Spec;
4+
5+
use FindBin;
6+
use File::Temp qw(tempdir);
7+
8+
use lib $FindBin::Bin;
9+
use App::perlbrew;
10+
use App::Perlbrew::Patchperl qw(maybe_patchperl);
11+
require "test2_helpers.pl";
12+
13+
describe "App::Perlbrew::Patchperl maybe_patchperl" => sub {
14+
local $App::perlbrew::PERLBREW_ROOT;
15+
local $ENV{PATH};
16+
17+
before_each init => sub {
18+
# Since patchperl may exist in developer's environment,
19+
# we override PATH to make the probe fail not able to find patchperl.
20+
$App::perlbrew::PERLBREW_ROOT = tempdir();
21+
$ENV{PATH} = "/bin";
22+
};
23+
24+
it "should return undef, when patchperl does not exist" => sub {
25+
my $app_root = App::perlbrew->new->root;
26+
my $patchperl = maybe_patchperl( $app_root );
27+
is $patchperl, U();
28+
};
29+
30+
describe "When patchperl exist in PATH", sub {
31+
my $bin = tempdir();
32+
33+
before_each "fake-install a patchperl under PATH" => sub {
34+
$ENV{PATH} = "/bin:$bin";
35+
fake_install_patchperl("$bin/patchperl");
36+
};
37+
38+
it "should return just 'patchperl', when patchperl do not exist in PERLBREW_ROOT" => sub {
39+
my $app_root = App::perlbrew->new->root;
40+
my $patchperl = maybe_patchperl( $app_root );
41+
is $patchperl, string "patchperl";
42+
};
43+
44+
describe "When patchperl also exists in PERLBREW_ROOT", sub {
45+
before_each "fake-install a patchperl under PERLBREW_ROOT" => sub {
46+
fake_install_patchperl_under_app_root();
47+
};
48+
49+
it "should return the path of patchperl under app root", sub {
50+
my $app_root = App::perlbrew->new->root;
51+
my $patchperl = maybe_patchperl( $app_root );
52+
is $patchperl, string $app_root->bin("patchperl");
53+
};
54+
};
55+
};
56+
57+
describe "When patchperl exist in PERLBREW_ROOT but not in PATH", sub {
58+
before_each "fake-install a patchperl under PERLBREW_ROOT" => sub {
59+
fake_install_patchperl_under_app_root();
60+
};
61+
62+
it "should return the path of patchperl under app root", sub {
63+
my $app_root = App::perlbrew->new->root;
64+
my $patchperl = maybe_patchperl( $app_root );
65+
is $patchperl, string $app_root->bin("patchperl");
66+
};
67+
};
68+
};
69+
70+
done_testing;
71+
72+
sub fake_install_patchperl {
73+
my ($path) = @_;
74+
75+
open my $fh, ">", $path
76+
or die "Failed to fake-install a patchperl to $path";
77+
print $fh, '#!/usr/bin/env perl\nperl "Fake patchperl version 1.00\n";';
78+
close $fh;
79+
chmod 0755, $path;
80+
81+
diag "Fake-install a patchperl to $path";
82+
}
83+
84+
sub fake_install_patchperl_under_app_root {
85+
my $app_root_bin = App::perlbrew->new->root->bin();
86+
$app_root_bin->mkpath;
87+
fake_install_patchperl( $app_root_bin->child("patchperl") );
88+
}

0 commit comments

Comments
 (0)