Test::File(3) User Contributed Perl Documentation Test::File(3)

Test::File -- test file attributes

  use Test::File;

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. :) IF you want to pretend to be Windows on a non-Windows machine (for instance, to test "skip()"), you can set the "PRETEND_TO_BE_WINDOWS" environment variable.

The optional NAME parameter for every function allows you to specify a name for the test. If not supplied, a reasonable default will be generated.

Ok if the file exists, and not ok otherwise.
Ok if the file does not exist, and not okay if it does exist.
Ok if the file exists and has empty size, not ok if the file does not exist or exists with non-zero size.
Ok if the file exists and has non-zero size, not ok if the file does not exist or exists with zero size.
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.
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.
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.
Ok if the file exists and has COUNT lines (exactly), not ok if the file does not exist or exists with a line count other than COUNT.

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

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 uses the current value of $/ as the line ending and counts the lines by reading them and counting how many it read.

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

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

Ok if the file exists and its contents (as one big string) match PATTERN, not ok if the file does not exist, is not readable, or exists but doesn't match PATTERN.

Since the file contents are read into memory, you should not use this for large files. Besides memory consumption, test diagnostics for failing tests might be difficult to decipher. However, for short files this works very well.

Because the entire contents are treated as one large string, you can make a pattern that tests multiple lines. Don't forget that you may need to use the /s modifier for such patterns:

        # make sure file has one or more paragraphs with CSS class X
        file_contains_like($html_file, qr{<p class="X">.*?</p>}s);
    

Contrariwise, if you need to match at the beginning or end of a line inside the file, use the /m modifier:

        # make sure file has a setting for foo
        file_contains_like($config_file, qr/^ foo \s* = \s* \w+ $/mx);
    

If you want to test your file contents against multiple patterns, but don't want to have the file read in repeatedly, you can pass an arrayref of patterns instead of a single pattern, like so:

        # make sure our template has rendered correctly
        file_contains_like($template_out,
                [
                qr/^ $title_line $/mx,
                map { qr/^ $_ $/mx } @chapter_headings,
                qr/^ $footer_line $/mx,
                ]);
    

Please note that if you do this, and your file does not exist or is not readable, you'll only get one test failure instead of a failure for each pattern. This could cause your test plan to be off, although you may not care at that point because your test failed anyway. If you do care, either skip the test plan altogether by employing Test::More's "done_testing()" function, or use "file_readable_ok" in conjunction with a "SKIP" block.

Contributed by Buddy Burden "<barefoot@cpan.org>".

Ok if the file exists and its contents (as one big string) do not match PATTERN, not ok if the file does not exist, is not readable, or exists but matches PATTERN.

All notes and caveats for "file_contains_like" apply to this function as well.

Contributed by Buddy Burden "<barefoot@cpan.org>".

The same as "file_contains_like", except the file is opened as UTF-8.
The same as "file_contains_unlike", except the file is opened as UTF-8.
The same as "file_contains_like", except the file is opened with ENCODING
The same as "file_contains_unlike", except the file is opened with ENCODING.
Ok if the file exists and is readable, not ok if the file does not exist or is not readable.
Ok if the file exists and is not readable, not ok if the file does not exist or is readable.
Ok if the file exists and is writeable, not ok if the file does not exist or is not writeable.
Ok if the file exists and is not writeable, not ok if the file does not exist or is writeable.
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.

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.

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

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 if the file exists and has all the bits in mode turned on, not ok if the file does not exist or the mode does not match. That is, "FILEMODE & MODE == MODE" must be true.

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

Contributed by Ricardo Signes "<rjbs@cpan.org>"

Ok if the file exists and has all the bits in mode turned off, not ok if the file does not exist or the mode does not match. That is, "FILEMODE & MODE == 0" must be true.

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

Contributed by Ricardo Signes "<rjbs@cpan.org>"

Ok if 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.
Ok if 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.
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.
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.
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.
Ok if the file exists and is a directory, not ok if the file doesn't exist, or exists but isn't a directory.

Contributed by Buddy Burden "<barefoot@cpan.org>".

Ok if the directory exists and contains the file, not ok if the directory doesn't exist, or exists but doesn't contain the file.

Contributed by Buddy Burden "<barefoot@cpan.org>".

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

Contributed by Dylan Martin

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.

Contributed by Dylan Martin

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.

Contributed by Dylan Martin

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.

Contributed by Dylan Martin

Ok if FILE's modified time is WITHIN_SECONDS inclusive of the system's current time. This test uses stat() to obtain the mtime. If the file does not exist the test returns failure. If stat() fails, the test is skipped.
Ok if FILE's mtime is > UNIXTIME. This test uses stat() to get the mtime. If stat() fails this test is skipped. If FILE does not exist, this test fails.
Ok if FILE's modified time is < UNIXTIME. This test uses stat() to get the mtime. If stat() fails this test is skipped. If FILE does not exist, this test fails.

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

* check times

* check number of links to file

* check path parts (directory, filename, extension)

Test::Builder, Test::More

This module is in Github:

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

brian d foy, "<bdfoy@cpan.org>"

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

Buddy Burden "<barefoot@cpan.org>" provided "dir_exists_ok", "dir_contains_ok", "file_contains_like", and "file_contains_unlike".

xmikew "<https://github.com/xmikew>" provided the "mtime_age" stuff.

Copyright © 2002-2016, brian d foy <bdfoy@cpan.org>. All rights reserved.

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

2017-04-17 perl v5.34.0