|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use Test::More 'tests' => 6; |
| 4 | +use File::Spec; |
| 5 | +use Path::Tiny (); |
| 6 | +use Dancer2::FileUtils qw< path >; |
| 7 | + |
| 8 | +# This module tests merge_paths can be properly replaced by Path::Tiny::path() |
| 9 | +# It addresses the following comment by @wchristian, written bellow. |
| 10 | + |
| 11 | +# We removed this subroutine but still need to verify we converted correctly |
| 12 | +sub merge_paths { |
| 13 | + my ( undef, $path, $public_dir ) = @_; |
| 14 | + |
| 15 | + my ( $volume, $dirs, $file ) = File::Spec->splitpath( $path ); |
| 16 | + my @tokens = File::Spec->splitdir( "$dirs$file" ); |
| 17 | + my $updir = File::Spec->updir; |
| 18 | + return if grep $_ eq $updir, @tokens; |
| 19 | + |
| 20 | + my ( $pub_vol, $pub_dirs, $pub_file ) = File::Spec->splitpath( $public_dir ); |
| 21 | + my @pub_tokens = File::Spec->splitdir( "$pub_dirs$pub_file" ); |
| 22 | + return if length $volume and length $pub_vol and $volume ne $pub_vol; |
| 23 | + |
| 24 | + my @final_vol = ( length $pub_vol ? $pub_vol : length $volume ? $volume : () ); |
| 25 | + my @file_path = ( @final_vol, @pub_tokens, @tokens ); |
| 26 | + my $file_path = path( @file_path ); |
| 27 | + return $file_path; |
| 28 | +} |
| 29 | + |
| 30 | +# From GH #1264: |
| 31 | +# |
| 32 | +# That merge_paths commit is no good in that form, i'm afraid. |
| 33 | +# |
| 34 | +# All of these should throw errors instead of "succeeding", otherwise they present huge security problems and/or break tests on systems with more than one file system tree. @haarg probably has other ideas of how this could break as well. |
| 35 | +# |
| 36 | +# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', 'c:/marp' )->stringify" |
| 37 | +# C:/meep/c:/marp |
| 38 | +# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', 'd:/marp' )->stringify" |
| 39 | +# C:/meep/d:/marp |
| 40 | +# C:\Users\Mithaldu>perl -e "use Path::Tiny; print Path::Tiny::path( 'c:/meep', '../marp' )->stringify" |
| 41 | +# C:/marp |
| 42 | +# |
| 43 | +# The first two are both straight-up non-sense in the vein of merge_path( "~/.ssh", "ftp://some.server/whatever" ) so they should error out, nothing else. |
| 44 | +# The third is "merely" dangerous and should be prevented. |
| 45 | + |
| 46 | + |
| 47 | +is( |
| 48 | + merge_paths( undef, 'c:/meep', 'c:/marp' ), |
| 49 | + 'c:/marp/c:/meep', |
| 50 | + 'merge_paths: c:/meep + c:/marp', |
| 51 | +); |
| 52 | + |
| 53 | +is( |
| 54 | + Path::Tiny::path( 'c:/meep', 'c:/marp' ), |
| 55 | + 'c:/meep/c:/marp', |
| 56 | + 'Path::Tiny: c:/meep + c:/marp', |
| 57 | +); |
| 58 | + |
| 59 | +is( |
| 60 | + merge_paths( undef, 'c:/meep', 'd:/marp' ), |
| 61 | + 'd:/marp/c:/meep', |
| 62 | + 'merge_paths: c:/meep + d:/marp', |
| 63 | +); |
| 64 | + |
| 65 | +is( |
| 66 | + Path::Tiny::path( 'c:/meep', 'd:/marp' ), |
| 67 | + 'c:/meep/d:/marp', |
| 68 | + 'Path::Tiny: c:/meep + d:/marp', |
| 69 | +); |
| 70 | + |
| 71 | +is( |
| 72 | + merge_paths( undef, 'c:/meep', '../marp' ), |
| 73 | + '../marp/c:/meep', |
| 74 | + 'merge_paths: c:/meep + ../marp', |
| 75 | +); |
| 76 | + |
| 77 | +is( |
| 78 | + Path::Tiny::path( 'c:/meep', '../marp' ), |
| 79 | + 'c:/meep/../marp', |
| 80 | + 'Path::Tiny: c:/meep + ../marp', |
| 81 | +); |
0 commit comments