NAME
SimpleMock::Mocks::Path::Tiny - mocks for testing Path::Tiny code
DESCRIPTION
This module overrides methods in Path::Tiny to allow you to unit test code with Path::Tiny in it.
It currently doesn't mock everything, but covers a lot of use cases.
I don't have any production code using this module, so I've written what I think are core mocks. If you have a specific use case that could do with mocking, or spot issues that affect usage, please implement via a pull request (or just request it and I'll implement when I have time)
USAGE
use SimpleMock qw(register_mocks);
register_mocks(
PATH_TINY => {
'/path/to/dir/pr/file' => {
# if data is set, it's implicitly a file, otherwise it's a directory
data => $file_content,
# these are all true by default, but you can set to false for them to throw
# or return false as noted
assert => 0, # throws
exists => 0, # return 0
has_same_bytes => 0, # return 0 - value is hard coded for ALL comparisons on a mock
# returns this hard coded value for the stat - set as appropriate (obviously fake below)
stat => [1,2,3,4],
# digest hash for the mock. Set as appropriate if calling digest()
digest => '1a2b3c4d536f',
},
}
);
For basic usage, you just need this:
register_mocks(
PATH_TINY => {
# file MUST have a data attribute
'/path/to/file.txt' => { data => 'file content' },
# directory must NOT have a data attribute
'/path/to/dir' => {},
}
);
MOCK ATTRIBUTES
These are all valid keys for mock attributes:
data
The data to return if the file was read via slurp or lines (and their raw and utf8 variants). Setting this attribute implicitly defines the mock as a file, omitting it implies a directory.
It is up to you to ensure utf8/raw values are set as expected.
assert
All calls on the mock to assert() return true by default. If you set this to 0, all calls to assert throw instead. Hard coded on a per mock basis.
exists
All calls on the mock to exists() return true by default. If you set this to 0, it returns a false value.
has_same_bytes
All calls on the mock to has_same_bytes() return true by default. If you set this to 0, it returns a false value.
stat
Arrayref to return when calling stat. Example above is obviously fake, so amend to suit your tests needs as appropriate.
This is also used for the return value of lstat.
digest
Hard coded digest value to return for all calls to digest() on the mock.
size()
This is set to the length of the data attribute.
A few notes
copy()
If you are using copy, there's small differences in behavior between copy to file and copy to a directory
my $p = path('/path/to/file.txt');
# copy to an explicit file - no mock is needed for the target
$p->copy('/path/to/file.txt')
# copy to a directory you must set the target dir as a mock
$p->copy('/path/to/dir');
ie, in your `register` mocks call, you must have:
'/path/to/dir' => {},
so that the code knows that the path '/path/to/dir' is not a file.
Note: if you explicitly set a mock for the target file, this will get overridden when making the copy.
children()
All children MUST be defined as mocks. Grep is used to retrieve immediate children in the mocks code.
Not Windows friendly (`/` path seperator expected).
Sorry. First stab at this, and I don't have a Windows system I can tweak and test this on. If someone wants to add in functionality for it to work in Windows, please do.
No recursion mocking
`iterator` does NOT recurse child directories. If the flag is set, an exception is thrown.
`visit` uses `iterator`, so an exception is thrown if you set the `recurse` argument.
If recursion is really needed, it can be added, but so far it's not implemented.