Skip to content

Commit

Permalink
test: don't count hard-to-test lines for coverage
Browse files Browse the repository at this point in the history
No change in logic.

Add `/* istanbul ignore next */` lines for hard-to-test lines so that
they don't count against us during code coverage.

I've also adjusted comments that I found confusing, and changed some
formatting.

Partial fix for #671
  • Loading branch information
nfischer committed Feb 26, 2017
1 parent 8dd2488 commit e4f57e7
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var pipeMethods = [];

function log() {
if (!config.silent) {
/* istanbul ignore next */
console.error.apply(console, arguments);
}
}
Expand Down Expand Up @@ -259,6 +260,7 @@ function unlinkSync(file) {
fs.unlinkSync(file);
} catch (e) {
// Try to override file permission
/* istanbul ignore next */
if (e.code === 'EPERM') {
fs.chmodSync(file, '0666');
fs.unlinkSync(file);
Expand Down Expand Up @@ -367,11 +369,13 @@ function wrap(cmd, fn, options) {
if (e.msg === 'earlyExit') {
retValue = e.retValue;
} else {
/* istanbul ignore next */
throw e; // this is probably a bug that should be thrown up the call stack
}
}
}
} catch (e) {
/* istanbul ignore next */
if (!state.error) {
// If state.error hasn't been set it's an error thrown by Node, not us - probably a bug...
console.error('ShellJS: internal error');
Expand Down
3 changes: 3 additions & 0 deletions src/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ function copyFileSync(srcFile, destFile, options) {
try {
fdr = fs.openSync(srcFile, 'r');
} catch (e) {
/* istanbul ignore next */
common.error('copyFileSync: could not read src file (' + srcFile + ')');
}

try {
fdw = fs.openSync(destFile, 'w');
} catch (e) {
/* istanbul ignore next */
common.error('copyFileSync: could not write to dest file (code=' + e.code + '):' + destFile);
}

Expand Down Expand Up @@ -243,6 +245,7 @@ function _cp(options, sources, dest) {
fs.statSync(path.dirname(dest));
cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink });
} catch (e) {
/* istanbul ignore next */
common.error("cannot create directory '" + dest + "': No such file or directory");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function _mkdir(options, dirs) {
if (e.code === 'EACCES') {
common.error('cannot create directory ' + dir + ': Permission denied');
} else {
/* istanbul ignore next */
throw e;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function _mv(options, sources, dest) {
} else if (typeof sources === 'string') {
sources = [sources];
} else {
// TODO(nate): figure out if we actually need this line
common.error('invalid arguments');
}

Expand Down Expand Up @@ -82,9 +83,12 @@ function _mv(options, sources, dest) {
try {
fs.renameSync(src, thisDest);
} catch (e) {
if (e.code === 'EXDEV') { // external partition
// if either of these fails, the appropriate error message will bubble
// up to the top level automatically
/* istanbul ignore next */
if (e.code === 'EXDEV') {
// If we're trying to `mv` to an external partition, we'll actually need
// to perform a copy and then clean up the original file. If either the
// copy or the rm fails with an exception, we should allow this
// exception to pass up to the top level.
cp('-r', src, thisDest);
rm('-rf', src);
}
Expand Down
6 changes: 5 additions & 1 deletion src/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ function rmdirSyncRecursive(dir, force) {
try {
common.unlinkSync(file);
} catch (e) {
common.error('could not remove file (code ' + e.code + '): ' + file, { continue: true });
/* istanbul ignore next */
common.error('could not remove file (code ' + e.code + '): ' + file, {
continue: true,
});
}
}
}
Expand All @@ -55,6 +58,7 @@ function rmdirSyncRecursive(dir, force) {
if (fs.existsSync(dir)) throw { code: 'EAGAIN' };
break;
} catch (er) {
/* istanbul ignore next */
// In addition to error codes, also check if the directory still exists and loop again if true
if (process.platform === 'win32' && (er.code === 'ENOTEMPTY' || er.code === 'EBUSY' || er.code === 'EPERM' || er.code === 'EAGAIN')) {
if (Date.now() - start > 1000) throw er;
Expand Down
1 change: 1 addition & 0 deletions src/tempdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function writeableDir(dir) {
common.unlinkSync(testFile);
return dir;
} catch (e) {
/* istanbul ignore next */
return false;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ function _test(options, path) {

if (options.file) return stats.isFile();

/* istanbul ignore next */
if (options.pipe) return stats.isFIFO();

/* istanbul ignore next */
if (options.socket) return stats.isSocket();

/* istanbul ignore next */
return false; // fallback
} // test
module.exports = _test;
1 change: 1 addition & 0 deletions src/to.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function _to(options, file) {
fs.writeFileSync(file, this.stdout || this.toString(), 'utf8');
return this;
} catch (e) {
/* istanbul ignore next */
common.error('could not write to file (code ' + e.code + '): ' + file, { continue: true });
}
}
Expand Down
1 change: 1 addition & 0 deletions src/toEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function _toEnd(options, file) {
fs.appendFileSync(file, this.stdout || this.toString(), 'utf8');
return this;
} catch (e) {
/* istanbul ignore next */
common.error('could not append to file (code ' + e.code + '): ' + file, { continue: true });
}
}
Expand Down

0 comments on commit e4f57e7

Please sign in to comment.