Skip to content

Commit

Permalink
"hasGreenkeeper" now returns "false" for projects without repo-url
Browse files Browse the repository at this point in the history
- ... instead of throwing an error
- Plus explicit tests for the helper
- using "nock" to moch http-responses to badges.greenkeeper.io
  to reduce load on that site and make tests faster
  • Loading branch information
nknapp committed Mar 19, 2017
1 parent 6288eac commit dac01c6
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions handlebars/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
61 changes: 61 additions & 0 deletions test/helper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('')
})
})
})
41 changes: 41 additions & 0 deletions test/lib/http-mocks.js
Original file line number Diff line number Diff line change
@@ -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, `<svg xmlns="http://www.w3.org/2000/svg" width="128" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="128" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h77v20H0z"/><path fill="#4c1" d="M77 0h51v20H77z"/><path fill="url(#b)" d="M0 0h128v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="38.5" y="15" fill="#010101" fill-opacity=".3">Greenkeeper</text><text x="38.5" y="14">Greenkeeper</text><text x="101.5" y="15" fill="#010101" fill-opacity=".3">enabled</text><text x="101.5" y="14">enabled</text></g></svg>`)
},

/**
* 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, `<svg xmlns="http://www.w3.org/2000/svg" width="138" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="138" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h77v20H0z"/><path fill="#9f9f9f" d="M77 0h61v20H77z"/><path fill="url(#b)" d="M0 0h138v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="38.5" y="15" fill="#010101" fill-opacity=".3">Greenkeeper</text><text x="38.5" y="14">Greenkeeper</text><text x="106.5" y="15" fill="#010101" fill-opacity=".3">not found</text><text x="106.5" y="14">not found</text></g></svg>`)
},

/**
* 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()
}
}

0 comments on commit dac01c6

Please sign in to comment.