Skip to content

Commit

Permalink
Merge pull request #1016 from qtomlinson/qt/fix-pypi-404
Browse files Browse the repository at this point in the history
Handle NotFound when mapping pypi coordinates
  • Loading branch information
qtomlinson authored Jan 10, 2024
2 parents 0e02529 + b85f32b commit 4ae722e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
13 changes: 11 additions & 2 deletions lib/pypiCoordinatesMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ class PypiCoordinatesMapper {

async _resolve(coordinates) {
const url = `${this.baseUrl}/pypi/${coordinates.name}/json`
const answer = await requestPromise({ url, method: 'GET', json: true })
return answer?.info?.name && { name: answer.info.name }
try {
const answer = await this._handleRequest(url)
return answer?.info?.name && { name: answer.info.name }
} catch (error) {
if (error.statusCode === 404) return null
throw error
}
}

_handleRequest(url) {
return requestPromise({ url, method: 'GET', json: true })
}
}

Expand Down
35 changes: 22 additions & 13 deletions test/lib/pypiCoordinatesMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,47 @@ function mockPypiCoordinates(name) {
return EntityCoordinates.fromString(`pypi/pypi/-/${name}/1.1.0a4`)
}

function mockPypiAnswer(name) {
return { info: { name } }
}

describe('PypiCoordinatesMapper', () => {
let coordinatesMapper
beforeEach(() => {
coordinatesMapper = new PypiCoordinatesMapper()
})

it('name containing "_" mapped to "-"', async () => {
const coordinatesMapper = new PypiCoordinatesMapper()
sinon.stub(coordinatesMapper, '_resolve').resolves({ name: '0-core-client' })
it('should map name containing "_" mapped to "-"', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').resolves(mockPypiAnswer('0-core-client'))
const mapped = await coordinatesMapper.map(mockPypiCoordinates('0_core_client'))
expect(mapped.name).to.be.eq('0-core-client')
})

it('name containing "." mapped to "-"', async () => {
const coordinatesMapper = new PypiCoordinatesMapper()
sinon.stub(coordinatesMapper, '_resolve').resolves({ name: '0-core-client' })
it('should map name containing "." mapped to "-"', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').resolves(mockPypiAnswer('0-core-client'))
const mapped = await coordinatesMapper.map(mockPypiCoordinates('0.core_client'))
expect(mapped.name).to.be.eq('0-core-client')
})

it('name containing "-" mapped to "_"', async () => {
const coordinatesMapper = new PypiCoordinatesMapper()
sinon.stub(coordinatesMapper, '_resolve').resolves({ name: 'backports.ssl_match_hostname' })
it('should map name containing "-" mapped to "_"', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').resolves(mockPypiAnswer('backports.ssl_match_hostname'))
const mapped = await coordinatesMapper.map(mockPypiCoordinates('Backports.ssl-match-hostname'))
expect(mapped.name).to.be.eq('backports.ssl_match_hostname')
})

it('name not resolved', async () => {
const coordinatesMapper = new PypiCoordinatesMapper()
it('should return null when pypi api returns 404', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').throws({ statusCode: 404 })
const mapped = await coordinatesMapper.map(mockPypiCoordinates('blivet-gui'))
expect(mapped).to.be.null
})

it('should handle name not resolved', async () => {
sinon.stub(coordinatesMapper, '_resolve').resolves(undefined)
const mapped = await coordinatesMapper.map(mockPypiCoordinates('0_core_client'))
expect(mapped).to.be.undefined
})

it('no mapping necessary', async () => {
const coordinatesMapper = new PypiCoordinatesMapper()
it('should handle no mapping necessary', async () => {
sinon.stub(coordinatesMapper, '_resolve').rejects('Should not be called')
const mapped = await coordinatesMapper.map(mockPypiCoordinates('backports'))
expect(mapped).to.be.null
Expand Down

0 comments on commit 4ae722e

Please sign in to comment.