cleanup-wrapper
Provide functions that leave a mess behind and break unit testing with some autocleaning capabilities
Purpose
Considering func
a dirty function that leaves some shared ressources/variables in an unwanted state because of an uncaught exception or some oversight in the design, cleanWrapper(func, options)
returns a wrapped func
that will restore the ressources/variables to their proper state after each run.
Simple usage
In the following example, an identity method is poorly overridden and a side-effect snowballs on each call. Using cleanupWrapper with an after
option, the side-effect is removed.
; let obj = {return x;};const id = objid; { const id = objid; obj { return ; } return objidx;} ; // Returns 'overridden: Hello'; // Returns 'overridden: overridden: World'; // Returns 'overridden: overridden: overridden: Hello'; // Returns 'overridden: overridden: overridden: overridden: World' objid = id; // Restores obj const clean = ; ; // Returns 'overridden: Hello'; // Returns 'overridden: World'; // Returns 'overridden: Hello'; // Returns 'overridden: World'
Options
Default options
before
: A function to be run prior to calling the wrapped function.after
: A function that cleans up after calling the wrapped function, whether it threw an exception or not.
Custom options
You can pass any custom option you like. They can be accessed within the optional functions before
and after
using the this
keyword.
; var hello = 'Hello'; { hello = 'Bye'; return hello;} const clean = ; === 'Bye'; // truehello === 'Bye'; // true === 'Bye'; // truehello === 'Hello'; // true
Convenience wrappers
tmpDir(pathName, func)
tmpDir ensures that a directory is actually temporary. Before executing any function, it will check for the existence of the directory and throw an error if it finds it. Upon completion of the function or upon encountering an exception while running it, it will remove the tmp dir automatically.
As a convenience, pathname
can actually be an array of pathnames.
; const dirName = '.tmp'; const dirty = { // Create dirName directory and do stuff in it} const clean = ; ; // Executes dirty(), creating dirName, then removing it on completion or on exception
chDir(pathName, func)
chDir allows to have a temporary working direction for your function and ensures that whatever happens the working directory will be changed back to its original value.
; const cwd = process;const dirName = '.tmp'; const dirty = { process;} const clean = ; // process.cwd() is cwd; // process.cwd(): cwd -> cwd/foo// process.cwd() is cwd/foo; // process.cwd(): cwd/foo -> .tmp -> ./tmp/foo -> cwd// process.cwd() is cwd
overrideMethod(object, methodName, newMethod, func)
overrideMethod allows to encapsulate a temporary method override. It will first override a specified method with some function passed as argument. Then it will run the code where the method should be overridden, and finally it will restore the method automatically.
; const original = { console; console;} const log = consolelog;const overridden = ; console; // Prints 'Hello\n'; // Prints 'Hello\nHello\n'; // Prints 'overridden: Hello\noverridden: Hello\n'console; // Prints 'Hello\n'
License
cleanup-wrapper is MIT licensed.
© 2016-2017 Jason Lenoble