Skip to content

Commit

Permalink
adding jshint to tests; all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
arturadib committed Dec 27, 2012
1 parent 802a0b4 commit ef795af
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 1 addition & 1 deletion global.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var shell = require('./shell.js');
for (cmd in shell)
for (var cmd in shell)
global[cmd] = shell[cmd];
4 changes: 4 additions & 0 deletions jshint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"loopfunc": true,
"sub": true
}
5 changes: 3 additions & 2 deletions make.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ global.target = {};
// been evaluated
var args = process.argv.slice(2);
setTimeout(function() {
var t;

if (args.length === 1 && args[0] === '--help') {
console.log('Available targets:');
Expand All @@ -25,15 +26,15 @@ setTimeout(function() {
return;
oldTarget.done = true;
return oldTarget.apply(oldTarget, arguments);
}
};

})(t, target[t]);
}

// Execute desired targets
if (args.length > 0) {
args.forEach(function(arg) {
if (arg in target)
if (arg in target)
target[arg]();
else {
console.log('no such target: ' + arg);
Expand Down
28 changes: 28 additions & 0 deletions scripts/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,40 @@ require('../global');

var failed = false;

//
// Lint
//
JSHINT_BIN = './node_modules/jshint/bin/hint';
cd(__dirname + '/..');
if (!test('-f', JSHINT_BIN)) {
exec('npm install jshint');
}

if (!test('-f', JSHINT_BIN)) {
failed = true;
echo('*** FAILED TO INSTALL JSHINT!');
echo();
} else {
if (exec(JSHINT_BIN + ' --config jshint.json *.js test/*.js').code !== 0) {
failed = true;
echo('*** JSHINT FAILED! (return code != 0)');
echo();
} else {
echo('All JSHint tests passed');
echo();
}
}

//
// Unit tests
//
cd(__dirname + '/../test');
ls('*.js').forEach(function(file) {
echo('Running test:', file);
if (exec('node '+file).code !== 123) { // 123 avoids false positives (e.g. premature exit)
failed = true;
echo('*** TEST FAILED! (missing exit code "123")');
echo();
}
});

Expand Down
85 changes: 43 additions & 42 deletions shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fs.existsSync = fs.existsSync || path.existsSync;
var config = {
silent: false,
fatal: false
}
};

var state = {
error: null,
Expand Down Expand Up @@ -48,7 +48,7 @@ function _cd(options, dir) {
error('not a directory: ' + dir);

process.chdir(dir);
};
}
exports.cd = wrap('cd', _cd);

//@
Expand All @@ -57,7 +57,7 @@ exports.cd = wrap('cd', _cd);
function _pwd(options) {
var pwd = path.resolve(process.cwd());
return ShellString(pwd);
};
}
exports.pwd = wrap('pwd', _pwd);


Expand Down Expand Up @@ -154,7 +154,7 @@ function _ls(options, paths) {
// Wildcard present on an existing dir? (e.g. '/tmp/*.js')
if (basename.search(/\*/) > -1 && fs.existsSync(dirname) && fs.statSync(dirname).isDirectory) {
// Escape special regular expression chars
var regexp = basename.replace(/(\^|\$|\(|\)|\<|\>|\[|\]|\{|\}|\.|\+|\?)/g, '\\$1');
var regexp = basename.replace(/(\^|\$|\(|\)|<|>|\[|\]|\{|\}|\.|\+|\?)/g, '\\$1');
// Translates wildcard into regex
regexp = '^' + regexp.replace(/\*/g, '.*') + '$';
// Iterate over directory contents
Expand All @@ -178,7 +178,7 @@ function _ls(options, paths) {
});

return list;
};
}
exports.ls = wrap('ls', _ls);


Expand Down Expand Up @@ -287,7 +287,7 @@ function _cp(options, sources, dest) {

// Create dest
try {
fs.mkdirSync(dest, 0777);
fs.mkdirSync(dest, parseInt('0777', 8));
} catch (e) {
// like Unix's cp, keep going even if we can't create dest dir
}
Expand Down Expand Up @@ -338,7 +338,7 @@ function _cp(options, sources, dest) {

copyFileSync(src, thisDest);
}); // forEach(src)
}; // cp
}
exports.cp = wrap('cp', _cp);

//@
Expand Down Expand Up @@ -412,7 +412,7 @@ function _rm(options, files) {
rmdirSyncRecursive(file, options.force);
}
}); // forEach(file)
}; // rm
} // rm
exports.rm = wrap('rm', _rm);

//@
Expand Down Expand Up @@ -486,7 +486,7 @@ function _mv(options, sources, dest) {

fs.renameSync(src, thisDest);
}); // forEach(src)
}; // mv
} // mv
exports.mv = wrap('mv', _mv);

//@
Expand Down Expand Up @@ -532,9 +532,9 @@ function _mkdir(options, dirs) {
if (options.fullpath)
mkdirSyncRecursive(dir);
else
fs.mkdirSync(dir, 0777);
fs.mkdirSync(dir, parseInt('0777', 8));
});
}; // mkdir
} // mkdir
exports.mkdir = wrap('mkdir', _mkdir);

//@
Expand Down Expand Up @@ -611,8 +611,8 @@ function _test(options, path) {
return stats.isFIFO();

if (options.socket)
return stats.isSocket()
}; // test
return stats.isSocket();
} // test
exports.test = wrap('test', _test);


Expand Down Expand Up @@ -654,7 +654,7 @@ function _cat(options, files) {
cat = cat.substring(0, cat.length-1);

return ShellString(cat);
};
}
exports.cat = wrap('cat', _cat);

//@
Expand All @@ -680,7 +680,7 @@ function _to(options, file) {
} catch(e) {
error('could not write to file (code '+e.code+'): '+file, true);
}
};
}
// In the future, when Proxies are default, we can add methods like `.to()` to primitive strings.
// For now, this is a dummy function to bookmark places we need such strings
function ShellString(str) {
Expand Down Expand Up @@ -726,7 +726,7 @@ function _sed(options, regex, replacement, file) {
fs.writeFileSync(file, result, 'utf8');

return ShellString(result);
};
}
exports.sed = wrap('sed', _sed);

//@
Expand Down Expand Up @@ -776,7 +776,7 @@ function _grep(options, regex, files) {
});

return ShellString(grep);
};
}
exports.grep = wrap('grep', _grep);


Expand Down Expand Up @@ -840,7 +840,7 @@ function _which(options, cmd) {
where = where || path.resolve(cmd);

return ShellString(where);
};
}
exports.which = wrap('which', _which);

//@
Expand All @@ -859,7 +859,7 @@ function _echo() {
var messages = [].slice.call(arguments, 0);
console.log.apply(this, messages);
return ShellString(messages.join(' '));
};
}
exports.echo = _echo; // don't wrap() as it could parse '-options'

//@
Expand Down Expand Up @@ -921,7 +921,7 @@ function _exec(command, options, callback) {
return execAsync(command, options, callback);
else
return execSync(command, options);
};
}
exports.exec = wrap('exec', _exec, {notUnix:true});


Expand Down Expand Up @@ -986,7 +986,7 @@ exports.tempdir = wrap('tempdir', tempDir);
//@ otherwise returns string explaining the error
exports.error = function() {
return state.error;
}
};



Expand Down Expand Up @@ -1092,7 +1092,7 @@ function wrap(cmd, fn, options) {

state.currentCmd = 'shell.js';
return retValue;
}
};
} // wrap

// Buffered file copy, synchronous
Expand Down Expand Up @@ -1172,7 +1172,7 @@ function cpdirSyncRecursive(sourceDir, destDir, opts) {
}

} // for files
}; // cpdirSyncRecursive
} // cpdirSyncRecursive

// Recursively removes 'dir'
// Adapted from https://github.com/ryanmcgrath/wrench-js
Expand Down Expand Up @@ -1227,30 +1227,30 @@ function rmdirSyncRecursive(dir, force) {
}

return result;
}; // rmdirSyncRecursive
} // rmdirSyncRecursive

// Recursively creates 'dir'
function mkdirSyncRecursive(dir) {
var baseDir = path.dirname(dir);

// Base dir exists, no recursion necessary
if (fs.existsSync(baseDir)) {
fs.mkdirSync(dir, 0777);
fs.mkdirSync(dir, parseInt('0777', 8));
return;
}

// Base dir does not exist, go recursive
mkdirSyncRecursive(baseDir);

// Base dir created, can create dir
fs.mkdirSync(dir, 0777);
};
fs.mkdirSync(dir, parseInt('0777', 8));
}

// e.g. 'makerjs_a5f185d0443ca...'
function randomFileName() {
function randomHash(count) {
if (count === 1)
return parseInt(16*Math.random()).toString(16);
return parseInt(16*Math.random(), 10).toString(16);
else {
var hash = '';
for (var i=0; i<count; i++)
Expand Down Expand Up @@ -1368,11 +1368,11 @@ function execSync(cmd, opts) {
cmd += ' > '+stdoutFile+' 2>&1'; // works on both win/unix

var script =
"var child = require('child_process'), \
fs = require('fs'); \
child.exec('"+escape(cmd)+"', {env: process.env}, function(err) { \
fs.writeFileSync('"+escape(codeFile)+"', err ? err.code.toString() : '0'); \
});";
"var child = require('child_process')," +
" fs = require('fs');" +
"child.exec('"+escape(cmd)+"', {env: process.env}, function(err) {" +
" fs.writeFileSync('"+escape(codeFile)+"', err ? err.code.toString() : '0');" +
"});";

if (fs.existsSync(scriptFile)) _unlinkSync(scriptFile);
if (fs.existsSync(stdoutFile)) _unlinkSync(stdoutFile);
Expand All @@ -1388,22 +1388,23 @@ function execSync(cmd, opts) {
// sleepFile is used as a dummy I/O op to mitigate unnecessary CPU usage
// (tried many I/O sync ops, writeFileSync() seems to be only one that is effective in reducing
// CPU usage, though apparently not so much on Windows)
while (!fs.existsSync(codeFile)) { updateStdout(); fs.writeFileSync(sleepFile, 'a'); };
while (!fs.existsSync(stdoutFile)) { updateStdout(); fs.writeFileSync(sleepFile, 'a'); };
while (!fs.existsSync(codeFile)) { updateStdout(); fs.writeFileSync(sleepFile, 'a'); }
while (!fs.existsSync(stdoutFile)) { updateStdout(); fs.writeFileSync(sleepFile, 'a'); }

// At this point codeFile exists, but it's not necessarily flushed yet.
// Keep reading it until it is.
var code = parseInt('');
while (isNaN(code))
code = parseInt(fs.readFileSync(codeFile, 'utf8'));
var code = parseInt('', 10);
while (isNaN(code)) {
code = parseInt(fs.readFileSync(codeFile, 'utf8'), 10);
}

var stdout = fs.readFileSync(stdoutFile, 'utf8');

// No biggie if we can't erase the files now -- they're in a temp dir anyway
try { _unlinkSync(scriptFile); } catch(e) {};
try { _unlinkSync(stdoutFile); } catch(e) {};
try { _unlinkSync(codeFile); } catch(e) {};
try { _unlinkSync(sleepFile); } catch(e) {};
try { _unlinkSync(scriptFile); } catch(e) {}
try { _unlinkSync(stdoutFile); } catch(e) {}
try { _unlinkSync(codeFile); } catch(e) {}
try { _unlinkSync(sleepFile); } catch(e) {}

// True if successful, false if not
var obj = {
Expand Down
2 changes: 1 addition & 1 deletion test/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function numLines(str) {
var cur = shell.pwd();

shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
shell.mkdir('tmp');

//
// Invalids
Expand Down
2 changes: 1 addition & 1 deletion test/cd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function numLines(str) {
var cur = shell.pwd();

shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
shell.mkdir('tmp');

//
// Invalids
Expand Down
2 changes: 1 addition & 1 deletion test/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function numLines(str) {
}

shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
shell.mkdir('tmp');

//
// Invalids
Expand Down
2 changes: 1 addition & 1 deletion test/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function numLines(str) {
}

shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
shell.mkdir('tmp');

//
// Valids
Expand Down
Loading

0 comments on commit ef795af

Please sign in to comment.