|
| 1 | +var vows = require('../lib/vows'), |
| 2 | + assert = require('assert'), |
| 3 | + path = require('path'), |
| 4 | + exec = require('child_process').exec; |
| 5 | + |
| 6 | +function generateTopic(args, file) { |
| 7 | + return function() { |
| 8 | + var cmd = './bin/vows' + ' -i ' + (args || '') + |
| 9 | + ' ./test/fixtures/isolate/' + file, |
| 10 | + options = {cwd: path.resolve(__dirname + '/../')}, |
| 11 | + callback = this.callback; |
| 12 | + |
| 13 | + exec(cmd, options, function(err, stdout, stderr) { |
| 14 | + callback(null, { |
| 15 | + err: err, |
| 16 | + stdout: stdout, |
| 17 | + stderr: stderr |
| 18 | + }); |
| 19 | + }); |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +function assertExecOk(r) { |
| 24 | + assert.isNull(r.err); |
| 25 | +} |
| 26 | + |
| 27 | +function assertExecNotOk(r) { |
| 28 | + assert.isNotNull(r.err); |
| 29 | +} |
| 30 | + |
| 31 | +function parseResults(stdout) { |
| 32 | + return stdout.split(/\n/g).map(function(s) { |
| 33 | + if (!s) return; |
| 34 | + return JSON.parse(s); |
| 35 | + }).filter(function(s) {return s}); |
| 36 | +} |
| 37 | + |
| 38 | +function assertResultTypePresent(results, type) { |
| 39 | + assert.ok(results.some(function(result) { |
| 40 | + return result[0] == type; |
| 41 | + })); |
| 42 | +} |
| 43 | + |
| 44 | +function assertResultsFinish(results, expected) { |
| 45 | + var finish = results[results.length - 1]; |
| 46 | + assert.equal(finish[0], 'finish'); |
| 47 | + |
| 48 | + finish = finish[1]; |
| 49 | + |
| 50 | + Object.keys(expected).forEach(function(key) { |
| 51 | + assert.equal(finish[key], expected[key]); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +vows.describe('vows/isolate').addBatch({ |
| 56 | + 'Running vows with -i flag for test/fixtures/isolate/': { |
| 57 | + 'passing-test.js': { |
| 58 | + 'with default reporter': { |
| 59 | + topic: generateTopic(null, 'passing-test.js'), |
| 60 | + 'should be ok': assertExecOk |
| 61 | + }, |
| 62 | + 'with json reporter': { |
| 63 | + topic: generateTopic('--json', 'passing-test.js'), |
| 64 | + 'should be ok': assertExecOk, |
| 65 | + 'should have correct output': function(r) { |
| 66 | + var results = parseResults(r.stdout) |
| 67 | + |
| 68 | + assertResultTypePresent(results, 'subject'); |
| 69 | + assertResultTypePresent(results, 'end'); |
| 70 | + |
| 71 | + assertResultsFinish(results, { |
| 72 | + total: 4, |
| 73 | + honored: 4 |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + 'failing-test.js': { |
| 79 | + 'with json reporter': { |
| 80 | + topic: generateTopic('--json', 'failing-test.js'), |
| 81 | + 'should be not ok': assertExecNotOk, |
| 82 | + 'should have correct output though': function(r) { |
| 83 | + var results = parseResults(r.stdout); |
| 84 | + |
| 85 | + assertResultsFinish(results, { |
| 86 | + total: 4, |
| 87 | + broken: 4 |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + }, |
| 92 | + 'stderr-test.js': { |
| 93 | + 'with json reporter': { |
| 94 | + topic: generateTopic('--json', 'stderr-test.js'), |
| 95 | + 'should be ok': assertExecOk, |
| 96 | + 'should have stderr': function(r) { |
| 97 | + assert.equal(r.stderr, |
| 98 | + ['oh no!', 'oh no!', 'oh no!', 'oh no!', ''].join('\n')); |
| 99 | + }, |
| 100 | + 'should have correct output': function(r) { |
| 101 | + var results= parseResults(r.stdout); |
| 102 | + |
| 103 | + assertResultsFinish(results, { |
| 104 | + total: 4, |
| 105 | + honored: 4 |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + 'log-test.js': { |
| 111 | + 'with json reporter': { |
| 112 | + topic: generateTopic('--json', 'log-test.js'), |
| 113 | + 'should be ok': assertExecOk, |
| 114 | + 'should have correct output': function(r) { |
| 115 | + var results= parseResults(r.stdout); |
| 116 | + |
| 117 | + assertResultsFinish(results, { |
| 118 | + total: 4, |
| 119 | + honored: 4 |
| 120 | + }); |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + 'all tests (*)': { |
| 125 | + 'with json reporter': { |
| 126 | + topic: generateTopic('--json', '*'), |
| 127 | + 'should be not ok': assertExecNotOk, |
| 128 | + 'should have correct output': function(r) { |
| 129 | + var results= parseResults(r.stdout); |
| 130 | + |
| 131 | + assertResultsFinish(results, { |
| 132 | + total: 16, |
| 133 | + broken: 4, |
| 134 | + honored: 12 |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +}).export(module); |
0 commit comments