Code coverage report for lib/checkers/file.js

Statements: 100% (16 / 16)      Branches: 100% (4 / 4)      Functions: 100% (4 / 4)      Lines: 100% (16 / 16)      Ignored: none     

All files » lib/checkers/ » file.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431                         1   8 8   8 1   7   7     8 8       1 7 8 5   3         1  
var checker = require('../checker'),
  fs = require('fs'),
  Result = require('../result'),
  validator = require('validator');
 
/**
 * Health check a file resource.
 *
 * @param {Object} setup: file check setup with fields:
 * - uri: a resource URI to check
 * - mode: a 3-digit file mode, e.g. 777, 644
 * @param {Function} cb: standard cb(err, result) callback
 */
function check(setup, cb) {
 
  fs.lstat(setup.uri.replace(/^file\:\/\//, ''), function (err, stats) {
    var result = new Result();
 
    if (err) {
      result.addError(err.message);
    } else {
      var modeString = parseInt(stats.mode.toString(8), 10).toString(),
        mode = modeString.slice(modeString.length - 3, modeString.length);
      checker.checkAttribute('mode', setup, _checkMode(mode), result);
    }
 
    result.setStatusByStats();
    cb(null, result);
  });
}
 
function _checkMode(actual) {
  return function (expected) {
    if (validator.matches(actual, new RegExp(expected))) {
      return 'Mode ' + actual + ' as expected';
    } else {
      throw new Error('Mode ' + actual + ' does not match the expected ' + expected);
    }
  };
}
 
exports.check = check;