Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NotFound when mapping pypi coordinates #1016

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading