mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
Merge pull request #23 from aeosynth/master
add further types to test(), -L, -e etc
This commit is contained in:
commit
0be8a1cbb9
51
shell.js
51
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);
|
||||
|
||||
|
||||
1
test/resources/link
Symbolic link
1
test/resources/link
Symbolic link
@ -0,0 +1 @@
|
||||
file1
|
||||
42
test/test.js
42
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user