4 ======== A Handy Little Nodeunit Reference ========
5 https://github.com/caolan/nodeunit
8 test.expect(numAssertions)
11 test.ok(value, [message])
12 test.equal(actual, expected, [message])
13 test.notEqual(actual, expected, [message])
14 test.deepEqual(actual, expected, [message])
15 test.notDeepEqual(actual, expected, [message])
16 test.strictEqual(actual, expected, [message])
17 test.notStrictEqual(actual, expected, [message])
18 test.throws(block, [error], [message])
19 test.doesNotThrow(block, [error], [message])
23 var fs = require('fs');
24 var exec = require('child_process').exec;
26 var _which = require('which').sync;
27 function which(command) {
36 // Look for grep first (any OS). If not found (but on Windows) look for find,
37 // which is Windows' horribly crippled grep alternative.
38 var grep = which('grep') || process.platform === 'win32' && which('find');
41 setUp: function(done) {
42 this.origCwd = process.cwd();
43 process.chdir('test/fixtures');
46 tearDown: function(done) {
47 process.chdir(this.origCwd);
50 'grep': function(test) {
52 // Many unit tests depend on this.
53 test.ok(grep, 'A suitable "grep" or "find" program was not found in the PATH.');
56 // The rest of the tests are built dynamically, to keep things sane.
59 // A few helper functions.
60 function normalizeLineEndings(s) {
61 return s.replace(/\r?\n/g, '\n');
64 // Capture command output, normalizing captured stdout to unix file endings.
65 function run(command, callback) {
66 exec(command, function(error, stdout) {
67 callback(error ? error.code : 0, normalizeLineEndings(stdout));
71 // Read a fixture file, normalizing file contents to unix file endings.
72 function fixture(filename) {
73 return normalizeLineEndings(String(fs.readFileSync(filename)));
76 function buildTests() {
77 // Build individual unit tests for command output.
78 var counts = [10, 100, 1000];
79 var outputs = [' stdout stderr', ' stdout', ' stderr'];
80 var pipes = ['', ' | ' + grep + ' "std"'];
81 counts.forEach(function(count) {
82 outputs.forEach(function(output) {
83 pipes.forEach(function(pipe) {
84 var command = 'node log.js 0 ' + count + output + ' 2>&1' + pipe;
85 exports['exit']['output (' + command + ')'] = function(test) {
87 run(command, function(code, actual) {
88 var expected = fixture(count + output.replace(/ /g, '-') + '.txt');
89 // Sometimes, the actual file lines are out of order on Windows.
90 // But since the point of this lib is to drain the buffer and not
91 // guarantee output order, we only test the length.
92 test.equal(actual.length, expected.length, 'should be the same length.');
93 // The "fail" lines in log.js should NOT be output!
94 test.ok(actual.indexOf('fail') === -1, 'should not output after exit is called.');
102 // Build individual unit tests for exit codes.
103 var codes = [0, 1, 123];
104 codes.forEach(function(code) {
105 var command = 'node log.js ' + code + ' 10 stdout stderr';
106 exports['exit']['exit code (' + command + ')'] = function(test) {
108 run(command, function(actual) {
109 // The specified exit code should be passed through.
110 test.equal(actual, code, 'should exit with ' + code + ' error code.');
117 // Don't bother building tests if grep wasn't found, otherwise everything will
118 // fail and the error will get lost.