diff --git a/handlebars/helpers.js b/handlebars/helpers.js index a854cc7..4aeb1a5 100644 --- a/handlebars/helpers.js +++ b/handlebars/helpers.js @@ -280,6 +280,12 @@ module.exports = { .then(function (response) { return require('cheerio')(response).find('text').last().text() !== 'not found' }) + .catch(function (err) { + if (err.statusCode === 404) { + return false + } + throw err + }) } } diff --git a/package.json b/package.json index d11d536..5cd06b0 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "husky": "^0.13.2", "istanbul": "^0.4.5", "mocha": "^3.2.0", + "nock": "^9.0.9", "recursive-copy": "^2.0.5", "thoughtful-release": "^0.3.0" }, diff --git a/test/helper-spec.js b/test/helper-spec.js index 3c4f9f8..4116664 100644 --- a/test/helper-spec.js +++ b/test/helper-spec.js @@ -18,6 +18,7 @@ var chaiAsPromised = require('chai-as-promised') chai.use(chaiAsPromised) var expect = chai.expect var path = require('path') +var httpMocks = require('./lib/http-mocks') function executeInDir (directory) { var oldCwd = null @@ -359,4 +360,64 @@ describe('thought-helpers:', function () { .not.to.eventually.be.ok() }) }) + + describe('The "hasGreenkeeper"-helper', function () { + afterEach(httpMocks.cleanup) + + it('should return false, if the githubRepo is not set', function () { + httpMocks.greenkeeperError('/null.svg', 404) + + return expectHbs('{{hasGreenkeeper}}', { + package: { + name: 'no-git-repo' + } + }) + .not.to.eventually.be.false() + }) + + it('should return false, project does not have a github url', function () { + httpMocks.greenkeeperError('/null.svg', 404) + + return expectHbs('{{hasGreenkeeper}}', { + package: { + name: 'no-git-repo', + 'repository': { + 'type': 'git', + 'url': 'https://custom-git.com/somepath.git' + } + } + }) + .not.to.eventually.be.false() + }) + + it('should return false, if the project does not have greenkeeper enabled', function () { + httpMocks.greenkeeperDisabled('/group/repo.svg') + + return expectHbs('{{hasGreenkeeper}}', { + package: { + name: 'no-git-repo', + 'repository': { + 'type': 'git', + 'url': 'https://github.com/group/repo.git' + } + } + }) + .not.to.eventually.be.false() + }) + + it('should throw an error, if the greenkeeper-badge returns an error other than 404', function () { + httpMocks.greenkeeperError('/group/repo.svg', 500) + + return expectHbs('{{hasGreenkeeper}}', { + package: { + name: 'no-git-repo', + 'repository': { + 'type': 'git', + 'url': 'https://github.com/group/repo.git' + } + } + }) + .to.be.rejectedWith('') + }) + }) }) diff --git a/test/lib/http-mocks.js b/test/lib/http-mocks.js new file mode 100644 index 0000000..526d0c9 --- /dev/null +++ b/test/lib/http-mocks.js @@ -0,0 +1,41 @@ +var nock = require('nock') + +module.exports = { + /** + * Setup mock response for a greenkeeper-badge that shows that a package *has* greenkeeper + * @param {string} urlPath the path-component of the badge-url for the project (e.g. 'nknapp/thought.svg`) + */ + greenkeeperEnabled: function (urlPath) { + nock(`https://badges.greenkeeper.io/`) + .get(urlPath) + .reply(200, `GreenkeeperGreenkeeperenabledenabled`) + }, + + /** + * Setup mock response for a greenkeeper-badge that shows that a package *does not have* greenkeeper + * @param {string} urlPath the path-component of the badge-url for the project (e.g. 'nknapp/thought.svg`) + */ + greenkeeperDisabled: function (urlPath) { + nock(`https://badges.greenkeeper.io/`) + .get(urlPath) + .reply(200, `GreenkeeperGreenkeepernot foundnot found`) + }, + + /** + * Setup a greenkeeper error (for invalid paths) + * @param {string} urlPath the path-component of the badge-url for the project (e.g. 'nknapp/thought.svg`) + * @param {number} statusCode the status-code of the error (e.g. 404) + */ + greenkeeperError: function (urlPath, statusCode) { + nock(`https://badges.greenkeeper.io/`) + .get(urlPath) + .reply(statusCode, `{"statusCode":${statusCode},"error":"Not Found"}`) + }, + + /** + * Runs https://github.com/node-nock/nock#cleanall to clean all nock settings + */ + cleanup: function () { + nock.cleanAll() + } +}