diff --git a/shell.js b/shell.js index 8136901..2300ffe 100644 --- a/shell.js +++ b/shell.js @@ -513,8 +513,14 @@ exports.mkdir = wrap('mkdir', _mkdir); //@ ### test(expression) //@ Available expression primaries: //@ +//@ + `'-b', 'path'`: true if path is a block device +//@ + `'-c', 'path'`: true if path is a character device //@ + `'-d', 'path'`: true if path is a directory +//@ + `'-e', 'path'`: true if path exists //@ + `'-f', 'path'`: true if path is a regular file +//@ + `'-L', 'path'`: true if path is a symboilc link +//@ + `'-p', 'path'`: true if path is a pipe (FIFO) +//@ + `'-S', 'path'`: true if path is a socket //@ //@ Examples: //@ @@ -530,17 +536,54 @@ function _test(options, path) { // hack - only works with unary primaries options = parseOptions(options, { + 'b': 'block', + 'c': 'character', 'd': 'directory', - 'f': 'file' + 'e': 'exists', + 'f': 'file', + 'L': 'link', + 'p': 'pipe', + 'S': 'socket' }); - if (!options.directory && !options.file) + + var canInterpret = false; + for (var key in options) + if (options[key] === true) { + canInterpret = true; + break; + } + + if (!canInterpret) error('could not interpret expression'); + if (!fs.existsSync(path)) + return false; + + if (options.exists) + return true; + + if (options.link) + return fs.lstatSync(path).isSymbolicLink(); + + stats = fs.statSync(path); + + if (options.block) + return stats.isBlockDevice(); + + if (options.character) + return stats.isCharacterDevice(); + if (options.directory) - return fs.existsSync(path) && fs.statSync(path).isDirectory(); + return stats.isDirectory(); if (options.file) - return fs.existsSync(path) && fs.statSync(path).isFile(); + return stats.isFile(); + + if (options.pipe) + return stats.isFIFO(); + + if (options.socket) + return stats.isSocket() }; // test exports.test = wrap('test', _test); diff --git a/test/resources/link b/test/resources/link new file mode 120000 index 0000000..08219db --- /dev/null +++ b/test/resources/link @@ -0,0 +1 @@ +file1 \ No newline at end of file diff --git a/test/test.js b/test/test.js index ab88001..f074c8a 100644 --- a/test/test.js +++ b/test/test.js @@ -32,20 +32,52 @@ assert.ok(shell.error()); // Valids // -var result = shell.test('-f', 'resources/file1'); +//exists +var result = shell.test('-e', 'resources/file1'); assert.equal(shell.error(), null); -assert.equal(result, true); +assert.equal(result, true);//true -var result = shell.test('-d', 'resources/file1'); +var result = shell.test('-e', 'resources/404'); assert.equal(shell.error(), null); assert.equal(result, false); +//directory +var result = shell.test('-d', 'resources'); +assert.equal(shell.error(), null); +assert.equal(result, true);//true + var result = shell.test('-f', 'resources'); assert.equal(shell.error(), null); assert.equal(result, false); -var result = shell.test('-d', 'resources'); +var result = shell.test('-L', 'resources'); assert.equal(shell.error(), null); -assert.equal(result, true); +assert.equal(result, false); + +//file +var result = shell.test('-d', 'resources/file1'); +assert.equal(shell.error(), null); +assert.equal(result, false); + +var result = shell.test('-f', 'resources/file1'); +assert.equal(shell.error(), null); +assert.equal(result, true);//true + +var result = shell.test('-L', 'resources/file1'); +assert.equal(shell.error(), null); +assert.equal(result, false); + +//link +var result = shell.test('-d', 'resources/link'); +assert.equal(shell.error(), null); +assert.equal(result, false); + +var result = shell.test('-f', 'resources/link'); +assert.equal(shell.error(), null); +assert.equal(result, true);//true + +var result = shell.test('-L', 'resources/link'); +assert.equal(shell.error(), null); +assert.equal(result, true);//true shell.exit(123);