Skip to content

Commit

Permalink
Merge pull request #842 from node-nock/feature-832-versioned-tests
Browse files Browse the repository at this point in the history
Feature #832: Versioned tests
  • Loading branch information
ianwsperber authored Mar 1, 2017
2 parents 2a012a0 + bdf3cf7 commit c5f265f
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ node_js:
- 4
- 6
- 7
script: "npm run coverage"
script: "npm run coverage && npm run integration"
sudo: false
after_success: "npm run coveralls"
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"browserify": "^13.0.0",
"changelog": "^1.0.7",
"coveralls": "^2.11.2",
"glob": "^7.1.1",
"hyperquest": "^1.3.0",
"isomorphic-fetch": "^2.2.0",
"istanbul": "^0.4.2",
Expand All @@ -201,7 +202,10 @@
"zombie": "^5.0.1"
},
"scripts": {
"test": "tap --harmony ./tests/test_*.js",
"unit": "tap --harmony ./tests/test_*.js",
"preintegration": "./tests/bin/install_sub_deps versioned",
"integration": "tap --harmony ./tests/versioned/*/*.tap.js",
"test": "npm run unit && npm run integration",
"coverage": "nyc tap --harmony ./tests/test_*.js",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"jshint": "jshint lib/*.js",
Expand Down
61 changes: 61 additions & 0 deletions tests/bin/install_sub_deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node
/* Modeled after: https://github.com/newrelic/node-newrelic/blob/master/test/bin/install_sub_deps */
'use strict'

const path = require('path')
const glob = require('glob')
const assert = require('assert')
const exec = require('child_process').exec

const folder = process.argv[2];
const retries = 2

assert(folder, 'Missing folder!');

glob(`**/tests/${folder}/**/package.json`, {}, (err, packages) => {
if (err) { fail(err); }

install(packages.filter((line) => !/node_modules|example/.test(line)));
});

const install = (packages, retry) => {
if (!retry) retry = 0

const failed = []
let counter = 0

packages.forEach((packageJsonPath) => {
const packagePath = path.resolve(packageJsonPath).split('/').slice(0, -1).join('/');

console.log('installing deps in %s', packagePath)
exec(`cd ${packagePath} && rm -rf node_modules && npm i`, {
maxBuffer: 1024 * 500
}, (err, stdout, stderr) => {
if (err || isErrorOutput(stderr)) {
printError(packageJsonPath, err || stderr)
if (retry < retries) {
failed.push(packageJsonPath)
}
}

if (++counter === packages.length) {
if (failed.length > 0 && retry < retries) {
console.log('%s package(s) failed to install, retrying...', failed.length)
exec('npm cache clean', () => install(failed, ++retry));
}
}
})
})
}

// determines if stderr contains errors
function isErrorOutput(text) {
return (text && text.indexOf('npm ERR') > -1)
}

function printError(packagePath, error) {
console.log('-----------------------------------------------------------')
console.log(packagePath + ' failed installing with the following error:')
console.log(error)
console.log('-----------------------------------------------------------')
}
49 changes: 49 additions & 0 deletions tests/versioned/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const tap = require('tap');
const nock = require('../../');

/**
* Run common test suite against an HTTP client library.
*
* The makeRequest fn should be a wrapper around an
* HTTP client, which maps our arguments to those accepted by the client.
*
* The client is expected to accept an options object with props:
* - uri
* - method
* - timeout
* And return a promise, which in success has properties
* - statusCode (number)
* - body (string)
* And in failure is a standard error object.
*
* @param {Function} makeRequest Request fn
* @param {string} name Lib we're testing
*/
const runCommonTests = (makeRequest, name) => {
const test = (description, cb) => {
return tap.test(`${name}: ${description}`, cb);
};

test('basic intercept', (t) => {
nock('http://www.example.com')
.get('/')
.reply(200, 'OK');

return makeRequest({
uri: 'http://www.example.com'
})
.then((res) => {
t.equal(res.statusCode, 200);
t.deepEqual(res.body, 'OK');

t.end();
})
.catch(t.threw);
});
};

module.exports = {
runCommonTests
};
14 changes: 14 additions & 0 deletions tests/versioned/got-latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

{
"name": "got-latest",
"version": "0.0.0",
"description": "Tests for got latest (>6)",
"main": "test.tap.js",
"scripts": {
"test": "tap test.tap.js"
},
"dependencies": {
"got": "^6"
},
"private": true
}
14 changes: 14 additions & 0 deletions tests/versioned/got-latest/test.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const tap = require('tap');
const got = require('got');
const common = require('../common');

const makeRequest = (options) => {
return got(options.uri, {
method: options.method,
timeout: options.timeout
});
};

common.runCommonTests(makeRequest, 'got@latest');
14 changes: 14 additions & 0 deletions tests/versioned/popsicle-latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

{
"name": "posicle-latest",
"version": "0.0.0",
"description": "Tests for popsicle latest (>9)",
"main": "test.tap.js",
"scripts": {
"test": "tap test.tap.js"
},
"dependencies": {
"popsicle": "^9"
},
"private": true
}
19 changes: 19 additions & 0 deletions tests/versioned/popsicle-latest/test.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const tap = require('tap');
const popsicle = require('popsicle');
const common = require('../common');

const makeRequest = (options) => {
return popsicle.request({
url: options.uri,
method: options.method || 'GET'
})
.then((res) => {
res.statusCode = res.status;

return res;
})
};

common.runCommonTests(makeRequest, 'popsicle@latest');
14 changes: 14 additions & 0 deletions tests/versioned/request-latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

{
"name": "request-latest",
"version": "0.0.0",
"description": "Tests for request latest (>2)",
"main": "test.tap.js",
"scripts": {
"test": "tap test.tap.js"
},
"dependencies": {
"request": "^2"
},
"private": true
}
21 changes: 21 additions & 0 deletions tests/versioned/request-latest/test.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const tap = require('tap');
const request = require('request');
const common = require('../common');

const makeRequest = (options) => {
return new Promise((resolve, reject) => {
request({
uri: options.uri,
method: options.method || 'GET'
}, (err, res, body) => {
if (err) { return reject(err); }

res.body = body;
resolve(res);
});
});
};

common.runCommonTests(makeRequest, 'request@latest');

0 comments on commit c5f265f

Please sign in to comment.