From 642e16f4955a94362ee6080eb154910d8eb4a04c Mon Sep 17 00:00:00 2001 From: Craig Nishina Date: Tue, 27 Nov 2018 15:00:22 -0800 Subject: [PATCH] chore(promises): fix promises for test_util and update specs that were failing --- scripts/test.js | 6 +- scripts/test/test_util.js | 206 +++++++++++++------------ spec/basic/expected_conditions_spec.js | 2 +- spec/plugins/skipStabilityConf.js | 1 + spec/plugins/smokeConf.js | 1 + spec/plugins/specs/smoke_spec.js | 4 +- 6 files changed, 112 insertions(+), 108 deletions(-) diff --git a/scripts/test.js b/scripts/test.js index de379a6e4..717552eff 100755 --- a/scripts/test.js +++ b/scripts/test.js @@ -5,7 +5,7 @@ const Executor = require('./test/test_util').Executor; const passingTests = [ 'node built/cli.js spec/basicConf.js', - // 'node built/cli.js spec/basicConf.js --useBlockingProxy', + 'node built/cli.js spec/basicConf.js --useBlockingProxy', 'node built/cli.js spec/multiConf.js', 'node built/cli.js spec/altRootConf.js', 'node built/cli.js spec/inferRootConf.js', @@ -21,7 +21,7 @@ const passingTests = [ 'node built/cli.js spec/suitesConf.js --suite okmany', 'node built/cli.js spec/suitesConf.js --suite okspec', 'node built/cli.js spec/suitesConf.js --suite okmany,okspec', - // 'node built/cli.js spec/plugins/smokeConf.js', + 'node built/cli.js spec/plugins/smokeConf.js', 'node built/cli.js spec/plugins/multiPluginConf.js', 'node built/cli.js spec/plugins/jasminePostTestConf.js', 'node built/cli.js spec/plugins/mochaPostTestConf.js', @@ -37,7 +37,7 @@ const passingTests = [ 'node built/cli.js spec/controlLockConf.js', 'node built/cli.js spec/customFramework.js', 'node built/cli.js spec/noGlobalsConf.js', - // 'node built/cli.js spec/angular2Conf.js', + 'node built/cli.js spec/angular2Conf.js', 'node built/cli.js spec/hybridConf.js', 'node built/cli.js spec/built/noCFBasicConf.js', 'node built/cli.js spec/built/noCFBasicConf.js --useBlockingProxy', diff --git a/scripts/test/test_util.js b/scripts/test/test_util.js index a608c1e5f..79518e232 100644 --- a/scripts/test/test_util.js +++ b/scripts/test/test_util.js @@ -1,41 +1,41 @@ #!/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.assertExitCodeOnly_ = false; + this.testLogStream = 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.assertExitCodeOnly_ = 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. @@ -45,75 +45,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/); + try { - var test_process; - - 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) { @@ -123,9 +123,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 @@ -167,25 +167,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: @@ -195,34 +195,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) { + 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'); + } catch (err) { + failed = true; + console.log('\n>>> \033[1;31mfail: ' + err.toString() + '\033[0m'); + } 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); }; }; diff --git a/spec/basic/expected_conditions_spec.js b/spec/basic/expected_conditions_spec.js index b537a7cb5..c28a8fbf2 100644 --- a/spec/basic/expected_conditions_spec.js +++ b/spec/basic/expected_conditions_spec.js @@ -177,7 +177,7 @@ describe('expected conditions', () => { describe('for forked browsers', () => { // ensure that we can run EC on forked browser instances it('should have alertIsPresent', async () => { - const browser2 = browser.forkNewDriverInstance(); + const browser2 = await browser.forkNewDriverInstance().ready; await browser2.get('index.html#/form'); const EC2 = browser2.ExpectedConditions; const alertIsPresent = EC2.alertIsPresent(); diff --git a/spec/plugins/skipStabilityConf.js b/spec/plugins/skipStabilityConf.js index 2b0f1c627..c5107876f 100644 --- a/spec/plugins/skipStabilityConf.js +++ b/spec/plugins/skipStabilityConf.js @@ -3,6 +3,7 @@ var env = require('../environment.js'); // Verifies that plugins can change skipAngularStability on the fly. exports.config = { seleniumAddress: env.seleniumAddress, + SELENIUM_PROMISE_MANAGER: false, framework: 'jasmine', diff --git a/spec/plugins/smokeConf.js b/spec/plugins/smokeConf.js index 44cbc5ef0..a1d2dd21d 100644 --- a/spec/plugins/smokeConf.js +++ b/spec/plugins/smokeConf.js @@ -4,6 +4,7 @@ var env = require('../environment.js'); // Tests the (potential) edge case of exactly one plugin being used exports.config = { mockSelenium: true, + SELENIUM_PROMISE_MANAGER: false, framework: 'jasmine', diff --git a/spec/plugins/specs/smoke_spec.js b/spec/plugins/specs/smoke_spec.js index bc7f67f1b..2dafffaf0 100644 --- a/spec/plugins/specs/smoke_spec.js +++ b/spec/plugins/specs/smoke_spec.js @@ -1,5 +1,5 @@ -describe('check if plugin setup ran', function() { - it('should have set protractor.__BASIC_PLUGIN_RAN_*', function() { +describe('check if plugin setup ran', () => { + it('should have set protractor.__BASIC_PLUGIN_RAN_*', () => { expect(protractor.__BASIC_PLUGIN_RAN_SETUP).toBe(true); expect(protractor.__BASIC_PLUGIN_RAN_ON_PREPARE).toBe(true); });