Skip to content

Commit fcc66c2

Browse files
authored
Refine purl parsing and tests
1 parent 1dd418b commit fcc66c2

6 files changed

Lines changed: 85 additions & 49 deletions

File tree

‎__tests__/config.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ test('it raises an error if an empty allow list is specified', async () => {
5757
test('it raises an error when an invalid package-url is used for deny-packages', async () => {
5858
setInput('deny-packages', 'not-a-purl')
5959

60-
await expect(readConfig()).rejects.toThrow(`Error parsing purl`)
60+
await expect(readConfig()).rejects.toThrow(`Error parsing package-url`)
6161
})
6262

6363
test('it raises an error when an argument to deny-groups is missing a namespace', async () => {
6464
setInput('deny-groups', 'pkg:npm/my-fun-org')
6565

66-
await expect(readConfig()).rejects.toThrow(`purl must have a namespace`)
66+
await expect(readConfig()).rejects.toThrow(
67+
`package-url must have a namespace`
68+
)
6769
})
6870

6971
test('it raises an error when given an unknown severity', async () => {

‎__tests__/purl.test.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import {parsePURL} from '../src/purl'
44
test('parsePURL returns an error if the purl does not start with "pkg:"', () => {
55
const purl = 'not-a-purl'
66
const result = parsePURL(purl)
7-
expect(result.error).toEqual('purl must start with "pkg:"')
7+
expect(result.error).toEqual('package-url must start with "pkg:"')
88
})
99

10-
test('parsePURL returns an error if the purl does not contain an ecosystem', () => {
10+
test('parsePURL returns an error if the purl does not contain a type', () => {
1111
const purl = 'pkg:/'
1212
const result = parsePURL(purl)
13-
expect(result.error).toEqual('purl must contain an ecosystem')
13+
expect(result.error).toEqual('package-url must contain a type')
1414
})
1515

1616
test('parsePURL returns an error if the purl does not contain a namespace or name', () => {
1717
const purl = 'pkg:ecosystem/'
1818
const result = parsePURL(purl)
1919
expect(result.type).toEqual('ecosystem')
20-
expect(result.error).toEqual('purl must contain a namespace or name')
20+
expect(result.error).toEqual('package-url must contain a namespace or name')
2121
})
2222

2323
test('parsePURL returns a PURL with the correct values in the happy case', () => {
@@ -88,6 +88,28 @@ test('parsePURL table test', () => {
8888
error: null
8989
}
9090
},
91+
{
92+
purl: 'pkg:/?',
93+
expected: {
94+
type: '',
95+
namespace: null,
96+
name: null,
97+
version: null,
98+
original: 'pkg:/?',
99+
error: 'package-url must contain a type'
100+
}
101+
},
102+
{
103+
purl: 'pkg:ecosystem/#',
104+
expected: {
105+
type: 'ecosystem',
106+
namespace: null,
107+
name: null,
108+
version: null,
109+
original: 'pkg:ecosystem/#',
110+
error: 'package-url must contain a namespace or name'
111+
}
112+
},
91113
{
92114
purl: 'pkg:ecosystem/name@version#subpath?attributes=123',
93115
expected: {

‎dist/index.js‎

Lines changed: 36 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/purl.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as z from 'zod'
22

3-
// the basic purl type, containing ecosystem, namespace, name, and version.
4-
// other than ecosystem, all fields are nullable. this is for maximum flexibility
3+
// the basic purl type, containing type, namespace, name, and version.
4+
// other than type, all fields are nullable. this is for maximum flexibility
55
// at the cost of strict adherence to the package-url spec.
66
export const PurlSchema = z.object({
77
type: z.string(),
@@ -14,7 +14,7 @@ export const PurlSchema = z.object({
1414

1515
export type PackageURL = z.infer<typeof PurlSchema>
1616

17-
const PURL_ECOSYSTEM = /pkg:([a-zA-Z0-9-_]+)\/.*/
17+
const PURL_TYPE = /pkg:([a-zA-Z0-9-_]+)\/.*/
1818

1919
export function parsePURL(purl: string): PackageURL {
2020
const result: PackageURL = {
@@ -26,19 +26,19 @@ export function parsePURL(purl: string): PackageURL {
2626
error: null
2727
}
2828
if (!purl.startsWith('pkg:')) {
29-
result.error = 'purl must start with "pkg:"'
29+
result.error = 'package-url must start with "pkg:"'
3030
return result
3131
}
32-
const ecosystem = purl.match(PURL_ECOSYSTEM)
33-
if (ecosystem === null) {
34-
result.error = 'purl must contain an ecosystem'
32+
const type = purl.match(PURL_TYPE)
33+
if (!type) {
34+
result.error = 'package-url must contain a type'
3535
return result
3636
}
37-
result.type = ecosystem[1]
37+
result.type = type[1]
3838
const parts = purl.split('/')
3939
// the first 'part' should be 'pkg:ecosystem'
40-
if (parts.length < 2 || parts[1].length === 0) {
41-
result.error = 'purl must contain a namespace or name'
40+
if (parts.length < 2 || !parts[1]) {
41+
result.error = 'package-url must contain a namespace or name'
4242
return result
4343
}
4444
let namePlusRest: string
@@ -49,13 +49,17 @@ export function parsePURL(purl: string): PackageURL {
4949
namePlusRest = parts[2]
5050
}
5151
const name = namePlusRest.match(/([^@#?]+)[@#?]?.*/)
52-
if (name === null) {
52+
if (!result.namespace && !name) {
53+
result.error = 'package-url must contain a namespace or name'
54+
return result
55+
}
56+
if (!name) {
5357
// we're done here
5458
return result
5559
}
5660
result.name = decodeURIComponent(name[1])
5761
const version = namePlusRest.match(/@([^#?]+)[#?]?.*/)
58-
if (version === null) {
62+
if (!version) {
5963
return result
6064
}
6165
result.version = decodeURIComponent(version[1])

‎src/schemas.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PackageURL = z
1515
if (purl.error) {
1616
context.addIssue({
1717
code: z.ZodIssueCode.custom,
18-
message: `Error parsing purl: ${purl.error}`
18+
message: `Error parsing package-url: ${purl.error}`
1919
})
2020
}
2121
})
@@ -35,7 +35,7 @@ const PackageURLWithNamespace = z
3535
if (purl.namespace === null) {
3636
context.addIssue({
3737
code: z.ZodIssueCode.custom,
38-
message: `purl must have a namespace, and the namespace must be followed by '/'`
38+
message: `package-url must have a namespace, and the namespace must be followed by '/'`
3939
})
4040
}
4141
})

0 commit comments

Comments
 (0)