| 1 |
use Test2::Bundle::Extended -target => 'Test2::Harness::Util'; |
| 2 |
#BEGIN { skip_all 'TODO' } |
| 3 |
|
| 4 |
use ok $CLASS => ':ALL'; |
| 5 |
|
| 6 |
use File::Temp qw/tempfile tempdir/; |
| 7 |
|
| 8 |
imported_ok qw{ |
| 9 |
fqmod |
| 10 |
maybe_open_file |
| 11 |
maybe_read_file |
| 12 |
open_file |
| 13 |
process_includes |
| 14 |
read_file |
| 15 |
write_file |
| 16 |
write_file_atomic |
| 17 |
|
| 18 |
is_same_file |
| 19 |
}; |
| 20 |
|
| 21 |
my ($line) = split /\n/, read_file(__FILE__), 2; |
| 22 |
like( |
| 23 |
$line, |
| 24 |
q{use Test2::Bundle::Extended -target => 'Test2::Harness::Util';}, |
| 25 |
"Read file (only checking first line)" |
| 26 |
); |
| 27 |
|
| 28 |
like( |
| 29 |
dies { read_file('/fake/file/that/must/not/exist cause I say so') }, |
| 30 |
qr{^\QCould not open file '/fake/file/that/must/not/exist cause I say so' (<)\E}, |
| 31 |
"Exception thrown when read_file used on non-existing file" |
| 32 |
); |
| 33 |
|
| 34 |
is( |
| 35 |
maybe_read_file(__FILE__), |
| 36 |
read_file(__FILE__), |
| 37 |
"maybe_read_file reads file when it exists" |
| 38 |
); |
| 39 |
|
| 40 |
is( |
| 41 |
maybe_read_file('/fake/file/that/must/not/exist cause I say so'), |
| 42 |
undef, |
| 43 |
"maybe_read_file is undef when file does not exist" |
| 44 |
); |
| 45 |
|
| 46 |
ok(my $fh = open_file(__FILE__), "opened file"); |
| 47 |
ok($line = <$fh>, "Can read from file, default mode is 'read'"); |
| 48 |
|
| 49 |
if (-e '/dev/null') { |
| 50 |
ok(my $null = open_file('/dev/null', '>'), "opened /dev/null for writing"); |
| 51 |
ok((print $null "xxx\n"), "printed to /dev/null"); |
| 52 |
|
| 53 |
is( |
| 54 |
[write_file('/dev/null', "AAA", "BBB")], |
| 55 |
["AAA", "BBB"], |
| 56 |
"wrote and returned content (/dev/null)" |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
is( |
| 61 |
maybe_open_file('/fake/file/that/must/not/exist cause I say so'), |
| 62 |
undef, |
| 63 |
"maybe_open_file is undef when file does not exist" |
| 64 |
); |
| 65 |
|
| 66 |
is(fqmod('Foo::Bar', 'Baz'), 'Foo::Bar::Baz', "fqmod on postfix"); |
| 67 |
is(fqmod('Foo::Bar', 'Baz::Bat'), 'Foo::Bar::Baz::Bat', "fqmod on longer postfix"); |
| 68 |
is(fqmod('Foo::Bar', '+Baz'), 'Baz', "fqmod on fq"); |
| 69 |
is(fqmod('Foo::Bar', '+Baz::Bat'), 'Baz::Bat', "fqmod on longer fq"); |
| 70 |
|
| 71 |
my $tmp = tempdir(CLEANUP => 1, TMPDIR => 1); |
| 72 |
write_file_atomic(File::Spec->canonpath("$tmp/xxx"), "data"); |
| 73 |
$fh = open_file(File::Spec->canonpath("$tmp/xxx"), '<'); |
| 74 |
is(<$fh>, "data", "read data from file"); |
| 75 |
|
| 76 |
open($fh, '>', "$tmp/foo"); |
| 77 |
print $fh "\n"; |
| 78 |
close($fh); |
| 79 |
|
| 80 |
open($fh, '>', "$tmp/bar"); |
| 81 |
print $fh "\n"; |
| 82 |
close($fh); |
| 83 |
|
| 84 |
link("$tmp/foo", "$tmp/foo2") or die "Could not create link: $!"; |
| 85 |
symlink("$tmp/foo", "$tmp/foo3") or die "Could not create link: $!"; |
| 86 |
|
| 87 |
ok(is_same_file("$tmp/foo", "$tmp/foo"), "Matching filenames"); |
| 88 |
ok(is_same_file("$tmp/foo", "$tmp/foo2"), "hard link"); |
| 89 |
ok(is_same_file("$tmp/foo", "$tmp/foo3"), "soft link"); |
| 90 |
ok(!is_same_file("$tmp/foo", "$tmp/bar"), "Different files"); |
| 91 |
|
| 92 |
# @INC may hold hooks (coderef, arrayref, blessed object) as documented in |
| 93 |
# perlvar "@INC". They are not paths, and clean_path() would turn them into |
| 94 |
# nonexistent 'CODE(0x...)' paths that then reach child processes as -I args. |
| 95 |
my ($plain, $cleaned); |
| 96 |
{ |
| 97 |
my $hook = sub { return }; |
| 98 |
local @INC = ('/some/lib', $hook, [$hook], bless({}, 'Some::INC::Hook'), '/other/lib'); |
| 99 |
|
| 100 |
$plain = [process_includes(include_current => 1)]; |
| 101 |
$cleaned = [process_includes(include_current => 1, clean => 1)]; |
| 102 |
} |
| 103 |
|
| 104 |
is($plain, ['/some/lib', '/other/lib'], "process_includes drops \@INC hooks and keeps paths in order"); |
| 105 |
ok(!(grep { m{(?:CODE|ARRAY|HASH)\(0x} } @$cleaned), "No stringified hook reached the cleaned list"); |
| 106 |
|
| 107 |
done_testing; |