Skip to content

Commit

Permalink
Merge branch 'master' into gh-121
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Dec 26, 2017
2 parents dc8d960 + 89d3114 commit b451cab
Show file tree
Hide file tree
Showing 8 changed files with 1,952 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .codeclimate.yml
Expand Up @@ -2,5 +2,8 @@ engines:
eslint:
enabled: true
channel: "eslint-3"
ratings:
paths:
- "lib/**/*.js"
exclude_paths:
- "docs/"
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -3,6 +3,9 @@ module.exports = {
"es6": true,
"node": true
},
"plugins": [
"mocha"
],
"extends": "eslint:recommended",
"rules": {
"indent": [
Expand Down
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules/
coverage/
.idea/
.*.swp
6 changes: 6 additions & 0 deletions .istanbul.yml
@@ -0,0 +1,6 @@
check:
global:
statements: 100
lines: 100
branches: 100
functions: 100
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -16,3 +16,8 @@ before_install:
# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
addons:
code_climate:
repo_token: ca200a6930ee90adbc4192a3bdb9f60dccdae00cb78f9880a2841d6b
after_success:
- codeclimate-test-reporter < coverage/lcov.info
36 changes: 32 additions & 4 deletions lib/tmp.js
Expand Up @@ -97,10 +97,12 @@ function _isUndefined(obj) {
* @private
*/
function _parseArguments(options, callback) {
/* istanbul ignore else */
if (typeof options == 'function') {
return [{}, options];
}

/* istanbul ignore else */
if (_isUndefined(options)) {
return [{}, callback];
}
Expand All @@ -116,14 +118,17 @@ function _parseArguments(options, callback) {
* @private
*/
function _generateTmpName(opts) {
/* istanbul ignore else */
if (opts.name) {
return path.join(opts.dir || tmpDir, opts.name);
}

// mkstemps like template
/* istanbul ignore else */
if (opts.template) {
var template = opts.template;
// make sure that we prepend the tmp path if none was given
/* istanbul ignore else */
if (path.basename(template) == template)
template = path.join(opts.dir || tmpDir, template);
return template.replace(TEMPLATE_PATTERN, _randomChars(6));
Expand Down Expand Up @@ -153,9 +158,11 @@ function tmpName(options, callback) {
cb = args[1],
tries = opts.name ? 1 : opts.tries || DEFAULT_TRIES;

/* istanbul ignore else */
if (isNaN(tries) || tries < 0)
return cb(new Error('Invalid tries'));

/* istanbul ignore else */
if (opts.template && !opts.template.match(TEMPLATE_PATTERN))
return cb(new Error('Invalid template provided'));

Expand All @@ -164,7 +171,9 @@ function tmpName(options, callback) {

// check whether the path exists then retry if needed
fs.stat(name, function (err) {
/* istanbul ignore else */
if (!err) {
/* istanbul ignore else */
if (tries-- > 0) return _getUniqueName();

return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name));
Expand All @@ -188,9 +197,11 @@ function tmpNameSync(options) {
opts = args[0],
tries = opts.name ? 1 : opts.tries || DEFAULT_TRIES;

/* istanbul ignore else */
if (isNaN(tries) || tries < 0)
throw new Error('Invalid tries');

/* istanbul ignore else */
if (opts.template && !opts.template.match(TEMPLATE_PATTERN))
throw new Error('Invalid template provided');

Expand Down Expand Up @@ -222,14 +233,17 @@ function file(options, callback) {

// gets a temporary filename
tmpName(opts, function _tmpNameCreated(err, name) {
/* istanbul ignore else */
if (err) return cb(err);

// create and open the file
fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
/* istanbul ignore else */
if (err) return cb(err);

if (opts.discardDescriptor) {
return fs.close(fd, function _discardCallback(err) {
/* istanbul ignore else */
if (err) {
// Low probability, and the file exists, so this could be
// ignored. If it isn't we certainly need to unlink the
Expand All @@ -247,6 +261,7 @@ function file(options, callback) {
cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts));
});
}
/* istanbul ignore else */
if (opts.detachDescriptor) {
return cb(null, name, fd, _prepareTmpFileRemoveCallback(name, -1, opts));
}
Expand All @@ -272,6 +287,7 @@ function fileSync(options) {
const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
const name = tmpNameSync(opts);
var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE);
/* istanbul ignore else */
if (opts.discardDescriptor) {
fs.closeSync(fd);
fd = undefined;
Expand Down Expand Up @@ -305,6 +321,7 @@ function _rmdirRecursiveSync(root) {
stat = fs.lstatSync(file); // lstat so we don't recurse into symlinked directories

if (stat.isDirectory()) {
/* istanbul ignore else */
if (!deferred) {
deferred = true;
dirs.push(dir);
Expand All @@ -315,6 +332,7 @@ function _rmdirRecursiveSync(root) {
}
}

/* istanbul ignore else */
if (!deferred) {
fs.rmdirSync(dir);
}
Expand All @@ -335,10 +353,12 @@ function dir(options, callback) {

// gets a temporary filename
tmpName(opts, function _tmpNameCreated(err, name) {
/* istanbul ignore else */
if (err) return cb(err);

// create the directory
fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) {
/* istanbul ignore else */
if (err) return cb(err);

cb(null, name, _prepareTmpDirRemoveCallback(name, opts));
Expand Down Expand Up @@ -379,6 +399,7 @@ function dirSync(options) {
function _prepareTmpFileRemoveCallback(name, fd, opts) {
const removeCallback = _prepareRemoveCallback(function _removeCallback(fdPath) {
try {
/* istanbul ignore else */
if (0 <= fdPath[0]) {
fs.closeSync(fdPath[0]);
}
Expand All @@ -387,6 +408,7 @@ function _prepareTmpFileRemoveCallback(name, fd, opts) {
// under some node/windows related circumstances, a temporary file
// may have not be created as expected or the file was already closed
// by the user, in which case we will simply ignore the error
/* istanbul ignore else */
if (!isEBADF(e) && !isENOENT(e)) {
// reraise any unanticipated error
throw e;
Expand All @@ -396,13 +418,15 @@ function _prepareTmpFileRemoveCallback(name, fd, opts) {
fs.unlinkSync(fdPath[1]);
}
catch (e) {
/* istanbul ignore else */
if (!isENOENT(e)) {
// reraise any unanticipated error
throw e;
}
}
}, [fd, name]);

/* istanbul ignore else */
if (!opts.keep) {
_removeObjects.unshift(removeCallback);
}
Expand All @@ -422,6 +446,7 @@ function _prepareTmpDirRemoveCallback(name, opts) {
const removeFunction = opts.unsafeCleanup ? _rmdirRecursiveSync : fs.rmdirSync.bind(fs);
const removeCallback = _prepareRemoveCallback(removeFunction, name);

/* istanbul ignore else */
if (!opts.keep) {
_removeObjects.unshift(removeCallback);
}
Expand All @@ -441,8 +466,10 @@ function _prepareRemoveCallback(removeFunction, arg) {
var called = false;

return function _cleanupCallback(next) {
/* istanbul ignore else */
if (!called) {
const index = _removeObjects.indexOf(_cleanupCallback);
/* istanbul ignore else */
if (index >= 0) {
_removeObjects.splice(index, 1);
}
Expand All @@ -451,6 +478,7 @@ function _prepareRemoveCallback(removeFunction, arg) {
removeFunction(arg);
}

/* istanbul ignore else */
if (next) next(null);
};
}
Expand All @@ -461,6 +489,7 @@ function _prepareRemoveCallback(removeFunction, arg) {
* @private
*/
function _garbageCollector() {
/* istanbul ignore else */
if (!_gracefulCleanup) {
return;
}
Expand Down Expand Up @@ -521,10 +550,6 @@ function setGracefulCleanup() {
_gracefulCleanup = true;
}

const version = process.versions.node.split('.').map(function (value) {
return parseInt(value, 10);
});

/**
* If there are multiple different versions of tmp in place, make sure that
* we recognize the old listeners.
Expand All @@ -541,7 +566,9 @@ function _safely_install_listener() {
var existingListeners = [];
for (var i = 0, length = listeners.length; i < length; i++) {
var lstnr = listeners[i];
/* istanbul ignore else */
if (lstnr.name == '_tmp$safe_listener' || _is_legacy_listener(lstnr)) {
/* istanbul ignore else */
if (lstnr.name != '_uncaughtExceptionThrown') existingListeners.push(lstnr);
process.removeListener(EVENT, lstnr);
}
Expand All @@ -566,6 +593,7 @@ function _safely_install_listener() {
});

process.addListener(EVENT, function _tmp$safe_listener(data) {
/* istanbul ignore else */
if (existingListeners.length) {
for (var i = 0, length = existingListeners.length; i < length; i++) {
existingListeners[i](data);
Expand Down

0 comments on commit b451cab

Please sign in to comment.