in reply to Re: mocking or trapping system calls
in thread mocking or trapping system calls

The script runs a handful of root-owned commands and parses the output to gather information into a reporting system.

For my own sake I'd like to add some basic automated tests which make it simple to run this and other scripts as a non-root user, both for development purposes, and to have some regression tests in place.

It's code I currently control, so refactoring is an option, but I'd rather avoid refactoring something which works into something that imposes what may be perceived as pointless complexity on future custodians.

So, I was hoping for was a module someone had already written, or a simple trick, which could do this fairly transparently and effortlessly.

By the way, thanks for the suggestions so far, it's always educational to ask for help. I quite like the PATH idea, and overriding readpipe is in fact doable in the same way as it is for system, but a quick experiment suggests that it's not that easy to change the behaviour of backticks:

$ perl -le 'BEGIN { *CORE::GLOBAL::readpipe = sub { print "hello $_[0 +]" } } readpipe "foo"; `bar` ' hello foo

Replies are listed 'Best First'.
Re^3: mocking or trapping system calls
by Anonymous Monk on May 12, 2008 at 17:38 UTC

    Trapping all exits from perl may indeed make your code too complex.

    I like pc88mxer's idea above of controlling your test environment with a VM or chroot solution. This way you can "mock" the expected, root owned external commands as you see fit w/o touching your perl code.