|
| 1 | +/* eslint-disable no-octal */ |
| 2 | + |
| 3 | +var |
| 4 | + assert = require('assert'), |
| 5 | + fs = require('fs'), |
| 6 | + path = require('path'), |
| 7 | + existsSync = fs.existsSync || path.existsSync; |
| 8 | + |
| 9 | + |
| 10 | +module.exports.assertName = function assertName(name, expected) { |
| 11 | + assert.ok(typeof name == 'string'); |
| 12 | + assert.ok(name.length > 0, 'an empty string is not a valid name'); |
| 13 | + if (expected) { |
| 14 | + assert.equal(path.basename(name), expected, 'should be the expected name'); |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | + |
| 19 | +module.exports.assertStat = function assertStat(name, mode) { |
| 20 | + var stat = fs.statSync(name); |
| 21 | + |
| 22 | + /* |
| 23 | + // geteuid() and getegid() do not exist on Windows. |
| 24 | + // must use the effective gid and effective uid for testing |
| 25 | + if (process.geteuid) { |
| 26 | + assert.equal(stat.uid, process.geteuid(), 'should have the same UID'); |
| 27 | + } |
| 28 | + if (process.getegid) { |
| 29 | + // FIXME does not always work as expected (setgid bit on parent directory) |
| 30 | + console.log('stat.gid ' + stat.gid); |
| 31 | + console.log('egid ' + process.getegid()); |
| 32 | + assert.equal(stat.gid, process.getegid(), 'should have the same GUID'); |
| 33 | + } |
| 34 | + */ |
| 35 | + |
| 36 | + // mode values do not work properly on Windows. Ignore “group” and |
| 37 | + // “other” bits then. Ignore execute bit on that platform because it |
| 38 | + // doesn’t exist—even for directories. |
| 39 | + if (process.platform == 'win32') { |
| 40 | + assert.equal(stat.mode & 000600, mode & 000600); |
| 41 | + } else { |
| 42 | + assert.equal(stat.mode & 000777, mode); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | + |
| 47 | +module.exports.assertPrefix = function assertPrefix(name, prefix) { |
| 48 | + assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix'); |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | +module.exports.assertPostfix = function assertPostfix(name, postfix) { |
| 53 | + assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix'); |
| 54 | +}; |
| 55 | + |
| 56 | + |
| 57 | +module.exports.assertProperResult = function assertProperResult(result, withfd) { |
| 58 | + assert.ok(result); |
| 59 | + assert.ok(result.name, 'should have a name'); |
| 60 | + if (withfd) assert.ok(result.fd, 'should have an fd'); |
| 61 | + else assert.ok(result.fd == undefined, 'should not have an fd'); |
| 62 | + assert.ok(typeof result.removeCallback == 'function', 'should have a removeCallback'); |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +module.exports.assertExists = function assertExists(name, isfile) { |
| 67 | + assert.ok(existsSync(name), 'should exist'); |
| 68 | + var stat = fs.statSync(name); |
| 69 | + if (isfile) assert.ok(stat.isFile(), 'should be a file'); |
| 70 | + else assert.ok(stat.isDirectory(), 'should be a directory'); |
| 71 | +} |
| 72 | + |
0 commit comments