Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

move test_util out of q, rework due to es6 #5036

Merged
merged 23 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ const passingTests = [
// 'node spec/install/test.js'
];

var executor = new Executor();
const executor = new Executor();

passingTests.forEach(function(passing_test) {
passingTests.forEach((passing_test) => {
executor.addCommandlineTest(passing_test)
.assertExitCodeOnly();
});
Expand Down
211 changes: 108 additions & 103 deletions scripts/test/test_util.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
#!/usr/bin/env node

var child_process = require('child_process'),
q = require('q'),
fs = require('fs');

var CommandlineTest = function(command) {
var self = this;
this.command_ = command;
this.expectedExitCode_ = 0;
this.expectedErrors_ = [];
this.assertExitCodeOnly_ = false;
this.testLogStream = undefined;
const child_process = require('child_process');
const fs = require('fs');

class CommandlineTest {
constructor(command) {
this.command = command;
this.expectedExitCode = 0;
this.expectedErrors = [];
this.isExitCode = false;
this.testLogStream = undefined;
this.expectedMinTestDuration = undefined;
this.expectedMaxTestDuration = undefined;
}


// Only assert the exit code and not failures.
// This must be true if the command you're running does not support
// the flag '--resultJsonOutputFile'.
this.assertExitCodeOnly = function() {
self.assertExitCodeOnly_ = true;
return self;
};
assertExitCodeOnly() {
this.isExitCode = true;
return this;
}

this.setTestLogFile = function(filename) {
self.testLogStream = fs.createWriteStream(filename, {flags: 'a'});
};
setTestLogFile(filename) {
this.testLogStream = fs.createWriteStream(filename, {flags: 'a'});
}

// Set the expected exit code for the test command.
this.expectExitCode = function(exitCode) {
self.expectedExitCode_ = exitCode;
return self;
};
expectExitCode(exitCode) {
this.expectedExitCode = exitCode;
return this;
}

// Set the expected total test duration in milliseconds.
this.expectTestDuration = function(min, max) {
self.expectedMinTestDuration_ = min;
self.expectedMaxTestDuration_ = max;
return self;
};
expectTestDuration(min, max) {
this.expectedMinTestDuration = min;
this.expectedMaxTestDuration = max;
return this;
}

/**
* Add expected error(s) for the test command.
Expand All @@ -45,75 +48,75 @@ var CommandlineTest = function(command) {
* stackTrace: string, //optional regex
* }
*/
this.expectErrors = function(expectedErrors) {
expectErrors(expectedErrors) {
if (expectedErrors instanceof Array) {
self.expectedErrors_ = self.expectedErrors_.concat(expectedErrors);
this.expectedErrors = this.expectedErrors.concat(expectedErrors);
} else {
self.expectedErrors_.push(expectedErrors);
this.expectedErrors.push(expectedErrors);
}
return self;
};
return this;
}

this.run = function() {
var start = new Date().getTime();
var testOutputPath = 'test_output_' + start + '.tmp';
var output = '';
async run() {
const start = new Date().getTime();
const testOutputPath = `test_output_${start}.tmp`;
let output = '';

var flushAndFail = function(errorMsg) {
const flushAndFail = (errorMsg) => {
process.stdout.write(output);
throw new Error(errorMsg);
};

return q.promise(function(resolve, reject) {
if (!self.assertExitCodeOnly_) {
self.command_ = self.command_ + ' --resultJsonOutputFile ' + testOutputPath;
}
var args = self.command_.split(/\s/);

var test_process;
try {

test_process = child_process.spawn(args[0], args.slice(1));

var processData = function(data) {
process.stdout.write('.');
output += data;
if (self.testLogStream) {
self.testLogStream.write(data);
let exitCode = await new Promise((resolve, reject) => {
if (!this.assertExitCodeOnly) {
this.command = `${this.command} --resultJsonOutputFile ${testOutputPath}`;
}
};
const args = this.command.split(/\s/);
const test_process = child_process.spawn(args[0], args.slice(1));

const processData = (data) => {
process.stdout.write('.');
output += data;
if (this.testLogStream) {
this.testLogStream.write(data);
}
};

test_process.stdout.on('data', processData);
test_process.stderr.on('data', processData);
test_process.stdout.on('data', processData);
test_process.stderr.on('data', processData);

test_process.on('error', function(err) {
reject(err);
});
test_process.on('error', (err) => {
reject(err);
});

test_process.on('exit', function(exitCode) {
resolve(exitCode);
test_process.on('exit', function(exitCode) {
resolve(exitCode);
});
});
}).then(function(exitCode) {
if (self.expectedExitCode_ !== exitCode) {
flushAndFail('expecting exit code: ' + self.expectedExitCode_ +
if (this.expectedExitCode !== exitCode) {
flushAndFail('expecting exit code: ' + this.expectedExitCode +
', actual: ' + exitCode);
}

if (self.testLogStream) {
self.testLogStream.end();
if (this.testLogStream) {
this.testLogStream.end();
}

// Skip the rest if we are only verify exit code.
// Note: we're expecting a file populated by '--resultJsonOutputFile' after
// this point.
if (self.assertExitCodeOnly_) {
if (this.assertExitCodeOnly) {
return;
}

var raw_data = fs.readFileSync(testOutputPath);
var testOutput = JSON.parse(raw_data);
const raw_data = fs.readFileSync(testOutputPath);
const testOutput = JSON.parse(raw_data);

var actualErrors = [];
var duration = 0;
let actualErrors = [];
let duration = 0;
testOutput.forEach(function(specResult) {
duration += specResult.duration;
specResult.assertions.forEach(function(assertion) {
Expand All @@ -123,9 +126,9 @@ var CommandlineTest = function(command) {
});
});

self.expectedErrors_.forEach(function(expectedError) {
var found = false;
for (var i = 0; i < actualErrors.length; ++i) {
this.expectedErrors.forEach((expectedError) => {
let found = false;
for (let i = 0; i < actualErrors.length; ++i) {
var actualError = actualErrors[i];

// if expected message is defined and messages don't match
Expand Down Expand Up @@ -167,25 +170,25 @@ var CommandlineTest = function(command) {
flushAndFail('failed with ' + actualErrors.length + ' unexpected failures');
}

if (self.expectedMinTestDuration_
&& duration < self.expectedMinTestDuration_) {
if (this.expectedMinTestDuration
&& duration < this.expectedMinTestDuration) {
flushAndFail('expecting test min duration: ' +
self.expectedMinTestDuration_ + ', actual: ' + duration);
this.expectedMinTestDuration + ', actual: ' + duration);
}
if (self.expectedMaxTestDuration_
&& duration > self.expectedMaxTestDuration_) {
if (this.expectedMaxTestDuration
&& duration > this.expectedMaxTestDuration) {
flushAndFail('expecting test max duration: ' +
self.expectedMaxTestDuration_ + ', actual: ' + duration);
this.expectedMaxTestDuration + ', actual: ' + duration);
}
}).fin(function() {
} finally {
try {
fs.unlinkSync(testOutputPath);
} catch (err) {
// don't do anything
}
});
};
};
}
}
}

/**
* Util for running tests and testing functionalities including:
Expand All @@ -195,34 +198,36 @@ var CommandlineTest = function(command) {
* For now, this means protractor tests (jasmine/mocha).
*/
exports.Executor = function() {
var tests = [];
this.addCommandlineTest = function(command) {
var test = new CommandlineTest(command);
let tests = [];
this.addCommandlineTest = (command) => {
let test = new CommandlineTest(command);
tests.push(test);
return test;
};

this.execute = function(logFile) {
var failed = false;

(function runTests(i) {
if (i < tests.length) {
console.log('running: ' + tests[i].command_);
this.runTests = async function(i, logFile, failed) {
if (i < tests.length) {
try {
console.log('running: ' + tests[i].command);
if (logFile) {
tests[i].setTestLogFile(logFile);
}
tests[i].run().then(function() {
console.log('\n>>> \033[1;32mpass\033[0m');
}, function(err) {
failed = true;
console.log('\n>>> \033[1;31mfail: ' + err.toString() + '\033[0m');
}).fin(function() {
runTests(i + 1);
}).done();
} else {
console.log('Summary: ' + (failed ? 'fail' : 'pass'));
process.exit(failed ? 1 : 0);
await tests[i].run();
console.log('\n>>> \033[1;32mpass\033[0m');
cnishina marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
failed = true;
console.log('\n>>> \033[1;31mfail: ' + err.toString() + '\033[0m');
cnishina marked this conversation as resolved.
Show resolved Hide resolved
} finally {
this.runTests(i + 1, logFile, failed);
}
}(0));
} else {
console.log('Summary: ' + (failed ? 'fail' : 'pass'));
process.exit(failed ? 1 : 0);
}
};

this.execute = (logFile) => {
let failed = false;
this.runTests(0, logFile, failed);
};
};