simple test()

This commit is contained in:
Artur Adib 2012-03-14 15:57:39 -04:00
parent 79133e53a7
commit c2ab790cde
3 changed files with 103 additions and 0 deletions

View File

@ -218,6 +218,21 @@ mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
Creates directories.
#### test(expression)
Available expression primaries:
+ `'-d', 'path'`: true if path is a directory
+ `'-f', 'path'`: true if path is a regular file
Examples:
```javascript
if (test('-d', path)) { /* do something with dir */ };
if (!test('-f', path)) continue; // skip if it's a regular file
```
Evaluates expression using the available primaries and returns corresponding value.
#### cat(file [, file ...])
#### cat(file_array)

View File

@ -57,6 +57,7 @@ function _pwd(options) {
};
exports.pwd = wrap('pwd', _pwd);
//@
//@ #### ls([options ,] path [,path ...])
//@ #### ls([options ,] path_array)
@ -486,6 +487,42 @@ function _mkdir(options, dirs) {
}; // mkdir
exports.mkdir = wrap('mkdir', _mkdir);
//@
//@ #### test(expression)
//@ Available expression primaries:
//@
//@ + `'-d', 'path'`: true if path is a directory
//@ + `'-f', 'path'`: true if path is a regular file
//@
//@ Examples:
//@
//@ ```javascript
//@ if (test('-d', path)) { /* do something with dir */ };
//@ if (!test('-f', path)) continue; // skip if it's a regular file
//@ ```
//@
//@ Evaluates expression using the available primaries and returns corresponding value.
function _test(options, path) {
if (!path)
error('no path given');
// hack - only works with unary primaries
options = parseOptions(options, {
'd': 'directory',
'f': 'file'
});
if (!options.directory && !options.file)
error('could not interpret expression');
if (options.directory)
return fs.existsSync(path) && fs.statSync(path).isDirectory();
if (options.file)
return fs.existsSync(path) && fs.statSync(path).isFile();
}; // test
exports.test = wrap('test', _test);
//@
//@ #### cat(file [, file ...])
//@ #### cat(file_array)

51
test/test.js Normal file
View File

@ -0,0 +1,51 @@
var shell = require('..');
var assert = require('assert'),
path = require('path'),
fs = require('fs');
// Node shims for < v0.7
fs.existsSync = fs.existsSync || path.existsSync;
shell.silent();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//
var result = shell.test(); // no expression given
assert.ok(shell.error());
var result = shell.test('asdf'); // bad expression
assert.ok(shell.error());
var result = shell.test('f', 'resources/file1'); // bad expression
assert.ok(shell.error());
var result = shell.test('-f'); // no file
assert.ok(shell.error());
//
// Valids
//
var result = shell.test('-f', 'resources/file1');
assert.equal(shell.error(), null);
assert.equal(result, true);
var result = shell.test('-d', 'resources/file1');
assert.equal(shell.error(), null);
assert.equal(result, false);
var result = shell.test('-f', 'resources');
assert.equal(shell.error(), null);
assert.equal(result, false);
var result = shell.test('-d', 'resources');
assert.equal(shell.error(), null);
assert.equal(result, true);
shell.exit(123);