-
Notifications
You must be signed in to change notification settings - Fork 9
/
README
492 lines (345 loc) · 16.3 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
NAME
Test::MockFile - Allows tests to validate code that can interact with
files without touching the file system.
VERSION
Version 0.029
SYNOPSIS
Intercepts file system calls for specific files so unit testing can take
place without any files being altered on disk.
This is useful for small tests
<https://testing.googleblog.com/2010/12/test-sizes.html> where file
interaction is discouraged.
A strict mode is even provided (and turned on by default) which can
throw a die when files are accessed during your tests!
# Loaded before Test::MockFile so uses the core perl functions without any hooks.
use Module::I::Dont::Want::To::Alter;
# strict mode by default
use Test::MockFile ();
# non-strict mode
use Test::MockFile qw< nostrict >;
# Be sure to assign the output of mocks, they disappear when they go out of scope
my $foobar = Test::MockFile->file( "/foo/bar", "contents\ngo\nhere" );
open my $fh, '<', '/foo/bar' or die; # Does not actually open the file on disk
say '/foo/bar exists' if -e $fh;
close $fh;
say '/foo/bar is a file' if -f '/foo/bar';
say '/foo/bar is THIS BIG: ' . -s '/foo/bar';
my $foobaz = Test::MockFile->file('/foo/baz'); # File starts out missing
my $opened = open my $baz_fh, '<', '/foo/baz'; # File reports as missing so fails
say '/foo/baz does not exist yet' if !-e '/foo/baz';
open $baz_fh, '>', '/foo/baz' or die; # open for writing
print {$baz_fh} "first line\n";
open $baz_fh, '>>', '/foo/baz' or die; # open for append.
print {$baz_fh} "second line";
close $baz_fh;
say "Contents of /foo/baz:\n>>" . $foobaz->contents() . '<<';
# Unmock your file.
# (same as the variable going out of scope
undef $foobaz;
# The file check will now happen on file system now the file is no longer mocked.
say '/foo/baz is missing again (no longer mocked)' if !-e '/foo/baz';
my $quux = Test::MockFile->file( '/foo/bar/quux.txt', '' );
my @matches = </foo/bar/*.txt>;
# ( '/foo/bar/quux.txt' )
say "Contents of /foo/bar directory: " . join "\n", @matches;
@matches = glob('/foo/bar/*.txt');
# same as above
say "Contents of /foo/bar directory (using glob()): " . join "\n", @matches;
IMPORT
When the module is loaded with no parameters, strict mode is turned on.
Any file checks, "open", "sysopen", "opendir", "stat", or "lstat" will
throw a die.
For example:
use Test::MockFile;
# This will not die.
my $file = Test::MockFile->file("/bar", "...");
my $symlink = Test::MockFile->symlink("/foo", "/bar");
-l '/foo' or print "ok\n";
open my $fh, '>', '/foo';
# All of these will die
open my $fh, '>', '/unmocked/file'; # Dies
sysopen my $fh, '/other/file', O_RDONLY;
opendir my $fh, '/dir';
-e '/file';
-l '/file';
If we want to load the module without strict mode:
use Test::MockFile qw< nostrict >;
Relative paths are not supported:
use Test::MockFile;
# Checking relative vs absolute paths
$file = Test::MockFile->file( '/foo/../bar', '...' ); # not ok - relative path
$file = Test::MockFile->file( '/bar', '...' ); # ok - absolute path
$file = Test::MockFile->file( 'bar', '...' ); # ok - current dir
file_arg_position_for_command
Args: ($command)
Provides a hint with the position of the argument most likely holding
the file name for the current $command call.
This is used internaly to provide better error messages. This can be
used when plugging hooks to know what's the filename we currently try to
access.
SUBROUTINES/METHODS
file
Args: ($file, $contents, $stats)
This will make cause $file to be mocked in all file checks, opens, etc.
"undef" contents means that the file should act like it's not there. You
can only set the stats if you provide content.
If you give file content, the directory inside it will be mocked as
well.
my $f = Test::MockFile->file( '/foo/bar' );
-d '/foo' # not ok
my $f = Test::MockFile->file( '/foo/bar', 'some content' );
-d '/foo' # ok
See "Mock Stats" for what goes into the stats hashref.
file_from_disk
Args: "($file_to_mock, $file_on_disk, $stats)"
This will make cause $file to be mocked in all file checks, opens, etc.
If "file_on_disk" isn't present, then this will die.
See "Mock Stats" for what goes into the stats hashref.
symlink
Args: ($readlink, $file )
This will cause $file to be mocked in all file checks, opens, etc.
$readlink indicates what "fake" file it points to. If the file $readlink
points to is not mocked, it will act like a broken link, regardless of
what's on disk.
If $readlink is undef, then the symlink is mocked but not present.(lstat
$file is empty.)
Stats are not able to be specified on instantiation but can in theory be
altered after the object is created. People don't normally mess with the
permissions on a symlink.
dir
Args: ($dir)
This will cause $dir to be mocked in all file checks, and "opendir"
interactions.
The directory name is normalized so any trailing slash is removed.
$dir = Test::MockFile->dir( 'mydir/', ... ); # ok
$dir->path(); # mydir
If there were previously mocked files (within the same scope), the
directory will exist. Otherwise, the directory will be nonexistent.
my $dir = Test::MockFile->dir('/etc');
-d $dir; # not ok since directory wasn't created yet
$dir->contents(); # undef
# Now we can create an empty directory
mkdir '/etc';
$dir_etc->contents(); # . ..
# Alternatively, we can already create files with ->file()
$dir_log = Test::MockFile->dir('/var');
$file_log = Test::MockFile->file( '/var/log/access_log', $some_content );
$dir_log->contents(); # . .. access_log
# If you create a nonexistent file but then give it content, it will create
# the directory for you
my $file = Test::MockFile->file('/foo/bar');
my $dir = Test::MockFile->dir('/foo');
-d '/foo' # false
-e '/foo/bar'; # false
$dir->contents(); # undef
$file->contents('hello');
-e '/foo/bar'; # true
-d '/foo'; # true
$dir->contents(); # . .. bar
NOTE: Because "." and ".." will always be the first things "readdir"
returns, These files are automatically inserted at the front of the
array. The order of files is sorted.
If you want to affect the stat information of a directory, you need to
use the available core Perl keywords. (We might introduce a special
helper method for it in the future.)
$d = Test::MockFile->dir( '/foo', [], { 'mode' => 0755 } ); # dies
$d = Test::MockFile->dir( '/foo', undef, { 'mode' => 0755 } ); # dies
$d = Test::MockFile->dir('/foo');
mkdir $d, 0755; # ok
Mock Stats
When creating mocked files or directories, we default their stats to:
my $attrs = Test::MockFile->file( $file, $contents, {
'dev' => 0, # stat[0]
'inode' => 0, # stat[1]
'mode' => $mode, # stat[2]
'nlink' => 0, # stat[3]
'uid' => int $>, # stat[4]
'gid' => int $), # stat[5]
'rdev' => 0, # stat[6]
'atime' => $now, # stat[8]
'mtime' => $now, # stat[9]
'ctime' => $now, # stat[10]
'blksize' => 4096, # stat[11]
'fileno' => undef, # fileno()
} );
You'll notice that mode, size, and blocks have been left out of this.
Mode is set to 666 (for files) or 777 (for directories), xored against
the current umask. Size and blocks are calculated based on the size of
'contents' a.k.a. the fake file.
When you want to override one of the defaults, all you need to do is
specify that when you declare the file or directory. The rest will
continue to default.
my $mfile = Test::MockFile->file("/root/abc", "...", {inode => 65, uid => 123, mtime => int((2000-1970) * 365.25 * 24 * 60 * 60 }));
my $mdir = Test::MockFile->dir("/sbin", "...", { mode => 0700 }));
new
This class method is called by file/symlink/dir. There is no good reason
to call this directly.
contents
Optional Arg: $contents
Retrieves or updates the current contents of the file.
Only retrieves the content of the directory (as an arrayref). You can
set directory contents with calling the "file()" method described above.
Symlinks have no contents.
filename
Deprecated. Same as "path".
path
The path (filename or dirname) of the file or directory this mock object
is controlling.
unlink
Makes the virtual file go away. NOTE: This also works for directories.
touch
Optional Args: ($epoch_time)
This function acts like the UNIX utility touch. It sets atime, mtime,
ctime to $epoch_time.
If no arguments are passed, $epoch_time is set to time(). If the file
does not exist, contents are set to an empty string.
stat
Returns the stat of a mocked file (does not follow symlinks.)
readlink
Optional Arg: $readlink
Returns the stat of a mocked file (does not follow symlinks.) You can
also use this to change what your symlink is pointing to.
is_link
returns true/false, depending on whether this object is a symlink.
is_dir
returns true/false, depending on whether this object is a directory.
is_file
returns true/false, depending on whether this object is a regular file.
size
returns the size of the file based on its contents.
exists
returns true or false based on if the file exists right now.
blocks
Calculates the block count of the file based on its size.
chmod
Optional Arg: $perms
Allows you to alter the permissions of a file. This only allows you to
change the 07777 bits of the file permissions. The number passed should
be the octal 0755 form, not the alphabetic "755" form
permissions
Returns the permissions of the file.
mtime
Optional Arg: $new_epoch_time
Returns and optionally sets the mtime of the file if passed as an
integer.
ctime
Optional Arg: $new_epoch_time
Returns and optionally sets the ctime of the file if passed as an
integer.
atime
Optional Arg: $new_epoch_time
Returns and optionally sets the atime of the file if passed as an
integer.
add_file_access_hook
Args: ( $code_ref )
You can use add_file_access_hook to add a code ref that gets called
every time a real file (not mocked) operation happens. We use this for
strict mode to die if we detect your program is unexpectedly accessing
files. You are welcome to use it for whatever you like.
Whenever the code ref is called, we pass 2 arguments:
"$code->($access_type, $at_under_ref)". Be aware that altering the
variables in $at_under_ref will affect the variables passed to open /
sysopen, etc.
One use might be:
Test::MockFile::add_file_access_hook(sub { my $type = shift; print "$type called at: " . Carp::longmess() } );
clear_file_access_hooks
Calling this subroutine will clear everything that was passed to
add_file_access_hook
How this mocking is done:
Test::MockFile uses 2 methods to mock file access:
-X via Overload::FileCheck
It is currently not possible in pure perl to override stat
<http://perldoc.perl.org/functions/stat.html>, lstat
<http://perldoc.perl.org/functions/lstat.html> and -X operators
<http://perldoc.perl.org/functions/-X.html>. In conjunction with this
module, we've developed Overload::FileCheck.
This enables us to intercept calls to stat, lstat and -X operators (like
-e, -f, -d, -s, etc.) and pass them to our control. If the file is
currently being mocked, we return the stat (or lstat) information on the
file to be used to determine the answer to whatever check was made. This
even works for things like "-e _". If we do not control the file in
question, we return "FALLBACK_TO_REAL_OP()" which then makes a normal
check.
CORE::GLOBAL:: overrides
Since 5.10, it has been possible to override function calls by defining
them. like:
*CORE::GLOBAL::open = sub(*;$@) {...}
Any code which is loaded AFTER this happens will use the alternate open.
This means you can place your "use Test::MockFile" statement after
statements you don't want to be mocked and there is no risk that the
code will ever be altered by Test::MockFile.
We oveload the following statements and then return tied handles to
enable the rest of the IO functions to work properly. Only open /
sysopen are needed to address file operations. However opendir file
handles were never setup for tie so we have to override all of opendir's
related functions.
* open
* sysopen
* opendir
* readdir
* telldir
* seekdir
* rewinddir
* closedir
CAEATS AND LIMITATIONS
DEBUGGER UNDER STRICT MODE
If you want to use the Perl debugger (perldebug) on any code that uses
Test::MockFile in strict mode, you will need to load Term::ReadLine
beforehand, because it loads a file. Under the debugger, the debugger
will load the module after Test::MockFile and get mad.
# Load it from the command line
perl -MTerm::ReadLine -d code.pl
# Or alternatively, add this to the top of your code:
use Term::ReadLine
FILENO IS UNSUPPORTED
Filehandles can provide the file descriptor (in number) using the
"fileno" keyword but this is purposefully unsupported in Test::MockFile.
The reaosn is that by mocking a file, we're creating an alternative file
system. Returning a "fileno" (file descriptor number) would require
creating file descriptor numbers that would possibly conflict with the
file desciptors you receive from the real filesystem.
In short, this is a recipe for buggy tests or worse - truly destructive
behavior. If you have a need for a real file, we suggest File::Temp.
BAREWORD FILEHANDLE FAILURES
There is a particular type of bareword filehandle failures that cannot
be fixed.
These errors occur because there's compile-time code that uses bareword
filehandles in a function call that cannot be expressed by this module's
prototypes for core functions.
The only solution to these is loading `Test::MockFile` after the other
code:
This will fail:
# This will fail because Test2::V0 will eventually load Term::Table::Util
# which calls open() with a bareword filehandle that is misparsed by this module's
# opendir prototypes
use Test::MockFile ();
use Test2::V0;
This will succeed:
# This will succeed because open() will be parsed by perl
# and only then we override those functions
use Test2::V0;
use Test::MockFile ();
(Using strict-mode will not fix it, even though you should use it.)
AUTHOR
Todd Rinaldo, "<toddr at cpan.org>"
BUGS
Please report any bugs or feature requests to
<https://github.com/CpanelInc/Test-MockFile>.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::MockFile
You can also look for information at:
* CPAN Ratings
<https://cpanratings.perl.org/d/Test-MockFile>
* Search CPAN
<https://metacpan.org/release/Test-MockFile>
ACKNOWLEDGEMENTS
Thanks to Nicolas R., "<atoomic at cpan.org>" for help with
Overload::FileCheck. This module could not have been completed without
it.
LICENSE AND COPYRIGHT
Copyright 2018 cPanel L.L.C.
All rights reserved.
<http://cpanel.net>
This is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See perlartistic.