|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var child_process = require('child_process'), |
| 4 | + q = require('q'), |
| 5 | + fs = require('fs'); |
| 6 | + |
| 7 | +var CommandlineTest = function(command) { |
| 8 | + var self = this; |
| 9 | + this.command_ = command; |
| 10 | + this.expectedExitCode_ = 0; |
| 11 | + this.stdioOnlyOnFailures_ = true; |
| 12 | + this.expectedErrors_ = []; |
| 13 | + this.assertExitCodeOnly_ = false; |
| 14 | + |
| 15 | + // If stdioOnlyOnFailures_ is true, do not stream stdio unless test failed. |
| 16 | + // This is to prevent tests with expected failures from polluting the output. |
| 17 | + this.alwaysEnableStdio = function() { |
| 18 | + self.stdioOnlyOnFailures_ = false; |
| 19 | + return self; |
| 20 | + }; |
| 21 | + |
| 22 | + // Only assert the exit code and not failures. |
| 23 | + // This must be true if the command you're running does not support |
| 24 | + // the flag '--resultJsonOutputFile'. |
| 25 | + this.assertExitCodeOnly = function() { |
| 26 | + self.assertExitCodeOnly_ = true; |
| 27 | + return self; |
| 28 | + }; |
| 29 | + |
| 30 | + // Set the expected exit code for the test command. |
| 31 | + this.expectExitCode = function(exitCode) { |
| 32 | + self.expectedExitCode_ = exitCode; |
| 33 | + return self; |
| 34 | + }; |
| 35 | + |
| 36 | + // Set the expected total test duration in milliseconds. |
| 37 | + this.expectTestDuration = function(min, max) { |
| 38 | + self.expectedMinTestDuration_ = min; |
| 39 | + self.expectedMaxTestDuration_ = max; |
| 40 | + return self; |
| 41 | + }; |
| 42 | + |
| 43 | + /** |
| 44 | + * Add expected error(s) for the test command. |
| 45 | + * Input is an object or list of objects of the following form: |
| 46 | + * { |
| 47 | + * message: string, // optional regex |
| 48 | + * stackTrace: string, //optional regex |
| 49 | + * } |
| 50 | + */ |
| 51 | + this.expectErrors = function(expectedErrors) { |
| 52 | + if (expectedErrors instanceof Array) { |
| 53 | + self.expectedErrors_ = self.expectedErrors_.concat(expectedErrors); |
| 54 | + } else { |
| 55 | + self.expectedErrors_.push(expectedErrors); |
| 56 | + } |
| 57 | + return self; |
| 58 | + }; |
| 59 | + |
| 60 | + this.run = function() { |
| 61 | + var start = new Date().getTime(); |
| 62 | + var testOutputPath = 'test_output_' + start + '.tmp'; |
| 63 | + var output = ''; |
| 64 | + |
| 65 | + var flushAndFail = function(errorMsg) { |
| 66 | + process.stdout.write(output); |
| 67 | + throw new Error(errorMsg); |
| 68 | + }; |
| 69 | + |
| 70 | + return q.promise(function(resolve, reject) { |
| 71 | + if (!self.assertExitCodeOnly_) { |
| 72 | + self.command_ = self.command_ + ' --resultJsonOutputFile ' + testOutputPath; |
| 73 | + } |
| 74 | + var args = self.command_.split(/\s/); |
| 75 | + |
| 76 | + var test_process; |
| 77 | + |
| 78 | + if (self.stdioOnlyOnFailures_) { |
| 79 | + test_process = child_process.spawn(args[0], args.slice(1)); |
| 80 | + |
| 81 | + test_process.stdout.on('data', function(data) { |
| 82 | + output += data; |
| 83 | + }); |
| 84 | + |
| 85 | + test_process.stderr.on('data', function(data) { |
| 86 | + output += data; |
| 87 | + }); |
| 88 | + } else { |
| 89 | + test_process = child_process.spawn(args[0], args.slice(1), {stdio:'inherit'}); |
| 90 | + } |
| 91 | + |
| 92 | + test_process.on('error', function(err) { |
| 93 | + reject(err); |
| 94 | + }); |
| 95 | + |
| 96 | + test_process.on('exit', function(exitCode) { |
| 97 | + resolve(exitCode); |
| 98 | + }); |
| 99 | + }).then(function(exitCode) { |
| 100 | + if (self.expectedExitCode_ !== exitCode) { |
| 101 | + flushAndFail('expecting exit code: ' + self.expectedExitCode_ + |
| 102 | + ', actual: ' + exitCode); |
| 103 | + } |
| 104 | + |
| 105 | + // Skip the rest if we are only verify exit code. |
| 106 | + // Note: we're expecting a file populated by '--resultJsonOutputFile' after |
| 107 | + // this point. |
| 108 | + if (self.assertExitCodeOnly_) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + var raw_data = fs.readFileSync(testOutputPath); |
| 113 | + var testOutput = JSON.parse(raw_data); |
| 114 | + |
| 115 | + var actualErrors = []; |
| 116 | + var duration = 0; |
| 117 | + testOutput.forEach(function(specResult) { |
| 118 | + duration += specResult.duration; |
| 119 | + specResult.assertions.forEach(function(assertion) { |
| 120 | + if (!assertion.passed) { |
| 121 | + actualErrors.push(assertion); |
| 122 | + } |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + self.expectedErrors_.forEach(function(expectedError) { |
| 127 | + var found = false; |
| 128 | + for (var i = 0; i < actualErrors.length; ++i) { |
| 129 | + var actualError = actualErrors[i]; |
| 130 | + |
| 131 | + // if expected message is defined and messages don't match |
| 132 | + if (expectedError.message) { |
| 133 | + if (!actualError.errorMsg || |
| 134 | + !actualError.errorMsg.match(new RegExp(expectedError.message))) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + } |
| 138 | + // if expected stackTrace is defined and stackTraces don't match |
| 139 | + if (expectedError.stackTrace) { |
| 140 | + if (!actualError.stackTrace || |
| 141 | + !actualError.stackTrace.match(new RegExp(expectedError.stackTrace))) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + } |
| 145 | + found = true; |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + if (!found) { |
| 150 | + if (expectedError.message && expectedError.stackTrace) { |
| 151 | + flushAndFail('did not fail with expected error with message: [' + |
| 152 | + expectedError.message + '] and stackTrace: [' + |
| 153 | + expectedError.stackTrace + ']'); |
| 154 | + } else if (expectedError.message) { |
| 155 | + flushAndFail('did not fail with expected error with message: [' + |
| 156 | + expectedError.message + ']'); |
| 157 | + } else if (expectedError.stackTrace) { |
| 158 | + flushAndFail('did not fail with expected error with stackTrace: [' + |
| 159 | + expectedError.stackTrace + ']'); |
| 160 | + } |
| 161 | + } else { |
| 162 | + actualErrors.splice(i, 1); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + if (actualErrors.length > 0) { |
| 167 | + flushAndFail('failed with ' + actualErrors.length + ' unexpected failures'); |
| 168 | + } |
| 169 | + |
| 170 | + if (self.expectedMinTestDuration_ |
| 171 | + && duration < self.expectedMinTestDuration_) { |
| 172 | + flushAndFail('expecting test min duration: ' + |
| 173 | + self.expectedMinTestDuration_ + ', actual: ' + duration); |
| 174 | + } |
| 175 | + if (self.expectedMaxTestDuration_ |
| 176 | + && duration > self.expectedMaxTestDuration_) { |
| 177 | + flushAndFail('expecting test max duration: ' + |
| 178 | + self.expectedMaxTestDuration_ + ', actual: ' + duration); |
| 179 | + } |
| 180 | + }).fin(function() { |
| 181 | + try { |
| 182 | + fs.unlinkSync(testOutputPath); |
| 183 | + } catch (err) { |
| 184 | + // don't do anything |
| 185 | + } |
| 186 | + }); |
| 187 | + }; |
| 188 | +}; |
| 189 | + |
| 190 | +/** |
| 191 | + * Util for running tests and testing functionalities including: |
| 192 | + * exitCode, test durations, expected errors, and expected stackTrace |
| 193 | + * Note, this will work with any commandline tests, but only if it supports |
| 194 | + * the flag '--resultJsonOutputFile', unless only exitCode is being tested. |
| 195 | + * For now, this means protractor tests (jasmine/mocha/cucumber). |
| 196 | + */ |
| 197 | +exports.Executor = function() { |
| 198 | + var tests = []; |
| 199 | + this.addCommandlineTest = function(command) { |
| 200 | + var test = new CommandlineTest(command); |
| 201 | + tests.push(test); |
| 202 | + return test; |
| 203 | + }; |
| 204 | + |
| 205 | + this.execute = function() { |
| 206 | + var failed = false; |
| 207 | + |
| 208 | + (function runTests(i) { |
| 209 | + if (i < tests.length) { |
| 210 | + console.log('running: ' + tests[i].command_); |
| 211 | + tests[i].run().then(function() { |
| 212 | + console.log('>>> \033[1;32mpass\033[0m'); |
| 213 | + }, function(err) { |
| 214 | + failed = true; |
| 215 | + console.log('>>> \033[1;31mfail: ' + err.toString() + '\033[0m'); |
| 216 | + }).fin(function() { |
| 217 | + runTests(i + 1); |
| 218 | + }).done(); |
| 219 | + } else { |
| 220 | + console.log('Summary: ' + (failed ? 'fail' : 'pass')); |
| 221 | + process.exit(failed ? 1 : 0); |
| 222 | + } |
| 223 | + }(0)); |
| 224 | + }; |
| 225 | +}; |
0 commit comments