Skip to content

Commit c193942

Browse files
committed
test: use nock to avoid hitting real servers
1 parent 0127ae8 commit c193942

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ internals.createRepositoryLoader = (repository) => {
1515

1616
return async (filename) => {
1717

18+
if (parsedRepository.source !== 'github.com') {
19+
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/node-support');
20+
}
21+
1822
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
1923

2024
const { payload } = await Wreck.get(url);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@hapi/code": "^7.0.0",
2121
"@hapi/lab": "^21.0.0",
2222
"allow-scripts": "^1.5.2",
23+
"nock": "^11.7.2",
2324
"semantic-release": "^15.14.0",
2425
"tmp": "^0.1.0"
2526
},

test/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
const Fs = require('fs');
4+
const Nock = require('nock');
45
const Path = require('path');
56
const Tmp = require('tmp');
67

78
const NodeSupport = require('..');
89

910

10-
const { describe, it, afterEach } = exports.lab = require('@hapi/lab').script();
11+
const { describe, it, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script();
1112
const { expect } = require('@hapi/code');
1213

1314

@@ -246,8 +247,27 @@ describe('node-support', () => {
246247

247248
describe('repository', () => {
248249

250+
beforeEach(() => {
251+
252+
if (!Nock.isActive()) {
253+
Nock.activate();
254+
}
255+
});
256+
257+
afterEach(() => {
258+
259+
Nock.restore();
260+
Nock.cleanAll();
261+
});
262+
249263
it('returns node versions from `.travis.yml` in the repository', async () => {
250264

265+
Nock('https://raw.githubusercontent.com')
266+
.get('/pkgjs/node-support/HEAD/package.json')
267+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
268+
.get('/pkgjs/node-support/HEAD/.travis.yml')
269+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
270+
251271
const result = await NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' });
252272

253273
expect(result).to.equal({
@@ -258,6 +278,12 @@ describe('node-support', () => {
258278
}
259279
});
260280
});
281+
282+
it('throws when a package does not live on public github.com', async () => {
283+
284+
await expect(NodeSupport.detect({ repository: 'git+https://github.example.com/pkgjs/node-support.git' }))
285+
.to.reject('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/node-support');
286+
});
261287
});
262288
});
263289
});

0 commit comments

Comments
 (0)