Skip to content

Commit 2415647

Browse files
committed
feat: throw when no package.json in repo
1 parent b3f7960 commit 2415647

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/loader.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ internals.createRepositoryLoader = (repository) => {
4545

4646
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
4747

48-
const { payload } = await Wreck.get(url);
48+
try {
49+
const { payload } = await Wreck.get(url);
4950

50-
return payload;
51+
return payload;
52+
}
53+
catch (err) {
54+
55+
if (err.output && err.output.statusCode === 404) {
56+
throw new Error(`${repository} does not contain a ${filename}`);
57+
}
58+
59+
throw err;
60+
}
5161
}
5262
};
5363
};

test/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Path = require('path');
66
const Sinon = require('sinon');
77
const SimpleGit = require('simple-git/promise');
88
const Tmp = require('tmp');
9+
const Wreck = require('@hapi/wreck');
910

1011
const NodeSupport = require('..');
1112

@@ -407,6 +408,43 @@ describe('node-support', () => {
407408
});
408409
});
409410

411+
it('throws when repository does not have a package.json', async () => {
412+
413+
listRemoteStub
414+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
415+
416+
Nock('https://raw.githubusercontent.com')
417+
.get('/pkgjs/node-support/HEAD/package.json')
418+
.reply(404)
419+
.get('/pkgjs/node-support/HEAD/.travis.yml')
420+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
421+
422+
await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' }))
423+
.to.reject(`git+https://github.com/pkgjs/node-support.git does not contain a package.json`);
424+
});
425+
426+
it('rethrows server errors', async () => {
427+
428+
Nock('https://raw.githubusercontent.com')
429+
.get('/pkgjs/node-support/HEAD/package.json')
430+
.reply(500)
431+
.get('/pkgjs/node-support/HEAD/.travis.yml')
432+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
433+
434+
await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' }))
435+
.to.reject(/Response Error/);
436+
});
437+
438+
it('rethrows generic errors', async () => {
439+
440+
const err = new Error('Something went wrong');
441+
442+
Sinon.stub(Wreck, 'get').throws(err);
443+
444+
await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/node-support.git' }))
445+
.to.reject('Something went wrong');
446+
});
447+
410448
it('throws when a package does not live on public github.com', async () => {
411449

412450
await expect(NodeSupport.detect({ repository: 'git+https://github.example.com/pkgjs/node-support.git' }))

0 commit comments

Comments
 (0)