Skip to content

Commit

Permalink
fix: issue with git urls being unsupported for repo (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Dec 4, 2020
1 parent 772131e commit ab40e70
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 75 deletions.
165 changes: 92 additions & 73 deletions __tests__/marshalls.repo.test.js
Expand Up @@ -28,90 +28,109 @@ const fullPkgData = {
}
}

test('has the right title', async () => {
expect(testMarshall.title()).toEqual('Identifying package repository...')
})

test('throws the right error when there is no pkg data available', async () => {
await expect(testMarshall.validate({ packageName: {} })).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when there is no repo in the pkg data', async () => {
const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {}
describe('Repo test suites', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.resetAllMocks()
})

test('has the right title', async () => {
expect(testMarshall.title()).toEqual('Identifying package repository...')
})

test('throws the right error when there is no pkg data available', async () => {
await expect(testMarshall.validate({ packageName: {} })).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when there is no repo in the pkg data', async () => {
const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {}
}
}
}
}

await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when there is no repo URL in the pkg data', async () => {
const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
repository: {}
await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when there is no repo URL in the pkg data', async () => {
const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
repository: {}
}
}
}
}
}

await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when the repository url does not exist', async () => {
fetch.mockImplementationOnce(() =>
Promise.reject(new Error('error'))
)

await expect(testMarshall.validate(fullPkgData)).rejects.toThrow(
'the repository associated with the package (url) does not exist or is unreachable at the moment.'
)
})

test('throws the right error when the homepage url does not exist', async () => {
fetch.mockImplementationOnce(() =>
Promise.reject(new Error('error'))
)

const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
repository: {},
homepage: 'homepage-url'
await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the package has no associated repository or homepage.'
)
})

test('throws the right error when the repository url does not exist', async () => {
fetch.mockImplementationOnce(() =>
Promise.reject(new Error('error'))
)

await expect(testMarshall.validate(fullPkgData)).rejects.toThrow(
'no valid repository is associated with the package'
)
})

test('throws the right error when the repository url is unreachable', async () => {
fetch.mockImplementationOnce(() =>
Promise.reject(new Error('error'))
)

fullPkgData.packageName.versions['1.0.0'].repository.url = 'https://dsfsdfsdfs.abcdeugwecwekjasda.com/'
await expect(testMarshall.validate(fullPkgData)).rejects.toThrow(
'the repository associated with the package (https://dsfsdfsdfs.abcdeugwecwekjasda.com/) does not exist or is unreachable at the moment.'
)
})

test('throws the right error when the homepage url does not exist', async () => {
fetch.mockImplementationOnce(() =>
Promise.reject(new Error('error'))
)

const pkgData = {
packageName: {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
repository: {},
homepage: 'homepage-url'
}
}
}
}
}

await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the homepage associated with the package (homepage-url) does not exist or is unreachable at the moment.'
)
})
await expect(testMarshall.validate(pkgData)).rejects.toThrow(
'the homepage associated with the package (homepage-url) does not exist or is unreachable at the moment.'
)
})

test('does not throw any errors if the url exists', async () => {
fetch.mockImplementationOnce(() =>
Promise.resolve('success')
)
test('does not throw any errors if the url exists', async () => {
fetch.mockImplementationOnce(() =>
Promise.resolve('success')
)

await expect(testMarshall.validate(fullPkgData)).resolves.toEqual(expect.anything())
fullPkgData.packageName.versions['1.0.0'].repository.url = 'https://google.com'
await expect(testMarshall.validate(fullPkgData)).resolves.toEqual(expect.anything())
})
})
12 changes: 10 additions & 2 deletions lib/marshalls/repo.marshall.js
Expand Up @@ -2,6 +2,7 @@

const BaseMarshall = require('./baseMarshall')
const fetch = require('node-fetch')
const URL = require('url').URL
const MARSHALL_NAME = 'repo'

class Marshall extends BaseMarshall {
Expand All @@ -19,9 +20,16 @@ class Marshall extends BaseMarshall {
const lastVersionData = (data.versions && data['dist-tags'] && data['dist-tags'].latest &&
data.versions[data['dist-tags'].latest]) || data
if (lastVersionData && lastVersionData.repository && lastVersionData.repository.url) {
return fetch(lastVersionData.repository.url)
let urlStructure, urlOfGitRepository
try {
urlStructure = new URL(lastVersionData.repository.url)
urlOfGitRepository = new URL(`https://${urlStructure.host}${urlStructure.pathname}`)
} catch (error) {
throw new Error(`no valid repository is associated with the package`)
}
return fetch(urlOfGitRepository.href)
.catch(() => {
throw new Error(`the repository associated with the package (${lastVersionData.repository.url}) does not exist or is unreachable at the moment.`)
throw new Error(`the repository associated with the package (${urlOfGitRepository.href}) does not exist or is unreachable at the moment.`)
})
} else if (lastVersionData && lastVersionData.homepage) {
return fetch(lastVersionData.homepage)
Expand Down

0 comments on commit ab40e70

Please sign in to comment.