The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::File -- test file attributes

SYNOPSIS

use Test::File;

DESCRIPTION

This modules provides a collection of test utilities for file attributes.

Some file attributes depend on the owner of the process testing the file in the same way the file test operators do. For instance, root (or super-user or Administrator) may always be able to read files no matter the permissions.

Some attributes don't make sense outside of Unix, either, so some tests automatically skip if they think they won't work on the platform. If you have a way to make these functions work on Windows, for instance, please send me a patch. :)

Functions

file_exists_ok( FILENAME [, NAME ] )

Ok if the file exists, and not ok otherwise.

file_not_exists_ok( FILENAME [, NAME ] )

Ok if the file does not exist, and not okay if it does exist.

file_empty_ok( FILENAME [, NAME ] )

Ok if the file exists and has empty size, not ok if the file does not exist or exists with non-zero size.

file_not_empty_ok( FILENAME [, NAME ] )

Ok if the file exists and has non-zero size, not ok if the file does not exist or exists with zero size.

file_size_ok( FILENAME, SIZE [, NAME ] )

Ok if the file exists and has SIZE size in bytes (exactly), not ok if the file does not exist or exists with size other than SIZE.

file_max_size_ok( FILENAME, MAX [, NAME ] )

Ok if the file exists and has size less than or equal to MAX bytes, not ok if the file does not exist or exists with size greater than MAX bytes.

file_min_size_ok( FILENAME, MIN [, NAME ] )

Ok if the file exists and has size greater than or equal to MIN bytes, not ok if the file does not exist or exists with size less than MIN bytes.

file_line_count_is( FILENAME, COUNT [, NAME ] )

Ok if the file exists and has COUNT lines (exactly), not ok if the file does not exist or exists with a line count than COUNT.

This function using the current value of $/ as the line ending and counts the lines by reading them and counting how many it read.

file_line_count_isnt( FILENAME, COUNT [, NAME ] )

Ok if the file exists and doesn't have exactly COUNT lines, not ok if the file does not exist or exists with a line count of COUNT. Read that carefully: the file must exist for this test to pass!

This function using the current value of $/ as the line ending and counts the lines by reading them and counting how many it read.

file_line_count_between( FILENAME, MIN, MAX, [, NAME ] )

Ok if the file exists and has a line count between MIN and MAX, inclusively.

This function using the current value of $/ as the line ending and counts the lines by reading them and counting how many it read.

file_readable_ok( FILENAME [, NAME ] )

Ok if the file exists and is readable, not ok if the file does not exist or is not readable.

file_not_readable_ok( FILENAME [, NAME ] )

Ok if the file exists and is not readable, not ok if the file does not exist or is readable.

file_writeable_ok( FILENAME [, NAME ] )

Ok if the file exists and is writeable, not ok if the file does not exist or is not writeable.

file_not_writeable_ok( FILENAME [, NAME ] )

Ok if the file exists and is not writeable, not ok if the file does not exist or is writeable.

file_executable_ok( FILENAME [, NAME ] )

Ok if the file exists and is executable, not ok if the file does not exist or is not executable.

This test automatically skips if it thinks it is on a Windows platform.

file_not_executable_ok( FILENAME [, NAME ] )

Ok if the file exists and is not executable, not ok if the file does not exist or is executable.

This test automatically skips if it thinks it is on a Windows platform.

file_mode_is( FILENAME, MODE [, NAME ] )

Ok if the file exists and the mode matches, not ok if the file does not exist or the mode does not match.

This test automatically skips if it thinks it is on a Windows platform.

Contributed by Shawn Sorichetti <ssoriche@coloredblocks.net>

file_mode_isnt( FILENAME, MODE [, NAME ] )

Ok if the file exists and mode does not match, not ok if the file does not exist or mode does match.

This test automatically skips if it thinks it is on a Windows platform.

Contributed by Shawn Sorichetti <ssoriche@coloredblocks.net>

