-
-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from node-nock/feature-832-versioned-tests
Feature #832: Versioned tests
- Loading branch information
Showing
10 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('-----------------------------------------------------------') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |