From d41256b6661bb7d60c93ecfcf56633277cd6d4f4 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Sun, 25 Jun 2017 01:19:32 -0400 Subject: [PATCH] fix(test): trying to handle errors better --- src/exec-test.js | 19 +++++++++++-------- src/next-update.js | 1 + src/npm-test.js | 11 ++++++----- src/test-module-version.js | 13 ++++++++++++- src/utils.js | 8 +++++++- test/as-parent-spec.js | 4 +++- test/test-next-updater/package.json | 2 +- 7 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/exec-test.js b/src/exec-test.js index 8bf11c8..d9cf515 100644 --- a/src/exec-test.js +++ b/src/exec-test.js @@ -4,11 +4,14 @@ var spawn = require('child_process').spawn var q = require('q') var npmPath = require('./npm-test').npmPath var _ = require('lodash') +const debug = require('debug')('next-update') // returns a promise +// TODO switch to execa function test (options, testCommand) { options = options || {} var log = options.tldr ? _.noop : console.log.bind(console) + debug('exec-test "%s"', testCommand) verify.unemptyString(testCommand, 'missing test command string') log(' ', testCommand) @@ -40,10 +43,10 @@ function test (options, testCommand) { console.error('test command: "' + testCommand + '"') console.error(err) testErrors += err.toString() - deferred.reject({ - code: err.code, - errors: testErrors - }) + const e = new Error('test command failed') + e.code = err.code + e.errors = testErrors + deferred.reject(e) }) testProcess.on('exit', function (code) { @@ -51,10 +54,10 @@ function test (options, testCommand) { console.error('testProcess test returned', code) console.error('test errors:\n' + testErrors) console.error(testOutput) - deferred.reject({ - code: code, - errors: testErrors - }) + const e = new Error('test exit code means error') + e.code = code + e.errors = testErrors + return deferred.reject(e) } deferred.resolve() }) diff --git a/src/next-update.js b/src/next-update.js index 8da32eb..6f021a6 100644 --- a/src/next-update.js +++ b/src/next-update.js @@ -68,6 +68,7 @@ function checkCurrentInstall (options) { return cleanDependencies() .then(checkDependenciesInstalled) .then(function () { + log('running test command') return runTest(options, options.testCommand)() }) .then(function () { diff --git a/src/npm-test.js b/src/npm-test.js index 38f0bcd..48435e2 100644 --- a/src/npm-test.js +++ b/src/npm-test.js @@ -61,14 +61,15 @@ function test (options) { var deferred = q.defer() npm.on('exit', function (code) { if (code) { - errorLog('npm test returned', code) + errorLog('npm test returned', code, '\n') errorLog('test output:\n' + testOutput) errorLog('test errors:\n' + testErrors) - deferred.reject({ - code: code, - errors: testErrors - }) + const e = new Error('npm test exit code means error') + e.code = code + e.errors = testErrors + + deferred.reject(e) } deferred.resolve() }) diff --git a/src/test-module-version.js b/src/test-module-version.js index 796f2e3..8cac201 100644 --- a/src/test-module-version.js +++ b/src/test-module-version.js @@ -174,6 +174,7 @@ function testModuleVersions (options, results) { }) var checkAllPromise = checkPromises.reduce(q.when, q()) if (options.keep) { + debug('keep working updates for %s', name) checkAllPromise = checkAllPromise.then(function (result) { verify.array(result, 'expected array of results', result) var lastSuccess = _.last(_.filter(result, { works: true })) @@ -194,10 +195,16 @@ function testModuleVersions (options, results) { }) } else { checkAllPromise = checkAllPromise - .then(restoreVersionFunc) + .then(restoreVersionFunc, (err) => { + console.error('Could not check all versions') + console.error(err) + throw err + }) } checkAllPromise .then(function (result) { + debug('got result') + debug(result) check.verify.array(result, 'could not get result array') results.push(result) deferred.resolve(results) @@ -290,6 +297,7 @@ function testModuleVersion (options, results) { }, function (error) { reportFailure(nameVersion + ' tests failed :(', options.color) + debug('sending stats results') stats.sendUpdateResult({ name: options.name, from: options.currentVersion, @@ -297,6 +305,7 @@ function testModuleVersion (options, results) { success: false }) + debug('checking error code', error.code) verify.number(error.code, 'expected code in error ' + JSON.stringify(error, null, 2)) @@ -324,6 +333,8 @@ function testPromise (options, command) { if (command) { verify.unemptyString(command, 'expected string command, not ' + command) testFunction = execTest.bind(null, options, command) + } else { + debug('missing test command') } return testFunction } diff --git a/src/utils.js b/src/utils.js index ee6e936..9b29aab 100644 --- a/src/utils.js +++ b/src/utils.js @@ -40,9 +40,15 @@ function getTestCommand (packageFilename, moduleName) { return config.commands[moduleName] } +const stringify = (x) => JSON.stringify(x, null, 2) + +const errorObject = (x) => (new Error(stringify(x))) + module.exports = { name, getConfig, getSkippedModules, - getTestCommand + getTestCommand, + stringify, + errorObject } diff --git a/test/as-parent-spec.js b/test/as-parent-spec.js index 86dbf60..84f3a7c 100644 --- a/test/as-parent-spec.js +++ b/test/as-parent-spec.js @@ -39,7 +39,8 @@ describe('testing check-types', () => { this.timeout(TWO_MINUTES) const opts = { module: 'check-types', - latest: true + latest: true, + keep: false } const removeVersions = (results) => results.map(r => { la(is.semver(r.version), 'expected version', r) @@ -64,6 +65,7 @@ describe('testing check-types', () => { const opts = { module: 'check-types', + keep: false, limit } return snapShot(nextUpdate(opts)) diff --git a/test/test-next-updater/package.json b/test/test-next-updater/package.json index cc4bf81..6c94c21 100644 --- a/test/test-next-updater/package.json +++ b/test/test-next-updater/package.json @@ -18,6 +18,6 @@ "author": "Gleb Bahmutov ", "license": "MIT", "dependencies": { - "check-types": "7.0.1" + "check-types": "0.6.5" } }