Ok is FILENAME is a symlink, even if it points to a non-existent file. This test automatically skips if the operating system does not support symlinks. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Ok is FILENAME is a symlink and it points to a existing file. With the optional TARGET argument, the test fails if SYMLINK's target is not TARGET. This test automatically skips if the operating system does not support symlinks. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Ok if FILENAME is a symlink and if it doesn't point to a existing file. This test automatically skips if the operating system does not support symlinks. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Ok if FILENAME is a symlink and if points to TARGET. This test automatically skips if the operating system does not support symlinks. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Ok if FILENAME is a symlink and if its target is an absolute path. This test automatically skips if the operating system does not support symlinks. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

sub symlink_target_is_absolute_ok { if( _no_symlinks_here() ) { $Test->skip( "symlink_target_exists_ok doesn't work on systems without symlinks" ); return; }

        my $file = shift;
        my $name = shift || "symlink $file points to an absolute path";

my ($from, $from_base, $to, $to_base, $name) = @_; my $link = readlink( $from ); my $link_err = defined( $link ) ? '' : $!; # $! doesn't always get reset my $link_abs = abs_path( rel2abs($link, $from_base) ); my $to_abs = abs_path( rel2abs($to, $to_base) );

if (defined( $link_abs ) && defined( $to_abs ) && $link_abs eq $to_abs) { $Test->ok( 1, $name ); } else { $Test->ok( 0, $name ); $link ||= 'undefined'; $link_abs ||= 'undefined'; $to_abs ||= 'undefined'; $Test->diag(" link: $from"); $Test->diag(" got: $link"); $Test->diag(" (abs): $link_abs"); $Test->diag(" expected: $to"); $Test->diag(" (abs): $to_abs"); $Test->diag(" readlink() error: $link_err") if ($link_err); } }

Ok if the link count to FILE is LINK_COUNT. LINK_COUNT is interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok if the file does not exist.

The optional NAME parameter is the name of the test.

Ok if the link count to FILE is greater than LINK_COUNT. LINK_COUNT is interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok if the file has at least one link.

The optional NAME parameter is the name of the test.

Ok if the link count to FILE is less than LINK_COUNT. LINK_COUNT is interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok if the file has at least one link.

The optional NAME parameter is the name of the test.

owner_is( FILE , OWNER [, NAME] )

Ok if FILE's owner is the same as OWNER. OWNER may be a text user name or a numeric userid. Test skips on Dos, and Mac OS <= 9. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Contributed by Dylan Martin

owner_isnt( FILE, OWNER [, NAME] )

Ok if FILE's owner is not the same as OWNER. OWNER may be a text user name or a numeric userid. Test skips on Dos and Mac OS <= 9. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Contributed by Dylan Martin

group_is( FILE , GROUP [, NAME] )

Ok if FILE's group is the same as GROUP. GROUP may be a text group name or a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating systems that do not support getpwuid() and friends. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Contributed by Dylan Martin

group_isnt( FILE , GROUP [, NAME] )

Ok if FILE's group is not the same as GROUP. GROUP may be a text group name or a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating systems that do not support getpwuid() and friends. If the file does not exist, the test fails.

The optional NAME parameter is the name of the test.

Contributed by Dylan Martin

TO DO

* check properties for other users (readable_by_root, for instance)

* check times

* check number of links to file

* check path parts (directory, filename, extension)

SEE ALSO

Test::Builder, Test::More

SOURCE AVAILABILITY

This module is in Github:

        git://github.com/briandfoy/test-file.git

AUTHOR

brian d foy, <bdfoy@cpan.org>

CREDITS

Shawn Sorichetti <ssoriche@coloredblocks.net> provided some functions.

Tom Metro helped me figure out some Windows capabilities.

Dylan Martin added owner_is and owner_isnt

David Wheeler added file_line_count_is.

COPYRIGHT AND LICENSE

Copyright (c) 2002-2009 brian d foy. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.