Chris Dolan


Test::Virtual::Filesystem - Validate a filesystem


    use Test::Virtual::Filesystem;
    Test::Virtual::Filesystem->new({mountdir => '/path/to/test'})->runtests;

or with more customization:

    use Test::Virtual::Filesystem;
    my $test = Test::Virtual::Filesystem->new({mountdir => '/path/to/test', compatible => '0.03'});

See the file t/filesys.t in this distribution or the file t/fusepdf.t in the Fuse::PDF distribution for thorough examples.

WARNING: all of the files in the mountdir will be deleted in the teardown method so BE CAREFUL that you specify the right folder!


Copyright 2008 Chris Dolan,

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


If you are creating a filesystem, say via Fuse or Filesys::Virtual, you need a fairly mundane set of tests to try out lots of typical filesystem operations. This package attempts to accumulate a bunch of those tests into a handy suite to make it easier for you to test your filesystem.

This suite is based on Test::Class, a fantastic library for organizing tests into bite-sized bundles. The power of Test::Class lets you select a subset of tests to run at author time. For example, when I was working on the extended attribute (aka xattr) tests, I found myself typing this:

  env TEST_METHOD='xattr_.*' perl -Ilib t/filesys.t

which runs just the test methods that begin with xattr_.

There are several methods that let you turn on or off a subset of the tests. For example, if you do not intend that your filesystem will support symbolic links, you can invoke $test-enable_test_symlink(0)> in your test program just before you call $test-runtests>.


Every time I add a new test to this suite, I annotate it with a version number. If client code specifies an expected version number (say, 1.10) and it's running against a newer version or this module (say, 1.20) then any newer test will be marked as a TODO test. That way if the test fails, it won't regress published code that used to work.

This policy will allow us to continue adding new filesystem tests without worrying about breaking existing CPAN modules.


This module needs a more complete suite of test cases. In particular, tests are needed for the following filesystem features:

    seek/rewinddir, tell/telldir
    read, sysread, syswrite
    overwrite (with open '+<')
    deep directories
    very full directories
    large files
    filenames with spaces
    non-ASCII filenames (maybe constructor should specify the encoding?)
    special file types (fifos, sockets, character and block devices, etc)
    binmode, non-binmode
    statfs (AKA `df` or `mount`)
    rename corner cases:
     * dest inside src
     * src or dest leaf is '.' or '..'
     * src or dest is FS root
     * dest leaf is symlink
    threading and re-entrancy
    file locking?
    async I/O??

Any help writing tests (or adapting tests from existing suites) will be appreciated!


This module is a subclass of Test::Class. All methods from that class are available, particularly runtests().

$pkg->new({mountdir => $mountdir, ...})

Create a new test suite which will operate on files contained within the specified mount directory. WARNING: any and all files and folders in that mount directory will be deleted!

The supported options are:


This required property indicates where tests should run.


Specify a Test::Virtual::Filesystem version number that is known to work. If the actual Test::Virtual::Filesystem version number is greater, then any test cases added after the specified compatible version are considered TODO tests. See Test::More for details about TODO tests.


Invoked just before then end of new(). This exists solely for subclassing convenience. This implementation does nothing.


The following accessor/mutator methods exist to turn on/off various features. They all behave in usual Perl fashion: with no argument, they return the current value. With one argument, they set the current value and return the newly set value.


As a getter, checks whether all of the other tests are enabled.

As a setter, turns on/off all the tests.


Default false.


Default true. If set false, it also sets atime, mtime and ctime false.


Default false.


Default true.


Default true.


Default false.


Default true. If set false, it also sets fifo false.


Default false. AKA named pipes.


Default true, except for platforms that do not support symlinks (for example MSWin32 and cygwin) as determined by $Config::Config{d_symlink}.


AKA the link() function. Default true. If set false, this also sets nlink false.


Count hard links. Default true.


Default false.



Runs before every test to prepare a directory for testing.


Runs after every test to clean up the test directory so the next test will have a clean workspace.


A subroutine attribute used to flag the Test::Virtual::Filesystem version number when that test was introduced. It's used like this:

  sub open_nonexistent_file : Tests(1) : Introduced('0.02') {
     ok(!open(my $f, '<', '/tmp/no_such_file'));

This is a subroutine attribute to specify one or more features used in the test. The features should be listed as a comma-separated list:

  sub symlink_create : Tests(1) : Features('symlink') {
     ok(symlink($src, $dest));
  sub symlink_permissions : Tests(2) : Features('symlink, permissions') {
     ok(symlink($src, $dest));
     ok(-w $dest);

Subfeatures must be separated from their parent features by a /. For example:

  sub atime_mtime_set : Tests(1) : Features('time/atime, time/mtime') {
     my $now = time;
     ok(utime($now, $now, $file));

Look at the source code for %feature_defaults to see the supported features and subfeatures. The enable_test_* methods above describe the all the features, but in those methods the subfeature names are flattened.

stat_dir(), introduced in v0.01
read_dir(), introduced in v0.01
read_dir_fail(), introduced in v0.01
read_file_fail(), introduced in v0.01
write_empty_file(), introduced in v0.01
write_file(), introduced in v0.01
write_file_subdir_fail(), introduced in v0.01
write_append_file(), introduced in v0.01
write_read_file(), introduced in v0.01
write_read_file_binary(), introduced in v0.08
write_mkdir(), introduced in v0.01
write_mkdir_fail(), introduced in v0.01
write_rmdir(), introduced in v0.01
write_subdir(), introduced in v0.01
truncate_file(), introduced in v0.06
truncate_no_file(), introduced in v0.06
truncate_file_no_dir(), introduced in v0.06
truncate_dir(), introduced in v0.06
time_mtime_create(), introduced in v0.06
time_ctime_create(), introduced in v0.06
time_mtime_set(), introduced in v0.06
time_atime_set(), introduced in v0.06
xattr_list(), introduced in v0.02
xattr_set(), introduced in v0.02
rename_file(), introduced in v0.08
rename_file_exists(), introduced in v0.08
rename_file_self(), introduced in v0.08
rename_file_subdir(), introduced in v0.08
rename_file_missing_src(), introduced in v0.08
rename_file_missing_srcdir(), introduced in v0.08
rename_file_missing_destdir(), introduced in v0.08
rename_dir(), introduced in v0.08
rename_dir_exists(), introduced in v0.08
rename_dir_notempty(), introduced in v0.08
rename_dir_self(), introduced in v0.08
rename_dir_subdir(), introduced in v0.08
rename_mismatch_dir(), introduced in v0.08
rename_mismatch_file(), introduced in v0.08
rename_symlink(), introduced in v0.08


These are some coding/design rules for the tests:

Use only core filesystem functions

Don't use File::Slurp, File::Path, etc. because they abstract filesystem operations and make it less clear what we're testing.

Keep the tests small

Test as little as possible in each method. Let authors know what's failed by the pattern of failing tests. This also helps avoid needing to edit the tests later.

Avoid editing methods

Don't break published CPAN code. If you want to test something new, write a new method.

Try to use a few different filesystem functions as practical in one method

For example, if you're testing chmod, don't mkdir or chown unless you're writing a chmod_mkdir_chown test.

Minimize test infrastructure

Use method attributes and Test::Class features to keep the test methods really simple.





Chris Dolan,