|
| 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