Skip to content

Commit 61282ae

Browse files
committed
fix: return results when no .travis.yml in the repo
Closes #16
1 parent 1fe42ef commit 61282ae

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

lib/loader.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ internals.createRepositoryLoader = (repository) => {
9494
catch (err) {
9595

9696
if (err.output && err.output.statusCode === 404) {
97-
throw new Error(`${repository} does not contain a ${filename}`);
97+
const error = new Error(`${repository} does not contain a ${filename}`);
98+
error.code = 'ENOENT';
99+
throw error;
98100
}
99101

100102
throw err;
@@ -126,10 +128,6 @@ internals.createPathLoader = async (path) => {
126128

127129
const fullPath = Path.join(path, filename);
128130

129-
if (!Fs.existsSync(fullPath)) {
130-
return;
131-
}
132-
133131
const buffer = Fs.readFileSync(fullPath);
134132

135133
if (options.json) {

lib/travis.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ internals.scan = async (travisYaml) => {
7878

7979
exports.detect = async ({ loadFile }) => {
8080

81-
const buffer = await loadFile('.travis.yml');
81+
try {
82+
var buffer = await loadFile('.travis.yml');
83+
}
84+
catch (err) {
85+
86+
if (err.code === 'ENOENT') {
87+
return;
88+
}
8289

83-
if (buffer === undefined) {
84-
return;
90+
throw err;
8591
}
8692

8793
const travisYaml = Yaml.safeLoad(buffer, { schema: Yaml.FAILSAFE_SCHEMA });

test/index.js

+69
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,46 @@ describe('detect-node-support', () => {
498498
});
499499
});
500500

501+
it('leaves out `travis` when no `.travis.yml` present', async () => {
502+
503+
listRemoteStub
504+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
505+
506+
Nock('https://raw.githubusercontent.com')
507+
.get('/pkgjs/detect-node-support/HEAD/package.json')
508+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
509+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
510+
.reply(404);
511+
512+
const result = await NodeSupport.detect({ repository: 'git+https://github.com./pkgjs/detect-node-support.git' });
513+
514+
expect(listRemoteStub.callCount).to.equal(1);
515+
expect(listRemoteStub.args[0]).to.equal([['https://github.com./pkgjs/detect-node-support.git', 'HEAD']]);
516+
517+
expect(result).to.equal({
518+
name: 'detect-node-support',
519+
version: '0.0.0-development',
520+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
521+
timestamp: 1580673602000,
522+
engines: '>=10'
523+
});
524+
});
525+
526+
it('throws when loading `.travis.yml` fails', async () => {
527+
528+
listRemoteStub
529+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
530+
531+
Nock('https://raw.githubusercontent.com')
532+
.get('/pkgjs/detect-node-support/HEAD/package.json')
533+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
534+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
535+
.reply(500);
536+
537+
await expect(NodeSupport.detect({ repository: 'git+https://github.com./pkgjs/detect-node-support.git' }))
538+
.to.reject('Response Error: 500 null'); // the null is a Nock/Wreck implementation detail
539+
});
540+
501541
it('throws when repository does not have a package.json', async () => {
502542

503543
listRemoteStub
@@ -581,6 +621,35 @@ describe('detect-node-support', () => {
581621
});
582622
});
583623

624+
it('leaves out `travis` when no `.travis.yml` present', async () => {
625+
626+
listRemoteStub
627+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
628+
629+
Nock('https://raw.githubusercontent.com')
630+
.get('/pkgjs/detect-node-support/HEAD/package.json')
631+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
632+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
633+
.reply(404);
634+
635+
Nock('https://registry.npmjs.org')
636+
.get('/detect-node-support')
637+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')));
638+
639+
const result = await NodeSupport.detect({ packageName: 'detect-node-support' });
640+
641+
expect(listRemoteStub.callCount).to.equal(1);
642+
expect(listRemoteStub.args[0]).to.equal([['https://github.com./pkgjs/detect-node-support.git', 'HEAD']]);
643+
644+
expect(result).to.equal({
645+
name: 'detect-node-support',
646+
version: '0.0.0-development',
647+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
648+
timestamp: 1580673602000,
649+
engines: '>=10'
650+
});
651+
});
652+
584653
it('throws when package does not exist in the registry', async () => {
585654

586655
Nock('https://registry.npmjs.org')

0 commit comments

Comments
 (0)