NAME
Test::Mock::Cmd - Mock system(), exec(), and qx() for testing
VERSION
This document describes Test::Mock::Cmd version 0.4
SYNOPSIS
use Test::Mock::Cmd 'system' => \&my_cmd_mocker, 'qr' => \&my_cmd_mocker;
or
use Test::Mock::Cmd \&my_cmd_mocker;
or
use Test::Mock::Cmd \&my_mock_system, \&my_mock_exec, \&my_mock_qx;
DESCRIPTION
Mock system(), exec(), qx() (AKA `` and readpipe()) with your own functions in order to test code that may call them.
Some uses might be:
avoid actually running the system command, just pretend we did (simulate [un]expected output, return values, etc)
test various return value handling (e.g. the system command core dumps how does the object handle that)
test that the arguments that will be passed to a system command are correct
etc etc
INTERFACE
Commence mocking
Per the synopsis, you can provide import() with a hash whose keys are 'system', 'exec', or 'qr' and whose values are the code reference you want to replace the key's functionality with, 1 code reference to replace all 3 functions or 3 code references to replace system(), exec(), and qx() (in that order).
Caveat
Any code loaded before the mock functions are setup will retain normal system(), etc behavior. (even if the system() does not happen until much later!)
use X; # has functions that call system()
use Test::Mock::Cmd ...
use Y; # has functions that call system()
X::i_call_system(...); # normal system() happens
Y::i_call_system(...); # mocked system() happens
Getting access to the original, un-mocked, functionality.
None of these are exportable.
- Test::Mock::Cmd::orig_system()
-
Original, not-mocked "system_LIST" in perlfunc
- Test::Mock::Cmd::orig_exec()
-
Original, not-mocked "exec" in perlfunc
- Test::Mock::Cmd::orig_qx()
-
Original, not-mocked "readpipe" in perlfunc
DIAGNOSTICS
Not a CODE reference
-
The given value is not a code reference and should be.
Key is not system, exec, or qr
-
A key in your argument hash is invalid.
Test::Mock::Cmd->import() requires a hash, 1 code reference, or 3 code references as arguments
-
You are not passing in the required one or three arguments.
CONFIGURATION AND ENVIRONMENT
Test::Mock::Cmd requires no configuration files or environment variables.
DEPENDENCIES
None.
INCOMPATIBILITIES
None reported.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-test-mock-cmd@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
See Also
Test::MockCommand for a more complex (and much heavier) object based approach to this.
AUTHOR
Daniel Muey <http://drmuey.com/cpan_contact.pl>
LICENCE AND COPYRIGHT
Copyright (c) 2011 cPanel, Inc. <copyright@cpanel.net>>
. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.